@hyperframes/studio 0.9.42 → 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.
@@ -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
+ });
@@ -39,7 +39,7 @@ function orientedRect(scale: OverlayRootScale, box: EditingElementBox): OverlayR
39
39
  }
40
40
 
41
41
  /** `groupAwareOverlayRect`, or with `oriented`, `orientedGroupAwareOverlayRect`. */
42
- function groupAwareRect(
42
+ export function groupAwareRect(
43
43
  scale: OverlayRootScale,
44
44
  geometry: EditingElementGeometry,
45
45
  oriented: boolean,
@@ -54,6 +54,12 @@ function groupAwareRect(
54
54
  return union ? { ...union, editScaleX: rect.editScaleX, editScaleY: rect.editScaleY } : rect;
55
55
  }
56
56
 
57
+ /** `toVisibleOverlayRect`. */
58
+ export function flatVisibleRect(scale: OverlayRootScale, box: EditingElementBox): OverlayRect {
59
+ const rect = baseRect(scale, box);
60
+ return { ...rect, ...hugRectForClipPath(rect, box.clipPath) };
61
+ }
62
+
57
63
  /** `orientedVisibleOverlayRect`. */
58
64
  function visibleRect(scale: OverlayRootScale, box: EditingElementBox): OverlayRect {
59
65
  const rect = orientedRect(scale, box);
@@ -111,18 +117,6 @@ export async function measurePreviewOverlay(
111
117
  };
112
118
  }
113
119
 
114
- /** `orientedGroupAwareOverlayRect` for each handle; null when the frame cannot answer. */
115
- export async function measurePreviewRects(
116
- iframe: HTMLIFrameElement,
117
- overlayEl: HTMLDivElement,
118
- handles: string[],
119
- ): Promise<Array<OverlayRect | null> | null> {
120
- const reply = await previewGeometry(iframe, handles);
121
- const scale = reply && computeOverlayRootScale(overlayEl, iframe, null);
122
- if (!reply || !scale) return null;
123
- return reply.elements.map((geometry) => geometry && groupAwareRect(scale, geometry, true));
124
- }
125
-
126
120
  /** `toVisibleOverlayRect` for each handle, with its frame rect and whether it renders. */
127
121
  export async function measurePreviewVisibleRects(
128
122
  iframe: HTMLIFrameElement,
@@ -135,14 +129,8 @@ export async function measurePreviewVisibleRects(
135
129
  const scale = reply && computeOverlayRootScale(overlayEl, iframe, null);
136
130
  if (!reply || !scale) return null;
137
131
  for (const box of reply.elements) {
138
- const rect = box && baseRect(scale, box);
139
132
  measured.push(
140
- box &&
141
- rect && {
142
- rect: { ...rect, ...hugRectForClipPath(rect, box.clipPath) },
143
- frame: box.rect,
144
- visible: box.visible,
145
- },
133
+ box && { rect: flatVisibleRect(scale, box), frame: box.rect, visible: box.visible },
146
134
  );
147
135
  }
148
136
  }
@@ -1,10 +1,7 @@
1
1
  // fallow-ignore-file code-duplication
2
+ import { snapCandidates } from "@hyperframes/core/runtime/editing-port";
2
3
  import type { DomEditSelection } from "./domEditing";
3
- import {
4
- isElementVisibleForOverlay,
5
- toVisibleOverlayRect,
6
- type OverlayRect,
7
- } from "./domEditOverlayGeometry";
4
+ import { toVisibleOverlayRect, type OverlayRect } from "./domEditOverlayGeometry";
8
5
  import {
9
6
  extractSnapTargets,
10
7
  buildCompositionSnapTarget,
@@ -27,33 +24,23 @@ function readPositiveDimension(value: string | null): number | null {
27
24
  return Number.isFinite(parsed) && parsed > 0 ? parsed : null;
28
25
  }
29
26
 
30
- const IGNORED_TAGS = new Set(["script", "style", "link", "meta", "base", "template", "br", "wbr"]);
31
-
32
- function isHtmlElement(node: Node): node is HTMLElement {
33
- return node.nodeType === 1;
34
- }
35
-
36
- function collectVisibleElements(
37
- root: HTMLElement,
38
- excludeElements: Set<HTMLElement>,
39
- maxItems: number,
40
- ): HTMLElement[] {
41
- const result: HTMLElement[] = [];
42
- // fallow-ignore-next-line complexity
43
- const visit = (el: HTMLElement) => {
44
- if (result.length >= maxItems) return;
45
- for (const child of Array.from(el.children)) {
46
- if (!isHtmlElement(child)) continue;
47
- if (IGNORED_TAGS.has(child.tagName.toLowerCase())) continue;
48
- if (child.hasAttribute("data-composition-id")) continue;
49
- if (excludeElements.has(child)) continue;
50
- if (!isElementVisibleForOverlay(child)) continue;
51
- result.push(child);
52
- visit(child);
53
- }
27
+ export function snapContextFromRects(
28
+ composition: OverlayRect,
29
+ rects: ReadonlyArray<OverlayRect | null>,
30
+ ): SnapContext {
31
+ const prefs = readStudioUiPreferences();
32
+ const entries = rects.flatMap((rect, i) => (rect ? [{ rect, id: `snap-target-${i}` }] : []));
33
+ const gridSpacing = prefs.gridSpacing ?? 50;
34
+ const snapToGrid = prefs.snapToGrid ?? false;
35
+ return {
36
+ targets: extractSnapTargets(entries),
37
+ compositionTarget: buildCompositionSnapTarget(composition),
38
+ gridEdges:
39
+ snapToGrid && gridSpacing > 0
40
+ ? buildGridSnapEdges(composition, gridSpacing, composition.editScaleX)
41
+ : null,
42
+ snapEnabled: prefs.snapEnabled ?? true,
54
43
  };
55
- visit(root);
56
- return result;
57
44
  }
58
45
 
59
46
  // fallow-ignore-next-line complexity
@@ -62,8 +49,7 @@ export function collectSnapContext(input: {
62
49
  iframe: HTMLIFrameElement;
63
50
  excludeElements: Set<HTMLElement>;
64
51
  }): SnapContext {
65
- const prefs = readStudioUiPreferences();
66
- const snapEnabled = prefs.snapEnabled ?? true;
52
+ const snapEnabled = readStudioUiPreferences().snapEnabled ?? true;
67
53
 
68
54
  const doc = input.iframe.contentDocument;
69
55
  if (!doc) {
@@ -84,41 +70,19 @@ export function collectSnapContext(input: {
84
70
 
85
71
  const iframeRect = input.iframe.getBoundingClientRect();
86
72
  const overlayRect = input.overlayEl.getBoundingClientRect();
87
- const rootScaleX = iframeRect.width / rootWidth;
88
- const rootScaleY = iframeRect.height / rootHeight;
89
-
90
73
  const compositionOverlayRect: OverlayRect = {
91
74
  left: iframeRect.left - overlayRect.left,
92
75
  top: iframeRect.top - overlayRect.top,
93
76
  width: iframeRect.width,
94
77
  height: iframeRect.height,
95
- editScaleX: rootScaleX,
96
- editScaleY: rootScaleY,
78
+ editScaleX: iframeRect.width / rootWidth,
79
+ editScaleY: iframeRect.height / rootHeight,
97
80
  };
98
- const compositionTarget = buildCompositionSnapTarget(compositionOverlayRect);
99
-
100
- const MAX_SNAP_TARGETS = 80;
101
- const elements = collectVisibleElements(root, input.excludeElements, MAX_SNAP_TARGETS);
102
-
103
- const entries: Array<{
104
- rect: { left: number; top: number; width: number; height: number };
105
- id: string;
106
- }> = [];
107
- for (let i = 0; i < elements.length; i++) {
108
- const rect = toVisibleOverlayRect(input.overlayEl, input.iframe, elements[i]);
109
- if (rect) entries.push({ rect, id: `snap-target-${i}` });
110
- }
111
-
112
- const targets = extractSnapTargets(entries);
113
-
114
- let gridEdges: { x: SnapEdge[]; y: SnapEdge[] } | null = null;
115
- const gridSpacing = prefs.gridSpacing ?? 50;
116
- const snapToGrid = prefs.snapToGrid ?? false;
117
- if (snapToGrid && gridSpacing > 0) {
118
- gridEdges = buildGridSnapEdges(compositionOverlayRect, gridSpacing, rootScaleX);
119
- }
120
-
121
- return { targets, compositionTarget, gridEdges, snapEnabled };
81
+ const elements = snapCandidates(root, input.excludeElements);
82
+ const rects = elements.map((element) =>
83
+ toVisibleOverlayRect(input.overlayEl, input.iframe, element),
84
+ );
85
+ return snapContextFromRects(compositionOverlayRect, rects);
122
86
  }
123
87
 
124
88
  // fallow-ignore-next-line complexity
@@ -8,9 +8,11 @@ import {
8
8
  MAX_ELEMENT_ENTRIES,
9
9
  MAX_ELEMENT_VALUE,
10
10
  MAX_GEOMETRY_CHILDREN,
11
+ MAX_SNAP_TARGETS,
11
12
  type EditingElementBox,
12
13
  type EditingElementDescription,
13
14
  type EditingElementGeometry,
15
+ type EditingGestureStart,
14
16
  type EditingElementHit,
15
17
  type EditingElementMatch,
16
18
  type EditingElementSource,
@@ -226,6 +228,41 @@ export async function previewGeometry(
226
228
  };
227
229
  }
228
230
 
231
+ const isFlag = (value: unknown) => typeof value === "boolean";
232
+ const isFiniteFields = (value: unknown, keys: string[]) =>
233
+ isFields(value) && keys.every((key) => Number.isFinite(value[key]));
234
+
235
+ const readGestureStart = (value: unknown): EditingGestureStart | null => {
236
+ if (!readGeometry(value) || !isFields(value)) return null;
237
+ const { layout, move, gsap } = value;
238
+ const valid = [
239
+ isFiniteFields(layout, ["width", "height"]),
240
+ move === null || isFiniteFields(move, ["a", "b", "c", "d"]),
241
+ gsap === null || (isFields(gsap) && isFiniteFields(gsap, ["x", "y"]) && isFlag(gsap.moving)),
242
+ ];
243
+ return valid.includes(false) ? null : (value as unknown as EditingGestureStart);
244
+ };
245
+
246
+ export interface PreviewGestureStartReply {
247
+ /** One entry per handle, in order: null for an element the frame no longer has. */
248
+ elements: Array<EditingGestureStart | null>;
249
+ snap: EditingElementBox[];
250
+ }
251
+
252
+ /** What a gesture on `handles` starts from, in one read; null when the runtime cannot answer. */
253
+ export async function previewGestureStart(
254
+ iframe: HTMLIFrameElement,
255
+ handles: readonly string[],
256
+ ): Promise<PreviewGestureStartReply | null> {
257
+ if (!handles.length || handles.length > MAX_DESCRIBE_HANDLES) return null;
258
+ const reply = await previewRequest(iframe, "gesture-start", { handles: [...handles] });
259
+ const elements: unknown = reply?.elements;
260
+ const snap: unknown = reply?.snap;
261
+ if (!Array.isArray(elements) || elements.length !== handles.length) return null;
262
+ if (!Array.isArray(snap) || snap.length > MAX_SNAP_TARGETS || !snap.every(isBox)) return null;
263
+ return { elements: elements.map(readGestureStart), snap };
264
+ }
265
+
229
266
  const LAYER_KINDS = new Set(["image", "video", "audio", "vector", "group", "text", "shape"]);
230
267
  const isLayer = (value: unknown): value is EditingLayer =>
231
268
  isFields(value) &&