Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
perf(studio): a film with no manual edits skips the edit reapply on each seek (#4592)
Miguel Ángele1e2b16e2191
diff --git a/packages/studio/src/components/editor/anchoredResizeReleaseShift.test.ts b/packages/studio/src/components/editor/anchoredResizeReleaseShift.test.tsindex 734fd3580d..166f0e372c 100644--- a/packages/studio/src/components/editor/anchoredResizeReleaseShift.test.ts+++ b/packages/studio/src/components/editor/anchoredResizeReleaseShift.test.ts@@ -1,11 +1,7 @@// @vitest-environment jsdomimport { afterEach, describe, expect, it } from "vitest";-import {- applyStudioBoxSize,- applyStudioPathOffset,- readStudioBoxSize,- reapplyPositionEditsAfterSeek,-} from "./manualEditsDom";+import { applyStudioBoxSize, applyStudioPathOffset, readStudioBoxSize } from "./manualEditsDom";+import { reapplyPositionEditsAfterSeek } from "./manualEditsSeekReapply";import { buildBoxSizePatches, buildPathOffsetPatches } from "./manualEditsDomPatches";import { createManualOffsetDragMember, applyManualOffsetDragCommit } from "./manualOffsetDrag";import { computeNextResizeAnchor } from "./domEditResizeLocal";diff --git a/packages/studio/src/components/editor/manualEdits.ts b/packages/studio/src/components/editor/manualEdits.tsindex ff6d20fd1a..0df9738b51 100644--- a/packages/studio/src/components/editor/manualEdits.ts+++ b/packages/studio/src/components/editor/manualEdits.ts@@ -30,9 +30,10 @@ export {applyStudioBoxSizeDraft,applyStudioRotation,applyStudioRotationDraft,- reapplyPositionEditsAfterSeek,} from "./manualEditsDom";+export { reapplyPositionEditsAfterSeek } from "./manualEditsSeekReapply";+export {captureStudioBoxSize,captureStudioRotation,diff --git a/packages/studio/src/components/editor/manualEditsDom.ts b/packages/studio/src/components/editor/manualEditsDom.tsindex 4a041e020f..99b9b0eea4 100644--- a/packages/studio/src/components/editor/manualEditsDom.ts+++ b/packages/studio/src/components/editor/manualEditsDom.ts@@ -33,7 +33,6 @@ import {STUDIO_ROTATION_TRANSFORM_ORIGIN,} from "./manualEditsTypes";import { roundRotationAngle } from "./manualEditsParsing";-import { applyStudioMotionFromDom } from "./studioMotion";import { gsapAnimatesProperty } from "./gsapAnimatesProperty";import { splitTopLevelWhitespace } from "./manualEditsStyleHelpers";@@ -533,67 +532,3 @@ export function applyStudioRotationDraft(element: HTMLElement, rotation: { anglecomposeStudioRotationValue(element, `${roundRotationAngle(rotation.angle)}deg`),);}--/* ── Seek reapply (position + motion) ────────────────────────────── */-function queryStudioElements(doc: Document, attr: string): HTMLElement[] {- const ctor = doc.defaultView?.HTMLElement;- if (!ctor) return [];- const elements = Array.from(doc.querySelectorAll(`[${attr}="true"]`)).filter(- (el): el is HTMLElement => el instanceof ctor,- );- // Handle legacy HTML files where attributes were persisted with a double data- prefix- const legacyAttr = `data-${attr}`;- for (const el of doc.querySelectorAll(`[${legacyAttr}="true"]`)) {- if (el instanceof ctor && !el.hasAttribute(attr)) {- el.setAttribute(attr, "true");- el.removeAttribute(legacyAttr);- elements.push(el);- }- }- return elements;-}--function reapplyPathOffsets(doc: Document): void {- for (const el of queryStudioElements(doc, STUDIO_PATH_OFFSET_ATTR)) {- // Unlike size below, the offset channels COMPOSE — applying both doubles the move.- if (gsapAnimatesProperty(el, "x", "y")) continue;- const x = el.style.getPropertyValue(STUDIO_OFFSET_X_PROP);- const y = el.style.getPropertyValue(STUDIO_OFFSET_Y_PROP);- if (!x && !y) continue;- const offset = { x: Number.parseFloat(x) || 0, y: Number.parseFloat(y) || 0 };- applyStudioPathOffset(el, offset, { updateBase: false });- }-}--/**- * Put the studio's committed size back after a seek, GSAP-sized elements included.- * Size does not compose the way the offset above does: both channels write width- * and height, so the later write wins on the same number. Standing aside meant- * nothing held the size while a soft reload reverted the old timeline (GSAP hands- * back each tween's recorded starting width), so the element sat at its stylesheet- * size until the new one rendered — the jump after a resize.- */-function reapplyBoxSizes(doc: Document): void {- for (const el of queryStudioElements(doc, STUDIO_BOX_SIZE_ATTR)) {- const w = Number.parseFloat(el.style.getPropertyValue(STUDIO_WIDTH_PROP));- const h = Number.parseFloat(el.style.getPropertyValue(STUDIO_HEIGHT_PROP));- if (Number.isFinite(w) && Number.isFinite(h) && w > 0 && h > 0) {- applyStudioBoxSize(el, { width: w, height: h });- }- }-}-function reapplyRotations(doc: Document): void {- for (const el of queryStudioElements(doc, STUDIO_ROTATION_ATTR)) {- const angle = Number.parseFloat(el.style.getPropertyValue(STUDIO_ROTATION_PROP));- if (Number.isFinite(angle)) {- applyStudioRotation(el, { angle });- }- }-}--export function reapplyPositionEditsAfterSeek(doc: Document): void {- reapplyPathOffsets(doc);- reapplyBoxSizes(doc);- reapplyRotations(doc);- applyStudioMotionFromDom(doc);-}diff --git a/packages/studio/src/components/editor/manualEditsSeekReapply.ts b/packages/studio/src/components/editor/manualEditsSeekReapply.tsnew file mode 100644index 0000000000..82e2a74766--- /dev/null+++ b/packages/studio/src/components/editor/manualEditsSeekReapply.ts@@ -0,0 +1,136 @@+// Puts Studio's committed edits back after a timeline seek has rendered over them.+import {+ STUDIO_BOX_SIZE_ATTR,+ STUDIO_HEIGHT_PROP,+ STUDIO_OFFSET_X_PROP,+ STUDIO_OFFSET_Y_PROP,+ STUDIO_PATH_OFFSET_ATTR,+ STUDIO_ROTATION_ATTR,+ STUDIO_ROTATION_PROP,+ STUDIO_WIDTH_PROP,+} from "./manualEditsTypes";+import { applyStudioBoxSize, applyStudioPathOffset, applyStudioRotation } from "./manualEditsDom";+import { applyStudioMotionFromDom } from "./studioMotion";+import { STUDIO_MOTION_ATTR, STUDIO_MOTION_TIMELINE_ID } from "./studioMotionTypes";+import { gsapAnimatesProperty } from "./gsapAnimatesProperty";++function queryStudioElements(doc: Document, attr: string): HTMLElement[] {+ const ctor = doc.defaultView?.HTMLElement;+ if (!ctor) return [];+ const elements = Array.from(doc.querySelectorAll(`[${attr}="true"]`)).filter(+ (el): el is HTMLElement => el instanceof ctor,+ );+ // Handle legacy HTML files where attributes were persisted with a double data- prefix+ const legacyAttr = `data-${attr}`;+ for (const el of doc.querySelectorAll(`[${legacyAttr}="true"]`)) {+ if (el instanceof ctor && !el.hasAttribute(attr)) {+ el.setAttribute(attr, "true");+ el.removeAttribute(legacyAttr);+ elements.push(el);+ }+ }+ return elements;+}++function reapplyPathOffsets(doc: Document): void {+ for (const el of queryStudioElements(doc, STUDIO_PATH_OFFSET_ATTR)) {+ // Unlike size below, the offset channels add up: applying both doubles the move.+ if (gsapAnimatesProperty(el, "x", "y")) continue;+ const x = el.style.getPropertyValue(STUDIO_OFFSET_X_PROP);+ const y = el.style.getPropertyValue(STUDIO_OFFSET_Y_PROP);+ if (!x && !y) continue;+ const offset = { x: Number.parseFloat(x) || 0, y: Number.parseFloat(y) || 0 };+ applyStudioPathOffset(el, offset, { updateBase: false });+ }+}++/**+ * Put the committed size back after a seek, GSAP-sized elements included: both write width and height,+ * so the later write wins. Standing aside let a soft reload revert to the stylesheet size for a frame.+ */+function reapplyBoxSizes(doc: Document): void {+ for (const el of queryStudioElements(doc, STUDIO_BOX_SIZE_ATTR)) {+ const w = Number.parseFloat(el.style.getPropertyValue(STUDIO_WIDTH_PROP));+ const h = Number.parseFloat(el.style.getPropertyValue(STUDIO_HEIGHT_PROP));+ if (Number.isFinite(w) && Number.isFinite(h) && w > 0 && h > 0) {+ applyStudioBoxSize(el, { width: w, height: h });+ }+ }+}+function reapplyRotations(doc: Document): void {+ for (const el of queryStudioElements(doc, STUDIO_ROTATION_ATTR)) {+ const angle = Number.parseFloat(el.style.getPropertyValue(STUDIO_ROTATION_PROP));+ if (Number.isFinite(angle)) {+ applyStudioRotation(el, { angle });+ }+ }+}++// Every mark a reapply below acts on, the legacy double-prefixed ones included.+const STUDIO_EDIT_ATTRS = [STUDIO_PATH_OFFSET_ATTR, STUDIO_BOX_SIZE_ATTR, STUDIO_ROTATION_ATTR]+ .flatMap((attr) => [attr, `data-${attr}`])+ .concat(STUDIO_MOTION_ATTR);+// One selector each: Chrome answers a lone attribute selector without walking the DOM, a comma list it walks.+const STUDIO_EDIT_SELECTORS = STUDIO_EDIT_ATTRS.map((attr) =>+ attr === STUDIO_MOTION_ATTR ? `[${attr}]` : `[${attr}="true"]`,+);++interface EditMarkCache {+ observer: MutationObserver;+ stale: boolean;+ has: boolean;+}+const editMarkCaches = new WeakMap<Document, EditMarkCache>();++function scanForEditMarks(doc: Document): boolean {+ return STUDIO_EDIT_SELECTORS.some((selector) => doc.querySelector(selector) !== null);+}++// Rescans only after a mark attribute or the tree changed, so a seek on a steady film does no lookup.+function hasEditMarks(doc: Document): boolean {+ const Observer = doc.defaultView?.MutationObserver;+ if (!Observer) return scanForEditMarks(doc);+ let cache = editMarkCaches.get(doc);+ if (!cache) {+ const entry: EditMarkCache = {+ observer: new Observer(() => {+ entry.stale = true;+ }),+ stale: true,+ has: false,+ };+ entry.observer.observe(doc, {+ subtree: true,+ childList: true,+ attributes: true,+ attributeFilter: STUDIO_EDIT_ATTRS,+ });+ editMarkCaches.set(doc, entry);+ cache = entry;+ }+ // A mark set earlier in this task has not reached the observer callback yet.+ if (cache.observer.takeRecords().length > 0) cache.stale = true;+ if (cache.stale) {+ cache.has = scanForEditMarks(doc);+ cache.stale = false;+ }+ return cache.has;+}++function hasStudioEdits(doc: Document): boolean {+ const timelines = (doc.defaultView as { __timelines?: Record<string, unknown> } | null)+ ?.__timelines;+ // A motion timeline outlives its marks until the next reapply kills it.+ return Boolean(timelines?.[STUDIO_MOTION_TIMELINE_ID]) || hasEditMarks(doc);+}++export function reapplyPositionEditsAfterSeek(doc: Document): void {+ // Runs after every seek, so several times per playback frame.+ if (!hasStudioEdits(doc)) return;+ reapplyPathOffsets(doc);+ reapplyBoxSizes(doc);+ reapplyRotations(doc);+ applyStudioMotionFromDom(doc);+ // The reapply rewrites the marks it read; the film still has them, so no rescan is due.+ editMarkCaches.get(doc)?.observer.takeRecords();+}diff --git a/packages/studio/src/components/editor/reapplyBoxSizeAfterSeek.test.ts b/packages/studio/src/components/editor/reapplyBoxSizeAfterSeek.test.tsindex 92a9087968..6ec9ee4f53 100644--- a/packages/studio/src/components/editor/reapplyBoxSizeAfterSeek.test.ts+++ b/packages/studio/src/components/editor/reapplyBoxSizeAfterSeek.test.ts@@ -1,6 +1,6 @@// @vitest-environment jsdomimport { afterEach, describe, expect, it } from "vitest";-import { reapplyPositionEditsAfterSeek } from "./manualEditsDom";+import { reapplyPositionEditsAfterSeek } from "./manualEditsSeekReapply";import { STUDIO_BOX_SIZE_ATTR, STUDIO_HEIGHT_PROP, STUDIO_WIDTH_PROP } from "./manualEditsTypes";/**diff --git a/packages/studio/src/components/editor/reapplyPositionEditsAfterSeek.test.ts b/packages/studio/src/components/editor/reapplyPositionEditsAfterSeek.test.tsnew file mode 100644index 0000000000..839aadd344--- /dev/null+++ b/packages/studio/src/components/editor/reapplyPositionEditsAfterSeek.test.ts@@ -0,0 +1,117 @@+// @vitest-environment jsdom+import { afterEach, describe, expect, it, vi } from "vitest";+import { reapplyPositionEditsAfterSeek } from "./manualEditsSeekReapply";+import {+ STUDIO_PATH_OFFSET_ATTR,+ STUDIO_ROTATION_ATTR,+ STUDIO_ROTATION_PROP,+} from "./manualEditsTypes";+import { STUDIO_MOTION_TIMELINE_ID } from "./studioMotionTypes";++describe("reapplyPositionEditsAfterSeek", () => {+ afterEach(() => {+ document.body.innerHTML = "";+ vi.restoreAllMocks();+ });++ it("does no per-edit work on a film Studio never edited", () => {+ document.body.innerHTML = '<div id="a"></div><div id="b"></div>';+ const queryAll = vi.spyOn(document, "querySelectorAll");++ reapplyPositionEditsAfterSeek(document);++ expect(queryAll).not.toHaveBeenCalled();+ });++ it("looks nothing up on later seeks of a film Studio never edited", () => {+ document.body.innerHTML = '<div id="a"></div><div id="b"></div>';+ const query = vi.spyOn(document, "querySelector");+ reapplyPositionEditsAfterSeek(document);+ // A comma selector list walks the whole DOM in Chrome; lone attribute selectors do not.+ for (const [selector] of query.mock.calls) expect(selector).not.toContain(",");+ query.mockClear();+ const queryAll = vi.spyOn(document, "querySelectorAll");++ reapplyPositionEditsAfterSeek(document);++ expect(query).not.toHaveBeenCalled();+ expect(queryAll).not.toHaveBeenCalled();+ });++ it("looks up no more on a film with an edit than the reapply itself does", () => {+ document.body.innerHTML = `<div id="a" ${STUDIO_ROTATION_ATTR}="true" style="${STUDIO_ROTATION_PROP}: 8deg"></div>`;+ reapplyPositionEditsAfterSeek(document);+ const query = vi.spyOn(document, "querySelector");+ const queryAll = vi.spyOn(document, "querySelectorAll");++ reapplyPositionEditsAfterSeek(document);++ // Three edit kinds, each with a legacy mark, plus motion: seven, as before the skip.+ expect(query.mock.calls.length + queryAll.mock.calls.length).toBeLessThanOrEqual(7);+ });++ it("reapplies an edit made after the last seek, in the same task", () => {+ document.body.innerHTML = '<div id="a"></div>';+ reapplyPositionEditsAfterSeek(document);+ const el = document.getElementById("a") as HTMLElement;+ el.setAttribute(STUDIO_ROTATION_ATTR, "true");+ el.style.setProperty(STUDIO_ROTATION_PROP, "8deg");++ reapplyPositionEditsAfterSeek(document);++ expect(el.style.getPropertyValue("rotate")).toContain(STUDIO_ROTATION_PROP);+ });++ it("reapplies an edited element inserted after the last seek", async () => {+ document.body.innerHTML = '<div id="a"></div>';+ reapplyPositionEditsAfterSeek(document);+ const queryAll = vi.spyOn(document, "querySelectorAll");+ reapplyPositionEditsAfterSeek(document);+ expect(queryAll).not.toHaveBeenCalled(); // the film reads as unedited+ queryAll.mockRestore();+ // A paste, scene swap or undo puts back a node that already carries its mark.+ const el = document.createElement("div");+ el.setAttribute(STUDIO_ROTATION_ATTR, "true");+ el.style.setProperty(STUDIO_ROTATION_PROP, "8deg");+ document.body.append(el);+ await Promise.resolve();++ reapplyPositionEditsAfterSeek(document);++ expect(el.style.getPropertyValue("rotate")).toContain(STUDIO_ROTATION_PROP);+ });++ it("does not rescan after a style write, the one GSAP makes every frame", async () => {+ document.body.innerHTML = '<div id="a"></div>';+ reapplyPositionEditsAfterSeek(document);+ (document.getElementById("a") as HTMLElement).style.setProperty("opacity", "0.5");+ await Promise.resolve();+ const query = vi.spyOn(document, "querySelector");++ reapplyPositionEditsAfterSeek(document);++ expect(query).not.toHaveBeenCalled();+ });++ it("still clears a motion timeline whose marks an undo removed", () => {+ document.body.innerHTML = '<div id="a"></div>';+ const kill = vi.fn();+ const win = window as unknown as { __timelines?: Record<string, unknown> };+ win.__timelines = { [STUDIO_MOTION_TIMELINE_ID]: { kill } };++ reapplyPositionEditsAfterSeek(document);++ expect(kill).toHaveBeenCalledTimes(1);+ delete win.__timelines;+ });++ it("still migrates a legacy double-prefixed edit mark", () => {+ document.body.innerHTML = `<div id="a" data-${STUDIO_PATH_OFFSET_ATTR}="true"></div>`;++ reapplyPositionEditsAfterSeek(document);++ const el = document.getElementById("a");+ expect(el?.getAttribute(STUDIO_PATH_OFFSET_ATTR)).toBe("true");+ expect(el?.hasAttribute(`data-${STUDIO_PATH_OFFSET_ATTR}`)).toBe(false);+ });+});