Skip to content

Commit 3e5be0e

Browse files
fix(studio): read the rotate property when measuring an element's angle (#3163)
* fix(studio): read the rotate property when measuring an element's angle Turning an element with Studio's rotate handle left every piece of overlay chrome square across it: the selection box, the crop outline and the child outlines all drew upright while the element underneath was clearly rotated. The handle writes the CSS `rotate` property. `rotate` is an individual transform property, not part of `transform`, so `getComputedStyle(el).transform` reports nothing for it and both places that measure an element's angle — the overlay geometry and the crop frame — read the element as upright. Both now read `rotate` alongside `transform` and compose them the way CSS does, individual properties first. A rotation about any axis but z has no single in-plane angle, so it reports nothing and the caller keeps its axis-aligned fallback rather than drawing chrome at a plausible wrong angle. * fix(studio): stop the crop outline refusing the transforms GSAP writes The crop outline still drew square on a rotated element after the rotate- property fix, because it refused the transform outright: it accepted only `matrix(...)`, and GSAP writes `matrix3d(...)` for an ordinary 2D move or spin (force3D). A composition that mirrors an element writes one with a negative z scale, and the negative determinant that follows was refused too. Both are ordinary planar transforms. The outline now reads the same 2D projection the rest of the chrome takes through DOMMatrix, and sizes a flipped element from the magnitude of its determinant. Only a perspective term still falls back, because that is where the mapping stops being affine and no single angle describes it. The test that asserted "a 3D matrix means give up" asserted the bug: its fixture was the identity written as matrix3d, which is as planar as a transform gets. It now checks the behaviour that replaced it, alongside the perspective case, which still falls back. * fix(studio): draw the crop outline at the angle the element paints under Selecting a text layer inside a rotated card drew its crop outline across the text at roughly a right angle. The outline read the element's own transform, but what the user sees is that composed with every ancestor's — the layer carries its own spin and its parent turns it again. It now walks to the composition root and composes each level, the element's `rotate` property before its `transform` and an ancestor outside its child, which is the order CSS applies them in. Nothing transformed anywhere still falls back to the caller's axis-aligned rect, since that comes from real layout and describes the element exactly. The chrome test stubbed getComputedStyle to answer "rotated 30deg" for every node in the document, so composing read the same turn once per ancestor. The stub now answers per element, which is what it always meant. * fix(studio): stop the dev server reloading the page on every canvas edit A composition lives under this package's root, so Vite's HMR saw a write to one as an html page dependency changing and full-reloaded the browser. That reload is the flash after every edit in the canvas: the whole app remounts, taking the preview iframe with it. The decision was never Vite's to make. Studio already knows whether a write was its own — that is what the write receipt is for — and refreshes the preview itself when it needs to. Vite's watcher now ignores the project data, and the dev plugin watches it on a watcher of its own, announcing changes as hf:file-change exactly as before. Measured on a drag: Vite full reloads went from one per edit to none, and the receipt now reports 'suppressed: own write token' where it previously never saw a matching path. * refactor(studio): compose an element's transform in one walk, not two Review: the crop frame hand-composed ancestor matrices while the geometry file did the same walk through DOMMatrix. Both were right, but the next individual transform property CSS grows — `translate`, `scale` — would have to land in both, and a miss puts the crop outline back at the wrong angle while the selection box draws the right one. The walk now lives in one place and takes the arithmetic as a parameter. The geometry file keeps DOMMatrix, because it goes on to transform corner points and needs the translation; the crop frame keeps plain 2D components, because it only needs an angle and a scale. Which transforms count, and in what order, is stated once. Also from review: the nested case was verified by hand only, so the composed walk is now covered on both sides — a child inside a rotated parent reports the angle it paints at, the parent's rotation alone when the child has none, and the walk stopping at the composition root. And `hasAttribute?.` was dead on a narrowed HTMLElement; it only survived because the crop test's fake element was not one. The fake now models an element and the guard is gone. * style(studio): format the shared transform module
1 parent 945988d commit 3e5be0e

9 files changed

Lines changed: 435 additions & 92 deletions

‎bun.lock‎

Lines changed: 14 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/studio/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"@types/react-dom": "19",
8686
"@vitejs/plugin-react": "^4.0.0",
8787
"autoprefixer": "^10.4.0",
88+
"chokidar": "^4.0.3",
8889
"fake-indexeddb": "^6.2.5",
8990
"postcss": "^8.4.0",
9091
"puppeteer-core": "^25.2.1",

‎packages/studio/src/components/editor/DomEditSelectionChrome.test.tsx‎

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,29 @@ import { DomEditSelectionChrome } from "./DomEditSelectionChrome";
88

99
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
1010

11+
/** A selection whose capabilities are all on or all off, plus a host to render into. */
12+
function selectionFixture(
13+
element: HTMLElement,
14+
selector: string,
15+
enabled: boolean,
16+
extra: Record<string, unknown> = {},
17+
) {
18+
const selection = {
19+
element,
20+
selector,
21+
...extra,
22+
capabilities: {
23+
canCrop: enabled,
24+
canApplyManualOffset: enabled,
25+
canApplyManualSize: enabled,
26+
canApplyManualRotation: enabled,
27+
},
28+
} as unknown as DomEditSelection;
29+
const host = document.createElement("div");
30+
document.body.append(host);
31+
return { selection, host, root: createRoot(host) };
32+
}
33+
1134
describe("DomEditSelectionChrome crop composition", () => {
1235
it("renders overlay-only transparent chrome at headline geometry without changing composition bytes", () => {
1336
const composition = document.implementation.createHTMLDocument();
@@ -18,19 +41,7 @@ describe("DomEditSelectionChrome crop composition", () => {
1841
`;
1942
const headline = composition.querySelector<HTMLElement>(".hl-text")!;
2043
const before = composition.documentElement.outerHTML;
21-
const selection = {
22-
element: headline,
23-
selector: ".hl-text",
24-
capabilities: {
25-
canCrop: false,
26-
canApplyManualOffset: false,
27-
canApplyManualSize: false,
28-
canApplyManualRotation: false,
29-
},
30-
} as unknown as DomEditSelection;
31-
const host = document.createElement("div");
32-
document.body.append(host);
33-
const root = createRoot(host);
44+
const { selection, host, root } = selectionFixture(headline, ".hl-text", false);
3445
act(() => {
3546
root.render(
3647
<DomEditSelectionChrome
@@ -69,24 +80,16 @@ describe("DomEditSelectionChrome crop composition", () => {
6980
offsetHeight: { value: 100 },
7081
});
7182
document.body.append(element);
72-
vi.spyOn(window, "getComputedStyle").mockReturnValue({
73-
clipPath: "inset(10px)",
74-
transform: "matrix(0.8660254, 0.5, -0.5, 0.8660254, 0, 0)",
75-
} as CSSStyleDeclaration);
76-
const selection = {
77-
element,
78-
id: "clip",
79-
selector: "#clip",
80-
capabilities: {
81-
canCrop: true,
82-
canApplyManualOffset: true,
83-
canApplyManualSize: true,
84-
canApplyManualRotation: true,
85-
},
86-
} as unknown as DomEditSelection;
87-
const host = document.createElement("div");
88-
document.body.append(host);
89-
const root = createRoot(host);
83+
// Per element, not blanket: the crop frame composes the element's transform
84+
// with its ancestors', so answering "rotated 30deg" for every node in the
85+
// document would have the frame read the same turn several times over.
86+
vi.spyOn(window, "getComputedStyle").mockImplementation(
87+
((node: Element) =>
88+
(node === element
89+
? { clipPath: "inset(10px)", transform: "matrix(0.8660254, 0.5, -0.5, 0.8660254, 0, 0)" }
90+
: { clipPath: "none", transform: "none" }) as CSSStyleDeclaration) as never,
91+
);
92+
const { selection, host, root } = selectionFixture(element, "#clip", true, { id: "clip" });
9093
act(() => {
9194
root.render(
9295
<DomEditSelectionChrome

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

Lines changed: 136 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
resolveCropInsetFromMoveDrag,
99
rotateDeltaIntoFrame,
1010
} from "./domEditOverlayCrop";
11+
import { individualRotateDegrees } from "./domEditOverlayTransform";
1112

1213
describe("resolveCropInsetFromEdgeDrag", () => {
1314
const startInsets = { top: 10, right: 20, bottom: 30, left: 40 };
@@ -160,10 +161,15 @@ describe("readElementCropInsets tri-state", () => {
160161
describe("readElementCropFrame", () => {
161162
const overlayRect = { left: 100, top: 50, width: 220, height: 130, editScaleX: 1, editScaleY: 1 };
162163

164+
// Models an element well enough for the ancestor walk: it reports its own
165+
// transform, claims no composition-root attribute, and has no parent, so the
166+
// walk composes exactly one node.
163167
const fakeEl = (transform: string, offsetWidth = 200, offsetHeight = 100) =>
164168
({
165169
offsetWidth,
166170
offsetHeight,
171+
parentElement: null,
172+
hasAttribute: () => false,
167173
ownerDocument: { defaultView: { getComputedStyle: () => ({ transform }) } },
168174
}) as unknown as HTMLElement;
169175

@@ -205,9 +211,43 @@ describe("readElementCropFrame", () => {
205211
expect(frame.height).toBeCloseTo(200, 3);
206212
});
207213

208-
it("3D transform falls back to the axis-aligned frame", () => {
214+
/**
215+
* A 3D matrix is not automatically unmeasurable. GSAP writes one for an
216+
* ordinary 2D move or spin (force3D), so refusing every `matrix3d` drew the
217+
* crop outline square on elements the rest of the chrome drew rotated.
218+
* The identity here is a 2D transform written the long way.
219+
*/
220+
it("reads a planar matrix3d rather than giving up on it", () => {
221+
// scale(1.5, 2) written the long way — planar, and not the identity.
209222
const frame = readElementCropFrame(
210-
fakeEl("matrix3d(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1)"),
223+
fakeEl("matrix3d(1.5,0,0,0,0,2,0,0,0,0,1,0,0,0,0,1)"),
224+
overlayRect,
225+
);
226+
expect(frame.scaleX).toBeCloseTo(1.5, 3);
227+
expect(frame.scaleY).toBeCloseTo(2, 3);
228+
});
229+
230+
it("reads a 2D rotation written as matrix3d", () => {
231+
const frame = readElementCropFrame(
232+
fakeEl("matrix3d(0.866025,0.5,0,0,-0.5,0.866025,0,0,0,0,1,0,0,0,0,1)"),
233+
overlayRect,
234+
);
235+
expect(frame.angleDeg).toBeCloseTo(30, 3);
236+
});
237+
238+
it("reads a flipped element, which still has a real size", () => {
239+
// Negative z scale — a composition that mirrors an element writes this.
240+
const frame = readElementCropFrame(
241+
fakeEl("matrix3d(-0.866025,-0.5,0,0,-0.5,0.866025,0,0,0,0,-1,0,0,0,0,1)"),
242+
overlayRect,
243+
);
244+
expect(frame.width).toBeGreaterThan(0);
245+
expect(frame.height).toBeGreaterThan(0);
246+
});
247+
248+
it("still falls back on a perspective transform, which no single angle describes", () => {
249+
const frame = readElementCropFrame(
250+
fakeEl("matrix3d(1,0,0,0.002,0,1,0,0,0,0,1,0,0,0,0,1)"),
211251
overlayRect,
212252
);
213253
expect(frame).toEqual({
@@ -241,3 +281,97 @@ describe("rotateDeltaIntoFrame", () => {
241281
expect(back.deltaY).toBeCloseTo(-3, 6);
242282
});
243283
});
284+
285+
describe("individualRotateDegrees", () => {
286+
/**
287+
* Studio's rotate handle writes the CSS `rotate` property, not `transform`.
288+
* Everything that measured an element's angle read `transform` alone, so a
289+
* turned element reported upright and the selection box, crop outline and
290+
* child outlines all drew square across it.
291+
*/
292+
it("reads a plain angle", () => {
293+
expect(individualRotateDegrees("-22deg")).toBeCloseTo(-22, 6);
294+
expect(individualRotateDegrees("45deg")).toBeCloseTo(45, 6);
295+
});
296+
297+
it("reads an explicit z-axis rotation, honouring the axis sign", () => {
298+
expect(individualRotateDegrees("0 0 1 30deg")).toBeCloseTo(30, 6);
299+
expect(individualRotateDegrees("0 0 -1 30deg")).toBeCloseTo(-30, 6);
300+
});
301+
302+
it("reports nothing for a rotation that leaves the overlay's plane", () => {
303+
// A 3D turn has no single in-plane angle. Reporting one would draw the
304+
// chrome at a plausible-looking wrong angle instead of falling back square.
305+
expect(individualRotateDegrees("1 0 0 45deg")).toBe(0);
306+
expect(individualRotateDegrees("0 1 0 45deg")).toBe(0);
307+
});
308+
309+
it("reports nothing when the property is absent or unparseable", () => {
310+
expect(individualRotateDegrees("none")).toBe(0);
311+
expect(individualRotateDegrees(undefined)).toBe(0);
312+
expect(individualRotateDegrees("")).toBe(0);
313+
expect(individualRotateDegrees("12")).toBe(0);
314+
});
315+
});
316+
317+
describe("readElementCropFrame — the composed walk", () => {
318+
/**
319+
* The case the crop outline got wrong: a text layer inside a rotated card.
320+
* The layer carries its own spin and its parent turns it again, so the box
321+
* belongs at the combination. Reading the element alone drew it across the
322+
* text at roughly a right angle.
323+
*/
324+
const nested = (childTransform: string, parentTransform: string) => {
325+
const style = (transform: string) => ({ transform }) as CSSStyleDeclaration;
326+
const parent = {
327+
offsetWidth: 400,
328+
offsetHeight: 300,
329+
parentElement: null,
330+
hasAttribute: (name: string) => name === "data-composition-id",
331+
ownerDocument: { defaultView: { getComputedStyle: () => style(parentTransform) } },
332+
};
333+
return {
334+
offsetWidth: 200,
335+
offsetHeight: 100,
336+
parentElement: parent,
337+
hasAttribute: () => false,
338+
ownerDocument: {
339+
defaultView: {
340+
getComputedStyle: (node: unknown) =>
341+
node === parent ? style(parentTransform) : style(childTransform),
342+
},
343+
},
344+
} as unknown as HTMLElement;
345+
};
346+
347+
const overlayRect = { left: 100, top: 50, width: 220, height: 130, editScaleX: 1, editScaleY: 1 };
348+
349+
it("adds the parent's rotation to the child's", () => {
350+
// child 30deg inside a parent turned 60deg → the layer paints at 90.
351+
const frame = readElementCropFrame(
352+
nested(
353+
"matrix(0.8660254, 0.5, -0.5, 0.8660254, 0, 0)",
354+
"matrix(0.5, 0.8660254, -0.8660254, 0.5, 0, 0)",
355+
),
356+
overlayRect,
357+
);
358+
expect(frame.angleDeg).toBeCloseTo(90, 3);
359+
});
360+
361+
it("takes the parent's rotation when the child has none of its own", () => {
362+
const frame = readElementCropFrame(
363+
nested("none", "matrix(0.8660254, 0.5, -0.5, 0.8660254, 0, 0)"),
364+
overlayRect,
365+
);
366+
expect(frame.angleDeg).toBeCloseTo(30, 3);
367+
});
368+
369+
it("stops at the composition root rather than walking the whole document", () => {
370+
// The root itself is marked, so its own transform is the last one counted.
371+
const frame = readElementCropFrame(
372+
nested("none", "matrix(0.8660254, 0.5, -0.5, 0.8660254, 0, 0)"),
373+
overlayRect,
374+
);
375+
expect(frame.angleDeg).toBeCloseTo(30, 3);
376+
});
377+
});

0 commit comments

Comments
 (0)