Files
hyperframes/packages/studio/src/utils/clipboardPayload.test.ts
Miguel Ángel 0b6d1039ed feat(studio): copy, paste and duplicate clip groups on the timeline (#4064)
* feat(studio): copy, paste and duplicate clip groups on the timeline

Cmd+C on a multi-clip selection now copies all of them, not just the
first. Cmd+V pastes the group at the playhead keeping each clip's offset
from the earliest one and its original track when free. Cmd+D duplicates
the selection in place. Context menu gains Copy/Paste/Duplicate, and a
failed copy now toasts why instead of silently returning false.

Root cause was a DOM lookup that couldn't address a composition-instance
clip's root; group copy reuses the composition-aware resolver every other
timeline editor already uses. Also fixes a duplicated data-hf-id on
pasted/duplicated clips found during live testing, and reorders the
post-paste selection to land on the new clips instead of stale ones.

* fix(studio): scope the data-hf-id strip to tag brackets, not text content

A clip's visible text can contain the literal string data-hf-id="...";
the previous whole-string regex stripped it as if it were the attribute,
corrupting the copied text. Reproduced and confirmed the fix with a test.

* fix(studio): extend root duration and coalesce cut on group paste/duplicate

Paste and duplicate could silently truncate a group from export: they
inserted clips past the composition's data-duration without growing it,
so content existed in the file but never played or rendered. Both now
route through the same grow-only ratchet blockInstaller already uses.

Cmd+X on a multi-clip selection now deletes the whole selection in one
call (the existing batched delete already used by the Delete hotkey),
so one Cmd+Z restores all of them instead of needing one per clip.

The data-hf-id strip and id-capture on paste now go through DOMParser
and a tighter regex respectively, closing two escape hatches: a > inside
an earlier attribute value, and a data-id attribute preceding the real id.

* refactor(studio): share the id-attribute regex and hoist the clip DOMParser

Simplify pass on the previous commit: the id-capture lookbehind was a
second copy of the same pattern deduplicateIds already uses, now a
single exported ID_ATTR_RE both call. The per-clip DOMParser instance
in pasteTimelineClips is now created once per call, not once per clip.
Adds a test pinning stripHfIds's no-parseable-root fallback.

* fix(studio): close the stripHfIds hoist gap

stripHfIds silently returned html unstripped when DOMParser found no
body-level element (a tag the parser hoists into <head>, e.g. title or
meta). Closes it with a regex fallback and a test reproducing the exact
hoisted-tag case, not just the no-tag-at-all case from the prior commit.

* refactor(studio): dedupe clip menu item classes into one helper

The new Copy/Paste/Duplicate rows in ClipContextMenu each got their own
static enabled/disabled className string instead of the parameterized
itemClass(enabled) pattern the sibling TrackGapContextMenu already uses
for the same job. Consolidated to one function; rendered classes are
unchanged for every state.

* refactor(studio): reuse timeRangesOverlap for clip paste placement

tracksOverlap re-derived the exact half-open interval check
timelineCollision.ts already exports and uses elsewhere on the timeline.
2026-09-18 18:25:12 -04:00

103 lines
3.2 KiB
TypeScript

// @vitest-environment node
import { describe, expect, it } from "vitest";
import {
deduplicateIds,
serializeClipboardPayload,
deserializeClipboardPayload,
type ClipboardPayload,
} from "./clipboardPayload";
describe("deduplicateIds", () => {
it("renames ids that collide with existing ids", () => {
const html = '<div id="hero"><img id="photo" src="a.png" /></div>';
const existingIds = ["hero", "other"];
const result = deduplicateIds(html, existingIds);
expect(result).not.toContain('id="hero"');
expect(result).toContain('id="photo"');
expect(result).toMatch(/id="hero-\d+"/);
});
it("returns html unchanged when no collisions", () => {
const html = '<div id="unique"><p>hello</p></div>';
const result = deduplicateIds(html, ["other"]);
expect(result).toBe(html);
});
it("does not rewrite data-composition-id or other data-*-id attributes", () => {
const html = '<div data-composition-id="hero" data-clip-id="hero" id="hero">content</div>';
const result = deduplicateIds(html, ["hero"]);
expect(result).toContain('data-composition-id="hero"');
expect(result).toContain('data-clip-id="hero"');
expect(result).toMatch(/\sid="hero-\d+"/);
});
});
describe("serializeClipboardPayload / deserializeClipboardPayload", () => {
it("round-trips a single-clip timeline payload", () => {
const payload: ClipboardPayload = {
kind: "timeline-clip",
clips: [
{
html: '<img id="photo" src="a.png" data-start="1" data-duration="3" />',
start: 1,
duration: 3,
track: 0,
},
],
sourceFile: "index.html",
};
const json = serializeClipboardPayload(payload);
const parsed = deserializeClipboardPayload(json);
expect(parsed).toEqual(payload);
});
it("round-trips a group timeline-clip payload", () => {
const payload: ClipboardPayload = {
kind: "timeline-clip",
clips: [
{
html: '<img id="a" data-start="1" data-duration="0.5" />',
start: 1,
duration: 0.5,
track: 0,
},
{
html: '<img id="b" data-start="2" data-duration="0.5" />',
start: 2,
duration: 0.5,
track: 1,
},
],
sourceFile: "index.html",
};
const json = serializeClipboardPayload(payload);
const parsed = deserializeClipboardPayload(json);
expect(parsed).toEqual(payload);
});
it("round-trips a dom-element payload", () => {
const payload: ClipboardPayload = {
kind: "dom-element",
html: '<div class="card"><p>Hello</p></div>',
sourceFile: "compositions/scene.html",
};
const json = serializeClipboardPayload(payload);
const parsed = deserializeClipboardPayload(json);
expect(parsed).toEqual(payload);
});
it("returns null for invalid JSON", () => {
expect(deserializeClipboardPayload("not json")).toBeNull();
expect(deserializeClipboardPayload('{"kind":"unknown"}')).toBeNull();
});
it("returns null for a timeline-clip payload with no clips", () => {
const json = serializeClipboardPayload({
kind: "timeline-clip",
clips: [],
sourceFile: "index.html",
});
expect(deserializeClipboardPayload(json)).toBeNull();
});
});