From 2f01fe583286ce1c3190c36beb9c5e3c952eec8e Mon Sep 17 00:00:00 2001 From: GogoVega <92022724+GogoVega@users.noreply.github.com> Date: Tue, 18 Jun 2024 20:48:33 +0200 Subject: [PATCH] Fix number of users when deleting nodes --- .../@node-red/editor-client/src/js/nodes.js | 56 ++++++++----------- .../editor-client/src/js/ui/subflow.js | 6 +- 2 files changed, 25 insertions(+), 37 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 86d117a0b..b33e1e1d0 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 @@ -679,7 +679,8 @@ RED.nodes = (function() { } else { if (n.wires && (n.wires.length > n.outputs)) { n.outputs = n.wires.length; } n.dirty = true; - updateConfigNodeUsers(n); + // TODO: The event should be triggered? + updateConfigNodeUsers(n, { action: "add", emitEvent: false }); if (n._def.category == "subflows" && typeof n.i === "undefined") { var nextId = 0; RED.nodes.eachNode(function(node) { @@ -2293,6 +2294,7 @@ RED.nodes = (function() { const isConfigNode = def?.category === "config"; // Update the node definition for subflow instance + // TODO: A thing with `node.i` if (!isUnknownNode && node.type.substring(0, 7) === "subflow") { const parentId = node.type.split(":")[1]; const subflow = subflowMap[parentId] || getSubflow(parentId); @@ -2343,20 +2345,6 @@ RED.nodes = (function() { } } - // Update Users Count - // NOTE: the only reliable way to get the right number is to scan all the nodes - // Replacing a Config Node overwrites the list giving a wrong number - allNodes.eachNode(function (node) { - const def = registry.getNodeType(node.type); - if (def?.category === "config") { - node.users = []; - } - }); - allNodes.eachNode(function (node) { - // TODO: The event will be triggered after the import - Quid? - updateConfigNodeUsers(node, { emitEvent: false }); - }); - const newLinks = []; // Remap all Wires and (Config) Node references for (const node of [...newNodes, ...newGroups]) { @@ -2378,6 +2366,7 @@ RED.nodes = (function() { delete node.wires; } + // Update the Group id if (node.g && nodeMap[node.g]) { node.g = nodeMap[node.g].id; } else { @@ -2392,27 +2381,18 @@ RED.nodes = (function() { }); } - // Update the old Node Id with the new one + // Update the node id for select inputs and Links nodes for (const d in node._def.defaults) { - // Config Node, Node or Links (not sure) if (node._def.defaults.hasOwnProperty(d) && node._def.defaults[d].type) { let nodeList = node[d]; if (!Array.isArray(nodeList)) { nodeList = [nodeList]; } - nodeList = nodeList.map(function(id) { + + nodeList = nodeList.map(function (id) { const n = nodeMap[id]; - if (n) { - if (n._def.category === "config") { - // TODO: addNode calls updateConfigNodeUsers so remove here - if (n.users.indexOf(node) === -1) { - n.users.push(node); - } - } - return n.id; - } - return id; + return n ? n.id : id; }); node[d] = Array.isArray(node[d]) ? nodeList : nodeList[0]; @@ -2643,7 +2623,7 @@ RED.nodes = (function() { // Update any config nodes referenced by the provided node to ensure their 'users' list is correct function updateConfigNodeUsers(n, options) { - const defaultOptions = { emitEvent: true }; + const defaultOptions = { action: "add", emitEvent: true }; options = Object.assign({}, defaultOptions, options); for (var d in n._def.defaults) { @@ -2654,10 +2634,20 @@ RED.nodes = (function() { if (type && type.category == "config") { var configNode = configNodes[n[d]]; if (configNode) { - if (configNode.users.indexOf(n) === -1) { - configNode.users.push(n); - if (options.emitEvent) { - RED.events.emit('nodes:change', configNode); + if (options.action === "add") { + if (configNode.users.indexOf(n) === -1) { + configNode.users.push(n); + if (options.emitEvent) { + RED.events.emit('nodes:change', configNode); + } + } + } else if (options.action === "remove") { + if (configNode.users.indexOf(n) !== -1) { + const users = configNode.users; + users.splice(users.indexOf(n), 1); + if (options.emitEvent) { + RED.events.emit('nodes:change', configNode); + } } } } diff --git a/packages/node_modules/@node-red/editor-client/src/js/ui/subflow.js b/packages/node_modules/@node-red/editor-client/src/js/ui/subflow.js index a06f8bca4..19631d76d 100644 --- a/packages/node_modules/@node-red/editor-client/src/js/ui/subflow.js +++ b/packages/node_modules/@node-red/editor-client/src/js/ui/subflow.js @@ -508,10 +508,8 @@ RED.subflow = (function() { var activeSubflow = RED.nodes.subflow(id); RED.nodes.eachNode(function(n) { - if (!keepInstanceNodes && n.type == "subflow:"+id) { - removedNodes.push(n); - } - if (n.z == id) { + if (n.z === id || !keepInstanceNodes && n.type === "subflow:" + id) { + RED.nodes.updateConfigNodeUsers(n, { emitEvent: false, action: "remove" }); removedNodes.push(n); } });