Skip to content

Commit 477f776

Browse files
committed
fix(studio): resize from the element's real box, not a 200px guess
Resizing an element whose size is driven by a scale animation committed a scale computed against a hardcoded 200px fallback, because the only original size the draft recorded was the element's INLINE width, and a composition sizes its elements from the stylesheet. A 630px chip dropped at 1260px wide committed a scale of 6.3 instead of 2, so it landed at over three times the size it was dropped at. The next drag compounded it, because that wrong scale then counted as the element's live one. The draft now records the box it measured, once, before it writes a width of its own, and the intercept reads that. The inline attributes keep their own job of restoring an inline style, which is why they cannot answer this question.
1 parent 5a7f465 commit 477f776

7 files changed

Lines changed: 120 additions & 6 deletions

File tree

‎packages/studio/src/components/editor/manualEditsDom.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import {
1212
STUDIO_ORIGINAL_INLINE_TRANSLATE_ATTR,
1313
STUDIO_ORIGINAL_WIDTH_ATTR,
1414
STUDIO_ORIGINAL_HEIGHT_ATTR,
15+
STUDIO_ORIGINAL_BOX_WIDTH_ATTR,
16+
STUDIO_ORIGINAL_BOX_HEIGHT_ATTR,
1517
STUDIO_ORIGINAL_MIN_WIDTH_ATTR,
1618
STUDIO_ORIGINAL_MIN_HEIGHT_ATTR,
1719
STUDIO_ORIGINAL_MAX_WIDTH_ATTR,
@@ -361,6 +363,12 @@ function writeStudioBoxSizeVars(
361363
if (!element.hasAttribute(STUDIO_BOX_SIZE_ATTR)) {
362364
element.setAttribute(STUDIO_ORIGINAL_WIDTH_ATTR, element.style.getPropertyValue("width"));
363365
element.setAttribute(STUDIO_ORIGINAL_HEIGHT_ATTR, element.style.getPropertyValue("height"));
366+
// Measured here and only here: this branch runs once, before the draft
367+
// writes a width, so it is the last moment the element still has the box
368+
// the user started with. Offset sizes are layout, so a scale animation
369+
// running on the element does not distort them.
370+
element.setAttribute(STUDIO_ORIGINAL_BOX_WIDTH_ATTR, String(element.offsetWidth));
371+
element.setAttribute(STUDIO_ORIGINAL_BOX_HEIGHT_ATTR, String(element.offsetHeight));
364372
element.setAttribute(
365373
STUDIO_ORIGINAL_MIN_WIDTH_ATTR,
366374
element.style.getPropertyValue("min-width"),

‎packages/studio/src/components/editor/manualEditsDomPatches.test.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import {
1717
STUDIO_ORIGINAL_INLINE_TRANSLATE_ATTR,
1818
STUDIO_ORIGINAL_WIDTH_ATTR,
1919
STUDIO_ORIGINAL_HEIGHT_ATTR,
20+
STUDIO_ORIGINAL_BOX_WIDTH_ATTR,
21+
STUDIO_ORIGINAL_BOX_HEIGHT_ATTR,
2022
STUDIO_ORIGINAL_MIN_WIDTH_ATTR,
2123
STUDIO_ORIGINAL_MIN_HEIGHT_ATTR,
2224
STUDIO_ORIGINAL_MAX_WIDTH_ATTR,
@@ -222,6 +224,9 @@ describe("buildBoxSizePatches / buildClearBoxSizePatches", () => {
222224
{ type: "attribute", property: STUDIO_ORIGINAL_WIDTH_ATTR, value: null },
223225
{ type: "inline-style", property: "height", value: "150px" },
224226
{ type: "attribute", property: STUDIO_ORIGINAL_HEIGHT_ATTR, value: null },
227+
// Measurements, so they are cleared without restoring a style.
228+
{ type: "attribute", property: STUDIO_ORIGINAL_BOX_WIDTH_ATTR, value: null },
229+
{ type: "attribute", property: STUDIO_ORIGINAL_BOX_HEIGHT_ATTR, value: null },
225230
{ type: "inline-style", property: "min-width", value: "0px" },
226231
{ type: "attribute", property: STUDIO_ORIGINAL_MIN_WIDTH_ATTR, value: null },
227232
{ type: "inline-style", property: "min-height", value: "0px" },
@@ -257,8 +262,8 @@ describe("buildBoxSizePatches / buildClearBoxSizePatches", () => {
257262

258263
it("clear: bare element emits only null ops — no style restores fire when orig attrs are absent", () => {
259264
const ops = buildClearBoxSizePatches(div());
260-
// 3 fixed (studio-width, studio-height, box-size marker) + 14 attr-null pushes (one per BOX_SIZE_ORIG_ATTR)
261-
expect(ops).toHaveLength(17);
265+
// 3 fixed (studio-width, studio-height, box-size marker) + 16 attr-null pushes (one per BOX_SIZE_ORIG_ATTR)
266+
expect(ops).toHaveLength(19);
262267
expect(ops.every((op) => op.value === null)).toBe(true);
263268
});
264269

‎packages/studio/src/components/editor/manualEditsDomPatches.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
STUDIO_ORIGINAL_INLINE_TRANSLATE_ATTR,
1414
STUDIO_ORIGINAL_WIDTH_ATTR,
1515
STUDIO_ORIGINAL_HEIGHT_ATTR,
16+
STUDIO_ORIGINAL_BOX_WIDTH_ATTR,
17+
STUDIO_ORIGINAL_BOX_HEIGHT_ATTR,
1618
STUDIO_ORIGINAL_MIN_WIDTH_ATTR,
1719
STUDIO_ORIGINAL_MIN_HEIGHT_ATTR,
1820
STUDIO_ORIGINAL_MAX_WIDTH_ATTR,
@@ -135,6 +137,9 @@ const BOX_SIZE_STYLE_PROPS = [
135137
const BOX_SIZE_ORIG_ATTRS: ReadonlyArray<[string, string]> = [
136138
[STUDIO_ORIGINAL_WIDTH_ATTR, "width"],
137139
[STUDIO_ORIGINAL_HEIGHT_ATTR, "height"],
140+
// Records a measurement rather than a style, so it restores nothing.
141+
[STUDIO_ORIGINAL_BOX_WIDTH_ATTR, ""],
142+
[STUDIO_ORIGINAL_BOX_HEIGHT_ATTR, ""],
138143
[STUDIO_ORIGINAL_MIN_WIDTH_ATTR, "min-width"],
139144
[STUDIO_ORIGINAL_MIN_HEIGHT_ATTR, "min-height"],
140145
[STUDIO_ORIGINAL_MAX_WIDTH_ATTR, "max-width"],

‎packages/studio/src/components/editor/manualEditsSnapshot.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import {
1717
STUDIO_ORIGINAL_INLINE_TRANSLATE_ATTR,
1818
STUDIO_ORIGINAL_WIDTH_ATTR,
1919
STUDIO_ORIGINAL_HEIGHT_ATTR,
20+
STUDIO_ORIGINAL_BOX_WIDTH_ATTR,
21+
STUDIO_ORIGINAL_BOX_HEIGHT_ATTR,
2022
STUDIO_ORIGINAL_MIN_WIDTH_ATTR,
2123
STUDIO_ORIGINAL_MIN_HEIGHT_ATTR,
2224
STUDIO_ORIGINAL_MAX_WIDTH_ATTR,
@@ -60,6 +62,8 @@ export function captureStudioBoxSize(element: HTMLElement): StudioBoxSizeSnapsho
6062
marker: element.getAttribute(STUDIO_BOX_SIZE_ATTR),
6163
originalWidth: element.getAttribute(STUDIO_ORIGINAL_WIDTH_ATTR),
6264
originalHeight: element.getAttribute(STUDIO_ORIGINAL_HEIGHT_ATTR),
65+
originalBoxWidth: element.getAttribute(STUDIO_ORIGINAL_BOX_WIDTH_ATTR),
66+
originalBoxHeight: element.getAttribute(STUDIO_ORIGINAL_BOX_HEIGHT_ATTR),
6367
originalMinWidth: element.getAttribute(STUDIO_ORIGINAL_MIN_WIDTH_ATTR),
6468
originalMinHeight: element.getAttribute(STUDIO_ORIGINAL_MIN_HEIGHT_ATTR),
6569
originalMaxWidth: element.getAttribute(STUDIO_ORIGINAL_MAX_WIDTH_ATTR),
@@ -128,6 +132,8 @@ export function restoreStudioBoxSize(element: HTMLElement, previous: StudioBoxSi
128132
restoreAttribute(element, STUDIO_BOX_SIZE_ATTR, previous.marker);
129133
restoreAttribute(element, STUDIO_ORIGINAL_WIDTH_ATTR, previous.originalWidth);
130134
restoreAttribute(element, STUDIO_ORIGINAL_HEIGHT_ATTR, previous.originalHeight);
135+
restoreAttribute(element, STUDIO_ORIGINAL_BOX_WIDTH_ATTR, previous.originalBoxWidth);
136+
restoreAttribute(element, STUDIO_ORIGINAL_BOX_HEIGHT_ATTR, previous.originalBoxHeight);
131137
restoreAttribute(element, STUDIO_ORIGINAL_MIN_WIDTH_ATTR, previous.originalMinWidth);
132138
restoreAttribute(element, STUDIO_ORIGINAL_MIN_HEIGHT_ATTR, previous.originalMinHeight);
133139
restoreAttribute(element, STUDIO_ORIGINAL_MAX_WIDTH_ATTR, previous.originalMaxWidth);

‎packages/studio/src/components/editor/manualEditsTypes.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ export const STUDIO_ORIGINAL_TRANSLATE_ATTR = "data-hf-studio-original-translate
1616
export const STUDIO_ORIGINAL_INLINE_TRANSLATE_ATTR = "data-hf-studio-original-inline-translate";
1717
export const STUDIO_ORIGINAL_WIDTH_ATTR = "data-hf-studio-original-width";
1818
export const STUDIO_ORIGINAL_HEIGHT_ATTR = "data-hf-studio-original-height";
19+
/**
20+
* The element's laid-out box before a resize draft touched it, in CSS pixels.
21+
*
22+
* The two attributes above record the element's INLINE width and height so a
23+
* reset can put them back, and are empty for the usual case of an element sized
24+
* by the stylesheet. That made them useless as a measurement, and the resize
25+
* intercept, which needs the original box to work out a scale, fell back to a
26+
* hardcoded guess and produced a wildly wrong one. These record the measurement
27+
* instead, and restore nothing.
28+
*/
29+
export const STUDIO_ORIGINAL_BOX_WIDTH_ATTR = "data-hf-studio-original-box-width";
30+
export const STUDIO_ORIGINAL_BOX_HEIGHT_ATTR = "data-hf-studio-original-box-height";
1931
export const STUDIO_ORIGINAL_MIN_WIDTH_ATTR = "data-hf-studio-original-min-width";
2032
export const STUDIO_ORIGINAL_MIN_HEIGHT_ATTR = "data-hf-studio-original-min-height";
2133
export const STUDIO_ORIGINAL_MAX_WIDTH_ATTR = "data-hf-studio-original-max-width";
@@ -70,6 +82,8 @@ export interface StudioBoxSizeSnapshot {
7082
marker: string | null;
7183
originalWidth: string | null;
7284
originalHeight: string | null;
85+
originalBoxWidth: string | null;
86+
originalBoxHeight: string | null;
7387
originalMinWidth: string | null;
7488
originalMinHeight: string | null;
7589
originalMaxWidth: string | null;

‎packages/studio/src/hooks/gsapResizeIntercept.test.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,50 @@ it("non-uniform drag commits scaleX/scaleY longhands", async () => {
284284
expect(serialized).toContain("scaleX");
285285
expect(serialized).toContain("scaleY");
286286
});
287+
288+
/**
289+
* The bug: the original box size was read only from the element's INLINE
290+
* width, and a composition sizes its elements from the stylesheet. With no
291+
* inline width the code fell back to a hardcoded 200, so the committed scale
292+
* came out `real / 200` times too large. Dropping a 630px chip at 2391px wide
293+
* left it rendering at 7532px, over three times where it was dropped, and the
294+
* next drag compounded it.
295+
*/
296+
it("scales from the element's real box, not a hardcoded fallback", async () => {
297+
const el = document.createElement("div");
298+
el.id = "clip";
299+
// Sized by a stylesheet, so it carries no inline width, and the draft
300+
// recorded the box it measured instead.
301+
el.setAttribute("data-hf-studio-original-box-width", "630");
302+
el.setAttribute("data-hf-studio-original-box-height", "252");
303+
document.body.append(el);
304+
const selection = { id: "clip", selector: "#clip", element: el } as DomEditSelection;
305+
const commitMutation = vi.fn();
306+
307+
await tryGsapResizeIntercept(
308+
selection,
309+
{ width: 1260, height: 504 },
310+
[keyframedScaleFixture()],
311+
fakeIframe(el, { scaleX: 1, scaleY: 1 }),
312+
commitMutation,
313+
);
314+
315+
type Mutation = {
316+
properties?: Record<string, number>;
317+
keyframes?: Array<{ percentage: number; properties: Record<string, number> }>;
318+
};
319+
const committed = commitMutation.mock.calls
320+
.map((call) => call[1] as Mutation)
321+
.flatMap((mutation) => [
322+
mutation.properties,
323+
...(mutation.keyframes ?? []).map((frame) => frame.properties),
324+
])
325+
.filter((properties): properties is Record<string, number> => properties != null)
326+
.find((properties) => properties.scale != null || properties.scaleX != null);
327+
328+
// Dropped at twice the element's own size, so the scale is about 2. The
329+
// number that matters is that it is not the 6.3 which 1260/200 produced.
330+
const scale = committed?.scale ?? committed?.scaleX ?? 0;
331+
expect(scale).toBeCloseTo(2, 1);
332+
expect(scale).toBeLessThan(3);
333+
});

‎packages/studio/src/hooks/gsapResizeIntercept.ts‎

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
import type { GsapAnimation, PropertyGroupName } from "@hyperframes/core/gsap-parser";
99
import type { DomEditSelection } from "../components/editor/domEditingTypes";
1010
import { clearStudioBoxSize } from "../components/editor/manualEdits";
11+
import {
12+
STUDIO_ORIGINAL_BOX_HEIGHT_ATTR,
13+
STUDIO_ORIGINAL_BOX_WIDTH_ATTR,
14+
} from "../components/editor/manualEditsTypes";
1115
import { setElementGsapPosition } from "../utils/elementGsap";
1216
import { usePlayerStore } from "../player/store/playerStore";
1317
import { readAllAnimatedProperties, readGsapProperty } from "./gsapRuntimeReaders";
@@ -49,6 +53,26 @@ function synthesizeIdentityProps(
4953
return id;
5054
}
5155

56+
/**
57+
* The element's box before the resize draft ran, in CSS pixels.
58+
*
59+
* Prefers the measurement the draft recorded. Falls back to the inline style it
60+
* saved for restoring, which is a real value for the elements that carry one,
61+
* and null when neither says anything.
62+
*/
63+
function originalBoxSize(
64+
el: HTMLElement | null,
65+
measuredAttr: string,
66+
inlineProperty: "width" | "height",
67+
): number | null {
68+
const measured = Number.parseFloat(el?.getAttribute(measuredAttr) ?? "");
69+
if (Number.isFinite(measured) && measured > 0) return measured;
70+
const inline = Number.parseFloat(
71+
el?.getAttribute(`data-hf-studio-original-${inlineProperty}`) ?? "",
72+
);
73+
return Number.isFinite(inline) && inline > 0 ? inline : null;
74+
}
75+
5276
// ── Resize intercept ──────────────────────────────────────────────────────
5377

5478
// fallow-ignore-next-line complexity
@@ -162,10 +186,15 @@ export async function tryGsapResizeIntercept(
162186
const el = iframe?.contentDocument?.querySelector(selector ?? "") as HTMLElement | null;
163187
// The resize draft modifies el.style.width/height, so read the ORIGINAL
164188
// dimensions saved by the draft system before it ran.
165-
const origW = Number.parseFloat(el?.getAttribute("data-hf-studio-original-width") ?? "");
166-
const origH = Number.parseFloat(el?.getAttribute("data-hf-studio-original-height") ?? "");
167-
const cssW = Number.isFinite(origW) && origW > 0 ? origW : 200;
168-
const cssH = Number.isFinite(origH) && origH > 0 ? origH : cssW;
189+
//
190+
// The measured box first, then the inline one. The inline attributes exist
191+
// to restore an inline style and are empty for anything sized by a
192+
// stylesheet, which is how compositions are written, so reading them alone
193+
// sent almost every element to the fallback below: a 630px chip scaled by
194+
// 630/200, landing over three times the size it was dropped at, and worse
195+
// on the next drag because the wrong scale then counted as its live one.
196+
const cssW = originalBoxSize(el, STUDIO_ORIGINAL_BOX_WIDTH_ATTR, "width") ?? 200;
197+
const cssH = originalBoxSize(el, STUDIO_ORIGINAL_BOX_HEIGHT_ATTR, "height") ?? cssW;
169198
// `size` is the draft's CSS box; on screen it is multiplied by the element's
170199
// LIVE scale (the draft divides the cursor delta by it — see
171200
// resolveDomEditResizeGesture). The committed keyframe REPLACES that live

0 commit comments

Comments
 (0)