|
1 | 1 | import { useRef, type RefObject } from "react"; |
2 | 2 | import { useMountEffect } from "../../hooks/useMountEffect"; |
3 | 3 | 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"; |
8 | 4 | import { subscribeOverlayFrame } from "./overlayFrameLoop"; |
9 | 5 |
|
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">; |
16 | 7 |
|
17 | 8 | interface DomEditGestureReadoutProps { |
18 | 9 | gestureRef: RefObject<ReadoutGesture | null>; |
19 | 10 | overlayRef: RefObject<HTMLDivElement | null>; |
20 | 11 | iframeRef: RefObject<HTMLIFrameElement | null>; |
21 | 12 | } |
22 | 13 |
|
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; |
59 | 19 | const minX = Math.max(0, -bounds.left) + 4; |
60 | 20 | 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))`; |
65 | 30 | } |
66 | 31 |
|
67 | | -export function DomEditGestureReadout({ |
68 | | - gestureRef, |
69 | | - overlayRef, |
70 | | - iframeRef, |
71 | | -}: DomEditGestureReadoutProps) { |
| 32 | +export function DomEditGestureReadout({ gestureRef }: DomEditGestureReadoutProps) { |
72 | 33 | const labelRef = useRef<HTMLDivElement | null>(null); |
73 | 34 | useMountEffect(() => |
74 | 35 | subscribeOverlayFrame(() => { |
75 | 36 | const label = labelRef.current; |
76 | 37 | if (!label) return; |
77 | 38 | 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) { |
86 | 41 | label.style.display = "none"; |
87 | 42 | return; |
88 | 43 | } |
89 | | - label.textContent = text; |
| 44 | + if (label.textContent !== text) label.textContent = text; |
90 | 45 | label.style.display = "block"; |
91 | | - positionGestureReadout(label, gesture, overlay, iframe); |
| 46 | + positionGestureReadout(label, gesture); |
92 | 47 | }), |
93 | 48 | ); |
94 | 49 | return ( |
95 | 50 | <div |
96 | 51 | ref={labelRef} |
97 | 52 | aria-hidden="true" |
98 | 53 | 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" |
100 | 55 | style={{ display: "none" }} |
101 | 56 | /> |
102 | 57 | ); |
|
0 commit comments