Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
fix(studio): undo works right after pasting or duplicating clips (#4482)
Miguel Ángelcaf704950368
diff --git a/packages/studio/src/hooks/useClipboard.test.ts b/packages/studio/src/hooks/useClipboard.test.tsindex 0f13256a3d..caedbea343 100644--- a/packages/studio/src/hooks/useClipboard.test.ts+++ b/packages/studio/src/hooks/useClipboard.test.ts@@ -1,6 +1,12 @@// @vitest-environment jsdom+import { ensureHfIds } from "@hyperframes/parsers";import { describe, expect, it } from "vitest";-import { pasteTimelineClips, resolveFreeTrack, type PlacedClip } from "./useClipboard";+import {+ pasteElementHtml,+ pasteTimelineClips,+ resolveFreeTrack,+ type PlacedClip,+} from "./useClipboard";import { extendRootDurationInSource } from "../utils/rootDuration";import type { TimelineClipboardClip } from "../utils/clipboardPayload";import type { TimelineElement } from "../player";@@ -8,6 +14,9 @@ import type { TimelineElement } from "../player";const ROOT ='<div id="claude-paper" data-composition-id="claude-paper" data-start="0" data-duration="10"></div>';+const hfIdsIn = (html: string) =>+ Array.from(html.matchAll(/data-hf-id=["']([^"']+)/g), (m) => m[1]);+function clip(id: string, start: number, duration: number, track: number): TimelineClipboardClip {return {html: `<audio id="${id}" src="typenew.mp3" data-start="${start}" data-duration="${duration}" data-track-index="${track}"></audio>`,@@ -77,7 +86,7 @@ describe("pasteTimelineClips", () => {expect(content).toMatch(/data-track-index="133"/);});- it("strips the copied data-hf-id so a fresh one mints on next save", () => {+ it("gives a pasted clip a fresh data-hf-id, never the copied one", () => {const clipWithHfId: TimelineClipboardClip = {html: '<audio data-hf-id="hf-75o7" id="sfx-type-0" src="typenew.mp3" data-start="7.8" data-duration="0.57" data-track-index="109"></audio>',start: 7.8,@@ -85,10 +94,44 @@ describe("pasteTimelineClips", () => {track: 109,};const { content } = pasteTimelineClips(ROOT, [clipWithHfId], 40, []);- expect(content).not.toContain("data-hf-id");+ expect(content).not.toContain("hf-75o7");+ expect(hfIdsIn(content)).toHaveLength(1);+ });++ it("leaves nothing for the server to stamp, so the file undo recorded is what stays on disk", () => {+ const stamped = ensureHfIds(+ ROOT.replace("</div>", `${clip("sfx-1", 1, 2, 0).html}${clip("sfx-2", 3, 2, 0).html}</div>`),+ );+ const copies = hfIdsIn(stamped).length;+ const original = (id: string) => ({+ ...clip(id, 1, 2, 0),+ html: stamped.match(new RegExp(`<audio[^>]*id="${id}"[^>]*></audio>`))![0],+ });+ const { content } = pasteTimelineClips(stamped, [original("sfx-1"), original("sfx-2")], 11, []);+ const ids = hfIdsIn(content);+ expect(ids).toHaveLength(copies + 2);+ expect(new Set(ids).size).toBe(ids.length);+ expect(hfIdsIn(ensureHfIds(content))).toHaveLength(ids.length);+ });++ it("remints a nested clip's descendants clear of their originals already in the file", () => {+ const nested =+ '<div id="comp-1" data-start="1" data-duration="2" data-track-index="0"><span>a</span><span>b</span></div>';+ const stamped = ensureHfIds(ROOT.replace("</div>", `${nested}</div>`));+ const copied = stamped.match(/<div[^>]*id="comp-1"[^>]*>.*?<\/div>/)![0];+ const { content } = pasteTimelineClips(+ stamped,+ [{ html: copied, start: 1, duration: 2, track: 0 }],+ 11,+ [],+ );+ const ids = hfIdsIn(content);+ expect(ids).toHaveLength(hfIdsIn(stamped).length + 3);+ expect(new Set(ids).size).toBe(ids.length);+ expect(hfIdsIn(ensureHfIds(content))).toHaveLength(ids.length);});- it("strips a nested descendant's data-hf-id too, not just the root's", () => {+ it("remints a nested descendant's data-hf-id too, not just the root's", () => {// A composition-instance clip's descendants carry their own hf-ids; a// clone must remint all of them, not just the root, or the descendants// collide with their originals the same way the root would.@@ -101,7 +144,7 @@ describe("pasteTimelineClips", () => {const { content } = pasteTimelineClips(ROOT, [nested], 40, []);expect(content).not.toContain("hf-root");expect(content).not.toContain("hf-child");- expect(content).not.toContain("data-hf-id");+ expect(hfIdsIn(content)).toHaveLength(2);});it("does not strip text content that happens to contain the data-hf-id string", () => {@@ -145,7 +188,7 @@ describe("pasteTimelineClips", () => {expect(content).toContain(`id="${ids[0]}"`);});- it("strips a single-quoted data-hf-id, not just a double-quoted one", () => {+ it("remints a single-quoted data-hf-id, not just a double-quoted one", () => {const singleQuoted: TimelineClipboardClip = {html: '<audio data-hf-id=\'hf-75o7\' id="sfx-type-0" src="typenew.mp3" data-start="7.8" data-duration="0.57" data-track-index="109"></audio>',start: 7.8,@@ -153,10 +196,11 @@ describe("pasteTimelineClips", () => {track: 109,};const { content } = pasteTimelineClips(ROOT, [singleQuoted], 40, []);- expect(content).not.toContain("data-hf-id");+ expect(content).not.toContain("hf-75o7");+ expect(hfIdsIn(content)).toHaveLength(1);});- it("strips data-hf-id even when an earlier attribute value contains a literal >", () => {+ it("remints data-hf-id even when an earlier attribute value contains a literal >", () => {// A `>` inside title's value would close a bracket-scoped tag regex// early, leaving data-hf-id (which comes after it) outside the matched// span and unstripped. A real parser doesn't have this failure mode.@@ -167,7 +211,8 @@ describe("pasteTimelineClips", () => {track: 109,};const { content } = pasteTimelineClips(ROOT, [gtInAttr], 40, []);- expect(content).not.toContain("data-hf-id");+ expect(content).not.toContain("hf-xyz");+ expect(hfIdsIn(content)).toHaveLength(1);});it("still strips data-hf-id from bare text with no element root", () => {@@ -182,7 +227,7 @@ describe("pasteTimelineClips", () => {});it("still strips data-hf-id from a tag the HTML parser hoists out of body (e.g. title)", () => {- // A <title> never reaches DOMParser's body, so stripHfIds's DOM walk sees+ // A <title> never reaches DOMParser's body, so remintHfIds's DOM walk sees// no root element here -- this exercises the regex fallback, not the walk.const hoisted: TimelineClipboardClip = {html: '<title data-hf-id="hf-leak">x</title>',@@ -232,3 +277,17 @@ describe("pasteTimelineClips + extendRootDurationInSource", () => {expect(extended).toContain('data-duration="10"');});});++describe("pasteElementHtml", () => {+ it("gives a pasted element and its children ids of their own, so an edit to the copy stays on it", () => {+ const stamped = ensureHfIds(+ ROOT.replace("</div>", '<p id="title" class="t">Hello <b>there</b></p></div>'),+ );+ const copied = stamped.match(/<p[^>]*id="title"[^>]*>.*?<\/p>/)![0];+ const content = pasteElementHtml(stamped, { html: copied, originSelector: "#title" });+ const ids = hfIdsIn(content);+ expect(ids).toHaveLength(hfIdsIn(stamped).length + 2);+ expect(new Set(ids).size).toBe(ids.length);+ expect(hfIdsIn(ensureHfIds(content))).toHaveLength(ids.length);+ });+});diff --git a/packages/studio/src/hooks/useClipboard.ts b/packages/studio/src/hooks/useClipboard.tsindex 7166ab5366..f7505e2366 100644--- a/packages/studio/src/hooks/useClipboard.ts+++ b/packages/studio/src/hooks/useClipboard.ts@@ -1,4 +1,5 @@import { useCallback, useRef } from "react";+import { EXCLUDED_TAGS, mintHfId, walkCompositionDescendants } from "@hyperframes/parsers/hf-ids";import type { TimelineElement } from "../player";import { usePlayerStore } from "../player";import type { DomEditSelection } from "../components/editor/domEditing";@@ -98,10 +99,10 @@ export function resolveFreeTrack(preferred: PlacedClip, taken: readonly PlacedClreturn maxTrack + 1;}-/** Strips data-hf-id from the root and every descendant so a clone re-mints- * its own. DOMParser, not a bracket-scoped regex, so a `>` inside an earlier- * attribute value or a single-quoted id can't defeat the strip. */-function stripHfIds(html: string, parser: DOMParser): string {+/** Gives a clone's root and every descendant a fresh data-hf-id, clear of `taken`. Minted here,+ * inside the edit, or the server stamps them on the next preview load, behind undo's back.+ * DOMParser, not a regex, so a `>` inside an attribute value can't defeat it. */+function remintHfIds(html: string, parser: DOMParser, taken: Set<string>): string {const root = parser.parseFromString(html, "text/html").body.firstElementChild;if (!root) {// A tag the HTML parser hoists out of <body> (title/meta/style/base/link)@@ -110,11 +111,28 @@ function stripHfIds(html: string, parser: DOMParser): string {// of these tags today.return html.replace(/\sdata-hf-id=("[^"]*"|'[^']*')/g, "");}- root.querySelectorAll("[data-hf-id]").forEach((el) => el.removeAttribute("data-hf-id"));- root.removeAttribute("data-hf-id");+ const elements = [root];+ walkCompositionDescendants(root, (el) => elements.push(el));+ for (const el of elements) {+ if (EXCLUDED_TAGS.has(el.tagName.toLowerCase())) el.removeAttribute("data-hf-id");+ else el.setAttribute("data-hf-id", mintHfId(el, taken));+ }return root.outerHTML;}+const HF_ID_ATTR_RE = /\bdata-hf-id\s*=\s*["']?([^"'\s>]+)/gi;+const hfIdsInFile = (content: string) =>+ new Set(Array.from(content.matchAll(HF_ID_ATTR_RE), (match) => match[1]));++export function pasteElementHtml(+ content: string,+ payload: { html: string; originSelector?: string; originSelectorIndex?: number },+): string {+ const reminted = remintHfIds(payload.html, new DOMParser(), hfIdsInFile(content));+ const deduped = deduplicateIds(reminted, collectHtmlIds(content));+ return insertAsSibling(content, deduped, payload.originSelector, payload.originSelectorIndex);+}+/** Shared insertion path for paste and duplicate, anchored at the playhead or* the selection's end respectively. Returns the final ids so the caller can* select what it just placed, and the furthest end any clip lands at so the@@ -136,9 +154,10 @@ export function pasteTimelineClips(let result = content;let requiredEnd = 0;const domParser = new DOMParser();+ const takenHfIds = hfIdsInFile(content);for (const clip of clips) {- const stripped = stripHfIds(clip.html, domParser);- const deduped = deduplicateIds(stripped, existingIds);+ const reminted = remintHfIds(clip.html, domParser, takenHfIds);+ const deduped = deduplicateIds(reminted, existingIds);existingIds = existingIds.concat(collectHtmlIds(deduped));const newStart = anchorTime + (clip.start - groupMinStart);const newTrack = resolveFreeTrack(@@ -278,13 +297,7 @@ export function useClipboard({patchedContent = extendRootDurationInSource(pasted.content, pasted.requiredEnd);pastedIds = pasted.ids;} else {- const deduped = deduplicateIds(payload.html, collectHtmlIds(originalContent));- patchedContent = insertAsSibling(- originalContent,- deduped,- payload.originSelector,- payload.originSelectorIndex,- );+ patchedContent = pasteElementHtml(originalContent, payload);}const label =