@hyperframes/studio 0.9.41 → 0.9.43

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 (61) hide show
  1. package/dist/assets/{codec-B_mpbc4w.js → codec-DHChmhF2.js} +1 -1
  2. package/dist/assets/{hyperframes-player-Dvi3I_mv.js → hyperframes-player-CdbweVDs.js} +1 -1
  3. package/dist/assets/{index-B5ExwsOp.js → index-CjWDrWWG.js} +166 -166
  4. package/dist/assets/{index-BrFm9Y5k.js → index-l-ti2dV8.js} +1 -1
  5. package/dist/assets/{pausedVideoDecoder-MHQ57yZ6.js → pausedVideoDecoder-xqOQ0ciK.js} +1 -1
  6. package/dist/index.d.ts +9 -1
  7. package/dist/index.html +1 -1
  8. package/dist/index.js +1380 -989
  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/domEditOverlayGeometry.ts +2 -0
  14. package/src/components/editor/domEditOverlayStartGesture.ts +31 -7
  15. package/src/components/editor/domEditingTypes.ts +7 -1
  16. package/src/components/editor/gestureUndoRevert.ts +18 -1
  17. package/src/components/editor/inlineTextStyleRange.fill.test.ts +129 -0
  18. package/src/components/editor/inlineTextStyleRange.test.ts +0 -82
  19. package/src/components/editor/layerKind.ts +2 -19
  20. package/src/components/editor/layersPanelSort.ts +2 -2
  21. package/src/components/editor/manualEdits.ts +1 -0
  22. package/src/components/editor/manualEditsSnapshot.ts +10 -0
  23. package/src/components/editor/manualOffsetDrag.test.ts +1 -1
  24. package/src/components/editor/manualOffsetDrag.ts +17 -6
  25. package/src/components/editor/marqueeCommit.ts +47 -4
  26. package/src/components/editor/plainTranslate.ts +37 -16
  27. package/src/components/editor/previewDrafts.test.ts +263 -0
  28. package/src/components/editor/previewDrafts.ts +73 -0
  29. package/src/components/editor/previewGestureStartRead.test.ts +279 -0
  30. package/src/components/editor/previewGestureStartRead.ts +67 -0
  31. package/src/components/editor/previewGestureStarts.ts +21 -15
  32. package/src/components/editor/previewLayers.test.ts +308 -0
  33. package/src/components/editor/previewLayers.ts +164 -0
  34. package/src/components/editor/previewOverlayRects.test.ts +62 -0
  35. package/src/components/editor/previewOverlayRects.ts +23 -8
  36. package/src/components/editor/previewPointerTargets.test.ts +68 -1
  37. package/src/components/editor/previewSelection.ts +55 -11
  38. package/src/components/editor/resizeDraft.ts +38 -0
  39. package/src/components/editor/snapTargetCollection.ts +26 -62
  40. package/src/components/editor/useDomEditOverlayGestures.ts +2 -2
  41. package/src/components/editor/useLayerDrag.ts +1 -1
  42. package/src/components/nle/NLEPreview.slowBoot.test.ts +0 -1
  43. package/src/hooks/domEditDeleteMembers.test.ts +7 -0
  44. package/src/hooks/domSelectionTimelineMirror.ts +3 -1
  45. package/src/hooks/timelineTimingSync.ts +4 -1
  46. package/src/hooks/useDomSelection.ts +2 -0
  47. package/src/hooks/useEditorSave.test.tsx +22 -1
  48. package/src/hooks/useEditorSave.ts +3 -1
  49. package/src/hooks/usePreviewInteraction.ts +4 -1
  50. package/src/hooks/useProjectFileWriter.ts +7 -3
  51. package/src/hooks/useTimelineSelectionPreviewSync.test.tsx +30 -0
  52. package/src/hooks/useTimelineSelectionPreviewSync.ts +7 -2
  53. package/src/player/lib/layerOrdering.ts +2 -11
  54. package/src/player/lib/previewElements.ts +68 -0
  55. package/src/test-setup.ts +2 -0
  56. package/src/utils/gsapSoftReload.ts +1 -1
  57. package/src/utils/studioFileHistory.ts +1 -1
  58. package/src/utils/studioHelpers.ts +27 -4
  59. package/src/utils/studioSaveDiagnostics.test.ts +34 -0
  60. package/src/utils/studioSaveDiagnostics.ts +35 -6
  61. package/src/utils/timelineCompositionInsert.ts +3 -1
@@ -23,14 +23,16 @@ import { playheadMoment } from "../../hooks/editMoment";
23
23
  import type { EditMoment } from "./manualEditsTypes";
24
24
  import { previewFrameOpaque } from "../../player/lib/previewChannel";
25
25
  import { findPreviewElementForSelection } from "./previewSelection";
26
- import { measurePreviewRects } from "./previewOverlayRects";
26
+ import { measurePreviewGestureStart, type PreviewGestureStart } from "./previewGestureStartRead";
27
+ import type { SnapContext } from "./snapTargetCollection";
27
28
 
28
- type Shown = { selection: DomEditSelection; rect: OverlayRect };
29
+ type Shown = { selection: DomEditSelection; rect: OverlayRect; start?: PreviewGestureStart };
30
+ type OverChannel = { shown: Shown[]; snap: SnapContext };
29
31
  type ShownStart = (
30
32
  e: React.PointerEvent<HTMLElement>,
31
33
  at: EditMoment,
32
34
  waited: boolean,
33
- overChannel?: Shown[],
35
+ overChannel?: OverChannel,
34
36
  ) => boolean;
35
37
 
36
38
  const MAX_SETTLED_WAIT_FRAMES = 60;
@@ -139,11 +141,11 @@ export function createPreviewGestureStarts(
139
141
  };
140
142
  };
141
143
 
142
- /** An opaque frame's `selections` on the page it shows now, read over its channel; null when any is gone. */
144
+ /** An opaque frame's `selections` on the page it shows now and what a gesture on them starts from. */
143
145
  const shownOverChannel = async (
144
146
  iframe: HTMLIFrameElement,
145
147
  selections: DomEditSelection[],
146
- ): Promise<Shown[] | null> => {
148
+ ): Promise<OverChannel | null> => {
147
149
  const overlay = opts.overlayRef.current;
148
150
  const path = opts.activeCompositionPathRef.current;
149
151
  const found = await Promise.all(
@@ -151,12 +153,12 @@ export function createPreviewGestureStarts(
151
153
  );
152
154
  const handles = found.map((element) => element?.handle);
153
155
  if (!overlay || !handles.length || !handles.every(Boolean)) return null;
154
- const rects = await measurePreviewRects(iframe, overlay, handles as string[]);
156
+ const read = await measurePreviewGestureStart(iframe, overlay, handles as string[]);
155
157
  const shown = selections.flatMap((selection, i) => {
156
- const rect = rects?.[i];
157
- return rect ? [{ selection: { ...selection, handle: handles[i] }, rect }] : [];
158
+ const element = read?.elements[i];
159
+ return element ? [{ selection: { ...selection, handle: handles[i] }, ...element }] : [];
158
160
  });
159
- return shown.length === selections.length ? shown : null;
161
+ return read && shown.length === selections.length ? { shown, snap: read.snap } : null;
160
162
  };
161
163
 
162
164
  // A press on the page a reload is replacing would edit it by its old rules: it starts on the new page instead.
@@ -168,7 +170,10 @@ export function createPreviewGestureStarts(
168
170
  targets: () => DomEditSelection[] = () => [],
169
171
  ): boolean => {
170
172
  const at = playheadMoment();
171
- if (!isPreviewChanging() && !opts.waitingPressRef.current) return start(e, at, false);
173
+ const iframe = opts.iframeRef.current;
174
+ const channel = iframe && previewFrameOpaque(iframe) ? iframe : null;
175
+ if (!channel && !isPreviewChanging() && !opts.waitingPressRef.current)
176
+ return start(e, at, false);
172
177
  e.preventDefault();
173
178
  e.stopPropagation();
174
179
  e.currentTarget.setPointerCapture(e.pointerId);
@@ -203,9 +208,7 @@ export function createPreviewGestureStarts(
203
208
  return elements.length;
204
209
  };
205
210
  // An opaque frame's pressed elements are found over its channel, once the preview settles.
206
- const iframe = opts.iframeRef.current;
207
- const channel = iframe && previewFrameOpaque(iframe) ? iframe : null;
208
- let overChannel: Shown[] | null | undefined;
211
+ let overChannel: OverChannel | null | undefined;
209
212
  let asked = false;
210
213
  const channelStep = (frame: HTMLIFrameElement): "wait" | "lost" | "run" => {
211
214
  const overdue = performance.now() - since > PRESS_WAIT_MAX_MS;
@@ -275,11 +278,12 @@ export function createPreviewGestureStarts(
275
278
  (pressed, at, waited, overChannel) => {
276
279
  if (!waited) return _startGroupDrag(pressed, opts, at);
277
280
  if (overChannel) {
278
- const measured = items.map((item, i) => ({ ...item, ...overChannel[i]! }));
281
+ const measured = items.map((item, i) => ({ ...item, ...overChannel.shown[i]! }));
279
282
  return _startGroupDrag(
280
283
  pressed,
281
284
  { ...opts, groupOverlayItemsRef: { current: measured } },
282
285
  at,
286
+ overChannel.snap,
283
287
  );
284
288
  }
285
289
  const overlay = opts.overlayRef.current;
@@ -335,7 +339,7 @@ export function createPreviewGestureStarts(
335
339
  },
336
340
  (pressed, at, waited, overChannel) => {
337
341
  if (!waited) return _startGesture(kind, pressed, opts, { ...options, at });
338
- const shown = overChannel ? overChannel[0] : shownOnPage(target);
342
+ const shown = overChannel ? overChannel.shown[0] : shownOnPage(target);
339
343
  return (
340
344
  !!shown &&
341
345
  _startGesture(kind, pressed, opts, {
@@ -344,6 +348,8 @@ export function createPreviewGestureStarts(
344
348
  resizeHandle: options?.resizeHandle,
345
349
  resizeCorner: options?.resizeCorner,
346
350
  at,
351
+ start: shown.start,
352
+ snap: overChannel?.snap,
347
353
  })
348
354
  );
349
355
  },
@@ -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
+ }
@@ -314,3 +314,65 @@ describe("a press held across a preview change on an opaque frame", () => {
314
314
  expect(press.start).not.toHaveBeenCalled();
315
315
  });
316
316
  });
317
+
318
+ describe("a press on a settled preview", () => {
319
+ function pressOn(iframe: HTMLIFrameElement, selection: DomEditSelection) {
320
+ press.changing = false;
321
+ press.start.mockClear();
322
+ const opts = {
323
+ waitingPressRef: { current: null },
324
+ boxRef: { current: null },
325
+ rafPausedRef: { current: false },
326
+ iframeRef: { current: iframe },
327
+ overlayRef: { current: sized(document.createElement("div"), [20, 10, 1200, 800]) },
328
+ activeCompositionPathRef: { current: "index.html" },
329
+ selectionRef: { current: selection },
330
+ overlayRectRef: { current: null },
331
+ setOverlayRect: vi.fn(),
332
+ } as unknown as UseDomEditOverlayGesturesOptions;
333
+ const { startGesture } = createPreviewGestureStarts(opts, vi.fn(), vi.fn());
334
+ const down = {
335
+ pointerId: 1,
336
+ clientX: 5,
337
+ clientY: 6,
338
+ currentTarget: { setPointerCapture: vi.fn() },
339
+ preventDefault: vi.fn(),
340
+ stopPropagation: vi.fn(),
341
+ };
342
+ startGesture("drag", down as unknown as React.PointerEvent<HTMLElement>);
343
+ return opts;
344
+ }
345
+
346
+ it("on a scriptable frame starts the gesture at once", () => {
347
+ pressOn(liveFrame(), makeSelection("turned", document.getElementById("turned")!));
348
+ expect(press.start).toHaveBeenCalledOnce();
349
+ // Synchronously, with nothing read over a channel.
350
+ expect(press.start.mock.calls[0]![3]).toEqual({ at: expect.any(Object) });
351
+ });
352
+
353
+ it("on an opaque frame waits for what the gesture starts from, read in one request", async () => {
354
+ const { port1 } = new MessageChannel();
355
+ const send = vi.spyOn(Object.getPrototypeOf(port1) as MessagePort, "postMessage");
356
+ const kinds = () => send.mock.calls.map(([message]) => (message as { kind?: string }).kind);
357
+ const iframe = opaqueFrame();
358
+ const handle = (await queryPreviewElements(iframe, "#turned"))!.matches[0]!.handle;
359
+ const turned = document.getElementById("turned")!;
360
+ send.mockClear();
361
+ const opts = pressOn(iframe, { ...makeSelection("turned", turned), handle });
362
+ expect(press.start).not.toHaveBeenCalled();
363
+ await vi.waitFor(() => expect(press.start).toHaveBeenCalledOnce());
364
+ const started = press.start.mock.calls[0]![3] as {
365
+ rect: OverlayRect;
366
+ start: { layout: { width: number } };
367
+ snap: { targets: unknown[] };
368
+ };
369
+ expect(started.start.layout.width).toBe(300);
370
+ expect(started.snap.targets.length).toBeGreaterThan(0);
371
+ const overlay = opts.overlayRef.current!;
372
+ const live = orientedGroupAwareOverlayRect(overlay, liveFrame(), turned);
373
+ expect(close(started.rect)).toEqual(close(live));
374
+ expect(kinds().filter((kind) => kind === "gesture-start")).toHaveLength(1);
375
+ expect(kinds()).not.toContain("geometry");
376
+ send.mockRestore();
377
+ });
378
+ });