Skip to content

Commit b7fddd5

Browse files
committed
fix(studio): crop restore value is owned by the lifted element's gesture
The deselect restore read a ref recomputed from RENDER state — on a direct A→B selection switch, state re-syncs to B before A's effect cleanup runs, so after a committed crop gesture A was restored with B's crop string (or lost its crop when B had none). The committed value is now written at gesture-commit time (tri-state: none committed / crop removal / the exact committed string), so cleanup never touches render state. Adds component tests for lift/restore ordering, including the direct A→B switch and the uneditable-clip stand-down.
1 parent fe52966 commit b7fddd5

2 files changed

Lines changed: 107 additions & 16 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// @vitest-environment happy-dom
2+
import React, { act } from "react";
3+
import { createRoot, type Root } from "react-dom/client";
4+
import { afterEach, describe, expect, it } from "vitest";
5+
import type { DomEditSelection } from "./domEditing";
6+
import type { OverlayRect } from "./domEditOverlayGeometry";
7+
import { DomEditCropHandles } from "./DomEditCropHandles";
8+
9+
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
10+
11+
afterEach(() => {
12+
document.body.innerHTML = "";
13+
});
14+
15+
const overlayRect: OverlayRect = {
16+
left: 0,
17+
top: 0,
18+
width: 200,
19+
height: 100,
20+
editScaleX: 1,
21+
editScaleY: 1,
22+
};
23+
24+
function selectionFor(el: HTMLElement): DomEditSelection {
25+
return { element: el, id: el.id, selector: `#${el.id}` } as unknown as DomEditSelection;
26+
}
27+
28+
function makeEl(id: string, clip: string): HTMLElement {
29+
const el = document.createElement("div");
30+
el.id = id;
31+
if (clip) el.style.setProperty("clip-path", clip);
32+
document.body.append(el);
33+
return el;
34+
}
35+
36+
function render(el: HTMLElement): { root: Root; rerender: (next: HTMLElement) => void } {
37+
const host = document.createElement("div");
38+
document.body.append(host);
39+
const root = createRoot(host);
40+
const draw = (target: HTMLElement) =>
41+
act(() => {
42+
root.render(
43+
<DomEditCropHandles
44+
selection={selectionFor(target)}
45+
overlayRect={overlayRect}
46+
onStyleCommit={() => undefined}
47+
/>,
48+
);
49+
});
50+
draw(el);
51+
return { root, rerender: draw };
52+
}
53+
54+
// Regression: the deselect restore used a ref recomputed from RENDER state — on
55+
// a direct A→B selection switch, state re-syncs to B before A's effect cleanup
56+
// runs, so A used to get B's crop string (or lose its crop entirely). The
57+
// restore value must be owned by A's own lift effect / crop gesture.
58+
describe("DomEditCropHandles clip lift/restore", () => {
59+
it("lifts on select and restores the inline clip verbatim on unmount", () => {
60+
const a = makeEl("a", "inset(16px round 12px)");
61+
const { root } = render(a);
62+
expect(a.style.getPropertyValue("clip-path")).toBe("none");
63+
act(() => root.unmount());
64+
expect(a.style.getPropertyValue("clip-path")).toBe("inset(16px round 12px)");
65+
});
66+
67+
it("restores A's own clip when switching directly to B", () => {
68+
const a = makeEl("a", "inset(16px)");
69+
const b = makeEl("b", "inset(40px 8px 4px 2px)");
70+
const { root, rerender } = render(a);
71+
rerender(b);
72+
// A got ITS clip back, not B's (and not removed); B is now lifted.
73+
expect(a.style.getPropertyValue("clip-path")).toBe("inset(16px)");
74+
expect(b.style.getPropertyValue("clip-path")).toBe("none");
75+
act(() => root.unmount());
76+
expect(b.style.getPropertyValue("clip-path")).toBe("inset(40px 8px 4px 2px)");
77+
});
78+
79+
it("never lifts an uneditable clip and leaves it untouched across select/deselect", () => {
80+
const a = makeEl("a", "circle(50% at 50% 50%)");
81+
const { root } = render(a);
82+
expect(a.style.getPropertyValue("clip-path")).toBe("circle(50% at 50% 50%)");
83+
act(() => root.unmount());
84+
expect(a.style.getPropertyValue("clip-path")).toBe("circle(50% at 50% 50%)");
85+
});
86+
});

‎packages/studio/src/components/editor/DomEditCropHandles.tsx‎

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export function DomEditCropHandles({
8484
const cropStateFor = (element: HTMLElement) => {
8585
const parsed = readElementCropInsets(element);
8686
const { radius, ...insets } = parsed ?? { top: 0, right: 0, bottom: 0, left: 0, radius: 0 };
87-
return { element, croppable: parsed !== null, insets: insets as ClipPathInsetSides, radius };
87+
return { element, croppable: parsed !== null, insets, radius };
8888
};
8989
const [state, setState] = useState(() => cropStateFor(selection.element));
9090

@@ -102,32 +102,32 @@ export function DomEditCropHandles({
102102
state.insets.bottom > 0 ||
103103
state.insets.left > 0;
104104

105-
// Latest committed crop — re-applied to the element when the selection drops.
106-
const committedRef = useRef<string | null>(null);
107-
committedRef.current = hasCrop ? buildInsetClipPathSides(state.insets, state.radius) : null;
108-
109105
// Lift the clip while the element is selected so the full content shows and the
110106
// cropped-away area can be dimmed; restore on deselect. Keyed on the element so
111107
// switching selections restores the previous one. Runs after render, so the
112108
// state re-sync above still reads the element's real committed clip. Restore
113109
// prefers the pre-lift inline value VERBATIM — the rebuilt inset only replaces
114110
// it after a crop gesture actually commits, so a mere select+deselect can
115-
// never reformat (or drop) what the author wrote.
111+
// never reformat (or drop) what the author wrote. Both refs are written only
112+
// by THIS element's lift effect and crop gestures — never derived from render
113+
// state, which by cleanup time already describes the NEXT selection (a direct
114+
// A→B switch re-syncs state to B before A's cleanup runs).
116115
const liftedRef = useRef(false);
117116
const preLiftInlineClipRef = useRef("");
118-
const cropCommittedRef = useRef(false);
117+
// null = no crop gesture committed this selection; "" = committed a crop
118+
// removal; anything else = the exact committed clip-path value.
119+
const committedClipRef = useRef<string | null>(null);
119120
useEffect(() => {
120121
const el = selection.element;
121122
if (readElementCropInsets(el) === null) return;
122123
preLiftInlineClipRef.current = el.style.getPropertyValue("clip-path");
123-
cropCommittedRef.current = false;
124+
committedClipRef.current = null;
124125
el.style.setProperty("clip-path", "none");
125126
liftedRef.current = true;
126127
return () => {
127128
liftedRef.current = false;
128-
const restore = cropCommittedRef.current
129-
? committedRef.current
130-
: preLiftInlineClipRef.current || null;
129+
const committed = committedClipRef.current;
130+
const restore = committed !== null ? committed || null : preLiftInlineClipRef.current || null;
131131
if (restore) el.style.setProperty("clip-path", restore);
132132
else el.style.removeProperty("clip-path");
133133
};
@@ -194,12 +194,17 @@ export function DomEditCropHandles({
194194
const reLift = () => {
195195
if (liftedRef.current) el.style.setProperty("clip-path", "none");
196196
};
197-
void Promise.resolve(
198-
onStyleCommit?.("clip-path", buildInsetClipPathSides(state.insets, state.radius)),
199-
).then(() => {
197+
const committedValue = buildInsetClipPathSides(state.insets, state.radius);
198+
const cropped =
199+
state.insets.top > 0 ||
200+
state.insets.right > 0 ||
201+
state.insets.bottom > 0 ||
202+
state.insets.left > 0;
203+
void Promise.resolve(onStyleCommit?.("clip-path", committedValue)).then(() => {
200204
// Only a landed commit makes the rebuilt inset the restore value; a
201-
// failed one keeps restoring the pre-lift clip.
202-
cropCommittedRef.current = true;
205+
// failed one keeps restoring the pre-lift clip. Store the value itself —
206+
// by deselect time, render state describes the next selection.
207+
committedClipRef.current = cropped ? committedValue : "";
203208
reLift();
204209
}, reLift);
205210
};

0 commit comments

Comments
 (0)