@hyperframes/studio 0.9.40 → 0.9.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/dist/assets/{codec-BPK_2CLL.js → codec-B_mpbc4w.js} +1 -1
  2. package/dist/assets/{hyperframes-player-D7qTTDIp.js → hyperframes-player-Dvi3I_mv.js} +1 -1
  3. package/dist/assets/{index-CdA00Ybn.js → index-B5ExwsOp.js} +146 -146
  4. package/dist/assets/{index-QS-DuxAU.js → index-BrFm9Y5k.js} +1 -1
  5. package/dist/assets/{pausedVideoDecoder-Byzz98Gb.js → pausedVideoDecoder-MHQ57yZ6.js} +1 -1
  6. package/dist/{chunk-KF6YVXJP.js → chunk-4QQMX4BX.js} +6 -3
  7. package/dist/{chunk-VZ5MD32A.js → chunk-GVW7AU57.js} +190 -254
  8. package/dist/{chunk-AV2PV7NE.js → chunk-VHV2ZDKY.js} +4 -40
  9. package/dist/{domEditingLayers-4KRREI5C.js → domEditingLayers-SA5KLSSS.js} +4 -2
  10. package/dist/index.d.ts +4 -0
  11. package/dist/index.html +1 -1
  12. package/dist/index.js +898 -551
  13. package/dist/player.js +2 -2
  14. package/package.json +8 -8
  15. package/src/components/editor/DomEditOverlay.tsx +3 -15
  16. package/src/components/editor/clickAfterPreventedPress.test.tsx +17 -1
  17. package/src/components/editor/domEditOverlayBasis.ts +7 -2
  18. package/src/components/editor/domEditOverlayCrop.test.ts +1 -1
  19. package/src/components/editor/domEditOverlayCrop.ts +17 -3
  20. package/src/components/editor/domEditOverlayGeometry.ts +64 -139
  21. package/src/components/editor/domEditSelectionRules.ts +138 -0
  22. package/src/components/editor/domEditing.test.ts +6 -28
  23. package/src/components/editor/domEditingDom.preview.test.ts +3 -3
  24. package/src/components/editor/domEditingDom.ts +3 -76
  25. package/src/components/editor/domEditingElement.ts +19 -240
  26. package/src/components/editor/domEditingGroups.ts +0 -31
  27. package/src/components/editor/domEditingLayers.ts +95 -100
  28. package/src/components/editor/domEditingRootLayer.ts +35 -30
  29. package/src/components/editor/domEditingTypes.ts +4 -0
  30. package/src/components/editor/previewGestureStarts.ts +73 -10
  31. package/src/components/editor/previewOverlayRects.test.ts +316 -0
  32. package/src/components/editor/previewOverlayRects.ts +123 -0
  33. package/src/components/editor/previewPointerTargets.test.ts +260 -0
  34. package/src/components/editor/previewSelection.test.ts +481 -0
  35. package/src/components/editor/previewSelection.ts +472 -0
  36. package/src/components/editor/probeSourceElement.ts +19 -1
  37. package/src/components/editor/propertyPanelFill.tsx +5 -1
  38. package/src/components/editor/useDomEditOverlayRects.ts +80 -1
  39. package/src/components/editor/useInlineTextEditing.tsx +1 -6
  40. package/src/hooks/domSelectionTestHarness.ts +1 -1
  41. package/src/hooks/useDomEditSession.test.tsx +23 -1
  42. package/src/hooks/useDomEditSession.ts +2 -1
  43. package/src/hooks/useDomSelection.ts +61 -47
  44. package/src/hooks/useGsapSelectionHandlers.test.tsx +17 -0
  45. package/src/hooks/useGsapSelectionHandlers.ts +2 -1
  46. package/src/hooks/usePreviewInteraction.test.ts +10 -2
  47. package/src/hooks/usePreviewInteraction.ts +5 -4
  48. package/src/hooks/useStudioSelectionPublisher.test.tsx +50 -0
  49. package/src/hooks/useStudioSelectionPublisher.ts +9 -7
  50. package/src/player/lib/previewChannel.ts +4 -3
  51. package/src/player/lib/previewElements.ts +112 -3
  52. package/src/styles/hex-baseline.json +1 -2
  53. package/src/utils/studioHelpers.ts +0 -13
  54. package/src/utils/studioPreviewHelpers.test.ts +8 -8
  55. package/src/utils/studioPreviewHelpers.ts +26 -242
  56. package/src/components/editor/domEditOverlayTransform.ts +0 -126
@@ -0,0 +1,316 @@
1
+ // @vitest-environment happy-dom
2
+
3
+ import React, { act } from "react";
4
+ import { createRoot } from "react-dom/client";
5
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
6
+ // The frame's real runtime agent, serving the opaque frame from this document.
7
+ import { installEditingAgent } from "../../../../core/src/runtime/editingAgent";
8
+ import { makeSelection, installReactActEnvironment } from "../../hooks/domSelectionTestHarness";
9
+ import { watchPreviewFrame } from "../../player/lib/bridgePlayback";
10
+ import { queryPreviewElements } from "../../player/lib/previewElements";
11
+ import { detachedFakeIframe } from "../../player/hooks/timelinePlayerTestHarness";
12
+ import type { DomEditSelection } from "./domEditing";
13
+ import { orientedGroupAwareOverlayRect, type OverlayRect } from "./domEditOverlayGeometry";
14
+ import type { UseDomEditOverlayGesturesOptions } from "./domEditOverlayGestures";
15
+ import { createPreviewGestureStarts } from "./previewGestureStarts";
16
+ import { useDomEditOverlayRects } from "./useDomEditOverlayRects";
17
+
18
+ const frames = vi.hoisted(() => new Set<() => void>());
19
+ vi.mock("./overlayFrameLoop", () => ({
20
+ subscribeOverlayFrame: (update: () => void) => {
21
+ frames.add(update);
22
+ return () => frames.delete(update);
23
+ },
24
+ }));
25
+
26
+ const press = vi.hoisted(() => ({ changing: true, start: vi.fn((..._args: unknown[]) => true) }));
27
+ vi.mock("./domEditOverlayStartGesture", () => ({
28
+ startGesture: press.start,
29
+ startGroupDrag: vi.fn(() => true),
30
+ }));
31
+ vi.mock("../../player/previewReloading", async (importOriginal) => ({
32
+ ...(await importOriginal<typeof import("../../player/previewReloading")>()),
33
+ isPreviewChanging: () => press.changing,
34
+ giveUpOnPreviewChange: vi.fn(),
35
+ }));
36
+
37
+ installReactActEnvironment();
38
+
39
+ // The 2D slice of each that the overlay reads; one set for both frames, which share this window.
40
+ class FakeDOMMatrix {
41
+ a = 1;
42
+ b = 0;
43
+ c = 0;
44
+ d = 1;
45
+ e = 0;
46
+ f = 0;
47
+ constructor(init?: string) {
48
+ const m = init ? /matrix\(([^)]+)\)/.exec(init) : null;
49
+ if (m) [this.a, this.b, this.c, this.d, this.e, this.f] = m[1]!.split(",").map(Number);
50
+ }
51
+ multiply(o: FakeDOMMatrix) {
52
+ const out = new FakeDOMMatrix();
53
+ out.a = this.a * o.a + this.c * o.b;
54
+ out.b = this.b * o.a + this.d * o.b;
55
+ out.c = this.a * o.c + this.c * o.d;
56
+ out.d = this.b * o.c + this.d * o.d;
57
+ out.e = this.a * o.e + this.c * o.f + this.e;
58
+ out.f = this.b * o.e + this.d * o.f + this.f;
59
+ return out;
60
+ }
61
+ rotateSelf() {
62
+ return this;
63
+ }
64
+ transformPoint(p: { x: number; y: number }) {
65
+ return { x: this.a * p.x + this.c * p.y + this.e, y: this.b * p.x + this.d * p.y + this.f };
66
+ }
67
+ }
68
+
69
+ const FIXTURE = `
70
+ <div data-composition-id="main" data-width="1920" data-height="1080">
71
+ <div id="host" data-composition-src="a.html" data-width="960" data-height="540">
72
+ <p id="inner"></p><span id="speck"></span>
73
+ </div>
74
+ <div id="turned" style="transform: matrix(0.8, 0.6, -0.6, 0.8, 0, 0)"></div>
75
+ <div id="grp" data-hf-group="g"><i id="m1"></i><i id="m2"></i></div>
76
+ <div id="clipped" style="clip-path: inset(10px 20px 30px 40px)"></div>
77
+ <div style="display: none"><span id="gone"></span></div>
78
+ </div>`;
79
+ const RECTS: Record<string, number[]> = {
80
+ host: [200, 100, 960, 540],
81
+ inner: [220, 140, 300, 80],
82
+ speck: [230, 150, 2, 2],
83
+ turned: [600, 400, 300, 200],
84
+ m1: [50, 60, 100, 100],
85
+ m2: [400, 500, 120, 90],
86
+ grp: [0, 0, 10, 10],
87
+ clipped: [900, 700, 400, 300],
88
+ gone: [1, 1, 50, 50],
89
+ };
90
+
91
+ function sized(element: Element, [x, y, width, height]: number[]) {
92
+ element.getBoundingClientRect = () =>
93
+ ({ x, y, width, height, left: x, top: y, right: x + width, bottom: y + height }) as DOMRect;
94
+ Object.defineProperty(element, "offsetWidth", { value: width, configurable: true });
95
+ Object.defineProperty(element, "offsetHeight", { value: height, configurable: true });
96
+ return element;
97
+ }
98
+
99
+ const closers: Array<() => void> = [];
100
+ beforeEach(() => {
101
+ Object.assign(window, { DOMMatrix: FakeDOMMatrix });
102
+ document.body.innerHTML = FIXTURE;
103
+ for (const [id, rect] of Object.entries(RECTS)) sized(document.getElementById(id)!, rect);
104
+ });
105
+ afterEach(() => {
106
+ closers.splice(0).forEach((close) => close());
107
+ frames.clear();
108
+ document.body.innerHTML = "";
109
+ });
110
+
111
+ /** A frame Studio scripts directly; its document and window are this one's. */
112
+ function liveFrame() {
113
+ const iframe = detachedFakeIframe(window as unknown as Record<string, unknown>);
114
+ Object.defineProperty(iframe, "contentDocument", { value: document });
115
+ return sized(iframe, [100, 50, 960, 540]) as HTMLIFrameElement;
116
+ }
117
+
118
+ /** A frame Studio cannot read, whose runtime is the real editing agent over this document. */
119
+ function opaqueFrame() {
120
+ const frame: Record<string, unknown> = {};
121
+ const iframe = sized(detachedFakeIframe(frame), [100, 50, 960, 540]) as HTMLIFrameElement;
122
+ Object.defineProperty(iframe, "contentDocument", { value: null });
123
+ Object.defineProperty(frame, "document", {
124
+ get: () => {
125
+ throw new DOMException("blocked", "SecurityError");
126
+ },
127
+ });
128
+ const parent = {
129
+ postMessage: (data: unknown, _origin: string, ports: MessagePort[]) =>
130
+ window.dispatchEvent(
131
+ new MessageEvent("message", { source: frame as unknown as Window, data, ports }),
132
+ ),
133
+ } as unknown as Window;
134
+ const agent = installEditingAgent(parent);
135
+ frame.postMessage = (data: unknown) =>
136
+ window.dispatchEvent(new MessageEvent("message", { source: parent, data }));
137
+ closers.push(() => agent.dispose());
138
+ // The size the runtime posts for its composition, as Studio mirrors it.
139
+ watchPreviewFrame(iframe);
140
+ window.dispatchEvent(
141
+ new MessageEvent("message", {
142
+ source: frame as unknown as Window,
143
+ origin: window.location.origin,
144
+ data: { source: "hf-preview", type: "stage-size", width: 1920, height: 1080 },
145
+ }),
146
+ );
147
+ return iframe;
148
+ }
149
+
150
+ type Shown = ReturnType<typeof useDomEditOverlayRects>;
151
+ interface Picked {
152
+ selection: string | null;
153
+ group?: string[];
154
+ hover?: string | null;
155
+ }
156
+
157
+ /** The overlay hook over `iframe`, with the selections `picked` names; returns its latest output. */
158
+ async function overlayOn(iframe: HTMLIFrameElement, picked: Picked) {
159
+ const opaque = iframe.contentDocument === null;
160
+ const pick = async (id: string): Promise<DomEditSelection> => {
161
+ const selection = makeSelection(id, document.getElementById(id)!);
162
+ // The host is the root of its own source file.
163
+ if (id === "host") selection.sourceFile = "a.html";
164
+ if (!opaque) return selection;
165
+ const reply = await queryPreviewElements(iframe, `#${id}`);
166
+ return { ...selection, handle: reply!.matches[0]!.handle };
167
+ };
168
+ const overlay = sized(document.createElement("div"), [20, 10, 1200, 800]) as HTMLDivElement;
169
+ const refs = {
170
+ iframeRef: { current: iframe },
171
+ overlayRef: { current: overlay },
172
+ selectionRef: { current: picked.selection ? await pick(picked.selection) : null },
173
+ activeCompositionPathRef: { current: "index.html" },
174
+ groupSelectionsRef: { current: await Promise.all((picked.group ?? []).map(pick)) },
175
+ hoverSelectionRef: { current: picked.hover ? await pick(picked.hover) : null },
176
+ rafPausedRef: { current: false },
177
+ };
178
+ let shown: Shown | null = null;
179
+ function Harness() {
180
+ shown = useDomEditOverlayRects(refs);
181
+ return null;
182
+ }
183
+ const root = createRoot(document.createElement("div"));
184
+ act(() => root.render(React.createElement(Harness)));
185
+ closers.push(() => act(() => root.unmount()));
186
+ return { refs, read: () => shown!, frame: () => act(() => frames.forEach((update) => update())) };
187
+ }
188
+
189
+ const settled = async (overlay: Awaited<ReturnType<typeof overlayOn>>) => {
190
+ for (let i = 0; i < 3; i += 1) {
191
+ overlay.frame();
192
+ await act(() => new Promise((resolve) => setTimeout(resolve, 20)));
193
+ }
194
+ const { overlayRect, childRects, groupOverlayItems, hoverRect } = overlay.read();
195
+ return { overlayRect, childRects, group: groupOverlayItems.map((item) => item.rect), hoverRect };
196
+ };
197
+
198
+ const close = (rect: OverlayRect | null) =>
199
+ rect && Object.fromEntries(Object.entries(rect).map(([key, value]) => [key, +value.toFixed(9)]));
200
+ const closeAll = (shown: Awaited<ReturnType<typeof settled>>) => ({
201
+ overlayRect: close(shown.overlayRect),
202
+ childRects: shown.childRects.map(close),
203
+ group: shown.group.map(close),
204
+ hoverRect: close(shown.hoverRect),
205
+ });
206
+
207
+ describe("overlay rects on an opaque frame match a scriptable one", () => {
208
+ it.each<[string, Picked]>([
209
+ [
210
+ "a source host with child outlines, a group and a clipped member, a rotated hover",
211
+ { selection: "host", group: ["grp", "clipped"], hover: "turned" },
212
+ ],
213
+ ["a rotated selection", { selection: "turned" }],
214
+ ["a hidden selection draws nothing", { selection: "gone", hover: "grp" }],
215
+ ])("%s", async (_name, picked) => {
216
+ const live = await settled(await overlayOn(liveFrame(), picked));
217
+ const remote = await settled(await overlayOn(opaqueFrame(), picked));
218
+ expect(closeAll(remote)).toEqual(closeAll(live));
219
+ if (picked.selection === "gone") expect(live.overlayRect).toBeNull();
220
+ else expect(live.overlayRect).not.toBeNull();
221
+ });
222
+
223
+ it("covers what each case decides", async () => {
224
+ const host = await settled(
225
+ await overlayOn(opaqueFrame(), {
226
+ selection: "host",
227
+ group: ["grp", "clipped"],
228
+ hover: "turned",
229
+ }),
230
+ );
231
+ // Scale 0.5 into an iframe 80,40 inside the overlay; the host is its own source boundary.
232
+ expect(host.overlayRect).toMatchObject({ left: 180, top: 90, width: 480, editScaleX: 0.5 });
233
+ // The 2px speck is too small to outline.
234
+ expect(host.childRects).toHaveLength(1);
235
+ expect(host.group[0]).toMatchObject({ left: 105, top: 70, width: 235, height: 265 });
236
+ expect(host.group[1]).toMatchObject({ left: 550, top: 395, width: 170, height: 130 });
237
+ expect(host.hoverRect!.angle).toBeCloseTo(36.87, 2);
238
+ });
239
+
240
+ it("keeps one geometry read in flight however many frames ask", async () => {
241
+ const { port1 } = new MessageChannel();
242
+ const send = vi.spyOn(Object.getPrototypeOf(port1) as MessagePort, "postMessage");
243
+ const geometryReads = () =>
244
+ send.mock.calls.filter(([message]) => (message as { kind?: string }).kind === "geometry");
245
+ const overlay = await overlayOn(opaqueFrame(), { selection: "turned" });
246
+ overlay.frame();
247
+ overlay.frame();
248
+ overlay.frame();
249
+ await vi.waitFor(() => expect(geometryReads()).toHaveLength(1));
250
+ await vi.waitFor(() => expect(overlay.read().overlayRect).not.toBeNull());
251
+ overlay.frame();
252
+ overlay.frame();
253
+ await vi.waitFor(() => expect(geometryReads()).toHaveLength(2));
254
+ send.mockRestore();
255
+ });
256
+ });
257
+
258
+ describe("a press held across a preview change on an opaque frame", () => {
259
+ async function pressDuringChange(selection: DomEditSelection) {
260
+ press.changing = true;
261
+ press.start.mockClear();
262
+ const iframe = opaqueFrame();
263
+ const overlay = sized(document.createElement("div"), [20, 10, 1200, 800]) as HTMLDivElement;
264
+ const opts = {
265
+ waitingPressRef: { current: null },
266
+ boxRef: { current: null },
267
+ rafPausedRef: { current: false },
268
+ iframeRef: { current: iframe },
269
+ overlayRef: { current: overlay },
270
+ activeCompositionPathRef: { current: "index.html" },
271
+ selectionRef: { current: selection },
272
+ overlayRectRef: { current: null },
273
+ setOverlayRect: vi.fn(),
274
+ } as unknown as UseDomEditOverlayGesturesOptions;
275
+ const { startGesture } = createPreviewGestureStarts(opts, vi.fn(), vi.fn());
276
+ const down = {
277
+ pointerId: 1,
278
+ clientX: 5,
279
+ clientY: 6,
280
+ currentTarget: { setPointerCapture: vi.fn() },
281
+ preventDefault: vi.fn(),
282
+ stopPropagation: vi.fn(),
283
+ };
284
+ startGesture("drag", down as unknown as React.PointerEvent<HTMLElement>);
285
+ await new Promise((resolve) => setTimeout(resolve, 50));
286
+ expect(press.start).not.toHaveBeenCalled();
287
+ press.changing = false;
288
+ return { opts, overlay };
289
+ }
290
+
291
+ it("re-finds the pressed element on the page shown and starts on its box, read over the channel", async () => {
292
+ const turned = document.getElementById("turned")!;
293
+ const stale = { ...makeSelection("turned", turned), handle: `${"f".repeat(16)}:1` };
294
+ const { overlay } = await pressDuringChange(stale);
295
+ await vi.waitFor(() => expect(press.start).toHaveBeenCalledOnce());
296
+ const [, , , started] = press.start.mock.calls[0] as [
297
+ unknown,
298
+ unknown,
299
+ unknown,
300
+ { selection: DomEditSelection; rect: OverlayRect },
301
+ ];
302
+ expect(started.selection.handle).not.toBe(stale.handle);
303
+ expect(started.selection.id).toBe("turned");
304
+ const live = orientedGroupAwareOverlayRect(overlay, liveFrame(), turned);
305
+ expect(close(started.rect)).toEqual(close(live));
306
+ expect(started.rect.angle).toBeCloseTo(36.87, 2);
307
+ });
308
+
309
+ it("drops the press when the page shown no longer has the element", async () => {
310
+ const turned = document.getElementById("turned")!;
311
+ const { opts } = await pressDuringChange(makeSelection("turned", turned));
312
+ turned.remove();
313
+ await vi.waitFor(() => expect(opts.waitingPressRef.current).toBeNull());
314
+ expect(press.start).not.toHaveBeenCalled();
315
+ });
316
+ });
@@ -0,0 +1,123 @@
1
+ /**
2
+ * Overlay rects for a preview frame Studio cannot script. The runtime measures the elements (see
3
+ * `editingGeometry.ts` in core); the arithmetic that places them on the overlay is
4
+ * `domEditOverlayGeometry`'s, so both kinds of frame draw an element's chrome in the same place.
5
+ */
6
+ import {
7
+ ROTATION_GATE_EPSILON_DEG,
8
+ type EditingElementBox,
9
+ type EditingElementGeometry,
10
+ type EditingRect,
11
+ } from "@hyperframes/core/runtime/editing-port";
12
+ import { previewGeometry } from "../../player/lib/previewElements";
13
+ import { computeOverlayRootScale, type OverlayRootScale } from "./domEditOverlayBasis";
14
+ import { hugRectForClipPath } from "./domEditOverlayCrop";
15
+ import {
16
+ frameCornersToOverlay,
17
+ frameRectToOverlay,
18
+ orientOverlayRect,
19
+ resolveDomEditGroupOverlayRect,
20
+ type OverlayRect,
21
+ } from "./domEditOverlayGeometry";
22
+
23
+ const clientRect = ({ x, y, width, height }: EditingRect) => ({ left: x, top: y, width, height });
24
+
25
+ /** `toOverlayRect`. */
26
+ const baseRect = (scale: OverlayRootScale, box: EditingElementBox) =>
27
+ frameRectToOverlay(scale, clientRect(box.rect), box.source);
28
+
29
+ /** `orientedOverlayRect`. */
30
+ function orientedRect(scale: OverlayRootScale, box: EditingElementBox): OverlayRect {
31
+ const base = baseRect(scale, box);
32
+ if (Math.abs(box.angle) < ROTATION_GATE_EPSILON_DEG) return base;
33
+ return orientOverlayRect(
34
+ base,
35
+ box.angle,
36
+ box.corners && frameCornersToOverlay(scale, box.corners),
37
+ );
38
+ }
39
+
40
+ /** `groupAwareOverlayRect`, or with `oriented`, `orientedGroupAwareOverlayRect`. */
41
+ function groupAwareRect(
42
+ scale: OverlayRootScale,
43
+ geometry: EditingElementGeometry,
44
+ oriented: boolean,
45
+ ): OverlayRect {
46
+ if (!geometry.members)
47
+ return oriented ? orientedRect(scale, geometry) : baseRect(scale, geometry);
48
+ const rect = baseRect(scale, geometry);
49
+ const members = geometry.members.map((member) =>
50
+ frameRectToOverlay(scale, clientRect(member), null),
51
+ );
52
+ const union = resolveDomEditGroupOverlayRect(members);
53
+ return union ? { ...union, editScaleX: rect.editScaleX, editScaleY: rect.editScaleY } : rect;
54
+ }
55
+
56
+ /** `orientedVisibleOverlayRect`. */
57
+ function visibleRect(scale: OverlayRootScale, box: EditingElementBox): OverlayRect {
58
+ const rect = orientedRect(scale, box);
59
+ return { ...rect, ...hugRectForClipPath(rect, box.clipPath) };
60
+ }
61
+
62
+ export interface PreviewOverlayTargets {
63
+ selection: string | null;
64
+ group: Array<string | null>;
65
+ hover: string | null;
66
+ }
67
+
68
+ export interface PreviewOverlayRects {
69
+ /** Null when the element is gone or does not render. */
70
+ selection: OverlayRect | null;
71
+ /** The selection's descendants' outlines, as `useDomEditOverlayRects` draws them. */
72
+ children: OverlayRect[];
73
+ group: Array<OverlayRect | null>;
74
+ hover: OverlayRect | null;
75
+ }
76
+
77
+ /** The rects `useDomEditOverlayRects` draws, in one geometry read; null when the frame cannot answer. */
78
+ export async function measurePreviewOverlay(
79
+ iframe: HTMLIFrameElement,
80
+ overlayEl: HTMLDivElement,
81
+ targets: PreviewOverlayTargets,
82
+ ): Promise<PreviewOverlayRects | null> {
83
+ const handles = [...new Set([targets.selection, targets.hover, ...targets.group])].filter(
84
+ (handle): handle is string => Boolean(handle),
85
+ );
86
+ // A selection with no handle is not in this frame; it draws nothing, as one not found there.
87
+ const reply = handles.length
88
+ ? await previewGeometry(iframe, handles, targets.selection ?? undefined)
89
+ : { elements: [], children: null };
90
+ const scale = reply && computeOverlayRootScale(overlayEl, iframe, null);
91
+ if (!reply || !scale) return null;
92
+ const read = (handle: string | null) =>
93
+ handle ? (reply.elements[handles.indexOf(handle)] ?? null) : null;
94
+ const selected = read(targets.selection);
95
+ const shown = selected?.visible ? selected : null;
96
+ const hovered = read(targets.hover);
97
+ return {
98
+ selection: shown && groupAwareRect(scale, shown, true),
99
+ children: shown
100
+ ? (reply.children ?? [])
101
+ .map((box) => visibleRect(scale, box))
102
+ .filter((rect) => rect.width > 2 && rect.height > 2)
103
+ : [],
104
+ group: targets.group.map((handle) => {
105
+ const member = read(handle);
106
+ const rect = member && groupAwareRect(scale, member, false);
107
+ return rect && { ...rect, ...hugRectForClipPath(rect, member.clipPath) };
108
+ }),
109
+ hover: hovered && groupAwareRect(scale, hovered, true),
110
+ };
111
+ }
112
+
113
+ /** `orientedGroupAwareOverlayRect` for each handle; null when the frame cannot answer. */
114
+ export async function measurePreviewRects(
115
+ iframe: HTMLIFrameElement,
116
+ overlayEl: HTMLDivElement,
117
+ handles: string[],
118
+ ): Promise<Array<OverlayRect | null> | null> {
119
+ const reply = await previewGeometry(iframe, handles);
120
+ const scale = reply && computeOverlayRootScale(overlayEl, iframe, null);
121
+ if (!reply || !scale) return null;
122
+ return reply.elements.map((geometry) => geometry && groupAwareRect(scale, geometry, true));
123
+ }