@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.
- package/dist/assets/{codec-B_mpbc4w.js → codec-DHChmhF2.js} +1 -1
- package/dist/assets/{hyperframes-player-Dvi3I_mv.js → hyperframes-player-CdbweVDs.js} +1 -1
- package/dist/assets/{index-B5ExwsOp.js → index-CjWDrWWG.js} +166 -166
- package/dist/assets/{index-BrFm9Y5k.js → index-l-ti2dV8.js} +1 -1
- package/dist/assets/{pausedVideoDecoder-MHQ57yZ6.js → pausedVideoDecoder-xqOQ0ciK.js} +1 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.html +1 -1
- package/dist/index.js +1380 -989
- package/package.json +10 -10
- package/src/components/editor/LayersPanel.opaque.test.ts +176 -0
- package/src/components/editor/LayersPanel.tsx +41 -10
- package/src/components/editor/domEditInlineText.ts +1 -16
- package/src/components/editor/domEditOverlayGeometry.ts +2 -0
- package/src/components/editor/domEditOverlayStartGesture.ts +31 -7
- package/src/components/editor/domEditingTypes.ts +7 -1
- package/src/components/editor/gestureUndoRevert.ts +18 -1
- package/src/components/editor/inlineTextStyleRange.fill.test.ts +129 -0
- package/src/components/editor/inlineTextStyleRange.test.ts +0 -82
- package/src/components/editor/layerKind.ts +2 -19
- package/src/components/editor/layersPanelSort.ts +2 -2
- package/src/components/editor/manualEdits.ts +1 -0
- package/src/components/editor/manualEditsSnapshot.ts +10 -0
- package/src/components/editor/manualOffsetDrag.test.ts +1 -1
- package/src/components/editor/manualOffsetDrag.ts +17 -6
- package/src/components/editor/marqueeCommit.ts +47 -4
- package/src/components/editor/plainTranslate.ts +37 -16
- package/src/components/editor/previewDrafts.test.ts +263 -0
- package/src/components/editor/previewDrafts.ts +73 -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/previewLayers.test.ts +308 -0
- package/src/components/editor/previewLayers.ts +164 -0
- package/src/components/editor/previewOverlayRects.test.ts +62 -0
- package/src/components/editor/previewOverlayRects.ts +23 -8
- package/src/components/editor/previewPointerTargets.test.ts +68 -1
- package/src/components/editor/previewSelection.ts +55 -11
- package/src/components/editor/resizeDraft.ts +38 -0
- package/src/components/editor/snapTargetCollection.ts +26 -62
- package/src/components/editor/useDomEditOverlayGestures.ts +2 -2
- package/src/components/editor/useLayerDrag.ts +1 -1
- package/src/components/nle/NLEPreview.slowBoot.test.ts +0 -1
- package/src/hooks/domEditDeleteMembers.test.ts +7 -0
- package/src/hooks/domSelectionTimelineMirror.ts +3 -1
- package/src/hooks/timelineTimingSync.ts +4 -1
- package/src/hooks/useDomSelection.ts +2 -0
- package/src/hooks/useEditorSave.test.tsx +22 -1
- package/src/hooks/useEditorSave.ts +3 -1
- package/src/hooks/usePreviewInteraction.ts +4 -1
- package/src/hooks/useProjectFileWriter.ts +7 -3
- package/src/hooks/useTimelineSelectionPreviewSync.test.tsx +30 -0
- package/src/hooks/useTimelineSelectionPreviewSync.ts +7 -2
- package/src/player/lib/layerOrdering.ts +2 -11
- package/src/player/lib/previewElements.ts +68 -0
- package/src/test-setup.ts +2 -0
- package/src/utils/gsapSoftReload.ts +1 -1
- package/src/utils/studioFileHistory.ts +1 -1
- package/src/utils/studioHelpers.ts +27 -4
- package/src/utils/studioSaveDiagnostics.test.ts +34 -0
- package/src/utils/studioSaveDiagnostics.ts +35 -6
- package/src/utils/timelineCompositionInsert.ts +3 -1
|
@@ -0,0 +1,263 @@
|
|
|
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 { detachedFakeIframe } from "../../player/hooks/timelinePlayerTestHarness";
|
|
7
|
+
import { describePreviewElements, queryPreviewElements } from "../../player/lib/previewElements";
|
|
8
|
+
import { resolvePreviewSelection, standInFor } from "./previewSelection";
|
|
9
|
+
import { manualOffsetMoveRevert } from "./gestureUndoRevert";
|
|
10
|
+
import { writePlainMove } from "./plainTranslate";
|
|
11
|
+
import {
|
|
12
|
+
beginStudioManualEditGesture,
|
|
13
|
+
captureStudioBoxSize,
|
|
14
|
+
restoreStudioBoxSize,
|
|
15
|
+
} from "./manualEdits";
|
|
16
|
+
import { STUDIO_ORIGINAL_INLINE_TRANSLATE_ATTR } from "./manualEditsTypes";
|
|
17
|
+
import {
|
|
18
|
+
applyManualOffsetDragCommit,
|
|
19
|
+
applyManualOffsetDragDraft,
|
|
20
|
+
createManualOffsetDragMember,
|
|
21
|
+
endManualOffsetDragMembers,
|
|
22
|
+
restoreManualOffsetDragMembers,
|
|
23
|
+
type ManualOffsetDragMember,
|
|
24
|
+
} from "./manualOffsetDrag";
|
|
25
|
+
import { endResizeWithoutMember, resolveResizeDraftRect } from "./resizeDraft";
|
|
26
|
+
import { applySoftReloadFinalization } from "../../utils/gsapSoftReload";
|
|
27
|
+
import type { DomEditSelection } from "./domEditing";
|
|
28
|
+
import type { GestureState } from "./domEditOverlayGestures";
|
|
29
|
+
import { previewRequest } from "../../player/lib/previewChannel";
|
|
30
|
+
|
|
31
|
+
vi.mock("../../player/lib/previewChannel", async (importOriginal) => {
|
|
32
|
+
const actual = await importOriginal<typeof import("../../player/lib/previewChannel")>();
|
|
33
|
+
return { ...actual, previewRequest: vi.fn(actual.previewRequest) };
|
|
34
|
+
});
|
|
35
|
+
const draftRequests = () =>
|
|
36
|
+
vi.mocked(previewRequest).mock.calls.filter(([, kind]) => kind === "draft");
|
|
37
|
+
|
|
38
|
+
const closers: Array<() => void> = [];
|
|
39
|
+
beforeEach(() => {
|
|
40
|
+
document.body.innerHTML = `<main data-composition-id="main"><div id="card" style="width: 200px; height: 100px"></div><div id="chip" style="translate: 30px 0px"></div><div id="centred" style="width: 200px; height: 100px; translate: -50% -50%"></div><div id="moved" data-hf-studio-original-inline-translate="" style="translate: 10px 20px"></div></main>`;
|
|
41
|
+
});
|
|
42
|
+
afterEach(() => {
|
|
43
|
+
closers.splice(0).forEach((close) => close());
|
|
44
|
+
document.body.innerHTML = "";
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
/** A frame whose window refuses every read, as a cross-origin one does; its runtime serves this document. */
|
|
48
|
+
function opaqueFrame(): HTMLIFrameElement {
|
|
49
|
+
const blocked = () => {
|
|
50
|
+
throw new DOMException("blocked", "SecurityError");
|
|
51
|
+
};
|
|
52
|
+
const target: Record<string, unknown> = {};
|
|
53
|
+
const frame = new Proxy(target, {
|
|
54
|
+
get: (_, key) => (key === "postMessage" ? target.postMessage : blocked()),
|
|
55
|
+
});
|
|
56
|
+
const iframe = detachedFakeIframe(frame);
|
|
57
|
+
Object.defineProperty(iframe, "contentDocument", { value: null });
|
|
58
|
+
const parent = {
|
|
59
|
+
postMessage: (data: unknown, _origin: string, ports: MessagePort[]) =>
|
|
60
|
+
window.dispatchEvent(
|
|
61
|
+
new MessageEvent("message", { source: frame as unknown as Window, data, ports }),
|
|
62
|
+
),
|
|
63
|
+
} as unknown as Window;
|
|
64
|
+
const agent = installEditingAgent(parent);
|
|
65
|
+
target.postMessage = (data: unknown) =>
|
|
66
|
+
window.dispatchEvent(new MessageEvent("message", { source: parent, data }));
|
|
67
|
+
closers.push(() => agent.dispose());
|
|
68
|
+
return iframe;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function described(iframe: HTMLIFrameElement, selector: string) {
|
|
72
|
+
const handle = (await queryPreviewElements(iframe, selector))!.matches[0]!.handle;
|
|
73
|
+
return (await describePreviewElements(iframe, [handle]))![0]!;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function standIn(iframe: HTMLIFrameElement, selector: string): Promise<HTMLElement> {
|
|
77
|
+
return standInFor(iframe, await described(iframe, selector));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const settled = () => new Promise((resolve) => setTimeout(resolve, 20));
|
|
81
|
+
|
|
82
|
+
const RECT = { left: 10, top: 20, width: 100, height: 50, editScaleX: 0.5, editScaleY: 0.5 };
|
|
83
|
+
|
|
84
|
+
function dragMember(element: HTMLElement): ManualOffsetDragMember {
|
|
85
|
+
const selection = { element } as DomEditSelection;
|
|
86
|
+
const made = createManualOffsetDragMember({
|
|
87
|
+
key: "k",
|
|
88
|
+
selection,
|
|
89
|
+
element,
|
|
90
|
+
rect: RECT,
|
|
91
|
+
gesture: "drag",
|
|
92
|
+
});
|
|
93
|
+
if (!made.ok) throw new Error(made.reason);
|
|
94
|
+
return made.member;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const card = () => document.getElementById("card")!;
|
|
98
|
+
/** The test DOM keeps a calc() unsimplified, so each axis is read back as the sum of its px terms. */
|
|
99
|
+
const shown = (element: HTMLElement) =>
|
|
100
|
+
element.style
|
|
101
|
+
.getPropertyValue("translate")
|
|
102
|
+
.split(/ (?![^(]*\))/)
|
|
103
|
+
.map((axis) =>
|
|
104
|
+
[...axis.matchAll(/-?[\d.]+px/g)].reduce((sum, [px]) => sum + parseFloat(px), 0),
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
describe("gesture drafts on an opaque frame", () => {
|
|
108
|
+
it("moves the frame's element with a drag, and puts it back when the drag is cancelled", async () => {
|
|
109
|
+
const member = dragMember(await standIn(opaqueFrame(), "#card"));
|
|
110
|
+
applyManualOffsetDragDraft(member, 10, 0);
|
|
111
|
+
applyManualOffsetDragDraft(member, 40, 20);
|
|
112
|
+
await vi.waitFor(() => expect(shown(card())).toEqual([80, 40]));
|
|
113
|
+
restoreManualOffsetDragMembers([member]);
|
|
114
|
+
await vi.waitFor(() =>
|
|
115
|
+
expect(card().getAttribute("style")).toBe("width: 200px; height: 100px;"),
|
|
116
|
+
);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("holds a saved drag where it was dropped, and the next drag starts from there", async () => {
|
|
120
|
+
const element = await standIn(opaqueFrame(), "#card");
|
|
121
|
+
const first = dragMember(element);
|
|
122
|
+
applyManualOffsetDragCommit(first, 40, 20);
|
|
123
|
+
endManualOffsetDragMembers([first]);
|
|
124
|
+
await vi.waitFor(() => expect(shown(card())).toEqual([80, 40]));
|
|
125
|
+
const second = dragMember(element);
|
|
126
|
+
applyManualOffsetDragDraft(second, -10, 0);
|
|
127
|
+
await vi.waitFor(() => expect(shown(card())).toEqual([60, 40]));
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("draws a drag that grabs the layer again before the last save settles from where that one left it", async () => {
|
|
131
|
+
const element = await standIn(opaqueFrame(), "#card");
|
|
132
|
+
const first = dragMember(element);
|
|
133
|
+
applyManualOffsetDragCommit(first, 40, 20);
|
|
134
|
+
await vi.waitFor(() => expect(shown(card())).toEqual([80, 40]));
|
|
135
|
+
const second = dragMember(element);
|
|
136
|
+
applyManualOffsetDragDraft(second, 10, 0);
|
|
137
|
+
endManualOffsetDragMembers([first]);
|
|
138
|
+
await vi.waitFor(() => expect(shown(card())).toEqual([100, 40]));
|
|
139
|
+
applyManualOffsetDragCommit(second, 10, 0);
|
|
140
|
+
endManualOffsetDragMembers([second]);
|
|
141
|
+
await settled();
|
|
142
|
+
expect(shown(card())).toEqual([100, 40]);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("puts a move back on the frame when an undo painted over it does not go through", async () => {
|
|
146
|
+
const member = dragMember(await standIn(opaqueFrame(), "#card"));
|
|
147
|
+
applyManualOffsetDragCommit(member, 40, 20);
|
|
148
|
+
await vi.waitFor(() => expect(shown(card())).toEqual([80, 40]));
|
|
149
|
+
const putBack = manualOffsetMoveRevert([member])();
|
|
150
|
+
await vi.waitFor(() =>
|
|
151
|
+
expect(card().getAttribute("style")).toBe("width: 200px; height: 100px;"),
|
|
152
|
+
);
|
|
153
|
+
putBack();
|
|
154
|
+
await vi.waitFor(() => expect(shown(card())).toEqual([80, 40]));
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("moves a % translate from the px the frame renders it at, and keeps the author's value to reset to", async () => {
|
|
158
|
+
const iframe = opaqueFrame();
|
|
159
|
+
const options = { activeCompositionPath: "index.html", isMasterView: false };
|
|
160
|
+
const selection = await resolvePreviewSelection(
|
|
161
|
+
iframe,
|
|
162
|
+
await described(iframe, "#centred"),
|
|
163
|
+
options,
|
|
164
|
+
);
|
|
165
|
+
const member = dragMember(selection!.element);
|
|
166
|
+
expect(member.initialOffset).toEqual({ x: -100, y: -50 });
|
|
167
|
+
const offset = applyManualOffsetDragCommit(member, 40, 0);
|
|
168
|
+
expect(offset).toEqual({ x: -20, y: -50 });
|
|
169
|
+
expect(writePlainMove(selection!.element, offset).slice(0, 2)).toEqual([
|
|
170
|
+
{ type: "inline-style", property: "translate", value: "-20px -50px" },
|
|
171
|
+
{ type: "attribute", property: STUDIO_ORIGINAL_INLINE_TRANSLATE_ATTR, value: "-50% -50%" },
|
|
172
|
+
]);
|
|
173
|
+
const centred = document.getElementById("centred")!;
|
|
174
|
+
await vi.waitFor(() => expect(shown(centred)).toEqual([-20, -50]));
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("refuses to move a % translate it has no rendered px for, rather than drop it", async () => {
|
|
178
|
+
const element = await standIn(opaqueFrame(), "#centred");
|
|
179
|
+
const made = createManualOffsetDragMember({
|
|
180
|
+
key: "k",
|
|
181
|
+
selection: { element } as DomEditSelection,
|
|
182
|
+
element,
|
|
183
|
+
rect: RECT,
|
|
184
|
+
gesture: "drag",
|
|
185
|
+
});
|
|
186
|
+
expect(made.ok).toBe(false);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it("keeps the translate the author had before Studio moved the layer as the one to reset to", async () => {
|
|
190
|
+
const element = await standIn(opaqueFrame(), "#moved");
|
|
191
|
+
applyManualOffsetDragCommit(dragMember(element), 40, 0);
|
|
192
|
+
expect(element.getAttribute(STUDIO_ORIGINAL_INLINE_TRANSLATE_ATTR)).toBe("");
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("starts a drag on a freshly loaded frame from the translate its element has", async () => {
|
|
196
|
+
const member = dragMember(await standIn(opaqueFrame(), "#chip"));
|
|
197
|
+
expect(member.initialOffset).toEqual({ x: 30, y: 0 });
|
|
198
|
+
expect(applyManualOffsetDragCommit(member, 40, 0)).toEqual({ x: 110, y: 0 });
|
|
199
|
+
const chip = document.getElementById("chip")!;
|
|
200
|
+
await vi.waitFor(() => expect(shown(chip)).toEqual([110, 0]));
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("sends one request at a time and draws only the latest of the moves that waited", async () => {
|
|
204
|
+
const member = dragMember(await standIn(opaqueFrame(), "#card"));
|
|
205
|
+
vi.mocked(previewRequest).mockClear();
|
|
206
|
+
for (let dx = 2; dx <= 40; dx += 2) applyManualOffsetDragDraft(member, dx, 0);
|
|
207
|
+
await vi.waitFor(() => expect(shown(card())).toEqual([80, 0]));
|
|
208
|
+
expect(draftRequests()).toHaveLength(2);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it("grows a resized box about its centre without measuring the frame, and draws it there", async () => {
|
|
212
|
+
const iframe = opaqueFrame();
|
|
213
|
+
const element = await standIn(iframe, "#card");
|
|
214
|
+
const g = {
|
|
215
|
+
...{ originLeft: 10, originTop: 20, originWidth: 100, originHeight: 50 },
|
|
216
|
+
...{ actualWidth: 200, actualHeight: 100, editScaleX: 0.5, editScaleY: 0.5 },
|
|
217
|
+
actualRotation: 0,
|
|
218
|
+
pathOffsetMember: dragMember(element),
|
|
219
|
+
} as GestureState;
|
|
220
|
+
const size = { width: 300, height: 140 };
|
|
221
|
+
const rect = resolveResizeDraftRect(g, element, null, iframe, () => null, {
|
|
222
|
+
wanted: size,
|
|
223
|
+
written: size,
|
|
224
|
+
});
|
|
225
|
+
expect(rect).toMatchObject({ left: -15, top: 10, width: 150, height: 70, angle: 0 });
|
|
226
|
+
expect(g.lastResizeAnchor).toEqual({ dx: -25, dy: -10 });
|
|
227
|
+
await vi.waitFor(() => expect(card().style.width).toBe("300px"));
|
|
228
|
+
expect(card().style.height).toBe("140px");
|
|
229
|
+
expect(shown(card())).toEqual([-50, -20]);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it("keeps a saved resize with no offset member on the frame when a later restore runs", async () => {
|
|
233
|
+
const iframe = opaqueFrame();
|
|
234
|
+
const element = await standIn(iframe, "#card");
|
|
235
|
+
const before = captureStudioBoxSize(element);
|
|
236
|
+
const token = beginStudioManualEditGesture(element, "resize");
|
|
237
|
+
const g = {
|
|
238
|
+
...{ originLeft: 10, originTop: 20, originWidth: 100, originHeight: 50 },
|
|
239
|
+
...{ actualWidth: 200, actualHeight: 100, editScaleX: 0.5, editScaleY: 0.5 },
|
|
240
|
+
actualRotation: 0,
|
|
241
|
+
} as GestureState;
|
|
242
|
+
const size = { width: 300, height: 140 };
|
|
243
|
+
resolveResizeDraftRect(g, element, null, iframe, () => null, { wanted: size, written: size });
|
|
244
|
+
await vi.waitFor(() => expect(card().style.width).toBe("300px"));
|
|
245
|
+
endResizeWithoutMember(element, token);
|
|
246
|
+
restoreStudioBoxSize(element, before);
|
|
247
|
+
await settled();
|
|
248
|
+
expect(card().style.width).toBe("300px");
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it("leaves elements of a frame Studio scripts to the gesture alone", async () => {
|
|
252
|
+
vi.mocked(previewRequest).mockClear();
|
|
253
|
+
const member = dragMember(document.getElementById("chip")!);
|
|
254
|
+
applyManualOffsetDragDraft(member, 40, 20);
|
|
255
|
+
restoreManualOffsetDragMembers([member]);
|
|
256
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
257
|
+
expect(draftRequests()).toHaveLength(0);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it("reports a timeline rebind it cannot reach, so the caller syncs the preview another way", () => {
|
|
261
|
+
expect(applySoftReloadFinalization(opaqueFrame(), 1)).toBe(false);
|
|
262
|
+
});
|
|
263
|
+
});
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A gesture on a preview frame Studio cannot script edits the selection's stand-in element. What the
|
|
3
|
+
* gesture draws on the stand-in is drawn on the frame's own element too, over the frame's channel,
|
|
4
|
+
* so the preview follows the pointer. One request is in flight per frame; later draws wait and merge.
|
|
5
|
+
*/
|
|
6
|
+
import { MAX_DESCRIBE_HANDLES, type EditingDraft } from "@hyperframes/core/runtime/editing-port";
|
|
7
|
+
import { previewRequest } from "../../player/lib/previewChannel";
|
|
8
|
+
|
|
9
|
+
type Drawn = {
|
|
10
|
+
translate: { x: number; y: number } | null;
|
|
11
|
+
size: { width: number; height: number } | null;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
interface Queue {
|
|
15
|
+
ops: EditingDraft[];
|
|
16
|
+
drawn: Map<string, Drawn>;
|
|
17
|
+
sending: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const standIns = new WeakMap<Element, { iframe: HTMLIFrameElement; handle: string }>();
|
|
21
|
+
const queues = new WeakMap<HTMLIFrameElement, Queue>();
|
|
22
|
+
|
|
23
|
+
export function registerPreviewStandIn(
|
|
24
|
+
standIn: Element,
|
|
25
|
+
iframe: HTMLIFrameElement,
|
|
26
|
+
handle: string,
|
|
27
|
+
): void {
|
|
28
|
+
standIns.set(standIn, { iframe, handle });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function queueFor(iframe: HTMLIFrameElement): Queue {
|
|
32
|
+
let queue = queues.get(iframe);
|
|
33
|
+
if (!queue) {
|
|
34
|
+
queue = { ops: [], drawn: new Map(), sending: false };
|
|
35
|
+
queues.set(iframe, queue);
|
|
36
|
+
}
|
|
37
|
+
return queue;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function send(iframe: HTMLIFrameElement, queue: Queue): void {
|
|
41
|
+
if (queue.sending || !queue.ops.length) return;
|
|
42
|
+
const drafts = queue.ops.splice(0, MAX_DESCRIBE_HANDLES);
|
|
43
|
+
queue.sending = true;
|
|
44
|
+
void previewRequest(iframe, "draft", { drafts }).finally(() => {
|
|
45
|
+
queue.sending = false;
|
|
46
|
+
send(iframe, queue);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Draws `part` of the stand-in's draft on its frame element; any other element is left alone. */
|
|
51
|
+
export function drawPreviewDraft(element: Element, part: Partial<Drawn>): void {
|
|
52
|
+
const target = standIns.get(element);
|
|
53
|
+
if (!target) return;
|
|
54
|
+
const queue = queueFor(target.iframe);
|
|
55
|
+
const drawn = { translate: null, size: null, ...queue.drawn.get(target.handle), ...part };
|
|
56
|
+
queue.drawn.set(target.handle, drawn);
|
|
57
|
+
const op = { handle: target.handle, ...drawn };
|
|
58
|
+
const last = queue.ops.map((queued) => queued.handle).lastIndexOf(target.handle);
|
|
59
|
+
const waiting = queue.ops[last];
|
|
60
|
+
if (waiting && !("end" in waiting)) queue.ops[last] = op;
|
|
61
|
+
else queue.ops.push(op);
|
|
62
|
+
send(target.iframe, queue);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Ends the stand-in's draft on its frame: "keep" once saved or regrabbed mid-save, else "restore". */
|
|
66
|
+
export function endPreviewDraft(element: Element, end: "keep" | "restore"): void {
|
|
67
|
+
const target = standIns.get(element);
|
|
68
|
+
if (!target) return;
|
|
69
|
+
const queue = queueFor(target.iframe);
|
|
70
|
+
if (!queue.drawn.delete(target.handle)) return;
|
|
71
|
+
queue.ops.push({ handle: target.handle, end });
|
|
72
|
+
send(target.iframe, queue);
|
|
73
|
+
}
|
|
@@ -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
|
+
}
|