From c3264b85c1d6fab1ff2f789e90aefe9dcf83b907 Mon Sep 17 00:00:00 2001 From: hku Date: Mon, 12 Feb 2024 11:06:51 +0800 Subject: [PATCH 1/3] improve litegraph.core.js to include associated groups when 'Save Selected as Template' --- web/lib/litegraph.core.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/web/lib/litegraph.core.js b/web/lib/litegraph.core.js index 4ff05ae81..cf97e6b62 100644 --- a/web/lib/litegraph.core.js +++ b/web/lib/litegraph.core.js @@ -7139,7 +7139,8 @@ LGraphNode.prototype.executeAction = function(action) LGraphCanvas.prototype.copyToClipboard = function(nodes) { var clipboard_info = { nodes: [], - links: [] + links: [], + groups: [] }; var index = 0; var selected_nodes_array = []; @@ -7153,6 +7154,8 @@ LGraphNode.prototype.executeAction = function(action) index += 1; } + var _selected_groups = []; + for (var i = 0; i < selected_nodes_array.length; ++i) { var node = selected_nodes_array[i]; var cloned = node.clone(); @@ -7162,6 +7165,15 @@ LGraphNode.prototype.executeAction = function(action) continue; } clipboard_info.nodes.push(cloned.serialize()); + + for (var group of this.graph._groups) { + group.recomputeInsideNodes(); + if (group._nodes?.includes(node) && (!_selected_groups.includes(group))) { + _selected_groups.push(group); + clipboard_info.groups.push(group.serialize()); + } + } + if (node.inputs && node.inputs.length) { for (var j = 0; j < node.inputs.length; ++j) { var input = node.inputs[j]; @@ -7243,6 +7255,21 @@ LGraphNode.prototype.executeAction = function(action) nodes.push(node); } } + + //restore groups + if (clipboard_info.groups) { + for (var i = 0; i < clipboard_info.groups.length; ++i) { + var group = new LiteGraph.LGraphGroup(); + var group_data = clipboard_info.groups[i]; + group.configure(group_data); + + group.pos[0] += this.graph_mouse[0] - posMin[0]; //+= 5; + group.pos[1] += this.graph_mouse[1] - posMin[1]; //+= 5; + app.graph.add(group,{doProcessChange:false}); + } + } + + //create links for (var i = 0; i < clipboard_info.links.length; ++i) { From de5055c98151e9e45522e73eae9dda7616918595 Mon Sep 17 00:00:00 2001 From: hku Date: Wed, 14 Feb 2024 02:10:01 +0800 Subject: [PATCH 2/3] copy group to clipboard only when all nodes in the group are selected --- web/lib/litegraph.core.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/web/lib/litegraph.core.js b/web/lib/litegraph.core.js index cf97e6b62..911e61b21 100644 --- a/web/lib/litegraph.core.js +++ b/web/lib/litegraph.core.js @@ -7166,13 +7166,6 @@ LGraphNode.prototype.executeAction = function(action) } clipboard_info.nodes.push(cloned.serialize()); - for (var group of this.graph._groups) { - group.recomputeInsideNodes(); - if (group._nodes?.includes(node) && (!_selected_groups.includes(group))) { - _selected_groups.push(group); - clipboard_info.groups.push(group.serialize()); - } - } if (node.inputs && node.inputs.length) { for (var j = 0; j < node.inputs.length; ++j) { @@ -7200,6 +7193,17 @@ LGraphNode.prototype.executeAction = function(action) } } } + + //if every node in the group are selected, then the group is "selected" + for (var group of this.graph._groups) { + group.recomputeInsideNodes(); + if (group._nodes?.every(function(node){ + return selected_nodes_array.includes(node) + })) { + clipboard_info.groups.push(group.serialize()); + } + } + localStorage.setItem( "litegrapheditor_clipboard", JSON.stringify(clipboard_info) From dfbe08181d92311d99e822e89cf3bf9a3691cb06 Mon Sep 17 00:00:00 2001 From: hku Date: Wed, 14 Feb 2024 02:11:28 +0800 Subject: [PATCH 3/3] copy group to clipboard only when all nodes in the group are selected --- web/lib/litegraph.core.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/web/lib/litegraph.core.js b/web/lib/litegraph.core.js index 911e61b21..0846c7c3a 100644 --- a/web/lib/litegraph.core.js +++ b/web/lib/litegraph.core.js @@ -7154,8 +7154,6 @@ LGraphNode.prototype.executeAction = function(action) index += 1; } - var _selected_groups = []; - for (var i = 0; i < selected_nodes_array.length; ++i) { var node = selected_nodes_array[i]; var cloned = node.clone();