@hyperframes/studio 0.9.41 → 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 (54) hide show
  1. package/dist/assets/{codec-B_mpbc4w.js → codec-CKqLGYc6.js} +1 -1
  2. package/dist/assets/{hyperframes-player-Dvi3I_mv.js → hyperframes-player-DoEYqOIa.js} +1 -1
  3. package/dist/assets/{index-B5ExwsOp.js → index-DLyQ91Ek.js} +160 -160
  4. package/dist/assets/{index-BrFm9Y5k.js → index-s9iqoWnr.js} +1 -1
  5. package/dist/assets/{pausedVideoDecoder-MHQ57yZ6.js → pausedVideoDecoder-B-ReRg6i.js} +1 -1
  6. package/dist/index.d.ts +9 -1
  7. package/dist/index.html +1 -1
  8. package/dist/index.js +1057 -721
  9. package/package.json +10 -10
  10. package/src/components/editor/LayersPanel.opaque.test.ts +176 -0
  11. package/src/components/editor/LayersPanel.tsx +41 -10
  12. package/src/components/editor/domEditInlineText.ts +1 -16
  13. package/src/components/editor/domEditingTypes.ts +7 -1
  14. package/src/components/editor/gestureUndoRevert.ts +18 -1
  15. package/src/components/editor/inlineTextStyleRange.fill.test.ts +129 -0
  16. package/src/components/editor/inlineTextStyleRange.test.ts +0 -82
  17. package/src/components/editor/layerKind.ts +2 -19
  18. package/src/components/editor/layersPanelSort.ts +2 -2
  19. package/src/components/editor/manualEdits.ts +1 -0
  20. package/src/components/editor/manualEditsSnapshot.ts +10 -0
  21. package/src/components/editor/manualOffsetDrag.test.ts +1 -1
  22. package/src/components/editor/manualOffsetDrag.ts +7 -1
  23. package/src/components/editor/marqueeCommit.ts +47 -4
  24. package/src/components/editor/plainTranslate.ts +35 -16
  25. package/src/components/editor/previewDrafts.test.ts +263 -0
  26. package/src/components/editor/previewDrafts.ts +73 -0
  27. package/src/components/editor/previewLayers.test.ts +308 -0
  28. package/src/components/editor/previewLayers.ts +164 -0
  29. package/src/components/editor/previewOverlayRects.ts +27 -0
  30. package/src/components/editor/previewPointerTargets.test.ts +68 -1
  31. package/src/components/editor/previewSelection.ts +55 -11
  32. package/src/components/editor/resizeDraft.ts +38 -0
  33. package/src/components/editor/useDomEditOverlayGestures.ts +2 -2
  34. package/src/components/editor/useLayerDrag.ts +1 -1
  35. package/src/components/nle/NLEPreview.slowBoot.test.ts +0 -1
  36. package/src/hooks/domEditDeleteMembers.test.ts +7 -0
  37. package/src/hooks/domSelectionTimelineMirror.ts +3 -1
  38. package/src/hooks/timelineTimingSync.ts +4 -1
  39. package/src/hooks/useDomSelection.ts +2 -0
  40. package/src/hooks/useEditorSave.test.tsx +22 -1
  41. package/src/hooks/useEditorSave.ts +3 -1
  42. package/src/hooks/usePreviewInteraction.ts +4 -1
  43. package/src/hooks/useProjectFileWriter.ts +7 -3
  44. package/src/hooks/useTimelineSelectionPreviewSync.test.tsx +30 -0
  45. package/src/hooks/useTimelineSelectionPreviewSync.ts +7 -2
  46. package/src/player/lib/layerOrdering.ts +2 -11
  47. package/src/player/lib/previewElements.ts +31 -0
  48. package/src/test-setup.ts +2 -0
  49. package/src/utils/gsapSoftReload.ts +1 -1
  50. package/src/utils/studioFileHistory.ts +1 -1
  51. package/src/utils/studioHelpers.ts +27 -4
  52. package/src/utils/studioSaveDiagnostics.test.ts +34 -0
  53. package/src/utils/studioSaveDiagnostics.ts +35 -6
  54. package/src/utils/timelineCompositionInsert.ts +3 -1
@@ -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
+ }
@@ -4,6 +4,7 @@
4
4
  * `domEditOverlayGeometry`'s, so both kinds of frame draw an element's chrome in the same place.
5
5
  */
6
6
  import {
7
+ MAX_DESCRIBE_HANDLES,
7
8
  ROTATION_GATE_EPSILON_DEG,
8
9
  type EditingElementBox,
9
10
  type EditingElementGeometry,
@@ -121,3 +122,29 @@ export async function measurePreviewRects(
121
122
  if (!reply || !scale) return null;
122
123
  return reply.elements.map((geometry) => geometry && groupAwareRect(scale, geometry, true));
123
124
  }
125
+
126
+ /** `toVisibleOverlayRect` for each handle, with its frame rect and whether it renders. */
127
+ export async function measurePreviewVisibleRects(
128
+ iframe: HTMLIFrameElement,
129
+ overlayEl: HTMLDivElement,
130
+ handles: string[],
131
+ ) {
132
+ const measured: Array<{ rect: OverlayRect; frame: EditingRect; visible: boolean } | null> = [];
133
+ for (let at = 0; at < handles.length; at += MAX_DESCRIBE_HANDLES) {
134
+ const reply = await previewGeometry(iframe, handles.slice(at, at + MAX_DESCRIBE_HANDLES));
135
+ const scale = reply && computeOverlayRootScale(overlayEl, iframe, null);
136
+ if (!reply || !scale) return null;
137
+ for (const box of reply.elements) {
138
+ const rect = box && baseRect(scale, box);
139
+ measured.push(
140
+ box &&
141
+ rect && {
142
+ rect: { ...rect, ...hugRectForClipPath(rect, box.clipPath) },
143
+ frame: box.rect,
144
+ visible: box.visible,
145
+ },
146
+ );
147
+ }
148
+ }
149
+ return measured;
150
+ }
@@ -20,6 +20,8 @@ import type { PreviewElement } from "../../player/lib/previewElements";
20
20
  import { detachedFakeIframe } from "../../player/hooks/timelinePlayerTestHarness";
21
21
  import { installReactActEnvironment } from "../../hooks/domSelectionTestHarness";
22
22
  import { useDomSelection } from "../../hooks/useDomSelection";
23
+ import { usePreviewInteraction } from "../../hooks/usePreviewInteraction";
24
+ import { useCanvasContextMenuState } from "./useCanvasContextMenuState";
23
25
  import type { TimelineElement } from "../../player";
24
26
 
25
27
  installReactActEnvironment();
@@ -172,7 +174,10 @@ describe("canvas hit-tests on an opaque frame match same-origin ones", () => {
172
174
  });
173
175
 
174
176
  describe("useDomSelection over an opaque frame's canvas", () => {
175
- function renderSelection(iframe: HTMLIFrameElement) {
177
+ type Resolve = ReturnType<typeof useDomSelection>["resolveDomSelectionFromPreviewPoint"];
178
+ let interaction: ReturnType<typeof usePreviewInteraction> | null = null;
179
+ let menu: ReturnType<typeof useCanvasContextMenuState> | null = null;
180
+ function renderSelection(iframe: HTMLIFrameElement, wrapResolve = (resolve: Resolve) => resolve) {
176
181
  let hook: ReturnType<typeof useDomSelection> | null = null;
177
182
  function Harness() {
178
183
  hook = useDomSelection({
@@ -192,6 +197,25 @@ describe("useDomSelection over an opaque frame's canvas", () => {
192
197
  refreshKey: 0,
193
198
  rightPanelTab: "design",
194
199
  });
200
+ interaction = usePreviewInteraction({
201
+ captionEditMode: false,
202
+ compositionLoading: false,
203
+ previewIframeRef: { current: iframe },
204
+ showToast: vi.fn(),
205
+ applyDomSelection: hook.applyDomSelection,
206
+ resolveDomSelectionFromPreviewPoint: wrapResolve(hook.resolveDomSelectionFromPreviewPoint),
207
+ resolveAllDomSelectionsFromPreviewPoint: hook.resolveAllDomSelectionsFromPreviewPoint,
208
+ updateDomEditHoverSelection: hook.updateDomEditHoverSelection,
209
+ setActiveGroupElement: hook.setActiveGroupElement,
210
+ });
211
+ // The overlay's right-click path, which awaits the same lookup a pointer move runs.
212
+ menu = useCanvasContextMenuState({
213
+ selection: hook.domEditSelection,
214
+ selectionRef: { current: hook.domEditSelection },
215
+ hoverSelectionRef: { current: hook.domEditHoverSelection },
216
+ onCanvasPointerMoveRef: { current: interaction.handlePreviewCanvasPointerMove },
217
+ onSelectionChangeRef: { current: hook.applyDomSelection },
218
+ });
195
219
  return null;
196
220
  }
197
221
  const root = createRoot(document.createElement("div"));
@@ -211,6 +235,49 @@ describe("useDomSelection over an opaque frame's canvas", () => {
211
235
  expect(cycle.map((selection) => selection.id)).toEqual(["title", "mask", "block"]);
212
236
  });
213
237
 
238
+ const move = (x: number, y: number) => {
239
+ const [clientX, clientY] = client(x, y);
240
+ const event = { clientX, clientY } as React.PointerEvent<HTMLDivElement>;
241
+ return interaction!.handlePreviewCanvasPointerMove(event);
242
+ };
243
+
244
+ it.each([
245
+ ["opaque", opaqueFrame],
246
+ ["same-origin", liveFrame],
247
+ ])("drops a hover the pointer left before it resolved (%s)", async (_kind, frame) => {
248
+ const hook = renderSelection(frame());
249
+ const moved = move(60, 60);
250
+ act(() => interaction!.handlePreviewCanvasPointerLeave());
251
+ await act(() => moved);
252
+ expect(hook().domEditHoverSelection).toBeNull();
253
+ });
254
+
255
+ it("keeps the newest move's hover when an older one resolves after it", async () => {
256
+ let calls = 0;
257
+ const hook = renderSelection(opaqueFrame(), (resolve) => async (...args) => {
258
+ const delay = ++calls === 1 ? 60 : 0;
259
+ const picked = await resolve(...args);
260
+ await new Promise((done) => setTimeout(done, delay));
261
+ return picked;
262
+ });
263
+ const older = move(60, 60);
264
+ const newer = move(1050, 650);
265
+ await act(() => Promise.all([older, newer]));
266
+ expect(hook().domEditHoverSelection).toMatchObject({ id: "pair" });
267
+ });
268
+
269
+ it("opens the menu on a right-click whose lookup a pointer move overtook", async () => {
270
+ const hook = renderSelection(opaqueFrame());
271
+ const [clientX, clientY] = client(60, 60);
272
+ const rightClick = { clientX, clientY, preventDefault: () => {} };
273
+ const opened = menu!.handleContextMenu(rightClick as React.MouseEvent<HTMLDivElement>);
274
+ const jitter = move(1050, 650);
275
+ await act(() => Promise.all([opened, jitter]));
276
+ expect(hook().domEditSelection).toMatchObject({ id: "title" });
277
+ expect(menu!.contextMenu?.sel).toMatchObject({ id: "title" });
278
+ expect(hook().domEditHoverSelection).toMatchObject({ id: "pair" });
279
+ });
280
+
214
281
  it("takes a hover of the same element from a reloaded frame, whose handle is new", async () => {
215
282
  const hook = renderSelection(opaqueFrame());
216
283
  const picked = (await act(() =>