Skip to content

Commit fdd8564

Browse files
authored
Deep copy subgraphs to clipboard, update nested ids on paste (#5003)
* Deep copy to clipboard, update nested ids on paste The copyToClipboard function wasn't walking subgraphs and leaving nested subgraphs unserialized. This has now been fixed. This requires that equivalent support be added to _pasteFromClipboard to update the ids of nested subgraphs which are pasted. * Add extra advisory comments
1 parent d18081a commit fdd8564

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/lib/litegraph/src/LGraphCanvas.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3608,6 +3608,7 @@ export class LGraphCanvas
36083608
subgraphs: []
36093609
}
36103610

3611+
// NOTE: logic for traversing nested subgraphs depends on this being a set.
36113612
const subgraphs = new Set<Subgraph>()
36123613

36133614
// Create serialisable objects
@@ -3646,8 +3647,13 @@ export class LGraphCanvas
36463647
}
36473648

36483649
// Add unique subgraph entries
3649-
// TODO: Must find all nested subgraphs
3650+
// NOTE: subgraphs is appended to mid iteration.
36503651
for (const subgraph of subgraphs) {
3652+
for (const node of subgraph.nodes) {
3653+
if (node instanceof SubgraphNode) {
3654+
subgraphs.add(node.subgraph)
3655+
}
3656+
}
36513657
const cloned = subgraph.clone(true).asSerialisable()
36523658
serialisable.subgraphs.push(cloned)
36533659
}
@@ -3764,12 +3770,19 @@ export class LGraphCanvas
37643770
created.push(group)
37653771
}
37663772

3773+
// Update subgraph ids with nesting
3774+
function updateSubgraphIds(nodes: { type: string }[]) {
3775+
for (const info of nodes) {
3776+
const subgraph = results.subgraphs.get(info.type)
3777+
if (!subgraph) continue
3778+
info.type = subgraph.id
3779+
updateSubgraphIds(subgraph.nodes)
3780+
}
3781+
}
3782+
updateSubgraphIds(parsed.nodes)
3783+
37673784
// Nodes
37683785
for (const info of parsed.nodes) {
3769-
// If the subgraph was cloned, update references to use the new subgraph ID.
3770-
const subgraph = results.subgraphs.get(info.type)
3771-
if (subgraph) info.type = subgraph.id
3772-
37733786
const node = info.type == null ? null : LiteGraph.createNode(info.type)
37743787
if (!node) {
37753788
// failedNodes.push(info)

0 commit comments

Comments
 (0)