Skip to content

Commit

Permalink
Fix Copy-Paste does not retain connection involving dynamic interfaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
newcat committed Dec 31, 2023
1 parent 657a341 commit d6bc70c
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions packages/renderer-vue/src/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface IClipboard {
export function useClipboard(
displayedGraph: Ref<Graph>,
editor: Ref<Editor>,
commandHandler: ICommandHandler
commandHandler: ICommandHandler,
): IClipboard {
const token = Symbol("ClipboardToken");

Expand All @@ -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()));
Expand All @@ -58,7 +58,7 @@ export function useClipboard(
const findInterface = (
nodes: AbstractNode[],
id: string,
io?: "input" | "output"
io?: "input" | "output",
): NodeInterface<any> | undefined => {
for (const n of nodes) {
let intf: NodeInterface<any> | undefined;
Expand Down Expand Up @@ -103,21 +103,6 @@ export function useClipboard(
const generatedId = copiedNode.id;
newNodes.push(copiedNode);

const tapInterfaces = (intfs: Record<string, NodeInterface<any>>) => {
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) {
Expand All @@ -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) {
Expand All @@ -146,6 +142,9 @@ export function useClipboard(
}
}

// select all new nodes
displayedGraph.value.selectedNodes = newNodes;

commandHandler.executeCommand<CommitTransactionCommand>(COMMIT_TRANSACTION_COMMAND);

return {
Expand All @@ -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);
Expand Down

0 comments on commit d6bc70c

Please sign in to comment.