Skip to content

Commit c90c9d4

Browse files
authored
fix(studio): derive gesture readout from pointer drafts
1 parent f8233c9 commit c90c9d4

6 files changed

Lines changed: 156 additions & 95 deletions

File tree

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

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import React, { act, type ComponentProps } from "react";
33
import { createRoot, type Root } from "react-dom/client";
44
import { afterEach, describe, expect, it, vi } from "vitest";
55
import { DomEditGestureReadout } from "./DomEditGestureReadout";
6-
import { applyStudioBoxSizeDraft } from "./manualEdits";
7-
import { readRotationBase } from "./rotationDraft";
6+
import { createGestureReadout, updateGestureReadout } from "./domEditGestureReadoutState";
87

98
const frames = vi.hoisted(() => ({ update: () => {} }));
109
vi.mock("./overlayFrameLoop", () => ({
@@ -15,7 +14,6 @@ vi.mock("./overlayFrameLoop", () => ({
1514
};
1615
},
1716
}));
18-
vi.mock("./rotationDraft", () => ({ readRotationBase: vi.fn(() => 0) }));
1917
// React needs its runtime test flag, which the global typings do not declare.
2018
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
2119
let root: Root | undefined;
@@ -53,13 +51,31 @@ function mount(kind: "drag" | "resize" | "rotate") {
5351
});
5452
const gestureRef: ComponentProps<typeof DomEditGestureReadout>["gestureRef"] = {
5553
current: {
56-
kind,
57-
selection: { element },
58-
plainRotation: null,
59-
actualWidth: width,
60-
actualHeight: height,
54+
travelled: true,
55+
readout: createGestureReadout(overlay, iframe, {
56+
left: x / 2,
57+
top: y / 2,
58+
width: width / 2,
59+
height: height / 2,
60+
editScaleX: 0.5,
61+
editScaleY: 0.5,
62+
}),
6163
},
6264
};
65+
const update = (text?: string) =>
66+
updateGestureReadout(
67+
gestureRef.current?.readout,
68+
{
69+
left: x / 2,
70+
top: y / 2,
71+
width: width / 2,
72+
height: height / 2,
73+
editScaleX: 0.5,
74+
editScaleY: 0.5,
75+
},
76+
text,
77+
);
78+
update(kind === "resize" ? "100 × 60" : kind === "rotate" ? "0°" : undefined);
6379
root = createRoot(host);
6480
act(() =>
6581
root?.render(
@@ -79,26 +95,49 @@ function mount(kind: "drag" | "resize" | "rotate") {
7995
draw();
8096
};
8197
return {
98+
measurements: [overlay, iframe, composition, element].map((node) =>
99+
vi.spyOn(node, "getBoundingClientRect"),
100+
),
82101
element,
102+
gestureRef,
103+
update,
83104
doc,
84105
label,
85106
draw,
86107
end,
87108
move: (nextX: number, nextY: number) => {
88109
x = nextX;
89110
y = nextY;
111+
update();
90112
},
91113
};
92114
}
93115

94116
describe("gesture readout", () => {
117+
it("adds no layout reads while drawing and stays hidden on a plain press", () => {
118+
const view = mount("drag");
119+
const measure = vi.spyOn(Element.prototype, "getBoundingClientRect");
120+
if (!view.gestureRef.current) throw new Error("Missing gesture");
121+
view.gestureRef.current.travelled = false;
122+
view.draw();
123+
expect(view.label.style.display).toBe("none");
124+
view.gestureRef.current.travelled = true;
125+
view.move(120, 70);
126+
view.draw();
127+
view.draw();
128+
expect(view.label.textContent).toBe("Canvas X 120 · Y 70");
129+
expect(measure).not.toHaveBeenCalled();
130+
for (const measurement of view.measurements) expect(measurement).not.toHaveBeenCalled();
131+
measure.mockRestore();
132+
for (const measurement of view.measurements) measurement.mockRestore();
133+
});
95134
it("shows rounded composition coordinates during a move, accounting for preview zoom", () => {
96135
const view = mount("drag");
97136
view.draw();
98-
expect(view.label.textContent).toBe("X 70 · Y 41");
137+
expect(view.label.textContent).toBe("Canvas X 70 · Y 41");
99138
view.move(120.25, 70.75);
100139
view.draw();
101-
expect(view.label.textContent).toBe("X 120 · Y 71");
140+
expect(view.label.textContent).toBe("Canvas X 120 · Y 71");
102141
expect(view.doc.querySelector("[data-dom-edit-gesture-readout]")).toBeNull();
103142
view.end();
104143
expect(view.label.style.display).toBe("none");
@@ -107,19 +146,18 @@ describe("gesture readout", () => {
107146
const view = mount("resize");
108147
view.draw();
109148
expect(view.label.textContent).toBe("100 × 60");
110-
applyStudioBoxSizeDraft(view.element, { width: 143.6, height: 87.2 });
149+
view.update("144 × 87");
111150
view.draw();
112151
expect(view.label.textContent).toBe("144 × 87");
113152
view.end();
114153
expect(view.label.style.display).toBe("none");
115154
});
116155
it("reads the live angle during rotation and hides on cancellation", () => {
117156
const view = mount("rotate");
118-
vi.mocked(readRotationBase).mockReturnValue(31.6);
157+
view.update("32°");
119158
view.draw();
120159
expect(view.label.textContent).toBe("32°");
121-
expect(readRotationBase).toHaveBeenCalledWith(view.element, false);
122-
vi.mocked(readRotationBase).mockReturnValue(-46.2);
160+
view.update("-46°");
123161
view.draw();
124162
expect(view.label.textContent).toBe("-46°");
125163
view.end();
@@ -129,11 +167,11 @@ describe("gesture readout", () => {
129167
const view = mount("drag");
130168
view.move(900, -100);
131169
view.draw();
132-
expect(view.label.style.left).toBe("376px");
170+
expect(view.label.style.left).toBe("496px");
133171
expect(view.label.style.top).toBe("4px");
134172
view.move(-900, 900);
135173
view.draw();
136174
expect(view.label.style.left).toBe("4px");
137-
expect(view.label.style.top).toBe("372px");
175+
expect(view.label.style.top).toBe("396px");
138176
});
139177
});

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

Lines changed: 21 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,57 @@
11
import { useRef, type RefObject } from "react";
22
import { useMountEffect } from "../../hooks/useMountEffect";
33
import type { GestureState } from "./domEditOverlayGestures";
4-
import { computeOverlayRootScale } from "./domEditOverlayBasis";
5-
import { orientedOverlayRect } from "./domEditOverlayGeometry";
6-
import { readStudioBoxSize } from "./manualEdits";
7-
import { readRotationBase } from "./rotationDraft";
84
import { subscribeOverlayFrame } from "./overlayFrameLoop";
95

10-
type ReadoutGesture = Pick<
11-
GestureState,
12-
"kind" | "plainRotation" | "actualWidth" | "actualHeight"
13-
> & {
14-
selection: Pick<GestureState["selection"], "element">;
15-
};
6+
type ReadoutGesture = Pick<GestureState, "readout" | "travelled">;
167

178
interface DomEditGestureReadoutProps {
189
gestureRef: RefObject<ReadoutGesture | null>;
1910
overlayRef: RefObject<HTMLDivElement | null>;
2011
iframeRef: RefObject<HTMLIFrameElement | null>;
2112
}
2213

23-
function readGestureText(
24-
gesture: ReadoutGesture,
25-
overlay: HTMLDivElement,
26-
iframe: HTMLIFrameElement,
27-
): string | null {
28-
const element = gesture.selection.element;
29-
if (gesture.kind === "resize") {
30-
const size = readStudioBoxSize(element);
31-
return `${Math.round(size.width || gesture.actualWidth)} × ${Math.round(size.height || gesture.actualHeight)}`;
32-
}
33-
if (gesture.kind === "rotate") {
34-
return `${Math.round(readRotationBase(element, gesture.plainRotation !== null))}°`;
35-
}
36-
const scale = computeOverlayRootScale(overlay, iframe, iframe.contentDocument);
37-
const rect = orientedOverlayRect(overlay, iframe, element, scale);
38-
if (!scale || !rect || scale.rootScaleX <= 0 || scale.rootScaleY <= 0) return null;
39-
const x = (rect.left - (scale.iframeRect.left - scale.overlayRect.left)) / scale.rootScaleX;
40-
const y = (rect.top - (scale.iframeRect.top - scale.overlayRect.top)) / scale.rootScaleY;
41-
return `X ${Math.round(x)} · Y ${Math.round(y)}`;
42-
}
43-
44-
function positionGestureReadout(
45-
label: HTMLDivElement,
46-
gesture: ReadoutGesture,
47-
overlay: HTMLDivElement,
48-
iframe: HTMLIFrameElement,
49-
): void {
50-
const bounds = overlay.getBoundingClientRect();
51-
const element = gesture.selection.element.getBoundingClientRect();
52-
const scale = computeOverlayRootScale(overlay, iframe, iframe.contentDocument);
53-
if (!scale) {
54-
label.style.display = "none";
55-
return;
56-
}
57-
const right = scale.iframeRect.left + element.right * scale.rootScaleX - bounds.left;
58-
const top = scale.iframeRect.top + element.top * scale.rootScaleY - bounds.top;
14+
function positionGestureReadout(label: HTMLDivElement, gesture: ReadoutGesture): void {
15+
const readout = gesture.readout;
16+
if (!readout) return;
17+
const bounds = readout.basis.overlayRect;
18+
const rect = readout.rect;
5919
const minX = Math.max(0, -bounds.left) + 4;
6020
const minY = Math.max(0, -bounds.top) + 4;
61-
const maxX = Math.min(bounds.width, window.innerWidth - bounds.left) - label.offsetWidth - 4;
62-
const maxY = Math.min(bounds.height, window.innerHeight - bounds.top) - label.offsetHeight - 4;
63-
label.style.left = `${Math.max(minX, Math.min(maxX, right + 8))}px`;
64-
label.style.top = `${Math.max(minY, Math.min(maxY, top - label.offsetHeight - 8))}px`;
21+
const maxX = Math.min(bounds.width, readout.viewportWidth - bounds.left) - 4;
22+
const maxY = Math.min(bounds.height, readout.viewportHeight - bounds.top) - 4;
23+
const x = Math.max(minX, Math.min(maxX, rect.left + rect.width + 8));
24+
const y = Math.max(minY, Math.min(maxY, rect.top - 8));
25+
label.style.left = `${x}px`;
26+
label.style.top = `${y}px`;
27+
label.style.maxWidth = `${Math.max(0, maxX - minX)}px`;
28+
// CSS resolves the label's own size without a synchronous layout read after its text changes.
29+
label.style.transform = `translate(max(-100%, ${minX - x}px), max(-100%, ${minY - y}px))`;
6530
}
6631

67-
export function DomEditGestureReadout({
68-
gestureRef,
69-
overlayRef,
70-
iframeRef,
71-
}: DomEditGestureReadoutProps) {
32+
export function DomEditGestureReadout({ gestureRef }: DomEditGestureReadoutProps) {
7233
const labelRef = useRef<HTMLDivElement | null>(null);
7334
useMountEffect(() =>
7435
subscribeOverlayFrame(() => {
7536
const label = labelRef.current;
7637
if (!label) return;
7738
const gesture = gestureRef.current;
78-
const overlay = overlayRef.current;
79-
const iframe = iframeRef.current;
80-
if (!gesture || !overlay || !iframe) {
81-
label.style.display = "none";
82-
return;
83-
}
84-
const text = readGestureText(gesture, overlay, iframe);
85-
if (!text) {
39+
const text = gesture?.travelled && gesture.readout?.text;
40+
if (!gesture || !text) {
8641
label.style.display = "none";
8742
return;
8843
}
89-
label.textContent = text;
44+
if (label.textContent !== text) label.textContent = text;
9045
label.style.display = "block";
91-
positionGestureReadout(label, gesture, overlay, iframe);
46+
positionGestureReadout(label, gesture);
9247
}),
9348
);
9449
return (
9550
<div
9651
ref={labelRef}
9752
aria-hidden="true"
9853
data-dom-edit-gesture-readout="true"
99-
className="pointer-events-none absolute z-50 rounded bg-studio-surface px-2 py-1 font-mono text-xs whitespace-nowrap text-studio-text shadow-md"
54+
className="pointer-events-none absolute z-50 overflow-hidden rounded bg-studio-surface px-2 py-1 font-mono text-xs whitespace-nowrap text-studio-text shadow-md"
10055
style={{ display: "none" }}
10156
/>
10257
);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { computeOverlayRootScale, type OverlayRootScale } from "./domEditOverlayBasis";
2+
import type { OverlayRect } from "./domEditOverlayGeometry";
3+
4+
export interface GestureReadoutState {
5+
basis: OverlayRootScale;
6+
viewportWidth: number;
7+
viewportHeight: number;
8+
text: string;
9+
rect: OverlayRect;
10+
}
11+
12+
export function createGestureReadout(
13+
overlay: HTMLDivElement | null,
14+
iframe: HTMLIFrameElement | null,
15+
rect: OverlayRect,
16+
): GestureReadoutState | undefined {
17+
const basis =
18+
overlay && iframe && computeOverlayRootScale(overlay, iframe, iframe.contentDocument);
19+
if (!basis || basis.rootScaleX <= 0 || basis.rootScaleY <= 0) return undefined;
20+
return {
21+
basis,
22+
viewportWidth: window.innerWidth,
23+
viewportHeight: window.innerHeight,
24+
text: "",
25+
rect,
26+
};
27+
}
28+
29+
export function updateGestureReadout(
30+
readout: GestureReadoutState | undefined,
31+
rect: OverlayRect,
32+
text?: string,
33+
): void {
34+
if (!readout) return;
35+
const { basis } = readout;
36+
const x = (rect.left - (basis.iframeRect.left - basis.overlayRect.left)) / basis.rootScaleX;
37+
const y = (rect.top - (basis.iframeRect.top - basis.overlayRect.top)) / basis.rootScaleY;
38+
readout.text = text ?? `Canvas X ${Math.round(x)} · Y ${Math.round(y)}`;
39+
readout.rect = rect;
40+
}
41+
42+
export function updateGestureReadoutAngle(
43+
readout: GestureReadoutState | undefined,
44+
angle: number,
45+
): void {
46+
if (readout) updateGestureReadout(readout, readout.rect, `${Math.round(angle)}°`);
47+
}
48+
49+
export function updateGestureReadoutSize(
50+
readout: GestureReadoutState | undefined,
51+
rect: OverlayRect,
52+
size: { width: number; height: number },
53+
): void {
54+
updateGestureReadout(readout, rect, `${Math.round(size.width)} × ${Math.round(size.height)}`);
55+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { GestureReadoutState } from "./domEditGestureReadoutState";
12
import type { RefObject } from "react";
23
import type { DomEditSelection } from "./domEditing";
34
import type {
@@ -47,6 +48,7 @@ const ROTATION_SNAP_DEGREES = 15;
4748
export const ROTATED_SNAP_BYPASS_DEGREES = 0.5;
4849

4950
export interface GestureState {
51+
readout?: GestureReadoutState;
5052
kind: GestureKind;
5153
mode: "path-offset" | "box-size" | "rotation";
5254
selection: DomEditSelection;
@@ -311,3 +313,13 @@ export type UseDomEditOverlayGesturesOptions = {
311313
onCanvasMouseDown: (e: React.MouseEvent<HTMLDivElement>, o?: PreviewMouseDownOptions) => void;
312314
snapGuidesRef: RefObject<SnapGuidesState | null>;
313315
};
316+
317+
export function isDomEditGestureTap(
318+
g: { startX: number; startY: number; travelled?: boolean },
319+
e: React.PointerEvent,
320+
) {
321+
return (
322+
!g.travelled &&
323+
Math.hypot(e.clientX - g.startX, e.clientY - g.startY) < BLOCKED_MOVE_THRESHOLD_PX
324+
);
325+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createGestureReadout } from "./domEditGestureReadoutState";
12
/**
23
* Gesture-begin functions: startGroupDrag and startGesture.
34
* These are pure "start a new gesture" operations — no draft rect updates.
@@ -278,6 +279,7 @@ export function startGesture(
278279
opts.rafPausedRef.current = true;
279280
opts.gestureRef.current = {
280281
kind,
282+
readout: createGestureReadout(overlayEl, iframe, rect),
281283
mode,
282284
selection: sel,
283285
pointerId: e.pointerId,

0 commit comments

Comments
 (0)