From 58d4c36600605ea3755e9a404c000fb43308178c Mon Sep 17 00:00:00 2001 From: GogoVega <92022724+GogoVega@users.noreply.github.com> Date: Wed, 19 Jun 2024 12:39:02 +0200 Subject: [PATCH] Fix updating Subflow name when it's a copy --- .../@node-red/editor-client/src/js/nodes.js | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/packages/node_modules/@node-red/editor-client/src/js/nodes.js b/packages/node_modules/@node-red/editor-client/src/js/nodes.js index 76d191b26..9bc99c2aa 100644 --- a/packages/node_modules/@node-red/editor-client/src/js/nodes.js +++ b/packages/node_modules/@node-red/editor-client/src/js/nodes.js @@ -1006,23 +1006,34 @@ RED.nodes = (function() { return {nodes:removedNodes,links:removedLinks, groups: removedGroups, junctions: removedJunctions}; } + /** + * Add a Subflow to the Workspace + * + * @param {object} sf The Subflow to add. + * @param {boolean|undefined} createNewIds Whether to create a new ID and update the name. + */ function addSubflow(sf, createNewIds) { if (createNewIds) { - var subflowNames = Object.keys(subflows).map(function(sfid) { - return subflows[sfid].name; - }); + // Update the Subflow Id + sf.id = generateId(); - subflowNames.sort(); - var copyNumber = 1; - var subflowName = sf.name; + // Update the Subflow name to highlight that this is a copy + const subflowNames = Object.keys(subflows).map(function (sfid) { + return subflows[sfid].name || ""; + }).sort(); + + let copyNumber = 1; + let subflowName = sf.name; subflowNames.forEach(function(name) { if (subflowName == name) { + subflowName = sf.name + " (" + copyNumber + ")"; copyNumber++; - subflowName = sf.name+" ("+copyNumber+")"; } }); + sf.name = subflowName; } + subflows[sf.id] = sf; allNodes.addTab(sf.id); linkTabMap[sf.id] = []; @@ -2151,11 +2162,10 @@ RED.nodes = (function() { node.status.z = node.id; node.status.id = generateId(); } - if (createNewIds || options.importMap[node.id] === "copy") { - node.id = generateId(); - } + subflowMap[oldId] = node; newSubflows.push(node); + // NOTE: Update the id in the `addSubflow` function addSubflow(node, (createNewIds || options.importMap[node.id] === "copy")); } }