From b40551a8fa939fec70da9191595339437971a1e8 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Sun, 16 Oct 2022 22:38:11 +0100 Subject: [PATCH] Handle replacing unknown node inside group or subflow --- .../@node-red/editor-client/src/js/nodes.js | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 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 d387f01b3..4291f8b2e 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 @@ -1768,6 +1768,7 @@ RED.nodes = (function() { unknownTypes.push(n.type); } if (n.z) { +if (n.id === '42051f44c151e5d7') console.log('importing',n.z) nodeZmap[n.z] = nodeZmap[n.z] || []; nodeZmap[n.z].push(n); } else if (isInitialLoad && n.hasOwnProperty('x') && n.hasOwnProperty('y') && !n.z) { @@ -1965,7 +1966,7 @@ RED.nodes = (function() { } } } else { - const keepNodesCurrentZ = reimport && n.z && RED.workspaces.contains(n.z) + const keepNodesCurrentZ = reimport && n.z && (RED.workspaces.contains(n.z) || RED.nodes.subflow(n.z)) if (!keepNodesCurrentZ && n.z && !workspace_map[n.z] && !subflow_map[n.z]) { n.z = activeWorkspace; } @@ -2067,7 +2068,7 @@ RED.nodes = (function() { node.id = getID(); } else { node.id = n.id; - const keepNodesCurrentZ = reimport && node.z && RED.workspaces.contains(node.z) + const keepNodesCurrentZ = reimport && node.z && (RED.workspaces.contains(node.z) || RED.nodes.subflow(node.z)) if (!keepNodesCurrentZ && (node.z == null || (!workspace_map[node.z] && !subflow_map[node.z]))) { if (createMissingWorkspace) { if (missingWorkspace === null) { @@ -2740,6 +2741,7 @@ RED.nodes = (function() { } }); + var nodeGroupMap = {} var replaceNodeIds = Object.keys(replaceNodes); if (replaceNodeIds.length > 0) { var reimportList = []; @@ -2750,6 +2752,12 @@ RED.nodes = (function() { } else { allNodes.removeNode(n); } + if (n.g) { + // reimporting a node *without* including its group object + // will cause the g property to be cleared. Cache it + // here so we can restore it + nodeGroupMap[n.id] = n.g + } reimportList.push(convertNode(n)); RED.events.emit('nodes:remove',n); }); @@ -2771,6 +2779,18 @@ RED.nodes = (function() { var newNodeMap = {}; result.nodes.forEach(function(n) { newNodeMap[n.id] = n; + if (nodeGroupMap[n.id]) { + // This node is in a group - need to substitute the + // node reference inside the group + n.g = nodeGroupMap[n.id] + const group = RED.nodes.group(n.g) + if (group) { + var index = group.nodes.findIndex(gn => gn.id === n.id) + if (index > -1) { + group.nodes[index] = n + } + } + } }); RED.nodes.eachLink(function(l) { if (newNodeMap.hasOwnProperty(l.source.id)) {