From d6bc70c6a8b0a96f3acf819572c067eb3ec2d1d3 Mon Sep 17 00:00:00 2001 From: newcat Date: Sun, 31 Dec 2023 03:31:11 +0100 Subject: [PATCH] Fix Copy-Paste does not retain connection involving dynamic interfaces. #343 --- packages/renderer-vue/src/clipboard.ts | 39 +++++++++++++------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/packages/renderer-vue/src/clipboard.ts b/packages/renderer-vue/src/clipboard.ts index d8458d6d..a2acf1ea 100644 --- a/packages/renderer-vue/src/clipboard.ts +++ b/packages/renderer-vue/src/clipboard.ts @@ -24,7 +24,7 @@ export interface IClipboard { export function useClipboard( displayedGraph: Ref, editor: Ref, - commandHandler: ICommandHandler + commandHandler: ICommandHandler, ): IClipboard { const token = Symbol("ClipboardToken"); @@ -47,9 +47,9 @@ export function useClipboard( const connections = displayedGraph.value.connections .filter( - (conn) => interfacesOfSelectedNodes.includes(conn.from) || interfacesOfSelectedNodes.includes(conn.to) + (conn) => interfacesOfSelectedNodes.includes(conn.from) || interfacesOfSelectedNodes.includes(conn.to), ) - .map((conn) => ({ from: conn.from.id, to: conn.to.id } as IConnectionState)); + .map((conn) => ({ from: conn.from.id, to: conn.to.id }) as IConnectionState); connectionBuffer.value = JSON.stringify(connections); nodeBuffer.value = JSON.stringify(displayedGraph.value.selectedNodes.map((n) => n.save())); @@ -58,7 +58,7 @@ export function useClipboard( const findInterface = ( nodes: AbstractNode[], id: string, - io?: "input" | "output" + io?: "input" | "output", ): NodeInterface | undefined => { for (const n of nodes) { let intf: NodeInterface | undefined; @@ -103,21 +103,6 @@ export function useClipboard( const generatedId = copiedNode.id; newNodes.push(copiedNode); - const tapInterfaces = (intfs: Record>) => { - Object.values(intfs).forEach((intf) => { - intf.hooks.load.subscribe(token, (intfState) => { - const newIntfId = uuidv4(); - idmap.set(intfState.id, newIntfId); - intf.id = newIntfId; - intf.hooks.load.unsubscribe(token); - return intfState; - }); - }); - }; - - tapInterfaces(copiedNode.inputs); - tapInterfaces(copiedNode.outputs); - copiedNode.hooks.beforeLoad.subscribe(token, (nodeState) => { const ns = nodeState as any; if (ns.position) { @@ -132,6 +117,17 @@ export function useClipboard( copiedNode.load({ ...oldNode, id: generatedId }); copiedNode.id = generatedId; idmap.set(oldNode.id, generatedId); + + for (const intf of Object.values(copiedNode.inputs)) { + const newIntfId = uuidv4(); + idmap.set(intf.id, newIntfId); + intf.id = newIntfId; + } + for (const intf of Object.values(copiedNode.outputs)) { + const newIntfId = uuidv4(); + idmap.set(intf.id, newIntfId); + intf.id = newIntfId; + } } for (const c of parsedConnectionBuffer) { @@ -146,6 +142,9 @@ export function useClipboard( } } + // select all new nodes + displayedGraph.value.selectedNodes = newNodes; + commandHandler.executeCommand(COMMIT_TRANSACTION_COMMAND); return { @@ -155,7 +154,7 @@ export function useClipboard( }; commandHandler.registerCommand(COPY_COMMAND, { - canExecute: () => true, + canExecute: () => displayedGraph.value.selectedNodes.length > 0, execute: copy, }); commandHandler.registerHotkey(["Control", "c"], COPY_COMMAND);