Add select-up/downstream-nodes action to editor

This commit is contained in:
Nick O'Leary
2021-02-16 20:46:41 +00:00
parent efd8c3d6d2
commit d5314d2a85
3 changed files with 117 additions and 15 deletions

View File

@@ -17,6 +17,40 @@
RED.view.tools = (function() {
function selectDownstream() {
selectStream('source','target')
}
function selectUpstream() {
selectStream('target','source')
}
function selectStream(from,to) {
var selection = RED.view.selection();
var visited = new Set();
if (selection.nodes && selection.nodes.length > 0) {
var nodes = [];
selection.nodes.forEach(function(n) {
visited.add(n);
var filter = {};
filter[from] = {id:n.id}
var initialLinks = RED.nodes.filterLinks(filter);
nodes = nodes.concat(initialLinks.map(function(n){return n[to]}));
})
while(nodes.length > 0) {
var n = nodes.shift();
visited.add(n);
var filter = {};
filter[from] = {id:n.id}
var links = RED.nodes.filterLinks(filter);
links.forEach(function(l) {
if (!visited.has(l[to])) {
nodes.push(l[to]);
}
})
}
RED.view.select({nodes:Array.from(visited)});
}
}
function alignToGrid() {
var selection = RED.view.selection();
if (selection.nodes) {
@@ -206,6 +240,9 @@ RED.view.tools = (function() {
RED.actions.add("core:step-selection-right", function() { moveSelection(RED.view.gridSize(),0);});
RED.actions.add("core:step-selection-down", function() { moveSelection(0,RED.view.gridSize());});
RED.actions.add("core:step-selection-left", function() { moveSelection(-RED.view.gridSize(),0);});
RED.actions.add("core:select-downstream-nodes", selectDownstream);
RED.actions.add("core:select-upstream-nodes", selectUpstream);
},
/**
* Aligns all selected nodes to the current grid