Fix updating Subflow name when it's a copy

This commit is contained in:
GogoVega 2024-06-19 12:39:02 +02:00
parent 476e8345c5
commit 58d4c36600
No known key found for this signature in database
GPG Key ID: E1E048B63AC5AC2B

View File

@ -1006,23 +1006,34 @@ RED.nodes = (function() {
return {nodes:removedNodes,links:removedLinks, groups: removedGroups, junctions: removedJunctions}; 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) { function addSubflow(sf, createNewIds) {
if (createNewIds) { if (createNewIds) {
var subflowNames = Object.keys(subflows).map(function(sfid) { // Update the Subflow Id
return subflows[sfid].name; sf.id = generateId();
});
subflowNames.sort(); // Update the Subflow name to highlight that this is a copy
var copyNumber = 1; const subflowNames = Object.keys(subflows).map(function (sfid) {
var subflowName = sf.name; return subflows[sfid].name || "";
}).sort();
let copyNumber = 1;
let subflowName = sf.name;
subflowNames.forEach(function(name) { subflowNames.forEach(function(name) {
if (subflowName == name) { if (subflowName == name) {
subflowName = sf.name + " (" + copyNumber + ")";
copyNumber++; copyNumber++;
subflowName = sf.name+" ("+copyNumber+")";
} }
}); });
sf.name = subflowName; sf.name = subflowName;
} }
subflows[sf.id] = sf; subflows[sf.id] = sf;
allNodes.addTab(sf.id); allNodes.addTab(sf.id);
linkTabMap[sf.id] = []; linkTabMap[sf.id] = [];
@ -2151,11 +2162,10 @@ RED.nodes = (function() {
node.status.z = node.id; node.status.z = node.id;
node.status.id = generateId(); node.status.id = generateId();
} }
if (createNewIds || options.importMap[node.id] === "copy") {
node.id = generateId();
}
subflowMap[oldId] = node; subflowMap[oldId] = node;
newSubflows.push(node); newSubflows.push(node);
// NOTE: Update the id in the `addSubflow` function
addSubflow(node, (createNewIds || options.importMap[node.id] === "copy")); addSubflow(node, (createNewIds || options.importMap[node.id] === "copy"));
} }
} }