Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 40 additions & 13 deletions src/canvas/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,25 @@ export function pasteFragment(
document: CanvasDocument,
fragment: CanvasFragment,
offset: Point = { x: 40, y: 40 },
mode: "paste" | "duplicate" = "paste",
): { document: CanvasDocument; ids: string[] } {
if (!Number.isFinite(offset.x) || !Number.isFinite(offset.y))
throw new Error(tNow("errors.operations.pasteOffset"));
const byId = indexNodes(fragment.nodes);
const boundaryEdges = mode === "duplicate"
? document.edges.filter((edge) =>
edge.intent !== "post_edit_result" &&
edge.referenceBinding !== "pinned" &&
byId.has(edge.source) !== byId.has(edge.target))
: [];
const incomingBoundaryIds = new Set(boundaryEdges.map((edge) => edge.id));
const incomingSourceIds = new Map<string, Set<string>>();
for (const edge of boundaryEdges) {
if (!byId.has(edge.target) || edge.intent === "assembly_input") continue;
const sources = incomingSourceIds.get(edge.target) ?? new Set<string>();
sources.add(edge.source);
incomingSourceIds.set(edge.target, sources);
}
for (const node of fragment.nodes) ancestors(node, byId);
for (const node of fragment.nodes)
if (node.data.stack?.nodeIds.some((id) => !byId.has(id)))
Expand Down Expand Up @@ -391,7 +406,7 @@ export function pasteFragment(
copy.data.title = derivedNodeTitle(node.data.title, titles);
if (copy.type === "image") copy.data.copiedSourceId = node.id;
copy.data = remapDataReferences(copy.data, remapped);
if (copy.data.promptContent && copy.data.settings && node.data.settings) {
if (copy.data.settings && node.data.settings) {
for (const field of ["firstFrameNodeId", "lastFrameNodeId"] as const) {
const original = node.data.settings[field];
if (
Expand All @@ -400,9 +415,10 @@ export function pasteFragment(
[...document.nodes, ...(document.hiddenNodes ?? [])].some(
(source) => source.id === original,
) &&
copy.data.promptContent.some(
(part) => part.type === "reference" && part.nodeId === original,
)
(incomingSourceIds.get(node.id)?.has(original) ||
copy.data.promptContent?.some(
(part) => part.type === "reference" && part.nodeId === original,
))
)
copy.data.settings = { ...copy.data.settings, [field]: original };
}
Expand Down Expand Up @@ -430,10 +446,15 @@ export function pasteFragment(
return copy;
});
let ordered = orderNodesForCanvas(nodes);
const edges = fragment.edges.map((edge) => ({
const incomingEdges = mode === "duplicate"
? document.edges.filter((edge) =>
edgeIds.has(edge.id) ||
(byId.has(edge.target) && incomingBoundaryIds.has(edge.id)))
: fragment.edges;
const edges = incomingEdges.map((edge) => ({
...structuredClone(edge),
id: newId(),
source: remapped.get(edge.source)!,
source: remapped.get(edge.source) ?? edge.source,
target: remapped.get(edge.target)!,
}));
const knownSources = new Set(
Expand Down Expand Up @@ -481,14 +502,20 @@ export function pasteFragment(
data: { ...node.data, ...generationContentPatch(content) },
};
});
return {
document: {
...document,
nodes: [...document.nodes, ...ordered],
edges: [...document.edges, ...edges],
},
ids: ordered.map((node) => node.id),
let next = {
...document,
nodes: [...document.nodes, ...ordered],
edges: [...document.edges, ...edges],
};
for (const edge of boundaryEdges) {
const source = remapped.get(edge.source);
if (source)
next = applyCommand(next, {
type: "add-edge",
edge: { ...edge, id: newId(), source },
});
}
return { document: next, ids: ordered.map((node) => node.id) };
}

/** Rewrites node identities only; retained media resource IDs and sources are not node identities. */
Expand Down
17 changes: 11 additions & 6 deletions src/canvas/workspace/useWorkspaceTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,11 @@ export function useWorkspaceTransfer(rt: WorkspaceRuntime, readOnly: boolean) {
void pasteFromProject(fragment, media);
return;
}
insertFragment(fragment, 40 * (duplicate ? 1 : ++pasteCount.current));
insertFragment(
fragment,
40 * (duplicate ? 1 : ++pasteCount.current),
duplicate ? "duplicate" : "paste",
);
};
const pasteFromProject = async (
fragment: CanvasFragment,
Expand Down Expand Up @@ -540,12 +544,13 @@ export function useWorkspaceTransfer(rt: WorkspaceRuntime, readOnly: boolean) {
pasting.current = false;
}
};
const insertFragment = (fragment: CanvasFragment, distance: number) => {
const insertFragment = (
fragment: CanvasFragment,
distance: number,
mode: "paste" | "duplicate" = "paste",
) => {
const before = historyRef.current.present;
const result = pasteFragment(before, fragment, {
x: distance,
y: distance,
});
const result = pasteFragment(before, fragment, { x: distance, y: distance }, mode);
change(() => result.document);
// Keep each paste in its own durable undo step, even inside the autosave debounce.
void persist(before, false);
Expand Down
13 changes: 13 additions & 0 deletions tests/browser/first_last_frame_connections.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,19 @@ try {
(value) => value === images[1],
"last frame after reload",
);
await node(videoId).locator(".cw-node-title").click({ button: "right" });
await page.getByText("复制副本", { exact: true }).click();
const duplicated = await until(
snapshot,
(value) => value.references.filter((ref) => ref.active).length === 4,
"duplicate retained both frame inputs",
);
for (const [index, role] of ["first_frame", "last_frame"].entries()) {
const copied = duplicated.references.find((ref) =>
ref.active && ref.role === role &&
!saved.references.some((original) => original.id === ref.id));
assert.equal(copied?.source.id, images[index]);
}
assert.deepEqual(errors, []);
console.log("first/last frame connections ok");
} catch (error) {
Expand Down
31 changes: 31 additions & 0 deletions tests/canvasOperations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,37 @@ test("paste remaps nested identity and references, offsets roots once, and suppo
assert.equal(source.nodes.length, 3);
});

test("duplicate retains incoming, internal and outgoing connections with fresh identities", () => {
const image = (x: number) => createNode("image", { x, y: 0 }, {
src: "data:image/png;base64,AA==",
});
const [upstream, first, second] = [image(0), image(300), image(600)];
const downstream = createNode("video", { x: 900, y: 0 }, {
settings: { videoMode: "reference" },
});
const edges = [
{ id: "in", source: upstream.id, target: second.id, targetHandle: "in" },
{ id: "internal", source: first.id, target: second.id },
{ id: "out", source: second.id, target: downstream.id },
];
const document = { ...createDocument(), nodes: [upstream, first, second, downstream], edges };
const fragment = copySelection(document, [first.id, second.id]);
const pasted = pasteFragment(document, fragment);
assert.equal(pasted.document.edges.length, 4);

const duplicate = pasteFragment(document, fragment, undefined, "duplicate");
const [firstCopy, secondCopy] = duplicate.ids;
assert.deepEqual(duplicate.document.edges.slice(0, 3), edges);
const added = duplicate.document.edges.slice(3);
assert.deepEqual(added.map(({ source, target }) => [source, target]), [
[upstream.id, secondCopy],
[firstCopy, secondCopy],
[secondCopy, downstream.id],
]);
assert.equal(added[0]?.targetHandle, "in");
assert.equal(new Set(duplicate.document.edges.map(({ id }) => id)).size, 6);
});

test("paste orders parents first and rejects incomplete or cyclic fragments without mutating the destination", () => {
const document = createDocument();
const parent = createNode("group");
Expand Down
Loading