From 9b7be276a90d997954ad4b70f5aeae8322aea7d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Fri, 20 Nov 2026 14:02:07 +0000 Subject: [PATCH] fix(studio): a sealed resize of a layer whose GSAP keyframes animate its width saves (#8288) * fix(studio): a sealed resize of a keyframed size keeps the box's other side * fix(studio): say where a sealed resize's pre-gesture box comes from * refactor(studio): read a resize's pre-gesture box one side at a time --- .../src/hooks/gsapResizeIntercept.test.ts | 55 +++++++++++++++++++ .../studio/src/hooks/gsapResizeIntercept.ts | 36 +++++++----- 2 files changed, 78 insertions(+), 13 deletions(-) diff --git a/packages/studio/src/hooks/gsapResizeIntercept.test.ts b/packages/studio/src/hooks/gsapResizeIntercept.test.ts index 3005881102..69928af916 100644 --- a/packages/studio/src/hooks/gsapResizeIntercept.test.ts +++ b/packages/studio/src/hooks/gsapResizeIntercept.test.ts @@ -9,8 +9,16 @@ import type { CommitMutation } from "./gsapScriptCommitTypes"; import { computeCurrentPercentage } from "./gsapDragCommit"; import { commitSizeAtPlayhead, tryGsapResizeIntercept } from "./gsapResizeIntercept"; import { liveTween, previewWith, tween } from "./gsapParsedTween.test-helpers"; +import { measurePreviewGestureStart } from "../components/editor/previewGestureStartRead"; vi.mock("../utils/studioTelemetry", () => ({ trackStudioEvent: vi.fn() })); +vi.mock("../player/lib/previewElements", async (original) => ({ + ...(await original()), + previewGestureStart: async () => ({ + elements: [{ layout: { width: 260, height: 160 }, move: null, gsap: null }], + snap: [], + }), +})); afterEach(() => { vi.clearAllMocks(); @@ -744,3 +752,50 @@ it.each([ el.remove(); document.querySelector("#__hf_color_grading_clip")?.remove(); }); + +it.each([ + ["the draft marked the element", false], + ["the element is a sealed frame's stand-in, measured at the press", true], +])("a size written at the playhead backfills the box's other side when %s", async (_, sealed) => { + const selection = titleSelection(); + const el = selection.element; + if (sealed) { + selection.handle = "0123456789abcdef:1"; + await measurePreviewGestureStart( + document.createElement("iframe"), + document.createElement("div"), + [selection.handle], + ); + } else { + el.setAttribute("data-hf-studio-original-width", "260"); + el.setAttribute("data-hf-studio-original-height", "160"); + } + const slide = tween({ + id: "#title-to-0", + targetSelector: "#title", + propertyGroup: undefined, + method: "to", + properties: { width: 320 }, + resolvedStart: 0, + duration: 2, + ease: "none", + }); + usePlayerStore.setState({ currentTime: 1 }); + const commitMutation = vi.fn(); + const live = liveTween(el, { start: 0, duration: 2, vars: slide.properties }); + + const outcome = await commitSizeAtPlayhead( + selection, + slide, + { width: 300, height: 200 }, + previewWith(el, [live]), + undefined, + { commitMutation }, + ); + + expect(outcome.status).toBe("persisted"); + const keyframes = commitMutation.mock.calls[0]![1].keyframes; + expect( + keyframes.map((kf: { properties: Record }) => kf.properties.height), + ).toEqual(keyframes.map((kf: { percentage: number }) => (kf.percentage === 50 ? 200 : 160))); +}); diff --git a/packages/studio/src/hooks/gsapResizeIntercept.ts b/packages/studio/src/hooks/gsapResizeIntercept.ts index a6c73e86db..be2f08be71 100644 --- a/packages/studio/src/hooks/gsapResizeIntercept.ts +++ b/packages/studio/src/hooks/gsapResizeIntercept.ts @@ -75,11 +75,24 @@ function originalBoxSize( return Number.isFinite(inline) && inline > 0 ? inline : null; } -/** The box's width and height before the draft, for a size tween that animates only one. */ -function preGestureBoxSize(el: HTMLElement): Record { - const width = originalBoxSize(el, STUDIO_ORIGINAL_BOX_WIDTH_ATTR, "width"); - const height = originalBoxSize(el, STUDIO_ORIGINAL_BOX_HEIGHT_ATTR, "height"); - return { ...(width != null && { width }), ...(height != null && { height }) }; +const BOX_SIDES = [ + ["width", STUDIO_ORIGINAL_BOX_WIDTH_ATTR], + ["height", STUDIO_ORIGINAL_BOX_HEIGHT_ATTR], +] as const; + +/** The box's width and height before the draft. A sealed frame's draft marks the frame's element, + * not the stand-in, so there it is the box the press measured. */ +function preGestureBoxSize( + selection: DomEditSelection, + el: HTMLElement | null = selection.element, +): Record { + const started = selection.handle ? gestureStartLayout(selection.handle) : null; + const box: Record = {}; + for (const [side, attr] of BOX_SIDES) { + const value = originalBoxSize(el, attr, side) ?? (started?.[side] || null); + if (value != null) box[side] = value; + } + return box; } /** A size write at the playhead. When the same tween animates position, the resize's anchor move @@ -112,7 +125,7 @@ export async function commitSizeAtPlayhead( { label: "Resize", backfill: { - ...preGestureBoxSize(selection.element), + ...preGestureBoxSize(selection), ...(anchor && { x: anchor.baseGsapX, y: anchor.baseGsapY }), }, ...(anchor && { beforeReload: () => restoreDragOffset(selection.element, callbacks.stamp) }), @@ -322,12 +335,9 @@ export async function tryGsapResizeIntercept( // 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. - // A sealed preview has no element here: its gesture started from the box the frame measured. - const started = selection.handle ? gestureStartLayout(selection.handle) : null; - const cssW = - originalBoxSize(el, STUDIO_ORIGINAL_BOX_WIDTH_ATTR, "width") ?? (started?.width || 200); - const cssH = - originalBoxSize(el, STUDIO_ORIGINAL_BOX_HEIGHT_ATTR, "height") ?? (started?.height || cssW); + const box = preGestureBoxSize(selection, el); + const cssW = box.width ?? 200; + const cssH = box.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 @@ -391,7 +401,7 @@ export async function tryGsapResizeIntercept( width: roundToLayoutPx(size.width), height: roundToLayoutPx(size.height), }; - resizeBackfill = preGestureBoxSize(selection.element); + resizeBackfill = preGestureBoxSize(selection); } // Finalize a scale-route commit: tear down the gesture's inline width/height // draft (leaving it applied compounds with the committed scale — the element