@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.
@@ -34,7 +34,9 @@ import {
34
34
  type ResizeHandle,
35
35
  type UseDomEditOverlayGesturesOptions,
36
36
  } from "./domEditOverlayGestures";
37
- import { collectSnapContext, buildExcludeElements } from "./snapTargetCollection";
37
+ import { collectSnapContext, buildExcludeElements, type SnapContext } from "./snapTargetCollection";
38
+ import { MOVING_LAYER_UNREADABLE } from "./plainTranslate";
39
+ import type { PreviewGestureStart } from "./previewGestureStartRead";
38
40
  import { editsPlainCss } from "../../hooks/gsapRuntimeKeyframes";
39
41
  import { logResize, resetResizeMoveLog } from "../../utils/resizeDebug";
40
42
  import { logDrag, readDragPositions, resetDragMoveLog } from "../../utils/dragDebug";
@@ -52,6 +54,7 @@ export function startGroupDrag(
52
54
  e: React.PointerEvent<HTMLElement>,
53
55
  opts: UseDomEditOverlayGesturesOptions,
54
56
  at?: EditMoment,
57
+ snap?: SnapContext,
55
58
  ): boolean {
56
59
  const items = opts.groupOverlayItemsRef.current;
57
60
  if (items.length <= 1) return false;
@@ -65,6 +68,13 @@ export function startGroupDrag(
65
68
  notifyBlockedPress(e, opts, blockedSelection);
66
69
  return false;
67
70
  }
71
+ const moving = items.find((item) => item.start?.gsap?.moving)?.selection;
72
+ if (moving) {
73
+ e.preventDefault();
74
+ e.stopPropagation();
75
+ opts.onBlockedMoveRef.current(moving, MOVING_LAYER_UNREADABLE);
76
+ return false;
77
+ }
68
78
 
69
79
  opts.onManualDragStartRef.current?.();
70
80
  const dragItems = filterNestedDomEditGroupItems(items);
@@ -77,6 +87,7 @@ export function startGroupDrag(
77
87
  rect: item.rect,
78
88
  gesture: "drag",
79
89
  at,
90
+ start: item.start,
80
91
  });
81
92
  if (!result.ok) {
82
93
  restoreManualOffsetDragMembers(members);
@@ -108,7 +119,8 @@ export function startGroupDrag(
108
119
  const overlayEl = opts.overlayRef.current;
109
120
  const iframe = opts.iframeRef.current;
110
121
  const snapContext =
111
- overlayEl && iframe
122
+ snap ??
123
+ (overlayEl && iframe
112
124
  ? collectSnapContext({
113
125
  overlayEl,
114
126
  iframe,
@@ -117,7 +129,7 @@ export function startGroupDrag(
117
129
  groupSelections: items.map((i) => i.selection),
118
130
  }),
119
131
  })
120
- : undefined;
132
+ : undefined);
121
133
 
122
134
  e.preventDefault();
123
135
  e.stopPropagation();
@@ -150,9 +162,13 @@ export function startGesture(
150
162
  resizeHandle?: ResizeHandle;
151
163
  resizeCorner?: { x: number; y: number };
152
164
  at?: EditMoment;
165
+ /** Read off a frame Studio cannot script at the press: the element's start and the snap targets. */
166
+ start?: PreviewGestureStart;
167
+ snap?: SnapContext;
153
168
  },
154
169
  ): boolean {
155
170
  const at = options?.at;
171
+ const start = options?.start;
156
172
  const sel = options?.selection ?? opts.selectionRef.current;
157
173
  const rect = options?.rect ?? opts.overlayRectRef.current;
158
174
  const box = opts.boxRef.current;
@@ -172,8 +188,8 @@ export function startGesture(
172
188
  // layout size. offsetWidth/Height are transform-free; the overlay-rect
173
189
  // fallback (rect / editScale) includes the element's own GSAP scale and
174
190
  // would make a rescaled element's draft grow from the RENDERED size.
175
- const layoutWidth = sel.element.offsetWidth;
176
- const layoutHeight = sel.element.offsetHeight;
191
+ const layoutWidth = start?.layout.width ?? sel.element.offsetWidth;
192
+ const layoutHeight = start?.layout.height ?? sel.element.offsetHeight;
177
193
  const actualWidth =
178
194
  size.width > 0 ? size.width : layoutWidth > 0 ? layoutWidth : rect.width / rect.editScaleX;
179
195
  const actualHeight =
@@ -196,6 +212,11 @@ export function startGesture(
196
212
  let pathOffsetMember: ManualOffsetDragMember | undefined;
197
213
 
198
214
  if (kind === "drag") {
215
+ if (start?.gsap?.moving) {
216
+ opts.onBlockedMoveRef.current(sel, MOVING_LAYER_UNREADABLE);
217
+ e.preventDefault();
218
+ return false;
219
+ }
199
220
  opts.onManualDragStartRef.current?.();
200
221
  const result = createManualOffsetDragMember({
201
222
  key: selectionCacheKey(sel),
@@ -204,6 +225,7 @@ export function startGesture(
204
225
  rect,
205
226
  gesture: "drag",
206
227
  at,
228
+ start,
207
229
  });
208
230
  if (!result.ok) {
209
231
  opts.onBlockedMoveRef.current(result.selection, result.reason);
@@ -226,6 +248,7 @@ export function startGesture(
226
248
  rect,
227
249
  gesture: "resize",
228
250
  at,
251
+ start,
229
252
  });
230
253
  if (result.ok) {
231
254
  pathOffsetMember = result.member;
@@ -267,11 +290,12 @@ export function startGesture(
267
290
  }
268
291
  const snapContext =
269
292
  (kind === "drag" || kind === "resize") && overlayEl && iframe
270
- ? collectSnapContext({
293
+ ? (options?.snap ??
294
+ collectSnapContext({
271
295
  overlayEl,
272
296
  iframe,
273
297
  excludeElements: buildExcludeElements({ iframe, selection: sel }),
274
- })
298
+ }))
275
299
  : undefined;
276
300
  e.preventDefault();
277
301
  e.stopPropagation();
@@ -18,6 +18,7 @@ import { computeDraggedGsapPosition } from "../../hooks/draggedGsapPosition";
18
18
  import { editsPlainCss } from "../../hooks/gsapRuntimeKeyframes";
19
19
  import { readTranslatePx, UNREADABLE_TRANSLATE, writeTranslatePx } from "./plainTranslate";
20
20
  import { drawPreviewDraft, endPreviewDraft } from "./previewDrafts";
21
+ import type { PreviewGestureStart } from "./previewGestureStartRead";
21
22
 
22
23
  interface OffsetDragGsap {
23
24
  set: (el: Element, vars: Record<string, number | string>) => void;
@@ -320,9 +321,13 @@ export function createManualOffsetDragMember(input: {
320
321
  rect: ManualOffsetDragRect;
321
322
  gesture?: "drag" | "nudge" | "resize"; // resize: the anchor that keeps its centre planted
322
323
  at?: EditMoment;
324
+ start?: PreviewGestureStart;
323
325
  }): ManualOffsetDragMemberResult {
326
+ const { start, gesture } = input;
324
327
  const plainTranslate =
325
- !!input.gesture && editsPlainCss(input.element, input.gesture === "resize" ? "resize" : "move");
328
+ start && gesture !== "resize"
329
+ ? !start.gsap
330
+ : !!gesture && editsPlainCss(input.element, gesture === "resize" ? "resize" : "move");
326
331
  // The APPLIED offset, never a dormant var, so a stale one can't fling the element off-screen.
327
332
  const initialOffset = plainTranslate
328
333
  ? readTranslatePx(input.element)
@@ -338,7 +343,7 @@ export function createManualOffsetDragMember(input: {
338
343
  // getProperty makes GSAP bake the CSS translate into its transform, so a plain move never reads it.
339
344
  const gsapX = (!plainTranslate && win?.gsap?.getProperty?.(input.element, "x")) || 0;
340
345
  const gsapY = (!plainTranslate && win?.gsap?.getProperty?.(input.element, "y")) || 0;
341
- const baseGsap = { x: gsapX, y: gsapY };
346
+ const baseGsap = start?.gsap ? { x: start.gsap.x, y: start.gsap.y } : { x: gsapX, y: gsapY };
342
347
  if (!plainTranslate) stampGestureBase(input.element, initialOffset, baseGsap);
343
348
 
344
349
  if (win?.__timelines) {
@@ -371,8 +376,8 @@ export function createManualOffsetDragMember(input: {
371
376
  });
372
377
  endPreviewDraft(input.element, "keep");
373
378
  if (!measured.ok) {
374
- // GSAP transforms can defeat the probe: approximate with the preview scale. The commit
375
- // reads GSAP's real position, so only the drag preview is imprecise.
379
+ // A stand-in has no box: the frame probed its element at the press. GSAP transforms can defeat
380
+ // the probe: approximate with the preview scale; the commit reads GSAP's real position.
376
381
  const scaleX = input.rect.editScaleX || 1;
377
382
  const scaleY = input.rect.editScaleY || 1;
378
383
  const w = readTransformWDivisor(input.element);
@@ -387,7 +392,7 @@ export function createManualOffsetDragMember(input: {
387
392
  baseGsap,
388
393
  initialPathOffset,
389
394
  gestureToken,
390
- screenToOffset: { a: w / scaleX, b: 0, c: 0, d: w / scaleY },
395
+ screenToOffset: start?.screenToOffset ?? { a: w / scaleX, b: 0, c: 0, d: w / scaleY },
391
396
  originRect: input.rect,
392
397
  },
393
398
  };
@@ -128,6 +128,8 @@ export function readTranslatePxLeavingPercent(el: HTMLElement): Point {
128
128
 
129
129
  export const UNREADABLE_TRANSLATE =
130
130
  "Studio can't read this layer's translate. Move it in the Code tab.";
131
+ export const MOVING_LAYER_UNREADABLE =
132
+ "This preview can't save a move of a layer its animation moves yet. Move it in the Code tab.";
131
133
 
132
134
  /** Plain px only: GSAP's CSSPlugin splits `translate` on spaces and drops a calc(). */
133
135
  function formatTranslatePx(p: Point): string {
@@ -0,0 +1,279 @@
1
+ // @vitest-environment happy-dom
2
+
3
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
4
+ // The frame's real runtime agent, serving the opaque frame from this document.
5
+ import { installEditingAgent } from "../../../../core/src/runtime/editingAgent";
6
+ import { makeSelection } from "../../hooks/domSelectionTestHarness";
7
+ import { computeDraggedGsapPosition } from "../../hooks/draggedGsapPosition";
8
+ import { watchPreviewFrame } from "../../player/lib/bridgePlayback";
9
+ import { describePreviewElements, queryPreviewElements } from "../../player/lib/previewElements";
10
+ import { detachedFakeIframe } from "../../player/hooks/timelinePlayerTestHarness";
11
+ import type { UseDomEditOverlayGesturesOptions } from "./domEditOverlayGestures";
12
+ import { startGesture, startGroupDrag } from "./domEditOverlayStartGesture";
13
+ import {
14
+ applyManualOffsetDragCommit,
15
+ createManualOffsetDragMember,
16
+ restoreManualOffsetDragMembers,
17
+ } from "./manualOffsetDrag";
18
+ import { MOVING_LAYER_UNREADABLE } from "./plainTranslate";
19
+ import { measurePreviewGestureStart, type PreviewGestureStart } from "./previewGestureStartRead";
20
+ import { standInFor } from "./previewSelection";
21
+ import { collectSnapContext } from "./snapTargetCollection";
22
+
23
+ const FIXTURE = `
24
+ <div data-composition-id="main" data-width="1920" data-height="1080">
25
+ <div id="card" style="translate: 10px 0px"></div>
26
+ <div id="host" data-composition-src="a.html" data-width="960" data-height="540">
27
+ <p id="inner"></p>
28
+ </div>
29
+ <div id="clipped" style="clip-path: inset(10px 20px 30px 40px)"></div>
30
+ <div style="display: none"><span id="gone"></span></div>
31
+ <div id="sub" data-composition-id="sub"><b id="nested"></b></div>
32
+ <div id="last"></div>
33
+ </div>`;
34
+ const RECTS: Record<string, number[]> = {
35
+ host: [200, 100, 960, 540],
36
+ inner: [220, 140, 300, 80],
37
+ clipped: [900, 700, 400, 300],
38
+ gone: [1, 1, 50, 50],
39
+ sub: [5, 5, 50, 50],
40
+ nested: [6, 6, 10, 10],
41
+ last: [1500, 900, 120, 60],
42
+ };
43
+
44
+ function sized(element: Element, [x, y, width, height]: number[]) {
45
+ element.getBoundingClientRect = () =>
46
+ ({ x, y, width, height, left: x, top: y, right: x + width, bottom: y + height }) as DOMRect;
47
+ Object.defineProperty(element, "offsetWidth", { value: width, configurable: true });
48
+ Object.defineProperty(element, "offsetHeight", { value: height, configurable: true });
49
+ return element;
50
+ }
51
+
52
+ /** The card sits in a mirrored parent: a px of translate x moves it a px left, y moves it down. */
53
+ function mirroredCard(card: HTMLElement) {
54
+ sized(card, [0, 0, 200, 100]);
55
+ card.getBoundingClientRect = () => {
56
+ const [x = 0, y = 0] = card.style.translate
57
+ .split(/ (?![^(]*\))/)
58
+ .map((axis) =>
59
+ [...axis.matchAll(/-?[\d.]+px/g)].reduce((sum, [px]) => sum + Number.parseFloat(px), 0),
60
+ );
61
+ const left = 600 - x;
62
+ const top = 300 + y;
63
+ return { x: left, y: top, width: 200, height: 100, left, top } as DOMRect;
64
+ };
65
+ }
66
+
67
+ const closers: Array<() => void> = [];
68
+ beforeEach(() => {
69
+ document.body.innerHTML = FIXTURE;
70
+ for (const [id, rect] of Object.entries(RECTS)) sized(document.getElementById(id)!, rect);
71
+ mirroredCard(document.getElementById("card")!);
72
+ });
73
+ afterEach(() => {
74
+ closers.splice(0).forEach((close) => close());
75
+ document.body.innerHTML = "";
76
+ Reflect.deleteProperty(window, "gsap");
77
+ Reflect.deleteProperty(window, "__timelines");
78
+ });
79
+
80
+ /** A frame Studio scripts directly; its document and window are this one's. */
81
+ function liveFrame() {
82
+ const iframe = detachedFakeIframe(window as unknown as Record<string, unknown>);
83
+ Object.defineProperty(iframe, "contentDocument", { value: document });
84
+ return sized(iframe, [100, 50, 960, 540]) as HTMLIFrameElement;
85
+ }
86
+
87
+ /** A frame Studio cannot read, whose runtime is the real editing agent over this document. */
88
+ function opaqueFrame() {
89
+ const frame: Record<string, unknown> = {};
90
+ const iframe = sized(detachedFakeIframe(frame), [100, 50, 960, 540]) as HTMLIFrameElement;
91
+ Object.defineProperty(iframe, "contentDocument", { value: null });
92
+ Object.defineProperty(frame, "document", {
93
+ get: () => {
94
+ throw new DOMException("blocked", "SecurityError");
95
+ },
96
+ });
97
+ const parent = {
98
+ postMessage: (data: unknown, _origin: string, ports: MessagePort[]) =>
99
+ window.dispatchEvent(
100
+ new MessageEvent("message", { source: frame as unknown as Window, data, ports }),
101
+ ),
102
+ } as unknown as Window;
103
+ const agent = installEditingAgent(parent);
104
+ frame.postMessage = (data: unknown) =>
105
+ window.dispatchEvent(new MessageEvent("message", { source: parent, data }));
106
+ closers.push(() => agent.dispose());
107
+ watchPreviewFrame(iframe);
108
+ window.dispatchEvent(
109
+ new MessageEvent("message", {
110
+ source: frame as unknown as Window,
111
+ origin: window.location.origin,
112
+ data: { source: "hf-preview", type: "stage-size", width: 1920, height: 1080 },
113
+ }),
114
+ );
115
+ return iframe;
116
+ }
117
+
118
+ const overlayEl = () => sized(document.createElement("div"), [20, 10, 1200, 800]) as HTMLDivElement;
119
+ const round = (value: unknown) =>
120
+ JSON.parse(JSON.stringify(value, (_, v: unknown) => (typeof v === "number" ? +v.toFixed(9) : v)));
121
+
122
+ async function opaqueCard() {
123
+ const iframe = opaqueFrame();
124
+ const handle = (await queryPreviewElements(iframe, "#card"))!.matches[0]!.handle;
125
+ const description = (await describePreviewElements(iframe, [handle]))![0]!;
126
+ return { iframe, handle, standIn: standInFor(iframe, description) };
127
+ }
128
+
129
+ /** GSAP holds the card at x 200, y 5, as `gsap.set` leaves it. */
130
+ function gsapHoldsCard(tweens: unknown[] = []) {
131
+ const card = document.getElementById("card")!;
132
+ Object.assign(card, { _gsap: { renderTransform: () => {} } });
133
+ Object.assign(window, {
134
+ gsap: { getProperty: (_: Element, axis: string) => (axis === "x" ? 200 : 5) },
135
+ __timelines: { main: { getChildren: () => tweens } },
136
+ });
137
+ }
138
+
139
+ describe("a gesture's start on an opaque frame", () => {
140
+ it("reads the snap targets a scriptable frame collects, without the pressed element", async () => {
141
+ const overlay = overlayEl();
142
+ const card = document.getElementById("card")!;
143
+ const live = collectSnapContext({
144
+ overlayEl: overlay,
145
+ iframe: liveFrame(),
146
+ excludeElements: new Set([card]),
147
+ });
148
+ const { iframe, handle } = await opaqueCard();
149
+ const read = await measurePreviewGestureStart(iframe, overlay, [handle]);
150
+ expect(round(read!.snap)).toEqual(round(live));
151
+ // host, inner, clipped, last: the hidden span, the nested composition and the card are left out.
152
+ expect(live.targets).toHaveLength(4);
153
+ expect(live.compositionTarget).not.toBeNull();
154
+ });
155
+
156
+ it("reads GSAP's x/y, the layout size and the frame's move, so a drag saves from where it is", async () => {
157
+ gsapHoldsCard();
158
+ const { iframe, handle, standIn } = await opaqueCard();
159
+ const read = await measurePreviewGestureStart(iframe, overlayEl(), [handle]);
160
+ const { rect, start } = read!.elements[0]!;
161
+ // Mirrored x at a 0.5 page scale: a pointer px left is 2 translate px right.
162
+ expect(start).toEqual({
163
+ layout: { width: 200, height: 100 },
164
+ screenToOffset: { a: -2, b: 0, c: 0, d: 2 },
165
+ gsap: { x: 200, y: 5, moving: false },
166
+ });
167
+ const selection = { ...makeSelection("card", standIn), handle };
168
+ const made = createManualOffsetDragMember({
169
+ key: "card",
170
+ selection,
171
+ element: standIn,
172
+ rect,
173
+ gesture: "drag",
174
+ start,
175
+ });
176
+ if (!made.ok) throw new Error(made.reason);
177
+ closers.push(() => restoreManualOffsetDragMembers([made.member]));
178
+ expect(made.member.plainTranslate).toBe(false);
179
+ // The release stamps the gesture's base and offset, as the GSAP commit reads them.
180
+ const offset = applyManualOffsetDragCommit(made.member, 10, 0);
181
+ expect(offset).toEqual({ x: -20, y: 0 });
182
+ // GSAP's own x plus the drag, not the drag alone.
183
+ expect(computeDraggedGsapPosition(standIn, offset, { x: 0, y: 0 }).newX).toBe(180);
184
+ });
185
+
186
+ it("moves an element GSAP does not own by its translate, as before", async () => {
187
+ const { iframe, handle } = await opaqueCard();
188
+ const read = await measurePreviewGestureStart(iframe, overlayEl(), [handle]);
189
+ expect(read!.elements[0]!.start.gsap).toBeNull();
190
+ });
191
+ });
192
+
193
+ function gestureOpts(selection: ReturnType<typeof makeSelection>) {
194
+ const ref = <T>(current: T) => ({ current });
195
+ return {
196
+ selectionRef: ref(selection),
197
+ overlayRectRef: ref({
198
+ left: 0,
199
+ top: 0,
200
+ width: 50,
201
+ height: 25,
202
+ editScaleX: 0.5,
203
+ editScaleY: 0.5,
204
+ }),
205
+ boxRef: ref(document.createElement("div")),
206
+ overlayRef: ref(overlayEl()),
207
+ iframeRef: ref(liveFrame()),
208
+ gestureRef: ref(null),
209
+ groupGestureRef: ref(null),
210
+ groupOverlayItemsRef: ref([]),
211
+ rafPausedRef: ref(false),
212
+ onManualDragStartRef: ref(vi.fn()),
213
+ onBlockedMoveRef: ref(vi.fn()),
214
+ } as unknown as UseDomEditOverlayGesturesOptions;
215
+ }
216
+ const press = () =>
217
+ ({
218
+ clientX: 10,
219
+ clientY: 10,
220
+ pointerId: 1,
221
+ button: 0,
222
+ preventDefault() {},
223
+ stopPropagation() {},
224
+ currentTarget: { setPointerCapture() {} },
225
+ }) as unknown as React.PointerEvent<HTMLElement>;
226
+ const START: PreviewGestureStart = {
227
+ layout: { width: 200, height: 100 },
228
+ screenToOffset: { a: 2, b: 0, c: 0, d: 2 },
229
+ gsap: null,
230
+ };
231
+ const SNAP = { targets: [], compositionTarget: null, gridEdges: null, snapEnabled: true };
232
+
233
+ describe("starting a gesture from what an opaque frame read", () => {
234
+ it("grows a resize from the frame's layout size, and snaps with the targets it read", () => {
235
+ const element = document.createElement("div");
236
+ const opts = gestureOpts(makeSelection("card", element));
237
+ expect(startGesture("resize", press(), opts, { start: START, snap: SNAP })).toBe(true);
238
+ // The element has no box here; its rect over the edit scale would give 100 x 50.
239
+ const g = opts.gestureRef.current!;
240
+ expect([g.actualWidth, g.actualHeight]).toEqual([200, 100]);
241
+ expect(g.snapContext).toBe(SNAP);
242
+ restoreManualOffsetDragMembers(g.pathOffsetMember ? [g.pathOffsetMember] : []);
243
+ });
244
+
245
+ it("refuses a drag of a layer a tween moves, which this preview cannot save yet", () => {
246
+ const element = document.createElement("div");
247
+ const selection = makeSelection("card", element);
248
+ const opts = gestureOpts(selection);
249
+ const moving = { ...START, gsap: { x: 0, y: 0, moving: true } };
250
+ expect(startGesture("drag", press(), opts, { start: moving })).toBe(false);
251
+ expect(opts.onBlockedMoveRef.current).toHaveBeenCalledWith(selection, MOVING_LAYER_UNREADABLE);
252
+ expect(opts.gestureRef.current).toBeNull();
253
+ });
254
+
255
+ it("drags a group from each member's GSAP position, snapping with the targets it read", () => {
256
+ const items = ["a", "b"].map((key, i) => {
257
+ const element = document.createElement("div");
258
+ const gsap = { x: 100 * (i + 1), y: 0, moving: false };
259
+ const rect = { left: 0, top: 0, width: 10, height: 10, editScaleX: 1, editScaleY: 1 };
260
+ return {
261
+ key,
262
+ selection: makeSelection(key, element),
263
+ element,
264
+ rect,
265
+ start: { ...START, gsap },
266
+ };
267
+ });
268
+ const opts = gestureOpts(items[0]!.selection);
269
+ opts.groupOverlayItemsRef.current = items;
270
+ expect(startGroupDrag(press(), opts, undefined, SNAP)).toBe(true);
271
+ const g = opts.groupGestureRef.current!;
272
+ expect(g.snapContext).toBe(SNAP);
273
+ expect(g.members.map((member) => [member.plainTranslate, member.baseGsap.x])).toEqual([
274
+ [false, 100],
275
+ [false, 200],
276
+ ]);
277
+ restoreManualOffsetDragMembers(g.members);
278
+ });
279
+ });
@@ -0,0 +1,67 @@
1
+ /** A gesture on a frame Studio cannot script starts from one `gesture-start` read (core `editingGestureStart.ts`). */
2
+ import { previewGestureStart } from "../../player/lib/previewElements";
3
+ import { computeOverlayRootScale } from "./domEditOverlayBasis";
4
+ import type { OverlayRect } from "./domEditOverlayGeometry";
5
+ import { invertManualOffsetDragMatrix, type ManualOffsetDragMatrix } from "./manualOffsetDrag";
6
+ import { flatVisibleRect, groupAwareRect } from "./previewOverlayRects";
7
+ import { snapContextFromRects, type SnapContext } from "./snapTargetCollection";
8
+
9
+ export interface PreviewGestureStart {
10
+ /** The transform-free border box a resize grows from; 0 for an element with none. */
11
+ layout: { width: number; height: number };
12
+ /** Pointer px to translate px, as `measureManualOffsetDragScreenToOffsetMatrix`; null when unmeasured. */
13
+ screenToOffset: ManualOffsetDragMatrix | null;
14
+ /** GSAP's x/y when it owns the element's position, and whether a tween with a duration moves it. */
15
+ gsap: { x: number; y: number; moving: boolean } | null;
16
+ }
17
+
18
+ export interface PreviewGestureStarts {
19
+ elements: Array<{ rect: OverlayRect; start: PreviewGestureStart } | null>;
20
+ snap: SnapContext;
21
+ }
22
+
23
+ /** Everything a gesture on `handles` reads from the frame, in one request; null when it cannot answer. */
24
+ export async function measurePreviewGestureStart(
25
+ iframe: HTMLIFrameElement,
26
+ overlayEl: HTMLDivElement,
27
+ handles: string[],
28
+ ): Promise<PreviewGestureStarts | null> {
29
+ const reply = await previewGestureStart(iframe, handles);
30
+ const scale = reply && computeOverlayRootScale(overlayEl, iframe, null);
31
+ if (!reply || !scale) return null;
32
+ const { iframeRect, overlayRect, rootScaleX: sx, rootScaleY: sy } = scale;
33
+ // The frame measures its own px; a frame px is `rootScaleX` by `rootScaleY` pointer px on the page.
34
+ const screenToOffset = (move: { a: number; b: number; c: number; d: number } | null) =>
35
+ move &&
36
+ invertManualOffsetDragMatrix({
37
+ a: move.a * sx,
38
+ b: move.b * sy,
39
+ c: move.c * sx,
40
+ d: move.d * sy,
41
+ });
42
+ const composition: OverlayRect = {
43
+ left: iframeRect.left - overlayRect.left,
44
+ top: iframeRect.top - overlayRect.top,
45
+ width: iframeRect.width,
46
+ height: iframeRect.height,
47
+ editScaleX: sx,
48
+ editScaleY: sy,
49
+ };
50
+ return {
51
+ elements: reply.elements.map(
52
+ (start) =>
53
+ start && {
54
+ rect: groupAwareRect(scale, start, true),
55
+ start: {
56
+ layout: start.layout,
57
+ screenToOffset: screenToOffset(start.move),
58
+ gsap: start.gsap,
59
+ },
60
+ },
61
+ ),
62
+ snap: snapContextFromRects(
63
+ composition,
64
+ reply.snap.map((box) => flatVisibleRect(scale, box)),
65
+ ),
66
+ };
67
+ }
@@ -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
  },