Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
fix(studio): a moved layer holds through seeks and Reset puts back its authored translate (#4840)
Miguel Ángele90b1a0200c1
diff --git a/packages/studio/src/components/editor/manualEditsSnapshot.ts b/packages/studio/src/components/editor/manualEditsSnapshot.tsindex 11214050e4..79042fe118 100644--- a/packages/studio/src/components/editor/manualEditsSnapshot.ts+++ b/packages/studio/src/components/editor/manualEditsSnapshot.ts@@ -265,6 +265,7 @@ function restoreOriginalTranslateProperty(element: HTMLElement): void {export function clearStudioPathOffset(element: HTMLElement): void {if (element.hasAttribute(STUDIO_PATH_OFFSET_ATTR) ||+ element.hasAttribute(STUDIO_ORIGINAL_INLINE_TRANSLATE_ATTR) ||styleUsesStudioOffset(element.style.getPropertyValue("translate"))) {restoreOriginalTranslateProperty(element);diff --git a/packages/studio/src/components/editor/plainTranslate.ts b/packages/studio/src/components/editor/plainTranslate.tsindex 19f077d1c2..8fdb341042 100644--- a/packages/studio/src/components/editor/plainTranslate.ts+++ b/packages/studio/src/components/editor/plainTranslate.ts@@ -1,8 +1,14 @@import { roundTo3 } from "../../utils/rounding";import { gsapWritesPosition, gsapWritesRotation } from "../../hooks/gsapRuntimeKeyframes";import { readCssRotation } from "../../hooks/draggedGsapPosition";-import { readStudioPathOffset, readStudioRotation } from "./manualEditsDom";+import { readStudioPathOffset, readStudioRotation, styleUsesStudioOffset } from "./manualEditsDom";import { splitTopLevelWhitespace } from "./manualEditsStyleHelpers";+import {+ STUDIO_OFFSET_X_PROP,+ STUDIO_OFFSET_Y_PROP,+ STUDIO_ORIGINAL_INLINE_TRANSLATE_ATTR,+ STUDIO_PATH_OFFSET_ATTR,+} from "./manualEditsTypes";import type { PatchOperation } from "../../utils/sourcePatcher";type Point = { x: number; y: number };@@ -102,11 +108,36 @@ function formatTranslatePx(p: Point): string {return `${roundTo3(p.x)}px ${roundTo3(p.y)}px`;}-export function translatePatch(p: Point): PatchOperation & { value: string } {- return { type: "inline-style", property: "translate", value: formatTranslatePx(p) };+const LEGACY_OFFSET_VARS = [STUDIO_OFFSET_X_PROP, STUDIO_OFFSET_Y_PROP];++/** Writes a move as the element's own translate; ends a legacy offset's vars and mark. */+export function writePlainMove(el: HTMLElement, p: Point): PatchOperation[] {+ writeTranslatePx(el, p);+ const patches: PatchOperation[] = [+ { type: "inline-style", property: "translate", value: formatTranslatePx(p) },+ {+ type: "attribute",+ property: STUDIO_ORIGINAL_INLINE_TRANSLATE_ATTR,+ value: el.getAttribute(STUDIO_ORIGINAL_INLINE_TRANSLATE_ATTR),+ },+ ];+ for (const prop of LEGACY_OFFSET_VARS.filter((v) => el.style.getPropertyValue(v))) {+ el.style.removeProperty(prop);+ patches.push({ type: "inline-style", property: prop, value: null });+ }+ if (el.hasAttribute(STUDIO_PATH_OFFSET_ATTR)) {+ el.removeAttribute(STUDIO_PATH_OFFSET_ATTR);+ patches.push({ type: "attribute", property: STUDIO_PATH_OFFSET_ATTR, value: null });+ }+ return patches;}+/** The first write records the author's own translate ("" for none), which Reset puts back. */export function writeTranslatePx(el: HTMLElement, p: Point): void {+ if (!el.hasAttribute(STUDIO_ORIGINAL_INLINE_TRANSLATE_ATTR)) {+ const own = el.style.getPropertyValue("translate");+ el.setAttribute(STUDIO_ORIGINAL_INLINE_TRANSLATE_ATTR, styleUsesStudioOffset(own) ? "" : own);+ }el.style.setProperty("translate", formatTranslatePx(p));}diff --git a/packages/studio/src/hooks/elementOffsetStager.ts b/packages/studio/src/hooks/elementOffsetStager.tsindex 936b4746a0..f87db4f919 100644--- a/packages/studio/src/hooks/elementOffsetStager.ts+++ b/packages/studio/src/hooks/elementOffsetStager.ts@@ -5,7 +5,8 @@ import {} from "../components/editor/elementPositionOffset";import { LAYER_REVEAL_PRIOR_POSITION_ATTR } from "../player/lib/timelineElementHelpers";import type { PatchOperation } from "../utils/sourcePatcher";-import { translatePatch } from "../components/editor/plainTranslate";+import { writePlainMove } from "../components/editor/plainTranslate";+import { captureStudioPathOffset, restoreStudioPathOffset } from "../components/editor/manualEdits";import { gsapHoldsTranslate } from "./gsapRuntimeKeyframes";import { markStudioSaveErrorAlreadyToasted } from "../utils/studioSaveDiagnostics";@@ -59,17 +60,17 @@ function stagePlainTranslate(next: { x: number; y: number },coalesceKey?: string,): { save: () => Promise<void>; rollback: () => void } {- const style = selection.element.style;- const before = style.getPropertyValue("translate");- const patch = translatePatch(next);- style.setProperty("translate", patch.value);+ const el = selection.element;+ const before = captureStudioPathOffset(el);+ const patches = writePlainMove(el, next);+ const written = el.style.getPropertyValue("translate");const rollback = () => {- if (style.getPropertyValue("translate") === patch.value) style.setProperty("translate", before);+ if (el.style.getPropertyValue("translate") === written) restoreStudioPathOffset(el, before);};const key = coalesceKey ?? `move:${++plainMoveCounter}`;const options = { label: "Move layer", coalesceKey: key, coalesceMs: Number.POSITIVE_INFINITY };const save = () =>- commitPositionPatchToHtml(selection, [patch], options).catch((error) => {+ commitPositionPatchToHtml(selection, patches, options).catch((error) => {rollback();throw error;});diff --git a/packages/studio/src/hooks/useDomGeometryCommit.test.tsx b/packages/studio/src/hooks/useDomGeometryCommit.test.tsxindex 073062f542..1c3006e053 100644--- a/packages/studio/src/hooks/useDomGeometryCommit.test.tsx+++ b/packages/studio/src/hooks/useDomGeometryCommit.test.tsx@@ -168,7 +168,10 @@ describe("useDomGeometryCommit, from the package entry", () => {expect(element.style.getPropertyValue("translate")).toBe("130.5px 90px");expect(calls.patches).toEqual([expect.objectContaining({- operations: [{ type: "inline-style", property: "translate", value: "130.5px 90px" }],+ operations: [+ { type: "inline-style", property: "translate", value: "130.5px 90px" },+ { type: "attribute", property: "data-hf-studio-original-inline-translate", value: "" },+ ],}),]);expect(calls.urls.filter((url) => url.includes("gsap"))).toEqual([]);diff --git a/packages/studio/src/hooks/useDomGeometryCommits.test.tsx b/packages/studio/src/hooks/useDomGeometryCommits.test.tsxindex 1f8c0cd20c..38c57f6ebc 100644--- a/packages/studio/src/hooks/useDomGeometryCommits.test.tsx+++ b/packages/studio/src/hooks/useDomGeometryCommits.test.tsx@@ -13,6 +13,9 @@ import {readStudioRotation,} from "../components/editor/manualEdits";import { useDomGeometryCommits, type UseDomGeometryCommitsParams } from "./useDomGeometryCommits";+import { reapplyPositionEditsAfterSeek } from "../components/editor/manualEdits";+import { writeTranslatePx } from "../components/editor/plainTranslate";+import { applyPatch } from "../utils/sourcePatcher";import { DomEditCropHandles } from "../components/editor/DomEditCropHandles";import { withInlineLayoutBox } from "./domSelectionTestHarness";@@ -230,6 +233,180 @@ describe("useDomGeometryCommits resize rollback", () => {});});+describe("a plain move of an element an older Studio moved by its offset vars", () => {+ function legacyMoved() {+ const element = document.createElement("div");+ element.id = "legacy";+ element.style.setProperty("translate", "10px 5px");+ document.body.append(element);+ applyStudioPathOffset(element, { x: 40, y: 30 });+ const selection = { id: "legacy", selector: "#legacy", element } as unknown as DomEditSelection;+ return { element, selection };+ }+ const legacyEnds = [+ { type: "inline-style", property: "--hf-studio-offset-x", value: null },+ { type: "inline-style", property: "--hf-studio-offset-y", value: null },+ { type: "attribute", property: "data-hf-studio-path-offset", value: null },+ ];+ const keptMarks = [+ "data-hf-studio-original-inline-translate",+ "data-hf-studio-original-translate",+ ];+ const studioMarks = (el: HTMLElement) =>+ el+ .getAttributeNames()+ .filter((name) => name.startsWith("data-hf-studio"))+ .sort();++ it("saves the move as its translate, ends the old offset, and a seek keeps it", async () => {+ const { element, selection } = legacyMoved();+ const commitPositionPatchToHtml = vi+ .fn<UseDomGeometryCommitsParams["commitPositionPatchToHtml"]>()+ .mockResolvedValue(undefined);+ const { commits, unmount } = mountCommits(commitPositionPatchToHtml);++ await commits().stageElementPositionOffset(selection, { x: 140, y: 30 }, true).save();+ reapplyPositionEditsAfterSeek(document);++ expect(element.style.getPropertyValue("translate")).toBe("140px 30px");+ expect(commitPositionPatchToHtml.mock.calls[0]![1]).toEqual([+ { type: "inline-style", property: "translate", value: "140px 30px" },+ {+ type: "attribute",+ property: "data-hf-studio-original-inline-translate",+ value: "10px 5px",+ },+ ...legacyEnds,+ ]);+ expect(studioMarks(element)).toEqual(keptMarks);+ unmount();+ });++ it("resizes with the anchor as its translate, ends the old offset, and a seek keeps it", async () => {+ const { element, selection } = legacyMoved();+ const commit = vi+ .fn<UseDomGeometryCommitsParams["commitPositionPatchToHtml"]>()+ .mockResolvedValue(undefined);+ const { commits, unmount } = mountCommits(commit);++ await commits().handleDomBoxSizeCommit(selection, { width: 80, height: 60 }, { x: 60, y: 40 });+ reapplyPositionEditsAfterSeek(document);++ expect(element.style.getPropertyValue("translate")).toBe("60px 40px");+ expect(commit.mock.calls[0]![1]).toEqual(expect.arrayContaining(legacyEnds));+ expect(studioMarks(element)).toEqual(expect.arrayContaining(keptMarks));+ expect(element.hasAttribute("data-hf-studio-path-offset")).toBe(false);+ unmount();+ });++ it.each(["move", "resize"])(+ "puts the old offset back whole when the %s save fails",+ async (kind) => {+ const { element, selection } = legacyMoved();+ const translate = element.style.getPropertyValue("translate");+ const { commits, unmount } = mountCommits(+ vi.fn().mockRejectedValue(new Error("save failed")),+ );++ const save =+ kind === "move"+ ? commits().stageElementPositionOffset(selection, { x: 140, y: 30 }, true).save()+ : commits().handleDomBoxSizeCommit(+ selection,+ { width: 80, height: 60 },+ { x: 60, y: 40 },+ );+ await expect(save).rejects.toThrow();+ expect(element.style.getPropertyValue("translate")).toBe(translate);+ expect(element.hasAttribute("data-hf-studio-path-offset")).toBe(true);+ expect(readStudioPathOffset(element)).toEqual({ x: 40, y: 30 });+ unmount();+ },+ );+});++describe("Reset after a plain move puts the author's translate back", () => {+ function authoredLayer(translate: string | null, legacy = false) {+ const element = document.createElement("div");+ element.id = "layer";+ if (translate) element.style.setProperty("translate", translate);+ document.body.append(element);+ if (legacy) applyStudioPathOffset(element, { x: 40, y: 30 });+ const project = { file: element.outerHTML, history: [] as string[] };+ const commit = vi.fn<UseDomGeometryCommitsParams["commitPositionPatchToHtml"]>(+ async (_selection, patches) => {+ project.history.push(project.file);+ project.file = patches.reduce((html, op) => applyPatch(html, "layer", op), project.file);+ },+ );+ const selection = { id: "layer", selector: "#layer", element } as unknown as DomEditSelection;+ return { element, selection, project, ...mountCommits(commit) };+ }+ const saved = (html: string) =>+ new DOMParser().parseFromString(html, "text/html").getElementById("layer")!;+ const studioLeftovers = (el: Element) => [+ ...el.getAttributeNames().filter((name) => name.startsWith("data-hf-studio")),+ ...((el as HTMLElement).style.cssText.match(/--hf-studio-[\w-]+/g) ?? []),+ ];++ it("restores an older Studio's layer to its authored 50% 10px, in the file and live", async () => {+ const { element, selection, project, commits, unmount } = authoredLayer("50% 10px", true);++ await commits().stageElementPositionOffset(selection, { x: 140, y: 30 }, true).save();+ await commits().handleDomManualEditsReset(selection);++ expect(saved(project.file).style.getPropertyValue("translate")).toBe("50% 10px");+ expect(element.style.getPropertyValue("translate")).toBe("50% 10px");+ expect(studioLeftovers(saved(project.file))).toEqual([]);+ expect(studioLeftovers(element)).toEqual([]);+ unmount();+ });++ it("restores the authored 12px 4px after a drag and a second move", async () => {+ const { element, selection, project, commits, unmount } = authoredLayer("12px 4px");++ writeTranslatePx(element, { x: 90, y: 0 });+ await commits().stageElementPositionOffset(selection, { x: 140, y: 30 }, true).save();+ await commits().stageElementPositionOffset(selection, { x: 200, y: 50 }, true).save();+ await commits().handleDomManualEditsReset(selection);++ expect(saved(project.file).style.getPropertyValue("translate")).toBe("12px 4px");+ expect(element.style.getPropertyValue("translate")).toBe("12px 4px");+ expect(studioLeftovers(saved(project.file))).toEqual([]);+ unmount();+ });++ it("removes the translate of a layer that had none, in the file and live", async () => {+ const { element, selection, project, commits, unmount } = authoredLayer(null);++ await commits().stageElementPositionOffset(selection, { x: 140, y: 30 }, true).save();+ await commits().handleDomManualEditsReset(selection);++ expect(saved(project.file).style.getPropertyValue("translate")).toBe("");+ expect(element.style.getPropertyValue("translate")).toBe("");+ expect(studioLeftovers(saved(project.file))).toEqual([]);+ unmount();+ });++ it("undo of the Reset brings the move back, and Reset still restores the author's", async () => {+ const { selection, project, commits, unmount } = authoredLayer("12px 4px");++ await commits().stageElementPositionOffset(selection, { x: 140, y: 30 }, true).save();+ const moved = project.file;+ await commits().handleDomManualEditsReset(selection);+ project.file = project.history.pop()!;+ expect(project.file).toBe(moved);++ const reloaded = saved(project.file) as HTMLElement;+ document.body.append(reloaded);+ expect(reloaded.style.getPropertyValue("translate")).toBe("140px 30px");+ await commits().handleDomManualEditsReset({ ...selection, element: reloaded });+ expect(saved(project.file).style.getPropertyValue("translate")).toBe("12px 4px");+ expect(reloaded.style.getPropertyValue("translate")).toBe("12px 4px");+ unmount();+ });+});+describe("useDomGeometryCommits resize of a cropped element", () => {it("saves the crop scaled per axis with the box in the resize's own commit", async () => {const element = withInlineLayoutBox(document.createElement("div"));diff --git a/packages/studio/src/hooks/useDomGeometryCommits.ts b/packages/studio/src/hooks/useDomGeometryCommits.tsindex 5f0e7aed8d..e3827aaeb0 100644--- a/packages/studio/src/hooks/useDomGeometryCommits.ts+++ b/packages/studio/src/hooks/useDomGeometryCommits.ts@@ -17,7 +17,7 @@ import {import { stageElementOffset } from "./elementOffsetStager";import { savePlainRotation } from "./plainRotation";import { prepareCropResize } from "../components/editor/cropResize";-import { translatePatch, writeTranslatePx } from "../components/editor/plainTranslate";+import { writePlainMove } from "../components/editor/plainTranslate";import {buildPathOffsetPatches,buildBoxSizePatches,@@ -110,7 +110,7 @@ export function useDomGeometryCommits({if (readOnlyPreview) return Promise.resolve();const element = selection.element;const beforeSize = captureStudioBoxSize(element);- const beforeTranslate = element.style.getPropertyValue("translate");+ const beforeOffset = captureStudioPathOffset(element);const stageCrop = prepareCropResize(element);applyStudioBoxSize(element, next);const crop = stageCrop();@@ -118,16 +118,13 @@ export function useDomGeometryCommits({// (as a move writes it) that keeps the centre planted.const patches = buildBoxSizePatches(element);if (crop) patches.push(crop.patch);- if (offset) {- writeTranslatePx(element, offset);- patches.push(translatePatch(offset));- }+ if (offset) patches.push(...writePlainMove(element, offset));return commitPositionPatchToHtml(selection, patches, {label: "Resize layer box",coalesceKey: `box-size:${getDomEditTargetKey(selection)}`,}).catch((error) => {restoreStudioBoxSize(element, beforeSize);- if (offset) element.style.setProperty("translate", beforeTranslate);+ if (offset) restoreStudioPathOffset(element, beforeOffset);crop?.revert();restore?.();throw error;