@hyperframes/studio 0.9.42 → 0.9.44
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.
- package/dist/assets/{codec-CKqLGYc6.js → codec-DYhkxayl.js} +1 -1
- package/dist/assets/{hyperframes-player-DoEYqOIa.js → hyperframes-player-pPwFQY2W.js} +1 -1
- package/dist/assets/{index-4VGEAZyf.css → index-CU24FmKd.css} +1 -1
- package/dist/assets/{index-DLyQ91Ek.js → index-Crhhpl7F.js} +133 -133
- package/dist/assets/{index-s9iqoWnr.js → index-Cv1BkoPn.js} +1 -1
- package/dist/assets/{pausedVideoDecoder-B-ReRg6i.js → pausedVideoDecoder-Cns-60-8.js} +1 -1
- package/dist/{chunk-4QQMX4BX.js → chunk-67DSVBEI.js} +1 -1
- package/dist/{chunk-52AJB4PN.js → chunk-ZNUWV4FN.js} +1 -1
- package/dist/index.html +2 -2
- package/dist/index.js +790 -709
- package/dist/player.js +2 -2
- package/dist/styles.css +1 -1
- package/dist/ui.js +1 -1
- package/package.json +8 -8
- package/src/components/StudioOverlays.tsx +15 -13
- package/src/components/StudioToast.test.tsx +41 -0
- package/src/components/StudioToast.tsx +23 -39
- package/src/components/editor/domEditOverlayGeometry.ts +2 -0
- package/src/components/editor/domEditOverlayStartGesture.ts +31 -7
- package/src/components/editor/manualOffsetDrag.ts +10 -5
- package/src/components/editor/plainTranslate.ts +2 -0
- package/src/components/editor/previewGestureStartRead.test.ts +279 -0
- package/src/components/editor/previewGestureStartRead.ts +67 -0
- package/src/components/editor/previewGestureStarts.ts +21 -15
- package/src/components/editor/previewOverlayRects.test.ts +62 -0
- package/src/components/editor/previewOverlayRects.ts +8 -20
- package/src/components/editor/snapTargetCollection.ts +26 -62
- package/src/components/feedback/StudioFeedbackCard.tsx +5 -2
- package/src/components/ui/Menu.test.tsx +30 -1
- package/src/components/ui/Menu.tsx +1 -1
- package/src/hooks/timelineEditingHelpers.test.ts +48 -0
- package/src/hooks/timelineEditingHelpers.ts +2 -0
- package/src/hooks/timelineElementsAttribute.test.ts +46 -0
- package/src/hooks/timelineElementsAttribute.ts +14 -16
- package/src/hooks/timelineTrackVisibility.test.ts +34 -0
- package/src/hooks/useElementPicker.test.ts +9 -2
- package/src/hooks/useElementPicker.ts +4 -2
- package/src/hooks/useToast.ts +6 -18
- package/src/player/lib/previewElements.ts +37 -0
- package/src/player/lib/thumbnailFrameSink.test.ts +92 -0
- package/src/player/lib/thumbnailFrameSink.ts +83 -0
- package/src/player/lib/thumbnailVideoDecoder.test.ts +43 -16
- package/src/player/lib/thumbnailVideoDecoder.ts +6 -6
- package/src/styles/components.css +19 -1
- package/src/styles/theme.css +3 -0
- package/src/utils/previewSafeMargins.ts +6 -10
- package/src/utils/sourcePatcher.test.ts +21 -0
- package/src/utils/sourcePatcher.ts +16 -8
|
@@ -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 {
|
|
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?:
|
|
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
|
|
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<
|
|
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
|
|
156
|
+
const read = await measurePreviewGestureStart(iframe, overlay, handles as string[]);
|
|
155
157
|
const shown = selections.flatMap((selection, i) => {
|
|
156
|
-
const
|
|
157
|
-
return
|
|
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
|
-
|
|
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
|
-
|
|
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
|
},
|
|
@@ -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
|
}
|