From 8740ec55704ad46282b3428cce62b3adfc42e63d Mon Sep 17 00:00:00 2001 From: Marcus Sonntag Date: Sat, 4 Feb 2023 21:03:30 +0100 Subject: [PATCH 1/2] Added editor feature for connecting multiple nodes to single node --- .../editor-client/src/js/ui/view-tools.js | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/packages/node_modules/@node-red/editor-client/src/js/ui/view-tools.js b/packages/node_modules/@node-red/editor-client/src/js/ui/view-tools.js index f46167b24..302222fae 100644 --- a/packages/node_modules/@node-red/editor-client/src/js/ui/view-tools.js +++ b/packages/node_modules/@node-red/editor-client/src/js/ui/view-tools.js @@ -840,6 +840,62 @@ RED.view.tools = (function() { } } + function wireMultipleToNode() { + if (RED.workspaces.isActiveLocked()) { + return + } + var selection = RED.view.selection(); + if (selection.nodes) { + if (selection.nodes.length > 1) { + var targetNode = selection.nodes[selection.nodes.length - 1]; + if (targetNode.inputs === 0) { + return; + } + var i = 0; + var newLinks = []; + for (i = 0; i < selection.nodes.length - 1; i++) { + var sourceNode = selection.nodes[i]; + if (sourceNode.outputs > 0) { + + // Wire the first output to the target that has no link to the target yet. + // This allows for connecting all combinations of inputs/outputs. + // The user may then delete links quickly that aren't needed. + var sourceConnectedOutports = RED.nodes.filterLinks({ + source: sourceNode, + target: targetNode + }); + + // Get outport indices that have no link yet + var sourceOutportIndices = Array.from({ length: sourceNode.outputs }, (_, i) => i); + var sourceConnectedOutportIndices = sourceConnectedOutports.map( x => x.sourcePort ); + var sourceFreeOutportIndices = sourceOutportIndices.filter(x => !sourceConnectedOutportIndices.includes(x)); + + // Does an unconnected source port exist? + if( sourceFreeOutportIndices.length == 0 ) continue; + + // Connect the first free outport to the target + var newLink = { + source: sourceNode, + target: targetNode, + sourcePort: sourceFreeOutportIndices[0] + } + RED.nodes.addLink(newLink); + newLinks.push(newLink); + } + } + if (newLinks.length > 0) { + RED.history.push({ + t: 'add', + links: newLinks, + dirty: RED.nodes.dirty() + }) + RED.nodes.dirty(true); + RED.view.redraw(true); + } + } + } + } + /** * Splits selected wires and re-joins them with link-out+link-in * @param {Object || Object[]} wires The wire(s) to split and replace with link-out, link-in nodes. @@ -1307,6 +1363,7 @@ RED.view.tools = (function() { RED.actions.add("core:wire-series-of-nodes", function() { wireSeriesOfNodes() }) RED.actions.add("core:wire-node-to-multiple", function() { wireNodeToMultiple() }) + RED.actions.add("core:wire-multiple-to-node", function() { wireMultipleToNode() }) RED.actions.add("core:split-wire-with-link-nodes", function () { splitWiresWithLinkNodes() }); RED.actions.add("core:split-wires-with-junctions", function () { addJunctionsToWires() }); From 4c7c855f2c3a879089d8e16f9276bc45a5e10167 Mon Sep 17 00:00:00 2001 From: sonntam Date: Sat, 4 Feb 2023 21:29:54 +0100 Subject: [PATCH 2/2] fix code style --- .../@node-red/editor-client/src/js/ui/view-tools.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/node_modules/@node-red/editor-client/src/js/ui/view-tools.js b/packages/node_modules/@node-red/editor-client/src/js/ui/view-tools.js index 302222fae..a22f65f76 100644 --- a/packages/node_modules/@node-red/editor-client/src/js/ui/view-tools.js +++ b/packages/node_modules/@node-red/editor-client/src/js/ui/view-tools.js @@ -871,7 +871,9 @@ RED.view.tools = (function() { var sourceFreeOutportIndices = sourceOutportIndices.filter(x => !sourceConnectedOutportIndices.includes(x)); // Does an unconnected source port exist? - if( sourceFreeOutportIndices.length == 0 ) continue; + if (sourceFreeOutportIndices.length == 0) { + continue; + } // Connect the first free outport to the target var newLink = {