@hyperframes/studio 0.9.40 → 0.9.42

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 (93) hide show
  1. package/dist/assets/{codec-BPK_2CLL.js → codec-CKqLGYc6.js} +1 -1
  2. package/dist/assets/{hyperframes-player-D7qTTDIp.js → hyperframes-player-DoEYqOIa.js} +1 -1
  3. package/dist/assets/{index-CdA00Ybn.js → index-DLyQ91Ek.js} +166 -166
  4. package/dist/assets/{index-QS-DuxAU.js → index-s9iqoWnr.js} +1 -1
  5. package/dist/assets/{pausedVideoDecoder-Byzz98Gb.js → pausedVideoDecoder-B-ReRg6i.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 +12 -0
  11. package/dist/index.html +1 -1
  12. package/dist/index.js +1501 -818
  13. package/dist/player.js +2 -2
  14. package/package.json +10 -10
  15. package/src/components/editor/DomEditOverlay.tsx +3 -15
  16. package/src/components/editor/LayersPanel.opaque.test.ts +176 -0
  17. package/src/components/editor/LayersPanel.tsx +41 -10
  18. package/src/components/editor/clickAfterPreventedPress.test.tsx +17 -1
  19. package/src/components/editor/domEditInlineText.ts +1 -16
  20. package/src/components/editor/domEditOverlayBasis.ts +7 -2
  21. package/src/components/editor/domEditOverlayCrop.test.ts +1 -1
  22. package/src/components/editor/domEditOverlayCrop.ts +17 -3
  23. package/src/components/editor/domEditOverlayGeometry.ts +64 -139
  24. package/src/components/editor/domEditSelectionRules.ts +138 -0
  25. package/src/components/editor/domEditing.test.ts +6 -28
  26. package/src/components/editor/domEditingDom.preview.test.ts +3 -3
  27. package/src/components/editor/domEditingDom.ts +3 -76
  28. package/src/components/editor/domEditingElement.ts +19 -240
  29. package/src/components/editor/domEditingGroups.ts +0 -31
  30. package/src/components/editor/domEditingLayers.ts +95 -100
  31. package/src/components/editor/domEditingRootLayer.ts +35 -30
  32. package/src/components/editor/domEditingTypes.ts +10 -0
  33. package/src/components/editor/gestureUndoRevert.ts +18 -1
  34. package/src/components/editor/inlineTextStyleRange.fill.test.ts +129 -0
  35. package/src/components/editor/inlineTextStyleRange.test.ts +0 -82
  36. package/src/components/editor/layerKind.ts +2 -19
  37. package/src/components/editor/layersPanelSort.ts +2 -2
  38. package/src/components/editor/manualEdits.ts +1 -0
  39. package/src/components/editor/manualEditsSnapshot.ts +10 -0
  40. package/src/components/editor/manualOffsetDrag.test.ts +1 -1
  41. package/src/components/editor/manualOffsetDrag.ts +7 -1
  42. package/src/components/editor/marqueeCommit.ts +47 -4
  43. package/src/components/editor/plainTranslate.ts +35 -16
  44. package/src/components/editor/previewDrafts.test.ts +263 -0
  45. package/src/components/editor/previewDrafts.ts +73 -0
  46. package/src/components/editor/previewGestureStarts.ts +73 -10
  47. package/src/components/editor/previewLayers.test.ts +308 -0
  48. package/src/components/editor/previewLayers.ts +164 -0
  49. package/src/components/editor/previewOverlayRects.test.ts +316 -0
  50. package/src/components/editor/previewOverlayRects.ts +150 -0
  51. package/src/components/editor/previewPointerTargets.test.ts +327 -0
  52. package/src/components/editor/previewSelection.test.ts +481 -0
  53. package/src/components/editor/previewSelection.ts +516 -0
  54. package/src/components/editor/probeSourceElement.ts +19 -1
  55. package/src/components/editor/propertyPanelFill.tsx +5 -1
  56. package/src/components/editor/resizeDraft.ts +38 -0
  57. package/src/components/editor/useDomEditOverlayGestures.ts +2 -2
  58. package/src/components/editor/useDomEditOverlayRects.ts +80 -1
  59. package/src/components/editor/useInlineTextEditing.tsx +1 -6
  60. package/src/components/editor/useLayerDrag.ts +1 -1
  61. package/src/components/nle/NLEPreview.slowBoot.test.ts +0 -1
  62. package/src/hooks/domEditDeleteMembers.test.ts +7 -0
  63. package/src/hooks/domSelectionTestHarness.ts +1 -1
  64. package/src/hooks/domSelectionTimelineMirror.ts +3 -1
  65. package/src/hooks/timelineTimingSync.ts +4 -1
  66. package/src/hooks/useDomEditSession.test.tsx +23 -1
  67. package/src/hooks/useDomEditSession.ts +2 -1
  68. package/src/hooks/useDomSelection.ts +63 -47
  69. package/src/hooks/useEditorSave.test.tsx +22 -1
  70. package/src/hooks/useEditorSave.ts +3 -1
  71. package/src/hooks/useGsapSelectionHandlers.test.tsx +17 -0
  72. package/src/hooks/useGsapSelectionHandlers.ts +2 -1
  73. package/src/hooks/usePreviewInteraction.test.ts +10 -2
  74. package/src/hooks/usePreviewInteraction.ts +9 -5
  75. package/src/hooks/useProjectFileWriter.ts +7 -3
  76. package/src/hooks/useStudioSelectionPublisher.test.tsx +50 -0
  77. package/src/hooks/useStudioSelectionPublisher.ts +9 -7
  78. package/src/hooks/useTimelineSelectionPreviewSync.test.tsx +30 -0
  79. package/src/hooks/useTimelineSelectionPreviewSync.ts +7 -2
  80. package/src/player/lib/layerOrdering.ts +2 -11
  81. package/src/player/lib/previewChannel.ts +4 -3
  82. package/src/player/lib/previewElements.ts +143 -3
  83. package/src/styles/hex-baseline.json +1 -2
  84. package/src/test-setup.ts +2 -0
  85. package/src/utils/gsapSoftReload.ts +1 -1
  86. package/src/utils/studioFileHistory.ts +1 -1
  87. package/src/utils/studioHelpers.ts +27 -17
  88. package/src/utils/studioPreviewHelpers.test.ts +8 -8
  89. package/src/utils/studioPreviewHelpers.ts +26 -242
  90. package/src/utils/studioSaveDiagnostics.test.ts +34 -0
  91. package/src/utils/studioSaveDiagnostics.ts +35 -6
  92. package/src/utils/timelineCompositionInsert.ts +3 -1
  93. package/src/components/editor/domEditOverlayTransform.ts +0 -126
@@ -0,0 +1,308 @@
1
+ // @vitest-environment happy-dom
2
+
3
+ import { act, createElement, useRef } from "react";
4
+ import { MAX_QUERY_MATCHES } from "@hyperframes/core/runtime/editing-port";
5
+ import { createRoot } from "react-dom/client";
6
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
7
+ // The frame's real runtime agent, serving the opaque frame from this document.
8
+ import { installEditingAgent } from "../../../../core/src/runtime/editingAgent";
9
+ import { installReactActEnvironment } from "../../hooks/domSelectionTestHarness";
10
+ import { watchPreviewFrame } from "../../player/lib/bridgePlayback";
11
+ import { getElementZIndex } from "../../player/lib/layerOrdering";
12
+ import { detachedFakeIframe } from "../../player/hooks/timelinePlayerTestHarness";
13
+ import { previewLayers } from "../../player/lib/previewElements";
14
+ import type { TimelineElement } from "../../player";
15
+ import { findMatchingTimelineElementId } from "../../utils/studioHelpers";
16
+ import { collectDomEditLayerItems, type DomEditSelection } from "./domEditing";
17
+ import type { DomEditLayerItem } from "./domEditingTypes";
18
+ import { layerKindOf } from "./layerKind";
19
+ import { sortLayersByZIndex } from "./layersPanelSort";
20
+ import { useMarqueeGestures } from "./marqueeCommit";
21
+ import { collectPreviewLayerItems, layerDataAttribute } from "./previewLayers";
22
+ import { findPreviewElementForSelection, resolvePreviewSelection } from "./previewSelection";
23
+ import { isLayerDraggable } from "./useLayerDrag";
24
+
25
+ installReactActEnvironment();
26
+ HTMLElement.prototype.setPointerCapture ??= () => {};
27
+ HTMLElement.prototype.releasePointerCapture ??= () => {};
28
+
29
+ const FIXTURE = `
30
+ <main data-composition-id="main" data-width="1920" data-height="1080">
31
+ <div id="card" class="clip" style="z-index: 4"><h1 id="title">Launch <b>day</b></h1><img id="pic"></div>
32
+ <div id="pair" data-hf-group="pair"><i id="a"></i><i data-hf-id="b-hf"></i></div>
33
+ <div id="parked" data-hf-clip-hidden style="visibility: hidden"><p id="inside" style="visibility: visible">x</p></div>
34
+ <div id="cropped" style="clip-path: inset(0 0 0 200px); background: red"></div>
35
+ <div id="host" data-composition-src="compositions/a.html"><span id="in-host">in</span></div>
36
+ <i class="tile"></i><i class="tile" id="t2"></i>
37
+ <div id="backdrop" style="background: #111"></div>
38
+ <div id="faded" style="opacity: 0"></div>
39
+ </main>`;
40
+ const RECTS: Record<string, number[]> = {
41
+ card: [100, 100, 400, 300],
42
+ title: [110, 110, 300, 60],
43
+ pic: [110, 200, 200, 150],
44
+ pair: [600, 100, 10, 10],
45
+ a: [600, 100, 80, 80],
46
+ "b-hf": [700, 140, 80, 80],
47
+ host: [900, 500, 300, 200],
48
+ "in-host": [910, 510, 100, 20],
49
+ t2: [1300, 800, 50, 50],
50
+ backdrop: [0, 0, 1920, 1080],
51
+ faded: [120, 120, 50, 50],
52
+ cropped: [700, 300, 400, 60],
53
+ };
54
+ const FRAME = [100, 50, 960, 540];
55
+
56
+ function sized(element: Element, [x, y, width, height]: number[]) {
57
+ element.getBoundingClientRect = () =>
58
+ ({ x, y, width, height, left: x, top: y, right: x + width, bottom: y + height }) as DOMRect;
59
+ Object.defineProperty(element, "offsetWidth", { value: width, configurable: true });
60
+ Object.defineProperty(element, "offsetHeight", { value: height, configurable: true });
61
+ return element;
62
+ }
63
+ const byName = (name: string) =>
64
+ document.getElementById(name) ?? document.querySelector(`[data-hf-id="${name}"]`);
65
+
66
+ const closers: Array<() => void> = [];
67
+ beforeEach(() => {
68
+ document.body.innerHTML = FIXTURE;
69
+ sized(document.querySelector("main")!, [0, 0, 1920, 1080]);
70
+ for (const [name, rect] of Object.entries(RECTS)) sized(byName(name)!, rect);
71
+ });
72
+ afterEach(() => {
73
+ closers.splice(0).forEach((close) => close());
74
+ document.body.innerHTML = "";
75
+ });
76
+
77
+ /** A frame Studio scripts directly; its document is this one. */
78
+ function liveFrame() {
79
+ const iframe = detachedFakeIframe(window as unknown as Record<string, unknown>);
80
+ Object.defineProperty(iframe, "contentDocument", { value: document });
81
+ return sized(iframe, FRAME) as HTMLIFrameElement;
82
+ }
83
+
84
+ /** A frame Studio cannot read, whose runtime is the real editing agent over this document. */
85
+ function opaqueFrame() {
86
+ const frame: Record<string, unknown> = {};
87
+ const iframe = sized(detachedFakeIframe(frame), FRAME) as HTMLIFrameElement;
88
+ Object.defineProperty(iframe, "contentDocument", { value: null });
89
+ Object.defineProperty(frame, "document", {
90
+ get: () => {
91
+ throw new DOMException("blocked", "SecurityError");
92
+ },
93
+ });
94
+ const parent = {
95
+ postMessage: (data: unknown, _origin: string, ports: MessagePort[]) =>
96
+ window.dispatchEvent(
97
+ new MessageEvent("message", { source: frame as unknown as Window, data, ports }),
98
+ ),
99
+ } as unknown as Window;
100
+ const agent = installEditingAgent(parent);
101
+ frame.postMessage = (data: unknown) =>
102
+ window.dispatchEvent(new MessageEvent("message", { source: parent, data }));
103
+ closers.push(() => agent.dispose());
104
+ watchPreviewFrame(iframe);
105
+ window.dispatchEvent(
106
+ new MessageEvent("message", {
107
+ source: frame as unknown as Window,
108
+ origin: window.location.origin,
109
+ data: { source: "hf-preview", type: "stage-size", width: 1920, height: 1080 },
110
+ }),
111
+ );
112
+ return iframe;
113
+ }
114
+
115
+ const CONTEXT = { activeCompositionPath: "index.html", isMasterView: true };
116
+ const row = ({ element: _e, preview: _p, ...rest }: DomEditLayerItem) => rest;
117
+
118
+ describe("Layers rows on an opaque frame match same-origin ones", () => {
119
+ it("lists the same rows, with the kind and z-index each row shows", async () => {
120
+ const live = collectDomEditLayerItems(document.querySelector("main"), CONTEXT);
121
+ const remote = (await collectPreviewLayerItems(opaqueFrame(), CONTEXT))!.items;
122
+ expect(remote.map(row)).toEqual(live.map(row));
123
+ expect(remote.map((item) => item.preview!.kind)).toEqual(
124
+ live.map((item) => layerKindOf(item.element, item.childCount > 0)),
125
+ );
126
+ expect(remote.map((item) => item.preview!.zIndex)).toEqual(
127
+ live.map((item) => getElementZIndex(item.element)),
128
+ );
129
+ expect(remote.map((item) => item.label)).toContain("pair");
130
+ expect(sortLayersByZIndex(remote)[0]!.id).toBe("card");
131
+ });
132
+
133
+ it("scopes to a drilled-into group's stand-in, and stops at the row budget", async () => {
134
+ const opaque = opaqueFrame();
135
+ const found = await findPreviewElementForSelection(opaque, { id: "pair" }, "index.html");
136
+ const group = (await resolvePreviewSelection(opaque, found!, {
137
+ ...CONTEXT,
138
+ exactTarget: true,
139
+ }))!;
140
+ const scoped = { ...CONTEXT, activeGroupElement: group.element };
141
+ const live = collectDomEditLayerItems(document.querySelector("main"), {
142
+ ...CONTEXT,
143
+ activeGroupElement: document.getElementById("pair"),
144
+ });
145
+ const inGroup = (await collectPreviewLayerItems(opaque, scoped))!;
146
+ expect(inGroup.items.map(row)).toEqual(live.map(row));
147
+ expect(inGroup.missingGroup).toBe(false);
148
+ expect((await collectPreviewLayerItems(opaque, CONTEXT, 2))!.items).toHaveLength(2);
149
+
150
+ // A reload takes the group with it: the frame lists nothing and says so, not the whole page.
151
+ document.getElementById("pair")!.remove();
152
+ expect(await collectPreviewLayerItems(opaque, scoped)).toEqual({
153
+ items: [],
154
+ missingGroup: true,
155
+ });
156
+ });
157
+
158
+ it("reads a row's attributes off its description, and does not offer it for reordering", async () => {
159
+ const remote = (await collectPreviewLayerItems(opaqueFrame(), CONTEXT))!.items;
160
+ const pair = remote.find((item) => item.id === "pair")!;
161
+ expect(layerDataAttribute(pair, "data-hf-group")).toBe("pair");
162
+ expect(pair.element.hasAttribute("data-hf-group")).toBe(false);
163
+ const host = remote.find((item) => item.id === "host")!;
164
+ expect(layerDataAttribute(host, "data-composition-src")).toBe("compositions/a.html");
165
+ expect(isLayerDraggable(pair)).toBe(false);
166
+ });
167
+ });
168
+
169
+ describe("a marquee on an opaque frame matches a same-origin one", () => {
170
+ function band(iframe: HTMLIFrameElement) {
171
+ const picked: { selections: DomEditSelection[] | null; candidates: number } = {
172
+ selections: null,
173
+ candidates: 0,
174
+ };
175
+ function Surface() {
176
+ const overlayRef = useRef<HTMLDivElement>(null);
177
+ const marquee = useMarqueeGestures({
178
+ iframeRef: { current: iframe },
179
+ overlayRef,
180
+ activeCompositionPathRef: { current: "index.html" },
181
+ onMarqueeSelectRef: { current: (selections) => void (picked.selections = selections) },
182
+ });
183
+ picked.candidates = marquee.candidateRects.length;
184
+ return createElement("div", {
185
+ ref: (el: HTMLDivElement | null) => {
186
+ if (el) overlayRef.current = sized(el, [20, 10, 1200, 800]) as HTMLDivElement;
187
+ },
188
+ "data-band": "",
189
+ onPointerDown: marquee.begin,
190
+ onPointerMove: marquee.onPointerMove,
191
+ onPointerUp: marquee.onPointerUp,
192
+ });
193
+ }
194
+ const host = document.createElement("div");
195
+ document.body.append(host);
196
+ const root = createRoot(host);
197
+ act(() => root.render(createElement(Surface)));
198
+ closers.push(() => act(() => root.unmount()));
199
+ const surface = host.querySelector("[data-band]")!;
200
+ const pointer = (type: string, x: number, y: number) =>
201
+ act(() => {
202
+ surface.dispatchEvent(
203
+ new PointerEvent(type, {
204
+ bubbles: true,
205
+ buttons: type === "pointerup" ? 0 : 1,
206
+ pointerId: 1,
207
+ clientX: x,
208
+ clientY: y,
209
+ }),
210
+ );
211
+ });
212
+ return { picked, pointer };
213
+ }
214
+
215
+ const ids = async (iframe: HTMLIFrameElement) => {
216
+ const { picked, pointer } = band(iframe);
217
+ pointer("pointerdown", 130, 70);
218
+ pointer("pointermove", 500, 200);
219
+ pointer("pointermove", 520, 220);
220
+ await vi.waitFor(() => expect(picked.candidates).toBeGreaterThan(0));
221
+ const candidates = picked.candidates;
222
+ pointer("pointerup", 520, 220);
223
+ await vi.waitFor(() => expect(picked.selections).not.toBeNull());
224
+ return { candidates, ids: picked.selections!.map((s) => s.id ?? s.hfId), picked };
225
+ };
226
+
227
+ it("outlines and selects the same elements: visible, not full-bleed, inside the band", async () => {
228
+ const live = await ids(liveFrame());
229
+ const remote = await ids(opaqueFrame());
230
+ expect(remote.ids).toEqual(live.ids);
231
+ expect(remote.candidates).toBe(live.candidates);
232
+ // A group's members select as the group, on both kinds of frame.
233
+ expect(remote.ids).toEqual(["card", "title", "pic", "pair", "pair"]);
234
+ expect(remote.picked.selections!.every((selection) => selection.handle)).toBe(true);
235
+ });
236
+
237
+ it("selects layers past the cap on one layers reply, as same-origin does", async () => {
238
+ // Unmeasured fillers first, so every element in the band sits past the first reply.
239
+ const fillers = Array.from({ length: MAX_QUERY_MATCHES }, (_, i) => `<i id="f${i}"></i>`);
240
+ document.querySelector("main")!.insertAdjacentHTML("afterbegin", fillers.join(""));
241
+ expect((await previewLayers(opaqueFrame()))!.truncated).toBe(true);
242
+ const remote = await ids(opaqueFrame());
243
+ expect(remote.ids).toEqual(["card", "title", "pic", "pair", "pair"]);
244
+ }, 20_000);
245
+ });
246
+
247
+ it("maps an opaque selection to a timeline clip the frame knows only by its clip id", () => {
248
+ const clip = {
249
+ id: "card",
250
+ key: "index.html:card:0",
251
+ sourceFile: "index.html",
252
+ } as TimelineElement;
253
+ const selection = {
254
+ id: "card",
255
+ sourceFile: "index.html",
256
+ isCompositionHost: false,
257
+ handle: "0123456789abcdef:1",
258
+ };
259
+ expect(findMatchingTimelineElementId(selection, [clip], "index.html")).toBe("index.html:card:0");
260
+ const unhandled = { ...selection, handle: undefined };
261
+ expect(findMatchingTimelineElementId(unhandled, [clip], "index.html")).toBeNull();
262
+ const hydrated = { ...clip, domId: "other" } as TimelineElement;
263
+ expect(findMatchingTimelineElementId(selection, [hydrated], "index.html")).toBeNull();
264
+ // Without a scope (delete, group) the row is not this selection: it has no patch target.
265
+ expect(findMatchingTimelineElementId(selection, [clip])).toBeNull();
266
+
267
+ // A row naming no file is in the open composition, as the forward lookup reads it.
268
+ const bare = { id: "card", key: "compositions/a.html:card:0" } as TimelineElement;
269
+ const inSub = { ...selection, sourceFile: "compositions/a.html" };
270
+ expect(findMatchingTimelineElementId(inSub, [bare], "compositions/a.html")).toBe(bare.key);
271
+ expect(findMatchingTimelineElementId(inSub, [bare], "index.html")).not.toBe(bare.key);
272
+ });
273
+
274
+ it("refuses a layers reply that names an unknown kind", async () => {
275
+ const frame: Record<string, unknown> = {};
276
+ const iframe = detachedFakeIframe(frame);
277
+ Object.defineProperty(iframe, "contentDocument", { value: null });
278
+ Object.defineProperty(frame, "document", {
279
+ get: () => {
280
+ throw new DOMException("blocked", "SecurityError");
281
+ },
282
+ });
283
+ const { port1, port2 } = new MessageChannel();
284
+ closers.push(() => port1.close());
285
+ const layer = {
286
+ handle: "0123456789abcdef:1",
287
+ depth: 0,
288
+ childCount: 0,
289
+ zIndex: 0,
290
+ kind: "plugin",
291
+ };
292
+ port1.onmessage = ({ data }) =>
293
+ port1.postMessage({
294
+ v: 1,
295
+ id: data.id,
296
+ ok: true,
297
+ result: { layers: [layer], truncated: false, missingGroup: false },
298
+ });
299
+ frame.postMessage = () =>
300
+ window.dispatchEvent(
301
+ new MessageEvent("message", {
302
+ source: frame as unknown as Window,
303
+ data: { source: "hf-preview", type: "editing-port", v: 1 },
304
+ ports: [port2],
305
+ }),
306
+ );
307
+ expect(await previewLayers(iframe)).toBeNull();
308
+ });
@@ -0,0 +1,164 @@
1
+ /** Layers rows and marquee candidates for a preview frame Studio cannot script, from its runtime. */
2
+ import { MAX_DESCRIBE_HANDLES, type EditingLayer } from "@hyperframes/core/runtime/editing-port";
3
+ import {
4
+ describePreviewElements,
5
+ previewLayers,
6
+ queryPreviewElements,
7
+ type PreviewElement,
8
+ } from "../../player/lib/previewElements";
9
+ import type { Rect } from "../../utils/marqueeGeometry";
10
+ import { coversComposition } from "../../utils/studioPreviewHelpers";
11
+ import { buildElementLabel, getDataAttributes } from "./domEditingDom";
12
+ import { getDomEditLayerKey } from "./domEditingLayers";
13
+ import type { DomEditContextOptions, DomEditLayerItem, DomEditSelection } from "./domEditingTypes";
14
+ import { measurePreviewVisibleRects } from "./previewOverlayRects";
15
+ import {
16
+ previewIdentity,
17
+ readable,
18
+ resolvePreviewSelection,
19
+ standInFor,
20
+ standInHandle,
21
+ } from "./previewSelection";
22
+
23
+ async function describeAll(iframe: HTMLIFrameElement, handles: readonly string[]) {
24
+ const described: Array<PreviewElement | null> = [];
25
+ for (let at = 0; at < handles.length; at += MAX_DESCRIBE_HANDLES) {
26
+ const part = await describePreviewElements(
27
+ iframe,
28
+ handles.slice(at, at + MAX_DESCRIBE_HANDLES),
29
+ );
30
+ if (!part) return null;
31
+ described.push(...part);
32
+ }
33
+ return described;
34
+ }
35
+
36
+ function layerItem(
37
+ iframe: HTMLIFrameElement,
38
+ layer: EditingLayer,
39
+ element: PreviewElement,
40
+ path: string | null,
41
+ ): DomEditLayerItem {
42
+ const probe = readable(element);
43
+ const identity = previewIdentity(element, path);
44
+ return {
45
+ ...identity,
46
+ key: getDomEditLayerKey(identity),
47
+ element: standInFor(iframe, element),
48
+ label: buildElementLabel(probe),
49
+ tagName: element.tag,
50
+ depth: layer.depth,
51
+ childCount: layer.childCount,
52
+ preview: {
53
+ handle: layer.handle,
54
+ zIndex: layer.zIndex,
55
+ kind: layer.kind,
56
+ dataAttributes: getDataAttributes(probe),
57
+ },
58
+ };
59
+ }
60
+
61
+ /**
62
+ * `collectDomEditLayerItems` over the frame's runtime; null when it cannot answer. `missingGroup`
63
+ * says the drilled-into group is gone from the frame, as after a reload, and lists nothing.
64
+ */
65
+ export async function collectPreviewLayerItems(
66
+ iframe: HTMLIFrameElement,
67
+ options: Pick<DomEditContextOptions, "activeCompositionPath" | "activeGroupElement">,
68
+ maxItems = Number.POSITIVE_INFINITY,
69
+ ): Promise<{ items: DomEditLayerItem[]; missingGroup: boolean } | null> {
70
+ const group = options.activeGroupElement && standInHandle(options.activeGroupElement);
71
+ const reply = await previewLayers(iframe, group ?? undefined);
72
+ const layers = reply?.layers.slice(0, maxItems) ?? [];
73
+ const described =
74
+ reply &&
75
+ (await describeAll(
76
+ iframe,
77
+ layers.map((layer) => layer.handle),
78
+ ));
79
+ if (!reply || !described) return null;
80
+ const items = layers.flatMap((layer, index) => {
81
+ const element = described[index];
82
+ return element ? [layerItem(iframe, layer, element, options.activeCompositionPath)] : [];
83
+ });
84
+ return { items, missingGroup: reply.missingGroup };
85
+ }
86
+
87
+ // A bound on the pages one read follows, so a frame that never stops paging cannot hold it open.
88
+ const MAX_LAYER_PAGES = 64;
89
+
90
+ /** Every layer the frame lists, page by page past its per-reply cap; null when it cannot answer. */
91
+ async function allPreviewLayers(iframe: HTMLIFrameElement): Promise<EditingLayer[] | null> {
92
+ const layers: EditingLayer[] = [];
93
+ for (let page = 0; page < MAX_LAYER_PAGES; page += 1) {
94
+ const reply = await previewLayers(iframe, undefined, layers.length);
95
+ if (!reply) return null;
96
+ layers.push(...reply.layers);
97
+ if (!reply.truncated || !reply.layers.length) break;
98
+ }
99
+ return layers;
100
+ }
101
+
102
+ /** A layer's `data-` attribute; a stand-in's comes from its description. */
103
+ export function layerDataAttribute(layer: DomEditLayerItem, name: `data-${string}`) {
104
+ if (!layer.preview) return layer.element.getAttribute(name);
105
+ return layer.preview.dataAttributes[name.slice("data-".length)] ?? null;
106
+ }
107
+
108
+ async function compositionViewport(iframe: HTMLIFrameElement) {
109
+ const roots = await queryPreviewElements(iframe, "[data-composition-id]");
110
+ const root = roots?.matches[0] ?? (await queryPreviewElements(iframe, "html"))?.matches[0];
111
+ const [described] = (root && (await describePreviewElements(iframe, [root.handle]))) || [];
112
+ if (!described) return null;
113
+ const declared = (name: string) =>
114
+ Number.parseFloat(described.attributes.find(([key]) => key === name)?.[1] ?? "");
115
+ const [width, height] = [declared("data-width"), declared("data-height")];
116
+ return {
117
+ width: width > 0 ? width : described.rect.width || 1,
118
+ height: height > 0 ? height : described.rect.height || 1,
119
+ };
120
+ }
121
+
122
+ export interface PreviewMarqueeHit {
123
+ element: HTMLElement;
124
+ rect: Rect;
125
+ description: PreviewElement;
126
+ }
127
+
128
+ /** `collectMarqueeCandidates` over the frame's runtime: every visible, not full-bleed layer. */
129
+ export async function collectPreviewMarqueeCandidates(
130
+ iframe: HTMLIFrameElement,
131
+ overlayEl: HTMLDivElement,
132
+ ): Promise<PreviewMarqueeHit[]> {
133
+ // Hit-testing takes the whole document, as same-origin does: a layer past one reply's cap still selects.
134
+ const layers = await allPreviewLayers(iframe);
135
+ const handles = layers?.map((layer) => layer.handle) ?? [];
136
+ const viewport = handles.length ? await compositionViewport(iframe) : null;
137
+ const described = viewport && (await describeAll(iframe, handles));
138
+ const measured = described && (await measurePreviewVisibleRects(iframe, overlayEl, handles));
139
+ if (!described || !measured || !viewport) return [];
140
+ return handles.flatMap((_, index) => {
141
+ const description = described[index];
142
+ const box = measured[index];
143
+ if (!description || !box?.visible || coversComposition(box.frame, viewport)) return [];
144
+ const { left, top, width, height } = box.rect;
145
+ const element = standInFor(iframe, description);
146
+ return [{ element, rect: { left, top, width, height }, description }];
147
+ });
148
+ }
149
+
150
+ /** Marquee picks as selections, read like the frame's own descriptions. */
151
+ export async function resolvePreviewMarqueeHits(
152
+ iframe: HTMLIFrameElement,
153
+ hits: PreviewMarqueeHit[],
154
+ activeCompositionPath: string,
155
+ ): Promise<DomEditSelection[]> {
156
+ const isMasterView = !activeCompositionPath || activeCompositionPath === "index.html";
157
+ const options = { activeCompositionPath, isMasterView, skipSourceProbe: true };
158
+ const picks: DomEditSelection[] = [];
159
+ for (const hit of hits) {
160
+ const selection = await resolvePreviewSelection(iframe, hit.description, options);
161
+ if (selection) picks.push(selection);
162
+ }
163
+ return picks;
164
+ }