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

Allow use of arrow keys to move selected node

Closes #176
This commit is contained in:
Nick O'Leary 2014-04-16 13:39:16 +01:00
parent 4219681cfa
commit fa275646a1
2 changed files with 76 additions and 8 deletions

View File

@ -21,23 +21,36 @@ RED.keyboard = function() {
d3.select(window).on("keydown",function() { d3.select(window).on("keydown",function() {
if (!active) { return; } if (!active) { return; }
var handler = handlers[d3.event.keyCode]; var handler = handlers[d3.event.keyCode];
if (handler) { if (handler && handler.ondown) {
if (!handler.modifiers || if (!handler.modifiers ||
((!handler.modifiers.shift || d3.event.shiftKey)&& ((!handler.modifiers.shift || d3.event.shiftKey)&&
(!handler.modifiers.ctrl || d3.event.ctrlKey))) { (!handler.modifiers.ctrl || d3.event.ctrlKey))) {
handler.callback(); handler.ondown();
} }
} }
}); });
d3.select(window).on("keyup",function() {
function addHandler(key,modifiers,callback) { if (!active) { return; }
var handler = handlers[d3.event.keyCode];
if (handler && handler.onup) {
if (!handler.modifiers ||
((!handler.modifiers.shift || d3.event.shiftKey)&&
(!handler.modifiers.ctrl || d3.event.ctrlKey))) {
handler.onup();
}
}
});
function addHandler(key,modifiers,ondown,onup) {
var mod = modifiers; var mod = modifiers;
var cb = callback; var cbdown = ondown;
var cbup = onup;
if (typeof modifiers == "function") { if (typeof modifiers == "function") {
mod = {}; mod = {};
cb = modifiers; cbdown = modifiers;
cbup = ondown;
} }
handlers[key] = {modifiers:mod, callback:cb}; handlers[key] = {modifiers:mod, ondown:cbdown, onup:cbup};
} }
function removeHandler(key) { function removeHandler(key) {
delete handlers[key]; delete handlers[key];

View File

@ -398,6 +398,12 @@ RED.view = function() {
RED.history.push({t:'move',nodes:ns,dirty:dirty}); RED.history.push({t:'move',nodes:ns,dirty:dirty});
} }
} }
if (mouse_mode == RED.state.MOVING || mouse_mode == RED.state.MOVING_ACTIVE) {
for (var i=0;i<moving_set.length;i++) {
delete moving_set[i].ox;
delete moving_set[i].oy;
}
}
if (mouse_mode == RED.state.IMPORT_DRAGGING) { if (mouse_mode == RED.state.IMPORT_DRAGGING) {
RED.keyboard.remove(/* ESCAPE */ 27); RED.keyboard.remove(/* ESCAPE */ 27);
setDirty(true); setDirty(true);
@ -526,13 +532,59 @@ RED.view = function() {
RED.keyboard.add(/* c */ 67,{ctrl:true},function(){copySelection();d3.event.preventDefault();}); RED.keyboard.add(/* c */ 67,{ctrl:true},function(){copySelection();d3.event.preventDefault();});
RED.keyboard.add(/* x */ 88,{ctrl:true},function(){copySelection();deleteSelection();d3.event.preventDefault();}); RED.keyboard.add(/* x */ 88,{ctrl:true},function(){copySelection();deleteSelection();d3.event.preventDefault();});
} }
if (moving_set.length == 0) {
RED.keyboard.remove(/* up */ 38);
RED.keyboard.remove(/* down */ 40);
RED.keyboard.remove(/* left */ 37);
RED.keyboard.remove(/* right*/ 39);
} else {
RED.keyboard.add(/* up */ 38, function() { d3.event.shiftKey?moveSelection( 0,-20):moveSelection( 0,-1);d3.event.preventDefault();},endKeyboardMove);
RED.keyboard.add(/* down */ 40, function() { d3.event.shiftKey?moveSelection( 0, 20):moveSelection( 0, 1);d3.event.preventDefault();},endKeyboardMove);
RED.keyboard.add(/* left */ 37, function() { d3.event.shiftKey?moveSelection(-20, 0):moveSelection(-1, 0);d3.event.preventDefault();},endKeyboardMove);
RED.keyboard.add(/* right*/ 39, function() { d3.event.shiftKey?moveSelection( 20, 0):moveSelection( 1, 0);d3.event.preventDefault();},endKeyboardMove);
}
if (moving_set.length == 1) { if (moving_set.length == 1) {
RED.sidebar.info.refresh(moving_set[0].n); RED.sidebar.info.refresh(moving_set[0].n);
} else { } else {
RED.sidebar.info.clear(); RED.sidebar.info.clear();
} }
} }
function endKeyboardMove() {
var ns = [];
for (var i=0;i<moving_set.length;i++) {
ns.push({n:moving_set[i].n,ox:moving_set[i].ox,oy:moving_set[i].oy});
delete moving_set[i].ox;
delete moving_set[i].oy;
}
RED.history.push({t:'move',nodes:ns,dirty:dirty});
}
function moveSelection(dx,dy) {
var minX = 0;
var minY = 0;
for (var i=0;i<moving_set.length;i++) {
var node = moving_set[i];
if (node.ox == null && node.oy == null) {
node.ox = node.n.x;
node.oy = node.n.y;
}
node.n.x += dx;
node.n.y += dy;
node.n.dirty = true;
minX = Math.min(node.n.x-node.n.w/2-5,minX);
minY = Math.min(node.n.y-node.n.h/2-5,minY);
}
if (minX != 0 || minY != 0) {
for (var n = 0; n<moving_set.length; n++) {
var node = moving_set[n];
node.n.x -= minX;
node.n.y -= minY;
}
}
redraw();
}
function deleteSelection() { function deleteSelection() {
var removedNodes = []; var removedNodes = [];
var removedLinks = []; var removedLinks = [];
@ -1263,6 +1315,9 @@ RED.view = function() {
getWorkspace: function() { getWorkspace: function() {
return activeWorkspace; return activeWorkspace;
}, },
showWorkspace: function(id) {
workspace_tabs.activateTab(id);
},
redraw:redraw, redraw:redraw,
dirty: function(d) { dirty: function(d) {
if (d == null) { if (d == null) {