diff --git a/packages/studio/src/components/editor/manualEditsDom.ts b/packages/studio/src/components/editor/manualEditsDom.ts index 0883aa05dd..9ac4588a26 100644 --- a/packages/studio/src/components/editor/manualEditsDom.ts +++ b/packages/studio/src/components/editor/manualEditsDom.ts @@ -12,6 +12,8 @@ import { STUDIO_ORIGINAL_INLINE_TRANSLATE_ATTR, STUDIO_ORIGINAL_WIDTH_ATTR, STUDIO_ORIGINAL_HEIGHT_ATTR, + STUDIO_ORIGINAL_BOX_WIDTH_ATTR, + STUDIO_ORIGINAL_BOX_HEIGHT_ATTR, STUDIO_ORIGINAL_MIN_WIDTH_ATTR, STUDIO_ORIGINAL_MIN_HEIGHT_ATTR, STUDIO_ORIGINAL_MAX_WIDTH_ATTR, @@ -358,6 +360,18 @@ function writeStudioBoxSizeVars( element: HTMLElement, size: { width: number; height: number }, ): void { + // Keep the measurement on its own migration-safe guard. Elements drafted by + // an older Studio can already carry the box-size marker without these newer + // attributes; the next resize still reaches this function before its width + // and height are overwritten, so this is the last honest layout box to save. + // Offset sizes are layout values, so a running scale animation does not + // distort them. + if (!element.hasAttribute(STUDIO_ORIGINAL_BOX_WIDTH_ATTR)) { + element.setAttribute(STUDIO_ORIGINAL_BOX_WIDTH_ATTR, String(element.offsetWidth)); + } + if (!element.hasAttribute(STUDIO_ORIGINAL_BOX_HEIGHT_ATTR)) { + element.setAttribute(STUDIO_ORIGINAL_BOX_HEIGHT_ATTR, String(element.offsetHeight)); + } if (!element.hasAttribute(STUDIO_BOX_SIZE_ATTR)) { element.setAttribute(STUDIO_ORIGINAL_WIDTH_ATTR, element.style.getPropertyValue("width")); element.setAttribute(STUDIO_ORIGINAL_HEIGHT_ATTR, element.style.getPropertyValue("height")); diff --git a/packages/studio/src/components/editor/manualEditsDomPatches.test.ts b/packages/studio/src/components/editor/manualEditsDomPatches.test.ts index 5ef3a539fc..953e78b70c 100644 --- a/packages/studio/src/components/editor/manualEditsDomPatches.test.ts +++ b/packages/studio/src/components/editor/manualEditsDomPatches.test.ts @@ -17,6 +17,8 @@ import { STUDIO_ORIGINAL_INLINE_TRANSLATE_ATTR, STUDIO_ORIGINAL_WIDTH_ATTR, STUDIO_ORIGINAL_HEIGHT_ATTR, + STUDIO_ORIGINAL_BOX_WIDTH_ATTR, + STUDIO_ORIGINAL_BOX_HEIGHT_ATTR, STUDIO_ORIGINAL_MIN_WIDTH_ATTR, STUDIO_ORIGINAL_MIN_HEIGHT_ATTR, STUDIO_ORIGINAL_MAX_WIDTH_ATTR, @@ -222,6 +224,9 @@ describe("buildBoxSizePatches / buildClearBoxSizePatches", () => { { type: "attribute", property: STUDIO_ORIGINAL_WIDTH_ATTR, value: null }, { type: "inline-style", property: "height", value: "150px" }, { type: "attribute", property: STUDIO_ORIGINAL_HEIGHT_ATTR, value: null }, + // Measurements, so they are cleared without restoring a style. + { type: "attribute", property: STUDIO_ORIGINAL_BOX_WIDTH_ATTR, value: null }, + { type: "attribute", property: STUDIO_ORIGINAL_BOX_HEIGHT_ATTR, value: null }, { type: "inline-style", property: "min-width", value: "0px" }, { type: "attribute", property: STUDIO_ORIGINAL_MIN_WIDTH_ATTR, value: null }, { type: "inline-style", property: "min-height", value: "0px" }, @@ -257,11 +262,25 @@ describe("buildBoxSizePatches / buildClearBoxSizePatches", () => { it("clear: bare element emits only null ops — no style restores fire when orig attrs are absent", () => { const ops = buildClearBoxSizePatches(div()); - // 3 fixed (studio-width, studio-height, box-size marker) + 14 attr-null pushes (one per BOX_SIZE_ORIG_ATTR) - expect(ops).toHaveLength(17); + // 3 fixed (studio-width, studio-height, box-size marker) + 16 attr-null pushes (one per BOX_SIZE_ORIG_ATTR) + expect(ops).toHaveLength(19); expect(ops.every((op) => op.value === null)).toBe(true); }); + it("backfills the measured box on a legacy element that already has the resize marker", () => { + const e = div(); + e.setAttribute(STUDIO_BOX_SIZE_ATTR, "true"); + Object.defineProperties(e, { + offsetWidth: { configurable: true, value: 630 }, + offsetHeight: { configurable: true, value: 252 }, + }); + + applyStudioBoxSize(e, { width: 320, height: 128 }); + + expect(e.getAttribute(STUDIO_ORIGINAL_BOX_WIDTH_ATTR)).toBe("630"); + expect(e.getAttribute(STUDIO_ORIGINAL_BOX_HEIGHT_ATTR)).toBe("252"); + }); + it("build/clear symmetry: clear addresses every {type,property} key that build emits", () => { const e = populatedBoxEl(); assertClearCoversKeys(buildBoxSizePatches(e), buildClearBoxSizePatches(e)); diff --git a/packages/studio/src/components/editor/manualEditsDomPatches.ts b/packages/studio/src/components/editor/manualEditsDomPatches.ts index 83dea1bb47..1cfbf3e0a8 100644 --- a/packages/studio/src/components/editor/manualEditsDomPatches.ts +++ b/packages/studio/src/components/editor/manualEditsDomPatches.ts @@ -13,6 +13,8 @@ import { STUDIO_ORIGINAL_INLINE_TRANSLATE_ATTR, STUDIO_ORIGINAL_WIDTH_ATTR, STUDIO_ORIGINAL_HEIGHT_ATTR, + STUDIO_ORIGINAL_BOX_WIDTH_ATTR, + STUDIO_ORIGINAL_BOX_HEIGHT_ATTR, STUDIO_ORIGINAL_MIN_WIDTH_ATTR, STUDIO_ORIGINAL_MIN_HEIGHT_ATTR, STUDIO_ORIGINAL_MAX_WIDTH_ATTR, @@ -135,6 +137,9 @@ const BOX_SIZE_STYLE_PROPS = [ const BOX_SIZE_ORIG_ATTRS: ReadonlyArray<[string, string]> = [ [STUDIO_ORIGINAL_WIDTH_ATTR, "width"], [STUDIO_ORIGINAL_HEIGHT_ATTR, "height"], + // Records a measurement rather than a style, so it restores nothing. + [STUDIO_ORIGINAL_BOX_WIDTH_ATTR, ""], + [STUDIO_ORIGINAL_BOX_HEIGHT_ATTR, ""], [STUDIO_ORIGINAL_MIN_WIDTH_ATTR, "min-width"], [STUDIO_ORIGINAL_MIN_HEIGHT_ATTR, "min-height"], [STUDIO_ORIGINAL_MAX_WIDTH_ATTR, "max-width"], diff --git a/packages/studio/src/components/editor/manualEditsSnapshot.ts b/packages/studio/src/components/editor/manualEditsSnapshot.ts index afc8aa30e0..826fb16829 100644 --- a/packages/studio/src/components/editor/manualEditsSnapshot.ts +++ b/packages/studio/src/components/editor/manualEditsSnapshot.ts @@ -17,6 +17,8 @@ import { STUDIO_ORIGINAL_INLINE_TRANSLATE_ATTR, STUDIO_ORIGINAL_WIDTH_ATTR, STUDIO_ORIGINAL_HEIGHT_ATTR, + STUDIO_ORIGINAL_BOX_WIDTH_ATTR, + STUDIO_ORIGINAL_BOX_HEIGHT_ATTR, STUDIO_ORIGINAL_MIN_WIDTH_ATTR, STUDIO_ORIGINAL_MIN_HEIGHT_ATTR, STUDIO_ORIGINAL_MAX_WIDTH_ATTR, @@ -60,6 +62,8 @@ export function captureStudioBoxSize(element: HTMLElement): StudioBoxSizeSnapsho marker: element.getAttribute(STUDIO_BOX_SIZE_ATTR), originalWidth: element.getAttribute(STUDIO_ORIGINAL_WIDTH_ATTR), originalHeight: element.getAttribute(STUDIO_ORIGINAL_HEIGHT_ATTR), + originalBoxWidth: element.getAttribute(STUDIO_ORIGINAL_BOX_WIDTH_ATTR), + originalBoxHeight: element.getAttribute(STUDIO_ORIGINAL_BOX_HEIGHT_ATTR), originalMinWidth: element.getAttribute(STUDIO_ORIGINAL_MIN_WIDTH_ATTR), originalMinHeight: element.getAttribute(STUDIO_ORIGINAL_MIN_HEIGHT_ATTR), originalMaxWidth: element.getAttribute(STUDIO_ORIGINAL_MAX_WIDTH_ATTR), @@ -128,6 +132,8 @@ export function restoreStudioBoxSize(element: HTMLElement, previous: StudioBoxSi restoreAttribute(element, STUDIO_BOX_SIZE_ATTR, previous.marker); restoreAttribute(element, STUDIO_ORIGINAL_WIDTH_ATTR, previous.originalWidth); restoreAttribute(element, STUDIO_ORIGINAL_HEIGHT_ATTR, previous.originalHeight); + restoreAttribute(element, STUDIO_ORIGINAL_BOX_WIDTH_ATTR, previous.originalBoxWidth); + restoreAttribute(element, STUDIO_ORIGINAL_BOX_HEIGHT_ATTR, previous.originalBoxHeight); restoreAttribute(element, STUDIO_ORIGINAL_MIN_WIDTH_ATTR, previous.originalMinWidth); restoreAttribute(element, STUDIO_ORIGINAL_MIN_HEIGHT_ATTR, previous.originalMinHeight); restoreAttribute(element, STUDIO_ORIGINAL_MAX_WIDTH_ATTR, previous.originalMaxWidth); diff --git a/packages/studio/src/components/editor/manualEditsTypes.ts b/packages/studio/src/components/editor/manualEditsTypes.ts index 074a8762d8..d39c9df0e5 100644 --- a/packages/studio/src/components/editor/manualEditsTypes.ts +++ b/packages/studio/src/components/editor/manualEditsTypes.ts @@ -16,6 +16,18 @@ export const STUDIO_ORIGINAL_TRANSLATE_ATTR = "data-hf-studio-original-translate export const STUDIO_ORIGINAL_INLINE_TRANSLATE_ATTR = "data-hf-studio-original-inline-translate"; export const STUDIO_ORIGINAL_WIDTH_ATTR = "data-hf-studio-original-width"; export const STUDIO_ORIGINAL_HEIGHT_ATTR = "data-hf-studio-original-height"; +/** + * The element's laid-out box before a resize draft touched it, in CSS pixels. + * + * The two attributes above record the element's INLINE width and height so a + * reset can put them back, and are empty for the usual case of an element sized + * by the stylesheet. That made them useless as a measurement, and the resize + * intercept, which needs the original box to work out a scale, fell back to a + * hardcoded guess and produced a wildly wrong one. These record the measurement + * instead, and restore nothing. + */ +export const STUDIO_ORIGINAL_BOX_WIDTH_ATTR = "data-hf-studio-original-box-width"; +export const STUDIO_ORIGINAL_BOX_HEIGHT_ATTR = "data-hf-studio-original-box-height"; export const STUDIO_ORIGINAL_MIN_WIDTH_ATTR = "data-hf-studio-original-min-width"; export const STUDIO_ORIGINAL_MIN_HEIGHT_ATTR = "data-hf-studio-original-min-height"; export const STUDIO_ORIGINAL_MAX_WIDTH_ATTR = "data-hf-studio-original-max-width"; @@ -70,6 +82,8 @@ export interface StudioBoxSizeSnapshot { marker: string | null; originalWidth: string | null; originalHeight: string | null; + originalBoxWidth: string | null; + originalBoxHeight: string | null; originalMinWidth: string | null; originalMinHeight: string | null; originalMaxWidth: string | null; diff --git a/packages/studio/src/hooks/gsapResizeDropPoint.test.ts b/packages/studio/src/hooks/gsapResizeDropPoint.test.ts new file mode 100644 index 0000000000..19d5c816d4 --- /dev/null +++ b/packages/studio/src/hooks/gsapResizeDropPoint.test.ts @@ -0,0 +1,394 @@ +// @vitest-environment happy-dom +/** + * The scale-route resize invariant, swept over the shapes a composition + * actually produces: WHERE THE USER DROPS THE BOX IS WHERE IT LANDS. + * + * Each case builds the geometry the browser reports — a CSS box at a layout + * position, transformed by GSAP's translate/rotate/scale about the element + * centre — drives one resize through the real intercept, then re-renders the + * committed scale and the PERSISTED position and checks the box is still on + * the drop point. Asserting on the persisted values rather than the live ones + * is the point: every bug in this class showed as a correct-looking drop frame + * followed by the element sliding to whatever got written to disk. + * + * The model is calibrated against real `hf-resize-debug` output: the same + * inputs reproduce the drop rect, the post-commit rect and the rect width the + * browser reported to three decimal places. + */ +import { afterEach, describe, expect, it, vi } from "vitest"; +import type { GsapAnimation } from "@hyperframes/core/gsap-parser"; +import type { DomEditSelection } from "../components/editor/domEditingTypes"; +import { usePlayerStore } from "../player/store/playerStore"; +import { tryGsapResizeIntercept } from "./gsapResizeIntercept"; + +afterEach(() => { + vi.restoreAllMocks(); + usePlayerStore.setState({ currentTime: 0, activeKeyframePct: null }); + document.body.innerHTML = ""; +}); + +interface Pose { + /** Untransformed CSS box, in px. */ + box: { w: number; h: number }; + /** GSAP translate. */ + pos: { x: number; y: number }; + /** GSAP scale, per axis. */ + scale: { x: number; y: number }; +} + +/** Layout position of the untransformed box, shared by every pose in a case. */ +const LAYOUT = { left: 120, top: 520 }; + +/** + * The AABB a browser reports for `translate() rotate() scale()` about the + * element centre — the same rect `getBoundingClientRect` returns, which is why + * a rotated element's anchor is approximate rather than corner-exact. + */ +function renderRect( + pose: Pose, + rotationDeg: number, +): { x: number; y: number; w: number; h: number } { + const rad = (rotationDeg * Math.PI) / 180; + const [cos, sin] = [Math.abs(Math.cos(rad)), Math.abs(Math.sin(rad))]; + const [sw, sh] = [pose.box.w * pose.scale.x, pose.box.h * pose.scale.y]; + const w = sw * cos + sh * sin; + const h = sw * sin + sh * cos; + const cx = LAYOUT.left + pose.box.w / 2 + pose.pos.x; + const cy = LAYOUT.top + pose.box.h / 2 + pose.pos.y; + return { x: cx - w / 2, y: cy - h / 2, w, h }; +} + +interface ResizeCase { + name: string; + /** Untransformed box the stylesheet (or an inline style) gives the element. */ + box: { w: number; h: number }; + /** Where the element sat before the gesture. */ + base: { x: number; y: number }; + /** Scale already on the element — 1 for a never-resized one. */ + liveScale: { x: number; y: number }; + rotation?: number; + /** The box the user dragged to, and where the draft put it. */ + drop: { w: number; h: number; x: number; y: number }; + /** Sized inline rather than by a stylesheet. */ + inlineSized?: boolean; + /** The tween states scaleX/scaleY rather than the shorthand. */ + longhandTween?: boolean; + /** What already writes this element's position. */ + positionWrite?: "static-set" | "keyframed-tween" | "none"; +} + +function createResizeElement(testCase: ResizeCase): HTMLElement { + const el = document.createElement("div"); + el.id = "clip"; + if (testCase.inlineSized) { + el.setAttribute("data-hf-studio-original-width", `${testCase.box.w}px`); + el.setAttribute("data-hf-studio-original-height", `${testCase.box.h}px`); + } else { + el.setAttribute("data-hf-studio-original-box-width", `${testCase.box.w}`); + el.setAttribute("data-hf-studio-original-box-height", `${testCase.box.h}`); + el.setAttribute("data-hf-studio-original-width", ""); + el.setAttribute("data-hf-studio-original-height", ""); + } + return el; +} + +function scaleTween(longhand: boolean): GsapAnimation { + const at = (v: number) => (longhand ? { scaleX: v, scaleY: v } : { scale: v }); + return { + id: "#clip-to-0-scale", + targetSelector: "#clip", + propertyGroup: "scale", + method: "to", + properties: at(1), + position: 0, + resolvedStart: 0, + duration: 2, + keyframes: { + keyframes: [ + { percentage: 0, properties: at(1) }, + { percentage: 100, properties: at(1.08) }, + ], + }, + } as unknown as GsapAnimation; +} + +function positionAnimation( + kind: NonNullable, + base: { x: number; y: number }, +) { + if (kind === "none") return null; + if (kind === "static-set") { + return { + id: "#clip-set-0-position", + targetSelector: "#clip", + propertyGroup: "position", + method: "set", + properties: { x: base.x, y: base.y }, + position: 0, + resolvedStart: 0, + duration: 0, + global: true, + } as unknown as GsapAnimation; + } + return { + id: "#clip-to-0-position", + targetSelector: "#clip", + propertyGroup: "position", + method: "to", + properties: { x: base.x, y: base.y }, + position: 0, + resolvedStart: 0, + duration: 2, + keyframes: { + keyframes: [ + { percentage: 0, properties: { x: base.x, y: base.y } }, + { percentage: 100, properties: { x: base.x + 40, y: base.y + 40 } }, + ], + }, + } as unknown as GsapAnimation; +} + +/** Every x/y a run wrote, in commit order. */ +function persistedPositions(calls: unknown[][]): Array<{ x?: number; y?: number }> { + const written: Array<{ x?: number; y?: number }> = []; + for (const call of calls) { + const mutation = call[1] as { + properties?: Record; + keyframes?: Array<{ properties: Record }>; + x?: number; + y?: number; + }; + const sources = [mutation.properties, ...(mutation.keyframes ?? []).map((k) => k.properties)]; + if (mutation.x != null || mutation.y != null) written.push({ x: mutation.x, y: mutation.y }); + for (const source of sources) { + if (source && (source.x != null || source.y != null)) + written.push({ x: source.x, y: source.y }); + } + } + return written; +} + +/** The scale the run committed AT THE PLAYHEAD (percentage 0 here). */ +function persistedScale(calls: unknown[][]): { x: number; y: number } | null { + let found: { x: number; y: number } | null = null; + const take = (source: Record | undefined) => { + if (!source) return; + const x = source.scaleX ?? source.scale; + const y = source.scaleY ?? source.scale; + if (x != null && y != null) found = { x, y }; + }; + for (const call of calls) { + const mutation = call[1] as { + properties?: Record; + percentage?: number; + keyframes?: Array<{ percentage: number; properties: Record }>; + }; + if (mutation.keyframes) { + for (const frame of mutation.keyframes) if (frame.percentage === 0) take(frame.properties); + continue; + } + if (mutation.percentage != null && mutation.percentage !== 0) continue; + take(mutation.properties); + } + return found; +} + +async function runCase(testCase: ResizeCase) { + const rotation = testCase.rotation ?? 0; + const el = createResizeElement(testCase); + // What the gesture stamps at drag start, and the draft it leaves applied. + el.setAttribute("data-hf-drag-gsap-base-x", `${testCase.base.x}`); + el.setAttribute("data-hf-drag-gsap-base-y", `${testCase.base.y}`); + el.setAttribute("data-hf-studio-box-size", "true"); + el.style.width = `${testCase.drop.w}px`; + el.style.height = `${testCase.drop.h}px`; + document.body.append(el); + + // The live pose the intercept reads and mutates. + const live: Pose = { + box: { ...testCase.box }, + pos: { x: testCase.drop.x, y: testCase.drop.y }, + scale: { ...testCase.liveScale }, + }; + el.getBoundingClientRect = () => { + // The draft's inline box while it is applied, the real one once cleared. + const w = Number.parseFloat(el.style.width) || live.box.w; + const h = Number.parseFloat(el.style.height) || live.box.h; + const rect = renderRect({ ...live, box: { w, h } }, rotation); + return { ...rect, width: rect.w, height: rect.h } as unknown as DOMRect; + }; + const gsapStub = { + set: (_target: Element, vars: Record) => { + if (vars.x != null) live.pos.x = vars.x; + if (vars.y != null) live.pos.y = vars.y; + if (vars.scaleX != null) live.scale.x = vars.scaleX; + if (vars.scaleY != null) live.scale.y = vars.scaleY; + }, + getProperty: (_target: Element, prop: string) => + ({ + scaleX: live.scale.x, + scaleY: live.scale.y, + x: live.pos.x, + y: live.pos.y, + rotation, + })[prop] ?? 0, + }; + Object.assign(window, { gsap: gsapStub }); + const iframe = { + contentWindow: { gsap: gsapStub, __timelines: { main: { getChildren: () => [] } } }, + contentDocument: document, + } as unknown as HTMLIFrameElement; + + const dropPoint = el.getBoundingClientRect(); + const position = positionAnimation(testCase.positionWrite ?? "static-set", testCase.base); + const animations = [scaleTween(!!testCase.longhandTween), ...(position ? [position] : [])]; + const commitMutation = vi.fn(); + usePlayerStore.setState({ currentTime: 0 }); + + await tryGsapResizeIntercept( + { id: "clip", selector: "#clip", element: el } as DomEditSelection, + { width: testCase.drop.w, height: testCase.drop.h }, + animations, + iframe, + commitMutation, + async () => animations, + ); + + // Re-render what the file now says: the committed scale, the persisted + // position (or the pre-gesture one when nothing was written), the real box. + const scale = persistedScale(commitMutation.mock.calls) ?? testCase.liveScale; + const writes = persistedPositions(commitMutation.mock.calls); + const last = writes.at(-1); + const settled: Pose = { + box: testCase.box, + pos: { x: last?.x ?? testCase.base.x, y: last?.y ?? testCase.base.y }, + scale, + }; + return { + dropPoint: { x: dropPoint.x, y: dropPoint.y, w: dropPoint.width, h: dropPoint.height }, + settled: renderRect(settled, rotation), + writes, + }; +} + +const CASES: ResizeCase[] = [ + { + name: "shrinks a stylesheet-sized element", + box: { w: 630, h: 252 }, + base: { x: 489, y: 195 }, + liveScale: { x: 1.648, y: 1.648 }, + drop: { w: 320, h: 128, x: 587, y: 235 }, + }, + { + name: "grows a stylesheet-sized element", + box: { w: 630, h: 252 }, + base: { x: 489, y: 195 }, + liveScale: { x: 0.837, y: 0.837 }, + drop: { w: 1319, h: 527, x: -67, y: -26 }, + }, + { + name: "resizes an element for the first time (no scale of its own yet)", + box: { w: 630, h: 252 }, + base: { x: 489, y: 195 }, + liveScale: { x: 1, y: 1 }, + drop: { w: 900, h: 360, x: 300, y: 100 }, + }, + { + name: "resizes a rotated element", + box: { w: 630, h: 200 }, + base: { x: 329, y: 129 }, + liveScale: { x: 0.94, y: 0.939 }, + rotation: -8, + longhandTween: true, + drop: { w: 350, h: 111, x: 609, y: 219 }, + }, + { + name: "resizes a steeply rotated element", + box: { w: 630, h: 252 }, + base: { x: 17, y: -69 }, + liveScale: { x: 1.192, y: 1.2 }, + rotation: -47, + longhandTween: true, + drop: { w: 491, h: 196, x: 120, y: 40 }, + }, + { + name: "takes a non-uniform drag", + box: { w: 630, h: 252 }, + base: { x: 489, y: 195 }, + liveScale: { x: 1, y: 1 }, + longhandTween: true, + drop: { w: 1200, h: 300, x: -80, y: 60 }, + }, + { + name: "shrinks almost to nothing", + box: { w: 630, h: 252 }, + base: { x: 489, y: 195 }, + liveScale: { x: 1, y: 1 }, + drop: { w: 13, h: 5, x: 800, y: 320 }, + }, + { + name: "resizes an inline-sized element", + box: { w: 500, h: 252 }, + base: { x: 100, y: 100 }, + liveScale: { x: 0.5, y: 0.5 }, + inlineSized: true, + drop: { w: 700, h: 353, x: -40, y: -30 }, + }, + { + name: "resizes an element with no position write at all", + box: { w: 630, h: 252 }, + base: { x: 0, y: 0 }, + liveScale: { x: 1, y: 1 }, + positionWrite: "none", + drop: { w: 900, h: 360, x: 140, y: 55 }, + }, + { + name: "resizes an element whose position is animated", + box: { w: 630, h: 252 }, + base: { x: 489, y: 195 }, + liveScale: { x: 1.648, y: 1.648 }, + positionWrite: "keyframed-tween", + drop: { w: 320, h: 128, x: 587, y: 235 }, + }, +]; + +describe("a scale resize lands the element on the drop point", () => { + for (const testCase of CASES) { + it(testCase.name, async () => { + const { dropPoint, settled } = await runCase(testCase); + // 1px: the commit rounds position to whole pixels and scale to 3dp. + expect(settled.x).toBeCloseTo(dropPoint.x, 0); + expect(settled.y).toBeCloseTo(dropPoint.y, 0); + expect(settled.w).toBeCloseTo(dropPoint.w, 0); + expect(settled.h).toBeCloseTo(dropPoint.h, 0); + }); + } +}); + +/** + * Two drags in a row on the same element. The first bug in this class only + * showed on the second one, because the wrong value the first wrote then + * counted as the element's live pose. + */ +it("holds the drop point across a second drag", async () => { + const first = await runCase({ + name: "first", + box: { w: 630, h: 252 }, + base: { x: 489, y: 195 }, + liveScale: { x: 1, y: 1 }, + drop: { w: 900, h: 360, x: 300, y: 100 }, + }); + const settledPos = first.writes.at(-1); + document.body.innerHTML = ""; + const second = await runCase({ + name: "second", + box: { w: 630, h: 252 }, + base: { x: settledPos?.x ?? 489, y: settledPos?.y ?? 195 }, + // What the first drag committed: 900/630. + liveScale: { x: 1.429, y: 1.429 }, + drop: { w: 420, h: 168, x: 640, y: 260 }, + }); + expect(second.settled.x).toBeCloseTo(second.dropPoint.x, 0); + expect(second.settled.y).toBeCloseTo(second.dropPoint.y, 0); + expect(second.settled.w).toBeCloseTo(second.dropPoint.w, 0); +}); diff --git a/packages/studio/src/hooks/gsapResizeIntercept.test.ts b/packages/studio/src/hooks/gsapResizeIntercept.test.ts index cc1e241129..ba4a03db07 100644 --- a/packages/studio/src/hooks/gsapResizeIntercept.test.ts +++ b/packages/studio/src/hooks/gsapResizeIntercept.test.ts @@ -284,3 +284,201 @@ it("non-uniform drag commits scaleX/scaleY longhands", async () => { expect(serialized).toContain("scaleX"); expect(serialized).toContain("scaleY"); }); + +/** + * The bug: the original box size was read only from the element's INLINE + * width, and a composition sizes its elements from the stylesheet. With no + * inline width the code fell back to a hardcoded 200, so the committed scale + * came out `real / 200` times too large. Dropping a 630px chip at 2391px wide + * left it rendering at 7532px, over three times where it was dropped, and the + * next drag compounded it. + */ +// fallow-ignore-next-line code-duplication +it("scales from the element's real box, not a hardcoded fallback", async () => { + const el = document.createElement("div"); + el.id = "clip"; + // Sized by a stylesheet, so it carries no inline width, and the draft + // recorded the box it measured instead. + el.setAttribute("data-hf-studio-original-box-width", "630"); + el.setAttribute("data-hf-studio-original-box-height", "252"); + document.body.append(el); + const selection = { id: "clip", selector: "#clip", element: el } as DomEditSelection; + const commitMutation = vi.fn(); + + await tryGsapResizeIntercept( + selection, + { width: 1260, height: 504 }, + [keyframedScaleFixture()], + fakeIframe(el, { scaleX: 1, scaleY: 1 }), + commitMutation, + ); + + type Mutation = { + properties?: Record; + keyframes?: Array<{ percentage: number; properties: Record }>; + }; + const committed = commitMutation.mock.calls + .map((call) => call[1] as Mutation) + .flatMap((mutation) => [ + mutation.properties, + ...(mutation.keyframes ?? []).map((frame) => frame.properties), + ]) + .filter((properties): properties is Record => properties != null) + .find((properties) => properties.scale != null || properties.scaleX != null); + + // Dropped at twice the element's own size, so the scale is about 2. The + // number that matters is that it is not the 6.3 which 1260/200 produced. + const scale = committed?.scale ?? committed?.scaleX ?? 0; + expect(scale).toBeCloseTo(2, 1); + expect(scale).toBeLessThan(3); +}); + +/** + * The bug: a uniform drag committed the `scale` shorthand into a tween whose + * keyframes already stated `scaleX`/`scaleY`. GSAP animates each property name + * independently, so the keyframe ran as `{ scaleX: 1, scaleY: 1, scale: 0.61 }` + * and the longhands won. The resize computed the right number, wrote it, and + * the element snapped straight back to its old size on release. + */ +// fallow-ignore-next-line code-duplication +it("does not mix the scale shorthand into a tween that speaks longhands", async () => { + const el = document.createElement("div"); + el.id = "clip"; + el.setAttribute("data-hf-studio-original-box-width", "630"); + el.setAttribute("data-hf-studio-original-box-height", "252"); + document.body.append(el); + const selection = { id: "clip", selector: "#clip", element: el } as DomEditSelection; + const longhandTween = { + ...scaleFromTween(), + keyframes: { + keyframes: [ + { percentage: 0, properties: { scaleX: 1, scaleY: 1 } }, + { percentage: 100, properties: { scaleX: 1.15, scaleY: 1.15 } }, + ], + }, + } as unknown as GsapAnimation; + const commitMutation = vi.fn(); + + // A uniform drop, so the old code took the shorthand branch and wrote + // `scale` into keyframes that already stated the longhands. + await tryGsapResizeIntercept( + selection, + { width: 384, height: 216 }, + [longhandTween], + fakeIframe(el, { scaleX: 1, scaleY: 1 }), + commitMutation, + ); + + const frames = commitMutation.mock.calls + .map((call) => call[1] as { keyframes?: Array<{ properties: Record }> }) + .flatMap((mutation) => mutation.keyframes ?? []); + expect(frames.length).toBeGreaterThan(0); + for (const frame of frames) { + const names = Object.keys(frame.properties); + const hasShorthand = names.includes("scale"); + const hasLonghand = names.includes("scaleX") || names.includes("scaleY"); + expect(hasShorthand && hasLonghand).toBe(false); + } + // And the resize still lands: 384/630 is about 0.61. + const resized = frames.find((frame) => frame.properties.scaleX != null); + expect(resized?.properties.scaleX).toBeCloseTo(0.61, 1); +}); + +/** + * The bug: a scale resize measured its drop-point correction while the + * gesture's own translation was still applied, but the position commit adds + * that correction onto the element's PRE-gesture position (it reads the + * gesture's base attributes). The two disagreed by the whole drag distance, so + * every scale resize of a statically positioned element persisted a position a + * drag-length away from where it was dropped — the element held still for one + * frame and then slid off. + * + * The fixture models the geometry the browser reported: a 630x252 element + * dragged from x=432 to x=587 with its box drafted down to 320x128, dropped at + * a committed scale of 0.837. Scaling about the centre puts it back on the drop + * point at its pre-gesture position, so the correct persisted correction is + * NONE. + */ +it("does not move a statically positioned element when a scale resize lands", async () => { + document.body.innerHTML = ""; + const el = document.createElement("div"); + el.id = "clip"; + el.setAttribute("data-hf-studio-original-box-width", "630"); + el.setAttribute("data-hf-studio-original-box-height", "252"); + // The gesture's base pose — where the commit puts the element back, since a + // scale resize never persists the drag translation. + el.setAttribute("data-hf-drag-gsap-base-x", "432"); + el.setAttribute("data-hf-drag-gsap-base-y", "173"); + // The draft the gesture left applied: a smaller box at the dragged position. + el.setAttribute("data-hf-studio-box-size", "true"); + el.setAttribute("data-hf-studio-original-width", ""); + el.setAttribute("data-hf-studio-original-height", ""); + el.style.width = "320px"; + el.style.height = "128px"; + document.body.append(el); + + const pos = { x: 587, y: 235 }; + const scale = { x: 1.648, y: 1.648 }; + const [LEFT, TOP] = [120, 520]; + el.getBoundingClientRect = () => { + const cssW = Number.parseFloat(el.style.width) || 630; + const cssH = Number.parseFloat(el.style.height) || 252; + const [w, h] = [cssW * scale.x, cssH * scale.y]; + // GSAP scales about the element centre, so the box grows around it. + return { + x: LEFT + pos.x + cssW / 2 - w / 2, + y: TOP + pos.y + cssH / 2 - h / 2, + width: w, + height: h, + } as DOMRect; + }; + const gsapStub = { + set: (_target: Element, vars: Record) => { + if (vars.x != null) pos.x = vars.x; + if (vars.y != null) pos.y = vars.y; + if (vars.scaleX != null) scale.x = vars.scaleX; + if (vars.scaleY != null) scale.y = vars.scaleY; + }, + getProperty: (_target: Element, prop: string) => + ({ scaleX: scale.x, scaleY: scale.y, x: pos.x, y: pos.y })[prop] ?? 0, + }; + Object.assign(window, { gsap: gsapStub }); + const iframe = { + contentWindow: { gsap: gsapStub, __timelines: {} }, + contentDocument: document, + } as unknown as HTMLIFrameElement; + const positionHold = { + id: "#clip-set-0-position", + targetSelector: "#clip", + propertyGroup: "position", + method: "set", + properties: { x: 432, y: 173 }, + position: 0, + resolvedStart: 0, + duration: 0, + global: true, + } as unknown as GsapAnimation; + const selection = { id: "clip", selector: "#clip", element: el } as DomEditSelection; + usePlayerStore.setState({ currentTime: 0.5 }); + const commitMutation = vi.fn(); + + await tryGsapResizeIntercept( + selection, + { width: 320, height: 128 }, + [keyframedScaleFixture(), positionHold], + iframe, + commitMutation, + async () => [keyframedScaleFixture(), positionHold], + ); + + const positionWrites = commitMutation.mock.calls + .map((call) => call[1] as { properties?: Record }) + .filter((mutation) => mutation.properties?.x != null || mutation.properties?.y != null); + // Either it left the position alone, or it rewrote the same value. + for (const write of positionWrites) { + expect(write.properties?.x).toBe(432); + expect(write.properties?.y).toBe(173); + } + // And the live element ends on the drop point, not a drag away from it. + expect(el.getBoundingClientRect().x).toBeCloseTo(603.3, 0); +}); diff --git a/packages/studio/src/hooks/gsapResizeIntercept.ts b/packages/studio/src/hooks/gsapResizeIntercept.ts index 04eac1fc4d..82a6fbfb8b 100644 --- a/packages/studio/src/hooks/gsapResizeIntercept.ts +++ b/packages/studio/src/hooks/gsapResizeIntercept.ts @@ -8,8 +8,13 @@ import type { GsapAnimation, PropertyGroupName } from "@hyperframes/core/gsap-parser"; import type { DomEditSelection } from "../components/editor/domEditingTypes"; import { clearStudioBoxSize } from "../components/editor/manualEdits"; -import { setElementGsapPosition } from "../utils/elementGsap"; +import { + STUDIO_ORIGINAL_BOX_HEIGHT_ATTR, + STUDIO_ORIGINAL_BOX_WIDTH_ATTR, +} from "../components/editor/manualEditsTypes"; +import { setElementGsapPosition, setElementGsapScale } from "../utils/elementGsap"; import { usePlayerStore } from "../player/store/playerStore"; +import { hasNonHoldTweenForElement } from "./gsapRuntimeKeyframes"; import { readAllAnimatedProperties, readGsapProperty } from "./gsapRuntimeReaders"; import { commitStaticGsapPosition, @@ -21,13 +26,14 @@ import { materializeIfDynamic, } from "./gsapDragCommit"; import type { GsapDragCommitCallbacks } from "./gsapDragCommit"; +import { computeDraggedGsapPosition } from "./draggedGsapPosition"; import { pickClosestToPlayhead, readGsapPositionFromIframe } from "./gsapPositionDetection"; import { commitWholePropertyOffset } from "./gsapWholePropertyOffsetCommit"; +import { commitGsapPositionFromDrag } from "./gsapDragPositionCommit"; import { resolveTweenStart, resolveTweenDuration } from "../utils/globalTimeCompiler"; import { isInstantHold, selectorFromSelection, writeTargetSelector } from "./gsapShared"; import { roundTo3 } from "../utils/rounding"; -import { resolveGroupTween, POSITION_CHANNELS } from "./gsapRuntimeBridge"; -import { hasNonHoldTweenForElement } from "./gsapRuntimeKeyframes"; +import { resolveGroupTween } from "./gsapRuntimeBridge"; import { logResize } from "../utils/resizeDebug"; import { animationWritesAnyProperty, @@ -49,6 +55,42 @@ function synthesizeIdentityProps( return id; } +/** + * The element's box before the resize draft ran, in CSS pixels. + * + * Prefers the measurement the draft recorded. Falls back to the inline style it + * saved for restoring, which is a real value for the elements that carry one, + * and null when neither says anything. + */ +function originalBoxSize( + el: HTMLElement | null, + measuredAttr: string, + inlineProperty: "width" | "height", +): number | null { + const measured = Number.parseFloat(el?.getAttribute(measuredAttr) ?? ""); + if (Number.isFinite(measured) && measured > 0) return measured; + const inline = Number.parseFloat( + el?.getAttribute(`data-hf-studio-original-${inlineProperty}`) ?? "", + ); + return Number.isFinite(inline) && inline > 0 ? inline : null; +} + +/** + * Whether this tween already states scale as `scaleX`/`scaleY`. + * + * Both forms are legal, and either alone is fine. A tween holding both is not: + * GSAP animates each property name independently, so the longhands run + * alongside the shorthand and win, which silently discards whatever the + * shorthand was set to. + */ +function tweenUsesScaleLonghands(anim: GsapAnimation | null): boolean { + const isLonghand = (name: string) => name === "scaleX" || name === "scaleY"; + const inKeyframes = (anim?.keyframes?.keyframes ?? []).some((frame) => + Object.keys(frame.properties ?? {}).some(isLonghand), + ); + return inKeyframes || Object.keys(anim?.properties ?? {}).some(isLonghand); +} + // ── Resize intercept ────────────────────────────────────────────────────── // fallow-ignore-next-line complexity @@ -155,17 +197,26 @@ export async function tryGsapResizeIntercept( let resizeProps: Record; let scaleDraftEl: HTMLElement | null = null; let scaleDraftDropPoint: { x: number; y: number } | null = null; + /** The scale this commit is putting on the element, for the finalize step. */ + let committedScale: { x: number; y: number } | null = null; let nonUniformScale = false; + /** Whether this commit writes scaleX/scaleY rather than the `scale` shorthand. */ + let useScaleLonghands = false; if (resizeGroup === "scale") { // Iframe-realm element — instanceof HTMLElement fails across realms; the // selector targets composition elements, and every use below is duck-typed. const el = iframe?.contentDocument?.querySelector(selector ?? "") as HTMLElement | null; // The resize draft modifies el.style.width/height, so read the ORIGINAL // dimensions saved by the draft system before it ran. - const origW = Number.parseFloat(el?.getAttribute("data-hf-studio-original-width") ?? ""); - const origH = Number.parseFloat(el?.getAttribute("data-hf-studio-original-height") ?? ""); - const cssW = Number.isFinite(origW) && origW > 0 ? origW : 200; - const cssH = Number.isFinite(origH) && origH > 0 ? origH : cssW; + // + // The measured box first, then the inline one. The inline attributes exist + // to restore an inline style and are empty for anything sized by a + // stylesheet, which is how compositions are written, so reading them alone + // sent almost every element to the fallback below: a 630px chip scaled by + // 630/200, landing over three times the size it was dropped at, and worse + // on the next drag because the wrong scale then counted as its live one. + const cssW = originalBoxSize(el, STUDIO_ORIGINAL_BOX_WIDTH_ATTR, "width") ?? 200; + const cssH = originalBoxSize(el, STUDIO_ORIGINAL_BOX_HEIGHT_ATTR, "height") ?? cssW; // `size` is the draft's CSS box; on screen it is multiplied by the element's // LIVE scale (the draft divides the cursor delta by it — see // resolveDomEditResizeGesture). The committed keyframe REPLACES that live @@ -181,8 +232,18 @@ export async function tryGsapResizeIntercept( // can't represent it — committing width-derived scale used to snap the // height at drop. Commit scaleX/scaleY longhands instead; keep the uniform // shorthand when the two agree (aspect-true drags, shift-drags). + // + // Unless the tween already speaks longhands, in which case a uniform drag + // has to as well. GSAP animates each property name on its own, so a + // keyframe holding `{ scaleX: 1, scaleY: 1, scale: 0.61 }` runs all three + // and the longhands win: the resize commits correctly and then does + // nothing, and the element snaps back to its old size on release. The + // tween never mixes the two forms in either direction. nonUniformScale = Math.abs(newScaleX - newScaleY) > 0.01; - resizeProps = nonUniformScale ? { scaleX: newScaleX, scaleY: newScaleY } : { scale: newScaleX }; + useScaleLonghands = nonUniformScale || tweenUsesScaleLonghands(anim); + resizeProps = useScaleLonghands + ? { scaleX: newScaleX, scaleY: newScaleY } + : { scale: newScaleX }; logResize("intercept-route", { route: "scale-tween", cssW, @@ -194,6 +255,13 @@ export async function tryGsapResizeIntercept( nonUniformScale, }); scaleDraftEl = el; + // What the commit ACTUALLY writes, which is what the finalize step below + // has to measure against. A near-uniform drag collapses to the shorthand, + // so taking the per-axis pair here measured the element at a scaleY the + // file never gets and tilted the correction by the difference. + committedScale = useScaleLonghands + ? { x: newScaleX, y: newScaleY } + : { x: newScaleX, y: newScaleX }; // Where the user DROPPED the box: the draft (anchor-pinned to the // gesture-start top-left) is still applied here, so this rect is exactly // what the preview showed at release. The committed scale renders around @@ -226,35 +294,61 @@ export async function tryGsapResizeIntercept( if (!scaleDraftEl) return; clearStudioBoxSize(scaleDraftEl); if (!scaleDraftDropPoint || !selector) return; - const hasLivePositionTween = hasNonHoldTweenForElement( - iframe, - selector, - undefined, - POSITION_CHANNELS, - ); - if (hasLivePositionTween) { - logResize("scale-finalize", { skipped: "live-position-tween" }); - return; + // Put the committed scale on the live element before measuring. + // + // This step reads where the commit lands the box and shifts the position + // hold by the difference. That only works if the commit has actually + // rendered, and whether it had was luck: on the FIRST resize of an element + // the timeline had not re-seeked yet, so this measured the element at its + // natural size, still sitting on the drop point, computed a residual of + // zero, and skipped the correction entirely. The scale then landed, GSAP + // rendered it around the element's centre, and the element jumped by the + // whole drag distance. Elements that had been resized before got a + // correction only because their PREVIOUS scale made the residual non-zero. + // + // Setting it here costs nothing when the commit has already rendered (same + // value) and makes the measurement below mean what it says either way. + if (committedScale) { + setElementGsapScale(scaleDraftEl, committedScale.x, committedScale.y); } - // The scale commit has rendered (instant patch or soft-reload seek) and the - // draft is cleared — this rect is where the element ACTUALLY sits now. + // Measure from the pre-gesture position, not the draft one. + // + // The resize draft translates the element to keep the dragged corner under + // the cursor, but the scale route never persists that translation — the + // element renders back at its pre-gesture position as soon as the commit + // lands. Measuring while the draft translation was still applied made the + // residual carry the whole drag distance, and the position commit then + // composed that residual onto the pre-gesture base (it reads the gesture's + // own base attributes, not the live value), so the element landed a full + // drag away from the drop point on every scale resize. + const gsapPos = readGsapPositionFromIframe(iframe, selector) ?? { x: 0, y: 0 }; + const { baseGsapX, baseGsapY } = computeDraggedGsapPosition( + selection.element, + { x: 0, y: 0 }, + gsapPos, + ); + const base = { x: baseGsapX, y: baseGsapY }; + setElementGsapPosition(scaleDraftEl, base.x, base.y); const post = scaleDraftEl.getBoundingClientRect(); const residual = { x: scaleDraftDropPoint.x - post.x, y: scaleDraftDropPoint.y - post.y }; if (!Number.isFinite(residual.x) || !Number.isFinite(residual.y)) return; - if (Math.abs(residual.x) < 0.5 && Math.abs(residual.y) < 0.5) return; - const gsapPos = readGsapPositionFromIframe(iframe, selector) ?? { x: 0, y: 0 }; + if (Math.abs(residual.x) < 0.5 && Math.abs(residual.y) < 0.5) { + logResize("scale-finalize", { skipped: "already-on-drop-point", residual, base }); + return; + } // The ONE corrected position — rounded once so the live runtime and the // persisted file agree exactly (commitStaticGsapPosition composes the same // rounded value from this delta). const corrected = { - x: Math.round(gsapPos.x + residual.x), - y: Math.round(gsapPos.y + residual.y), + x: Math.round(base.x + residual.x), + y: Math.round(base.y + residual.y), }; logResize("scale-finalize", { dropPoint: scaleDraftDropPoint, post: { x: post.x, y: post.y }, residual, gsapPos, + base, corrected, }); // Correct the LIVE runtime NOW, synchronously: the soft reload above just @@ -270,20 +364,34 @@ export async function tryGsapResizeIntercept( const currentAnimations = fetchFallbackAnimations ? await fetchFallbackAnimations() : (resolved?.animations ?? animations); - const existingSet = findExistingPositionWrite(currentAnimations, selector, selection.element); // Delta chosen so the drag-path math composes back to exactly `corrected` - // (no drag scratch attrs exist during a resize, so base = gsapPos). - await commitStaticGsapPosition( - selection, - { x: corrected.x - gsapPos.x, y: corrected.y - gsapPos.y }, - gsapPos, - selector, - existingSet, - { + // — it adds this onto the same base the measurement above used. + const delta = { x: corrected.x - base.x, y: corrected.y - base.y }; + // An element whose position is animated needs the correction written into + // that animation, at the playhead, or the tween renders its own value a + // frame later and the element leaves the drop point anyway. This used to + // stand down here instead, on the grounds that a keyframed path has no + // single anchor to preserve. It has one: the frame the user is looking at. + // Writing it is the same thing a drag on the same element does, through + // the same commit. + const positionTween = pickClosestToPlayhead( + currentAnimations.filter( + (a) => a.propertyGroup === "position" && !isInstantHold(a) && resolveTweenDuration(a) > 0, + ), + ); + if (positionTween) { + logResize("scale-finalize", { route: "position-keyframe", tweenId: positionTween.id }); + await commitGsapPositionFromDrag(selection, positionTween, delta, base, iframe, selector, { commitMutation, fetchAnimations: fetchFallbackAnimations, - }, - ); + }); + return; + } + const existingSet = findExistingPositionWrite(currentAnimations, selector, selection.element); + await commitStaticGsapPosition(selection, delta, base, selector, existingSet, { + commitMutation, + fetchAnimations: fetchFallbackAnimations, + }); }; // With auto-keyframe off (#1808), `anim` is already a real (non-"set") @@ -341,7 +449,7 @@ export async function tryGsapResizeIntercept( // normalizes every keyframe to the longhands. For an in-range resize the // min/max window math below degenerates to the tween's own start/duration, // so timing is unchanged. - if ((outsideRange || nonUniformScale) && ts !== null) { + if ((outsideRange || useScaleLonghands) && ts !== null) { // For flat tweens, synthesize the keyframes from the tween's properties const kfs = anim.keyframes?.keyframes ?? diff --git a/packages/studio/src/utils/elementGsap.ts b/packages/studio/src/utils/elementGsap.ts index e89b6f9864..c4d1df5890 100644 --- a/packages/studio/src/utils/elementGsap.ts +++ b/packages/studio/src/utils/elementGsap.ts @@ -24,6 +24,20 @@ export function setElementGsapPosition(element: HTMLElement, x: number, y: numbe return true; } +/** + * Set the element's GSAP scale. Returns false when no runtime is reachable. + * + * Used to make the element show a scale that has been committed but not yet + * re-rendered by the timeline, so measuring it afterwards reports where the + * commit actually puts it rather than where it happened to be mid-flight. + */ +export function setElementGsapScale(element: HTMLElement, x: number, y: number): boolean { + const gsap = gsapOf(element); + if (!gsap?.set) return false; + gsap.set(element, { scaleX: x, scaleY: y }); + return true; +} + /** The element's GSAP numeric property, or null when unreadable. */ export function readElementGsapNumber(element: HTMLElement, prop: string): number | null { const value = Number(gsapOf(element)?.getProperty?.(element, prop));