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

Initial implementation of redo (un-undo)

This commit is contained in:
Kunihiko Toumura 2019-06-22 16:05:50 +09:00
parent 8c68e76c3e
commit 7adf102d8d
3 changed files with 167 additions and 4 deletions

View File

@ -15,6 +15,7 @@
**/
RED.history = (function() {
var undo_history = [];
var redo_history = [];
function undoEvent(ev) {
var i;
@ -22,52 +23,81 @@ RED.history = (function() {
var node;
var subflow;
var modifiedTabs = {};
var inv_ev;
if (ev) {
if (ev.t == 'multi') {
inv_ev = {
t: 'multi',
events: []
};
len = ev.events.length;
for (i=len-1;i>=0;i--) {
undoEvent(ev.events[i]);
var r = undoEvent(ev.events[i]);
inv_ev.events.push(r);
}
} else if (ev.t == 'replace') {
inv_ev = {
t: 'replace',
config: RED.nodes.createCompleteNodeSet(),
changed: [],
rev: RED.nodes.version()
};
RED.nodes.clear();
var imported = RED.nodes.import(ev.config);
imported[0].forEach(function(n) {
if (ev.changed[n.id]) {
n.changed = true;
inv_ev.changed[n.id] = true;
}
})
RED.nodes.version(ev.rev);
} else if (ev.t == 'add') {
inv_ev = {
t: "delete",
};
if (ev.nodes) {
inv_ev.nodes = [];
for (i=0;i<ev.nodes.length;i++) {
node = RED.nodes.node(ev.nodes[i]);
if (node.z) {
modifiedTabs[node.z] = true;
}
inv_ev.nodes.push(node);
RED.nodes.remove(ev.nodes[i]);
}
}
if (ev.links) {
inv_ev.links = [];
for (i=0;i<ev.links.length;i++) {
inv_ev.links.push(ev.links[i]);
RED.nodes.removeLink(ev.links[i]);
}
}
if (ev.workspaces) {
inv_ev.workspaces = [];
for (i=0;i<ev.workspaces.length;i++) {
var workspaceOrder = RED.nodes.getWorkspaceOrder();
ev.workspaces[i]._index = workspaceOrder.indexOf(ev.workspaces[i].id);
inv_ev.workspaces.push(ev.workspaces[i]);
RED.nodes.removeWorkspace(ev.workspaces[i].id);
RED.workspaces.remove(ev.workspaces[i]);
}
}
if (ev.subflows) {
inv_ev.subflows = [];
for (i=0;i<ev.subflows.length;i++) {
inv_ev.subflows.push(ev.subflows[i]);
RED.nodes.removeSubflow(ev.subflows[i]);
RED.workspaces.remove(ev.subflows[i]);
}
}
if (ev.subflow) {
inv_ev.subflow = {};
if (ev.subflow.instances) {
inv_ev.subflow.instances = [];
ev.subflow.instances.forEach(function(n) {
inv_ev.subflow.instances.push(n);
var node = RED.nodes.node(n.id);
if (node) {
node.changed = n.changed;
@ -83,21 +113,30 @@ RED.history = (function() {
}
}
if (ev.removedLinks) {
inv_ev.createdLinks = [];
for (i=0;i<ev.removedLinks.length;i++) {
inv_ev.createdLinks.push(ev.removedLinks[i]);
RED.nodes.addLink(ev.removedLinks[i]);
}
}
} else if (ev.t == "delete") {
inv_ev = {
t: "add"
};
if (ev.workspaces) {
inv_ev.workspaces = [];
for (i=0;i<ev.workspaces.length;i++) {
inv_ev.workspaces.push(ev.workspaces[i]);
RED.nodes.addWorkspace(ev.workspaces[i],ev.workspaces[i]._index);
RED.workspaces.add(ev.workspaces[i],undefined,ev.workspaces[i]._index);
delete ev.workspaces[i]._index;
}
}
if (ev.subflows) {
inv_ev.subflows = [];
for (i=0;i<ev.subflows.length;i++) {
inv_ev.subflows.push(ev.subflows[i]);
RED.nodes.addSubflow(ev.subflows[i]);
}
}
@ -126,8 +165,10 @@ RED.history = (function() {
}
}
if (ev.subflow) {
inv_ev.subflow = {};
if (ev.subflow.hasOwnProperty('instances')) {
ev.subflow.instances.forEach(function(n) {
inv_ev.subflow.instances.push(n);
var node = RED.nodes.node(n.id);
if (node) {
node.changed = n.changed;
@ -152,14 +193,25 @@ RED.history = (function() {
});
}
if (ev.nodes) {
inv_ev.nodes = [];
for (i=0;i<ev.nodes.length;i++) {
RED.nodes.add(ev.nodes[i]);
modifiedTabs[ev.nodes[i].z] = true;
inv_ev.nodes.push(ev.nodes[i].id);
}
}
if (ev.links) {
inv_ev.links = [];
for (i=0;i<ev.links.length;i++) {
RED.nodes.addLink(ev.links[i]);
inv_ev.links.push(ev.links[i]);
}
}
if (ev.createdLinks) {
inv_ev.removedLinks = [];
for (i=0;i<ev.createdLinks.length;i++) {
inv_ev.removedLinks.push(ev.createdLinks[i]);
RED.nodes.removeLink(ev.createdLinks[i]);
}
}
if (ev.changes) {
@ -179,8 +231,14 @@ RED.history = (function() {
}
} else if (ev.t == "move") {
inv_ev = {
t: 'move',
nodes: []
};
for (i=0;i<ev.nodes.length;i++) {
var n = ev.nodes[i];
var rn = {n: n.n, ox: n.n.x, oy: n.n.y, dirty: true, moved: n.moved};
inv_ev.nodes.push(rn);
n.n.x = n.ox;
n.n.y = n.oy;
n.n.dirty = true;
@ -188,18 +246,28 @@ RED.history = (function() {
}
// A move could have caused a link splice
if (ev.links) {
inv_ev.removedLinks = [];
for (i=0;i<ev.links.length;i++) {
inv_ev.removedLinks.push(ev.links[i]);
RED.nodes.removeLink(ev.links[i]);
}
}
if (ev.removedLinks) {
inv_ev.links = [];
for (i=0;i<ev.removedLinks.length;i++) {
inv_ev.links.push(ev.removedLinks[i]);
RED.nodes.addLink(ev.removedLinks[i]);
}
}
} else if (ev.t == "edit") {
inv_ev = {
t: "edit",
changes: {}
};
inv_ev.node = ev.node;
for (i in ev.changes) {
if (ev.changes.hasOwnProperty(i)) {
inv_ev.changes[i] = ev.node[i];
if (ev.node._def.defaults && ev.node._def.defaults[i] && ev.node._def.defaults[i].type) {
// This is a config node property
var currentConfigNode = RED.nodes.node(ev.node[i]);
@ -219,22 +287,29 @@ RED.history = (function() {
$("#red-ui-workspace").toggleClass("red-ui-workspace-disabled",!!ev.node.disabled);
}
if (ev.subflow) {
inv_ev.subflow = {};
if (ev.subflow.hasOwnProperty('inputCount')) {
inv_ev.subflow.inputCount = ev.node.in.length;
if (ev.node.in.length > ev.subflow.inputCount) {
inv_ev.subflow.inputs = ev.node.in.slice(ev.subflow.inputCount);
ev.node.in.splice(ev.subflow.inputCount);
} else if (ev.subflow.inputs.length > 0) {
ev.node.in = ev.node.in.concat(ev.subflow.inputs);
}
}
if (ev.subflow.hasOwnProperty('outputCount')) {
inv_ev.subflow.outputCount = ev.node.out.length;
if (ev.node.out.length > ev.subflow.outputCount) {
inv_ev.subflow.outputs = ev.node.out.slice(ev.subflow.outputCount);
ev.node.out.splice(ev.subflow.outputCount);
} else if (ev.subflow.outputs.length > 0) {
ev.node.out = ev.node.out.concat(ev.subflow.outputs);
}
}
if (ev.subflow.hasOwnProperty('instances')) {
inv_ev.subflow.instances = [];
ev.subflow.instances.forEach(function(n) {
inv_ev.subflow.instances.push(n);
var node = RED.nodes.node(n.id);
if (node) {
node.changed = n.changed;
@ -258,9 +333,11 @@ RED.history = (function() {
var outputMap;
if (ev.outputMap) {
outputMap = {};
inv_ev.outputMap = {};
for (var port in ev.outputMap) {
if (ev.outputMap.hasOwnProperty(port) && ev.outputMap[port] !== "-1") {
outputMap[ev.outputMap[port]] = port;
inv_ev.outputMap[ev.outputMap[port]] = port;
}
}
}
@ -268,39 +345,106 @@ RED.history = (function() {
RED.editor.validateNode(ev.node);
}
if (ev.links) {
inv_ev.createdLinks = [];
for (i=0;i<ev.links.length;i++) {
RED.nodes.addLink(ev.links[i]);
inv_ev.createdLinks.push(ev.links[i]);
}
}
if (ev.createdLinks) {
inv_ev.links = [];
for (i=0;i<ev.createdLinks.length;i++) {
RED.nodes.removeLink(ev.createdLinks[i]);
inv_ev.links.push(ev.createdLinks[i]);
}
}
ev.node.dirty = true;
ev.node.changed = ev.changed;
} else if (ev.t == "createSubflow") {
inv_ev = {
t: "deleteSubflow",
activeWorkspace: ev.activeWorkspace,
dirty: RED.nodes.dirty()
};
if (ev.nodes) {
inv_ev.movedNodes = [];
RED.nodes.filterNodes({z:ev.subflow.subflow.id}).forEach(function(n) {
n.x += ev.subflow.offsetX;
n.y += ev.subflow.offsetY;
n.z = ev.activeWorkspace;
n.dirty = true;
inv_ev.movedNodes.push(n.id);
});
inv_ev.subflows = [];
for (i=0;i<ev.nodes.length;i++) {
inv_ev.subflows.push(RED.nodes.node(ev.nodes[i]));
RED.nodes.remove(ev.nodes[i]);
}
}
if (ev.links) {
inv_ev.links = [];
for (i=0;i<ev.links.length;i++) {
inv_ev.links.push(ev.links[i]);
RED.nodes.removeLink(ev.links[i]);
}
}
inv_ev.subflow = ev.subflow;
RED.nodes.removeSubflow(ev.subflow.subflow);
RED.workspaces.remove(ev.subflow.subflow);
if (ev.removedLinks) {
inv_ev.createdLinks = [];
for (i=0;i<ev.removedLinks.length;i++) {
inv_ev.createdLinks.push(ev.removedLinks[i]);
RED.nodes.addLink(ev.removedLinks[i]);
}
}
} else if (ev.t == "deleteSubflow") {
inv_ev = {
t: "createSubflow",
activeWorkspace: ev.activeWorkspace,
ditry: RED.nodes.dirty(),
};
if (ev.subflow) {
RED.nodes.addSubflow(ev.subflow.subflow);
inv_ev.subflow = ev.subflow;
}
if (ev.subflows) {
inv_ev.nodes = [];
for (i=0;i<ev.subflows.length;i++) {
RED.nodes.add(ev.subflows[i]);
inv_ev.nodes.push(ev.subflows[i].id);
}
}
if (ev.movedNodes) {
ev.movedNodes.forEach(function(nid) {
nn = RED.nodes.node(nid);
nn.x -= ev.subflow.offsetX;
nn.y -= ev.subflow.offsetY;
nn.z = ev.subflow.subflow.id;
nn.dirty = true;
});
}
if (ev.links) {
inv_ev.links = [];
for (i=0;i<ev.links.length;i++) {
inv_ev.links.push(ev.links[i]);
RED.nodes.addLink(ev.links[i]);
}
}
if (ev.createdLinks) {
inv_ev.removedLinks = [];
for (i=0;i<ev.createdLinks.length;i++) {
inv_ev.removedLinks.push(ev.createdLinks[i]);
RED.nodes.removeLink(ev.createdLinks[i]);
}
}
} else if (ev.t == "reorder") {
inv_ev = {
t: 'reorder',
order: RED.nodes.getWorkspaceOrder()
};
if (ev.order) {
RED.workspaces.order(ev.order);
}
@ -320,6 +464,8 @@ RED.history = (function() {
RED.workspaces.refresh();
RED.sidebar.config.refresh();
RED.subflow.refresh();
return inv_ev;
}
}
@ -339,16 +485,30 @@ RED.history = (function() {
},
push: function(ev) {
undo_history.push(ev);
redo_history = [];
},
pop: function() {
var ev = undo_history.pop();
undoEvent(ev);
var rev = undoEvent(ev);
if (rev) {
redo_history.push(rev);
}
},
peek: function() {
return undo_history[undo_history.length-1];
},
clear: function() {
undo_history = [];
redo_history = [];
},
redo: function() {
var ev = redo_history.pop();
if (ev) {
var uev = undoEvent(ev);
if (uev) {
undo_history.push(uev);
}
}
}
}

View File

@ -29,7 +29,8 @@
"backspace": "core:delete-config-selection",
"delete": "core:delete-config-selection",
"ctrl-a": "core:select-all-config-nodes",
"ctrl-z": "core:undo"
"ctrl-z": "core:undo",
"ctrl-shift-z": "core:redo"
},
"red-ui-workspace": {
"backspace": "core:delete-selection",
@ -50,6 +51,7 @@
"shift-down": "core:step-selection-down",
"shift-left": "core:step-selection-left",
"ctrl-shift-j": "core:show-previous-tab",
"ctrl-shift-k": "core:show-next-tab"
"ctrl-shift-k": "core:show-next-tab",
"ctrl-shift-z": "core:redo"
}
}

View File

@ -406,6 +406,7 @@ RED.view = (function() {
RED.actions.add("core:delete-selection",deleteSelection);
RED.actions.add("core:edit-selected-node",editSelection);
RED.actions.add("core:undo",RED.history.pop);
RED.actions.add("core:redo",RED.history.redo);
RED.actions.add("core:select-all-nodes",selectAll);
RED.actions.add("core:zoom-in",zoomIn);
RED.actions.add("core:zoom-out",zoomOut);