From fdd8564c07c427b2834a52ad51dd55c5ed39d88d Mon Sep 17 00:00:00 2001 From: AustinMroz Date: Fri, 15 Aug 2025 16:03:29 -0500 Subject: [PATCH] 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 --- src/lib/litegraph/src/LGraphCanvas.ts | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index 19b32b630..ac0a9b8ed 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -3608,6 +3608,7 @@ export class LGraphCanvas subgraphs: [] } + // NOTE: logic for traversing nested subgraphs depends on this being a set. const subgraphs = new Set() // Create serialisable objects @@ -3646,8 +3647,13 @@ export class LGraphCanvas } // Add unique subgraph entries - // TODO: Must find all nested subgraphs + // NOTE: subgraphs is appended to mid iteration. for (const subgraph of subgraphs) { + for (const node of subgraph.nodes) { + if (node instanceof SubgraphNode) { + subgraphs.add(node.subgraph) + } + } const cloned = subgraph.clone(true).asSerialisable() serialisable.subgraphs.push(cloned) } @@ -3764,12 +3770,19 @@ export class LGraphCanvas created.push(group) } + // Update subgraph ids with nesting + function updateSubgraphIds(nodes: { type: string }[]) { + for (const info of nodes) { + const subgraph = results.subgraphs.get(info.type) + if (!subgraph) continue + info.type = subgraph.id + updateSubgraphIds(subgraph.nodes) + } + } + updateSubgraphIds(parsed.nodes) + // Nodes for (const info of parsed.nodes) { - // If the subgraph was cloned, update references to use the new subgraph ID. - const subgraph = results.subgraphs.get(info.type) - if (subgraph) info.type = subgraph.id - const node = info.type == null ? null : LiteGraph.createNode(info.type) if (!node) { // failedNodes.push(info)