1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Adds shift-click support for selecting up/down stream nodes

This commit is contained in:
Nick O'Leary 2021-02-17 17:32:55 +00:00
parent 35f788693d
commit ca75efcbaf
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
4 changed files with 60 additions and 65 deletions

View File

@ -47,7 +47,6 @@
"alt-s u": "core:select-upstream-nodes", "alt-s u": "core:select-upstream-nodes",
"alt-s d": "core:select-downstream-nodes", "alt-s d": "core:select-downstream-nodes",
"alt-s c": "core:select-connected-nodes", "alt-s c": "core:select-connected-nodes",
"alt-s d": "core:select-downstream-nodes",
"shift-?": "core:show-help", "shift-?": "core:show-help",
"w": "core:scroll-view-up", "w": "core:scroll-view-up",
"d": "core:scroll-view-right", "d": "core:scroll-view-right",

View File

@ -35,7 +35,7 @@ RED.nodes = (function() {
var initialLoad; var initialLoad;
var dirty = false; var dirty = false;
RED.ll = function() { return nodeLinks}
function setDirty(d) { function setDirty(d) {
dirty = d; dirty = d;
RED.events.emit("workspace:dirty",{dirty:dirty}); RED.events.emit("workspace:dirty",{dirty:dirty});
@ -617,29 +617,38 @@ RED.ll = function() { return nodeLinks}
return false; return false;
} }
function getAllFlowNodes(node) { function getAllDownstreamNodes(node) {
var visited = {}; return getAllFlowNodes(node,'down');
visited[node.id] = true;
var nns = [node];
var stack = [node];
while(stack.length !== 0) {
var n = stack.shift();
var childLinks = [].concat(nodeLinks[n.id].in).concat(nodeLinks[n.id].out);
for (var i=0;i<childLinks.length;i++) {
var child = (childLinks[i].source === n)?childLinks[i].target:childLinks[i].source;
var id = child.id;
if (!id) {
id = child.direction+":"+child.i;
} }
if (!visited[id]) { function getAllUpstreamNodes(node) {
visited[id] = true; return getAllFlowNodes(node,'up');
nns.push(child);
stack.push(child);
} }
function getAllFlowNodes(node, direction) {
var selection = RED.view.selection();
var visited = new Set();
var nodes = [node];
while(nodes.length > 0) {
var n = nodes.shift();
visited.add(n);
var links = [];
if (!direction || direction === 'up') {
links = links.concat(nodeLinks[n.id].in);
} }
if (!direction || direction === 'down') {
links = links.concat(nodeLinks[n.id].out);
} }
return nns; links.forEach(function(l) {
if (!visited.has(l.source)) {
nodes.push(l.source);
} }
if (!visited.has(l.target)) {
nodes.push(l.target);
}
})
}
return Array.from(visited);
}
function convertWorkspace(n) { function convertWorkspace(n) {
var node = {}; var node = {};
@ -2247,6 +2256,8 @@ RED.ll = function() { return nodeLinks}
identifyImportConflicts: identifyImportConflicts, identifyImportConflicts: identifyImportConflicts,
getAllFlowNodes: getAllFlowNodes, getAllFlowNodes: getAllFlowNodes,
getAllUpstreamNodes: getAllUpstreamNodes,
getAllDownstreamNodes: getAllDownstreamNodes,
createExportableNodeSet: createExportableNodeSet, createExportableNodeSet: createExportableNodeSet,
createCompleteNodeSet: createCompleteNodeSet, createCompleteNodeSet: createCompleteNodeSet,
updateConfigNodeUsers: updateConfigNodeUsers, updateConfigNodeUsers: updateConfigNodeUsers,

View File

@ -16,14 +16,20 @@
RED.view.tools = (function() { RED.view.tools = (function() {
function selectAllConnected() { function selectConnected(type) {
console.log(args);
var selection = RED.view.selection(); var selection = RED.view.selection();
var visited = new Set(); var visited = new Set();
if (selection.nodes && selection.nodes.length > 0) { if (selection.nodes && selection.nodes.length > 0) {
selection.nodes.forEach(function(n) { selection.nodes.forEach(function(n) {
if (!visited.has(n)) { if (!visited.has(n)) {
var connected = RED.nodes.getAllFlowNodes(n); var connected;
if (type === 'all') {
connected = RED.nodes.getAllFlowNodes(n);
} else if (type === 'up') {
connected = RED.nodes.getAllUpstreamNodes(n);
} else if (type === 'down') {
connected = RED.nodes.getAllDownstreamNodes(n);
}
connected.forEach(function(nn) { visited.add(nn) }) connected.forEach(function(nn) { visited.add(nn) })
} }
}); });
@ -32,40 +38,6 @@ 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() { function alignToGrid() {
var selection = RED.view.selection(); var selection = RED.view.selection();
if (selection.nodes) { if (selection.nodes) {
@ -256,9 +228,9 @@ RED.view.tools = (function() {
RED.actions.add("core:step-selection-down", function() { moveSelection(0,RED.view.gridSize());}); 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:step-selection-left", function() { moveSelection(-RED.view.gridSize(),0);});
RED.actions.add("core:select-connected-nodes", selectAllConnected); RED.actions.add("core:select-connected-nodes", function() { selectConnected("all") });
RED.actions.add("core:select-downstream-nodes", selectDownstream); RED.actions.add("core:select-downstream-nodes", function() { selectConnected("down") });
RED.actions.add("core:select-upstream-nodes", selectUpstream); RED.actions.add("core:select-upstream-nodes", function() { selectConnected("up") });
}, },
/** /**
* Aligns all selected nodes to the current grid * Aligns all selected nodes to the current grid

View File

@ -2992,6 +2992,7 @@ RED.view = (function() {
enterActiveGroup(ag); enterActiveGroup(ag);
activeGroup.selected = true; activeGroup.selected = true;
} }
console.log(d3.event);
var cnodes = RED.nodes.getAllFlowNodes(mousedown_node); var cnodes = RED.nodes.getAllFlowNodes(mousedown_node);
for (var n=0;n<cnodes.length;n++) { for (var n=0;n<cnodes.length;n++) {
if (!cnodes[n].selected) { if (!cnodes[n].selected) {
@ -3080,7 +3081,19 @@ RED.view = (function() {
// } else // } else
if (d3.event.shiftKey) { if (d3.event.shiftKey) {
clearSelection(); clearSelection();
var cnodes = RED.nodes.getAllFlowNodes(mousedown_node); var clickPosition = (d3.event.offsetX - mousedown_node.x)
var edgeDelta = (mousedown_node.w/2) - Math.abs(clickPosition);
var cnodes;
var targetEdgeDelta = mousedown_node.w > 30 ? 25 : 8;
if (edgeDelta < targetEdgeDelta) {
if (clickPosition < 0) {
cnodes = RED.nodes.getAllUpstreamNodes(mousedown_node);
} else {
cnodes = RED.nodes.getAllDownstreamNodes(mousedown_node);
}
} else {
cnodes = RED.nodes.getAllFlowNodes(mousedown_node);
}
for (var n=0;n<cnodes.length;n++) { for (var n=0;n<cnodes.length;n++) {
cnodes[n].selected = true; cnodes[n].selected = true;
cnodes[n].dirty = true; cnodes[n].dirty = true;