Skip to content

Commit ce76be5

Browse files
feat(studio): paste and duplicate can keep a clip at its own time (#4859)
* feat(studio): paste and duplicate can keep a clip's own time, on the next free track * fix(studio): an in-place paste uses the copied time only in the composition it was copied in * test(studio): an in-place duplicate inside a sub-composition keeps its local time * refactor(studio): the paste anchor is its own function, and the in-place tests share their setup * refactor(studio): paste names its undo step and note in one place, and two paste tests share their steps
1 parent e666a8a commit ce76be5

3 files changed

Lines changed: 254 additions & 144 deletions

File tree

‎packages/studio/src/hooks/useClipboard.source.test.ts‎

Lines changed: 99 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ function mountClipboard(
9898
sub = SUB,
9999
bundle: (host: Element) => void = () => {},
100100
saved = SAVED,
101+
view = { path: "index.html" },
101102
) {
102103
const files: Record<string, string> = { "index.html": saved, "compositions/sub.html": sub };
103104
const fail = { on: false };
@@ -115,7 +116,7 @@ function mountClipboard(
115116
function Harness() {
116117
api = useClipboard({
117118
projectId: "p",
118-
activeCompPath: "index.html",
119+
activeCompPath: view.path,
119120
domEditSelectionRef: domSelectionRef,
120121
showToast: () => {},
121122
writeProjectFile,
@@ -134,7 +135,9 @@ function mountClipboard(
134135
root = createRoot(document.createElement("div"));
135136
act(() => root?.render(React.createElement(Harness)));
136137
const clipboard = () => api as ReturnType<typeof useClipboard>;
138+
const rerender = () => act(() => root?.render(React.createElement(Harness)));
137139
return {
140+
rerender,
138141
clipboard,
139142
writes,
140143
deleted,
@@ -253,6 +256,17 @@ const SUB_SELECTION = {
253256
sourceFile: "compositions/sub.html",
254257
} as DomEditSelection;
255258

259+
const copySubNext = ({ clipboard, domSelectionRef }: ReturnType<typeof mountClipboard>) => {
260+
clearSelection();
261+
domSelectionRef.current = SUB_SELECTION;
262+
clipboard().handleCopy();
263+
};
264+
265+
const expectSubPastedAlone = (writes: string[]) => {
266+
expect(writes[0]).toContain(">Sub</h2>");
267+
expect(writes[0]?.match(/>Title<\/h1>/g)).toHaveLength(1);
268+
};
269+
256270
describe("paste of an element styled by its id", () => {
257271
it("gives the copy the look its original has", async () => {
258272
clearSelection();
@@ -339,30 +353,24 @@ describe("copy order", () => {
339353

340354
it("pastes the second of two copies", async () => {
341355
selectTitle();
342-
const { clipboard, writes, domSelectionRef } = mountClipboard();
343-
clipboard().handleCopy();
356+
const view = mountClipboard();
357+
view.clipboard().handleCopy();
344358
await new Promise((resolve) => setTimeout(resolve, 10));
345-
clearSelection();
346-
domSelectionRef.current = SUB_SELECTION;
347-
clipboard().handleCopy();
348-
await clipboard().handlePaste();
349-
expect(writes[0]).toContain(">Sub</h2>");
350-
expect(writes[0]?.match(/>Title<\/h1>/g)).toHaveLength(1);
359+
copySubNext(view);
360+
await view.clipboard().handlePaste();
361+
expectSubPastedAlone(view.writes);
351362
});
352363

353364
it("pastes the later copy even when the earlier copy's read lands last", async () => {
354365
selectTitle();
355-
const { clipboard, writes, delayMs, domSelectionRef } = mountClipboard();
356-
delayMs["index.html"] = 20;
357-
clipboard().handleCopy();
358-
clearSelection();
359-
domSelectionRef.current = SUB_SELECTION;
360-
clipboard().handleCopy();
366+
const view = mountClipboard();
367+
view.delayMs["index.html"] = 20;
368+
view.clipboard().handleCopy();
369+
copySubNext(view);
361370
await new Promise((resolve) => setTimeout(resolve, 40));
362-
delayMs["index.html"] = 0;
363-
await clipboard().handlePaste();
364-
expect(writes[0]).toContain(">Sub</h2>");
365-
expect(writes[0]?.match(/>Title<\/h1>/g)).toHaveLength(1);
371+
view.delayMs["index.html"] = 0;
372+
await view.clipboard().handlePaste();
373+
expectSubPastedAlone(view.writes);
366374
});
367375

368376
it("duplicates a hidden clip with its saved markup", async () => {
@@ -374,6 +382,78 @@ describe("copy order", () => {
374382
});
375383
});
376384

385+
describe("a copy in place, as the canvas makes one", () => {
386+
// The copy's start and track: the saved original has no data-hf-id, its copy is minted one.
387+
const copyAt = (html: string | undefined) => {
388+
const copy = html?.match(/<h1[^>]*data-hf-id[^>]*>Title<\/h1>/)?.[0] ?? "";
389+
return [copy.match(/data-start="([^"]*)"/)?.[1], copy.match(/data-track-index="([^"]*)"/)?.[1]];
390+
};
391+
392+
it("duplicates at the clip's own time, on the next free track", async () => {
393+
selectTitle();
394+
const { clipboard, writes } = mountClipboard();
395+
await clipboard().handleDuplicate({ inPlace: true });
396+
expect(copyAt(writes[0])).toEqual(["2", "1"]);
397+
});
398+
399+
it("pastes at the copied clip's time, not the playhead's", async () => {
400+
selectTitle();
401+
const { clipboard, writes } = mountClipboard();
402+
clipboard().handleCopy();
403+
await clipboard().handlePaste({ inPlace: true });
404+
expect(copyAt(writes[0])).toEqual(["2", "1"]);
405+
});
406+
407+
// Host at 2 s: the clip's local 1-3 s shows as a 3-5 s master row, the clock the film's own timeline has.
408+
const SUB_CLIP: TimelineElement = {
409+
id: SUB_HF_ID,
410+
hfId: SUB_HF_ID,
411+
tag: "h2",
412+
start: 3,
413+
duration: 2,
414+
track: 0,
415+
authoredTrack: 0,
416+
sourceFile: "compositions/sub.html",
417+
parentCompositionStart: 2,
418+
};
419+
const pastedSubStarts = (html: string | undefined) =>
420+
[...(html ?? "").matchAll(/<h2[^>]*data-hf-id[^>]*data-start="([^"]+)"/g)].map((m) => m[1]);
421+
const mountSubClip = (currentTime = 0, view = { path: "index.html" }) => {
422+
usePlayerStore.setState({
423+
elements: [TITLE, SUB_CLIP],
424+
selectedElementId: SUB_CLIP.id,
425+
selectedElementIds: new Set([SUB_CLIP.id]),
426+
currentTime,
427+
});
428+
return mountClipboard(null, SUB, (host) => host.setAttribute("data-start", "2"), SAVED, view);
429+
};
430+
431+
it("pastes a sub-composition's clip into the film at the moment the film shows it", async () => {
432+
const { clipboard, writes } = mountSubClip(7);
433+
clipboard().handleCopy();
434+
await clipboard().handlePaste({ inPlace: true });
435+
expect(pastedSubStarts(writes[0])).toEqual(["3"]);
436+
});
437+
438+
it("duplicates a sub-composition's clip at its own local time, on another track", async () => {
439+
const { clipboard, writes } = mountSubClip();
440+
await clipboard().handleDuplicate({ inPlace: true });
441+
const copy = /<h2[^>]*data-hf-id[^>]*>/.exec(writes[0] ?? "")?.[0] ?? "";
442+
expect(copy).toContain('data-start="1"');
443+
expect(copy).toContain('data-track-index="1"');
444+
});
445+
446+
it("pastes at the playhead once the edited composition is another, whose clock differs", async () => {
447+
const view = { path: "index.html" };
448+
const { clipboard, writes, rerender } = mountSubClip(0.5, view);
449+
clipboard().handleCopy();
450+
view.path = "compositions/sub.html";
451+
rerender();
452+
await clipboard().handlePaste({ inPlace: true });
453+
expect(pastedSubStarts(writes[0])).toEqual(["0.5"]);
454+
});
455+
});
456+
377457
describe("duplicate of a clip inside a sub-composition", () => {
378458
it("writes the copy at local time right after the original", async () => {
379459
// Host at 2 s, so the clip's local 1-3 s shows as a 3-5 s master row.

0 commit comments

Comments
 (0)