From a7cbf1b90c5a6d005f2d732c201456711ee21028 Mon Sep 17 00:00:00 2001 From: Comfy Org PR Bot Date: Sat, 16 Aug 2025 05:26:53 +0800 Subject: [PATCH] Deep copy subgraphs to clipboard, update nested ids on paste (#5003) (#5022) * 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 Co-authored-by: AustinMroz --- 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 dc0fbf6b3..5240cedba 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -3607,6 +3607,7 @@ export class LGraphCanvas subgraphs: [] } + // NOTE: logic for traversing nested subgraphs depends on this being a set. const subgraphs = new Set() // Create serialisable objects @@ -3645,8 +3646,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) } @@ -3763,12 +3769,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)