Skip to content

Commit 12e637f

Browse files
committed
fix(studio): cover legacy resize boxes
1 parent a693b12 commit 12e637f

4 files changed

Lines changed: 44 additions & 17 deletions

File tree

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,15 +360,21 @@ function writeStudioBoxSizeVars(
360360
element: HTMLElement,
361361
size: { width: number; height: number },
362362
): void {
363+
// Keep the measurement on its own migration-safe guard. Elements drafted by
364+
// an older Studio can already carry the box-size marker without these newer
365+
// attributes; the next resize still reaches this function before its width
366+
// and height are overwritten, so this is the last honest layout box to save.
367+
// Offset sizes are layout values, so a running scale animation does not
368+
// distort them.
369+
if (!element.hasAttribute(STUDIO_ORIGINAL_BOX_WIDTH_ATTR)) {
370+
element.setAttribute(STUDIO_ORIGINAL_BOX_WIDTH_ATTR, String(element.offsetWidth));
371+
}
372+
if (!element.hasAttribute(STUDIO_ORIGINAL_BOX_HEIGHT_ATTR)) {
373+
element.setAttribute(STUDIO_ORIGINAL_BOX_HEIGHT_ATTR, String(element.offsetHeight));
374+
}
363375
if (!element.hasAttribute(STUDIO_BOX_SIZE_ATTR)) {
364376
element.setAttribute(STUDIO_ORIGINAL_WIDTH_ATTR, element.style.getPropertyValue("width"));
365377
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));
372378
element.setAttribute(
373379
STUDIO_ORIGINAL_MIN_WIDTH_ATTR,
374380
element.style.getPropertyValue("min-width"),

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,20 @@ describe("buildBoxSizePatches / buildClearBoxSizePatches", () => {
267267
expect(ops.every((op) => op.value === null)).toBe(true);
268268
});
269269

270+
it("backfills the measured box on a legacy element that already has the resize marker", () => {
271+
const e = div();
272+
e.setAttribute(STUDIO_BOX_SIZE_ATTR, "true");
273+
Object.defineProperties(e, {
274+
offsetWidth: { configurable: true, value: 630 },
275+
offsetHeight: { configurable: true, value: 252 },
276+
});
277+
278+
applyStudioBoxSize(e, { width: 320, height: 128 });
279+
280+
expect(e.getAttribute(STUDIO_ORIGINAL_BOX_WIDTH_ATTR)).toBe("630");
281+
expect(e.getAttribute(STUDIO_ORIGINAL_BOX_HEIGHT_ATTR)).toBe("252");
282+
});
283+
270284
it("build/clear symmetry: clear addresses every {type,property} key that build emits", () => {
271285
const e = populatedBoxEl();
272286
assertClearCoversKeys(buildBoxSizePatches(e), buildClearBoxSizePatches(e));

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@ interface ResizeCase {
7777
positionWrite?: "static-set" | "keyframed-tween" | "none";
7878
}
7979

80+
function createResizeElement(testCase: ResizeCase): HTMLElement {
81+
const el = document.createElement("div");
82+
el.id = "clip";
83+
if (testCase.inlineSized) {
84+
el.setAttribute("data-hf-studio-original-width", `${testCase.box.w}px`);
85+
el.setAttribute("data-hf-studio-original-height", `${testCase.box.h}px`);
86+
} else {
87+
el.setAttribute("data-hf-studio-original-box-width", `${testCase.box.w}`);
88+
el.setAttribute("data-hf-studio-original-box-height", `${testCase.box.h}`);
89+
el.setAttribute("data-hf-studio-original-width", "");
90+
el.setAttribute("data-hf-studio-original-height", "");
91+
}
92+
return el;
93+
}
94+
8095
function scaleTween(longhand: boolean): GsapAnimation {
8196
const at = (v: number) => (longhand ? { scaleX: v, scaleY: v } : { scale: v });
8297
return {
@@ -180,17 +195,7 @@ function persistedScale(calls: unknown[][]): { x: number; y: number } | null {
180195

181196
async function runCase(testCase: ResizeCase) {
182197
const rotation = testCase.rotation ?? 0;
183-
const el = document.createElement("div");
184-
el.id = "clip";
185-
if (testCase.inlineSized) {
186-
el.setAttribute("data-hf-studio-original-width", `${testCase.box.w}px`);
187-
el.setAttribute("data-hf-studio-original-height", `${testCase.box.h}px`);
188-
} else {
189-
el.setAttribute("data-hf-studio-original-box-width", `${testCase.box.w}`);
190-
el.setAttribute("data-hf-studio-original-box-height", `${testCase.box.h}`);
191-
el.setAttribute("data-hf-studio-original-width", "");
192-
el.setAttribute("data-hf-studio-original-height", "");
193-
}
198+
const el = createResizeElement(testCase);
194199
// What the gesture stamps at drag start, and the draft it leaves applied.
195200
el.setAttribute("data-hf-drag-gsap-base-x", `${testCase.base.x}`);
196201
el.setAttribute("data-hf-drag-gsap-base-y", `${testCase.base.y}`);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ it("non-uniform drag commits scaleX/scaleY longhands", async () => {
293293
* left it rendering at 7532px, over three times where it was dropped, and the
294294
* next drag compounded it.
295295
*/
296+
// fallow-ignore-next-line code-duplication
296297
it("scales from the element's real box, not a hardcoded fallback", async () => {
297298
const el = document.createElement("div");
298299
el.id = "clip";
@@ -339,6 +340,7 @@ it("scales from the element's real box, not a hardcoded fallback", async () => {
339340
* and the longhands won. The resize computed the right number, wrote it, and
340341
* the element snapped straight back to its old size on release.
341342
*/
343+
// fallow-ignore-next-line code-duplication
342344
it("does not mix the scale shorthand into a tween that speaks longhands", async () => {
343345
const el = document.createElement("div");
344346
el.id = "clip";

0 commit comments

Comments
 (0)