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

[groups] Support undo of convert to subflow from inside group

This commit is contained in:
Nick O'Leary 2020-03-16 22:51:54 +00:00
parent a8bc753720
commit 9b2e9ec41a
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
5 changed files with 70 additions and 17 deletions

View File

@ -279,7 +279,7 @@ RED.history = (function() {
}
}
if (ev.addToGroup) {
RED.group.removeFromGroup(ev.addToGroup,ev.nodes.map(function(n) { return n.n }));
RED.group.removeFromGroup(ev.addToGroup,ev.nodes.map(function(n) { return n.n }),true);
inverseEv.removeFromGroup = ev.addToGroup;
} else if (ev.removeFromGroup) {
RED.group.addToGroup(ev.removeFromGroup,ev.nodes.map(function(n) { return n.n }));
@ -438,6 +438,9 @@ RED.history = (function() {
if (ev.subflow) {
RED.nodes.addSubflow(ev.subflow.subflow);
inverseEv.subflow = ev.subflow;
if (ev.subflow.subflow.g) {
RED.group.addToGroup(RED.nodes.group(ev.subflow.subflow.g),ev.subflow.subflow);
}
}
if (ev.subflows) {
inverseEv.nodes = [];
@ -465,7 +468,7 @@ RED.history = (function() {
RED.nodes.addLink(ev.links[i]);
}
}
if (ev.createdLinks) {
if (ev.createdLinks) {
inverseEv.removedLinks = [];
for (i=0;i<ev.createdLinks.length;i++) {
inverseEv.removedLinks.push(ev.createdLinks[i]);
@ -512,17 +515,19 @@ RED.history = (function() {
t: "removeFromGroup",
dirty: RED.nodes.dirty(),
group: ev.group,
nodes: ev.nodes
nodes: ev.nodes,
reparent: ev.reparent
}
if (ev.nodes) {
RED.group.removeFromGroup(ev.group,ev.nodes);
RED.group.removeFromGroup(ev.group,ev.nodes,(ev.hasOwnProperty('reparent')&&ev.hasOwnProperty('reparent')!==undefined)?ev.reparent:true);
}
} else if (ev.t == "removeFromGroup") {
inverseEv = {
t: "addToGroup",
dirty: RED.nodes.dirty(),
group: ev.group,
nodes: ev.nodes
nodes: ev.nodes,
reparent: ev.reparent
}
if (ev.nodes) {
RED.group.addToGroup(ev.group,ev.nodes);

View File

@ -1634,7 +1634,7 @@ RED.nodes = (function() {
addGroup: addGroup,
removeGroup: removeGroup,
group: function(id) { return groups[id] },
groups: function(z) { return groupsByZ[z] },
groups: function(z) { return groupsByZ[z]||[] },
eachNode: function(cb) {
for (var n=0;n<nodes.length;n++) {

View File

@ -61,9 +61,9 @@ RED.group = (function() {
RED.events.on("view:selection-changed",function(selection) {
RED.menu.setDisabled("menu-item-group-group",!!!selection.nodes);
RED.menu.setDisabled("menu-item-group-ungroup",!!!selection.groups);
RED.menu.setDisabled("menu-item-group-ungroup",!!!selection.nodes || selection.nodes.filter(function(n) { return n.type==='group'}).length === 0);
RED.menu.setDisabled("menu-item-group-merge",!!!selection.nodes);
RED.menu.setDisabled("menu-item-group-remove",!!!selection.groups);
RED.menu.setDisabled("menu-item-group-remove",!!!selection.nodes || selection.nodes.filter(function(n) { return !!n.g }).length === 0);
});
RED.actions.add("core:group-selection", function() { groupSelection() })
@ -202,7 +202,7 @@ RED.group = (function() {
var parentGroup = RED.nodes.group(selection.nodes[0].g);
if (parentGroup) {
try {
removeFromGroup(parentGroup,selection.nodes);
removeFromGroup(parentGroup,selection.nodes,true);
var historyEvent = {
t: "removeFromGroup",
dirty: RED.nodes.dirty(),
@ -306,7 +306,7 @@ RED.group = (function() {
group.h = Math.max(group.h,n.y+n.h/2+25-group.y);
}
}
function removeFromGroup(group, nodes) {
function removeFromGroup(group, nodes, reparent) {
if (!Array.isArray(nodes)) {
nodes = [nodes];
}
@ -319,7 +319,6 @@ RED.group = (function() {
return;
}
}
var parentGroup = RED.nodes.group(group.g);
for (var i=0; i<nodes.length; i++) {
n = nodes[i];
@ -327,13 +326,14 @@ RED.group = (function() {
var index = group.nodes.indexOf(n);
group.nodes.splice(index,1);
group.dirty = true;
if (group.g) {
if (reparent && group.g) {
n.g = group.g
parentGroup.nodes.push(n);
parentGroup.dirty = true;
} else {
delete n.g;
}
}
}

View File

@ -569,9 +569,11 @@ RED.subflow = (function() {
var i,n;
var nodeList = new Set();
var tmplist = selection.nodes.slice();
var includedGroups = new Set();
while(tmplist.length > 0) {
n = tmplist.shift();
if (n.type === "group") {
includedGroups.add(n.id);
tmplist = tmplist.concat(n.nodes);
}
nodeList.add(n);
@ -579,7 +581,20 @@ RED.subflow = (function() {
nodeList = Array.from(nodeList);
var containingGroup = nodeList[0].g;
var nodesMovedFromGroup = [];
for (i=0; i<nodeList.length;i++) {
if (nodeList[i].g && !includedGroups.has(nodeList[i].g)) {
if (containingGroup !== nodeList[i].g) {
RED.notify("Cannot create subflow across multiple groups","error");
return;
}
}
}
if (containingGroup) {
containingGroup = RED.nodes.group(containingGroup);
}
var nodes = {};
var new_links = [];
var removedLinks = [];
@ -703,6 +718,20 @@ RED.subflow = (function() {
RED.editor.validateNode(subflowInstance);
RED.nodes.add(subflowInstance);
if (containingGroup) {
RED.group.addToGroup(containingGroup, subflowInstance);
nodeList.forEach(function(nl) {
if (nl.g === containingGroup.id) {
delete nl.g;
var index = containingGroup.nodes.indexOf(nl);
containingGroup.nodes.splice(index,1);
nodesMovedFromGroup.push(nl);
}
})
containingGroup.dirty = true;
}
candidateInputs.forEach(function(l) {
var link = {source:l.source, sourcePort:l.sourcePort, target: subflowInstance};
new_links.push(link);
@ -758,7 +787,8 @@ RED.subflow = (function() {
RED.nodes.moveNodeToTab(n, subflow.id);
}
RED.history.push({
var historyEvent = {
t:'createSubflow',
nodes:[subflowInstance.id],
links:new_links,
@ -772,11 +802,29 @@ RED.subflow = (function() {
removedLinks: removedLinks,
dirty:RED.nodes.dirty()
});
RED.view.select(null);
}
if (containingGroup) {
historyEvent = {
t:'multi',
events: [ historyEvent ]
}
historyEvent.events.push({
t:'addToGroup',
group: containingGroup,
nodes: [subflowInstance]
})
historyEvent.events.push({
t:'removeFromGroup',
group: containingGroup,
nodes: nodesMovedFromGroup,
reparent: false
})
}
RED.history.push(historyEvent);
RED.editor.validateNode(subflow);
RED.nodes.dirty(true);
RED.view.redraw(true);
RED.view.updateActive();
RED.view.select(null);
}

View File

@ -4467,7 +4467,7 @@ if (DEBUG_EVENTS) { console.warn("nodeMouseDown", mouse_mode,d); }
mouse_mode = state;
}
},
updateActive: updateActiveNodes,
redraw: function(updateActive) {
if (updateActive) {
updateActiveNodes();