@hyperframes/studio 0.9.38 → 0.9.40
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-Dz2VdhdD.js → codec-BPK_2CLL.js} +1 -1
- package/dist/assets/{hyperframes-player-BkaWdGLq.js → hyperframes-player-D7qTTDIp.js} +1 -1
- package/dist/assets/{index-BJTCbv8z.css → index-4VGEAZyf.css} +1 -1
- package/dist/assets/{index-DsNxqAwr.js → index-CdA00Ybn.js} +145 -145
- package/dist/assets/{index-DONaC7VF.js → index-QS-DuxAU.js} +1 -1
- package/dist/assets/{pausedVideoDecoder-BGN2jKrN.js → pausedVideoDecoder-Byzz98Gb.js} +1 -1
- package/dist/{chunk-63DH4LRB.js → chunk-AV2PV7NE.js} +15 -68
- package/dist/chunk-FQ3VRKMC.js +148 -0
- package/dist/{chunk-YKBN653R.js → chunk-KF6YVXJP.js} +258 -1
- package/dist/{chunk-45KKBU63.js → chunk-VZ5MD32A.js} +1 -1
- package/dist/{domEditingLayers-7Q56QAU5.js → domEditingLayers-4KRREI5C.js} +2 -2
- package/dist/index.d.ts +3 -85
- package/dist/index.html +2 -2
- package/dist/index.js +321 -341
- package/dist/keys-CaWcxUST.d.ts +86 -0
- package/dist/keys.d.ts +1 -0
- package/dist/keys.js +11 -0
- package/dist/player.js +2 -2
- package/dist/styles.css +1 -1
- package/package.json +12 -8
- package/src/components/editor/DomEditGestureReadout.test.tsx +204 -20
- package/src/components/editor/DomEditGestureReadout.tsx +36 -18
- package/src/components/editor/InlineTextToolbar.tsx +2 -2
- package/src/components/editor/domEditGestureReadoutPlacement.ts +144 -0
- package/src/components/editor/domEditGestureReadoutState.ts +18 -8
- package/src/components/editor/domEditingDom.ts +23 -104
- package/src/components/editor/useDomEditCompositionRect.ts +13 -4
- package/src/components/editor/useDomEditOverlayGestures.ts +1 -1
- package/src/components/editor/usePreviewCompositionRect.test.tsx +39 -0
- package/src/hooks/elementPickerStyles.ts +53 -0
- package/src/hooks/useElementPicker.test.ts +83 -3
- package/src/hooks/useElementPicker.ts +27 -66
- package/src/keys.test.ts +53 -0
- package/src/keys.ts +11 -0
- package/src/player/components/Player.tsx +2 -0
- package/src/player/components/studioKeyDefaults.ts +68 -0
- package/src/player/components/studioShortcuts.ts +2 -67
- package/src/player/hooks/playbackAdapterResolution.ts +15 -0
- package/src/player/hooks/previewMessageRouter.manifest.test.ts +67 -0
- package/src/player/hooks/previewMessageRouter.ts +38 -8
- package/src/player/hooks/timelinePlayerTestHarness.ts +26 -1
- package/src/player/hooks/useShadowPreviewReload.ts +4 -1
- package/src/player/hooks/useTimelinePlayer.ts +5 -9
- package/src/player/lib/bridgePlayback.ts +156 -0
- package/src/player/lib/previewChannel.test.ts +260 -0
- package/src/player/lib/previewChannel.ts +115 -0
- package/src/player/lib/previewElements.test.ts +167 -0
- package/src/player/lib/previewElements.ts +117 -0
- package/src/player/sceneSwap.test.ts +12 -0
- package/src/player/sceneSwap.ts +2 -0
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
|
|
3
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
4
|
+
import {
|
|
5
|
+
AGENT_PROBE_MS,
|
|
6
|
+
previewClipManifest,
|
|
7
|
+
previewEditingClient,
|
|
8
|
+
previewFrameOpaque,
|
|
9
|
+
previewSeekPainted,
|
|
10
|
+
setFrameLoopStart,
|
|
11
|
+
} from "./previewChannel";
|
|
12
|
+
import { bridgePlaybackAdapter, watchPreviewFrame } from "./bridgePlayback";
|
|
13
|
+
import { runtimeProtocolMetadata } from "@hyperframes/core/runtime/protocol";
|
|
14
|
+
import { resolveFramePlaybackAdapter } from "../hooks/playbackAdapterResolution";
|
|
15
|
+
import { detachedFakeIframe, serveEditingRequests } from "../hooks/timelinePlayerTestHarness";
|
|
16
|
+
|
|
17
|
+
const closers: Array<() => void> = [];
|
|
18
|
+
afterEach(() => closers.splice(0).forEach((close) => close()));
|
|
19
|
+
|
|
20
|
+
/** A frame on another origin: any read of its window throws, as a browser's does. */
|
|
21
|
+
function opaqueFrame() {
|
|
22
|
+
const posted: unknown[] = [];
|
|
23
|
+
const postMessage = (message: unknown) => posted.push(message);
|
|
24
|
+
const frame = new Proxy({} as Record<string, unknown>, {
|
|
25
|
+
get: (_target, key) => {
|
|
26
|
+
if (key === "postMessage") return postMessage;
|
|
27
|
+
throw new DOMException(`blocked read of ${String(key)}`, "SecurityError");
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
const iframe = document.createElement("iframe");
|
|
31
|
+
iframe.setAttribute("sandbox", "allow-scripts");
|
|
32
|
+
Object.defineProperty(iframe, "contentWindow", { value: frame });
|
|
33
|
+
Object.defineProperty(iframe, "contentDocument", { value: null });
|
|
34
|
+
return { iframe, frame, posted };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Posts from `frame` as its runtime does, from an opaque origin unless told otherwise. */
|
|
38
|
+
function fromFrame(frame: object, data: Record<string, unknown>, origin = "null") {
|
|
39
|
+
const source = frame as Window;
|
|
40
|
+
window.dispatchEvent(
|
|
41
|
+
new MessageEvent("message", { source, origin, data: { source: "hf-preview", ...data } }),
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** A fake frame, same-origin unless `opaque`, whose runtime answers editing requests with `answer`. */
|
|
46
|
+
function servedFrame(answer: (kind: string, params: unknown) => unknown, opaque = false) {
|
|
47
|
+
const frame: Record<string, unknown> = {};
|
|
48
|
+
const iframe = detachedFakeIframe(frame);
|
|
49
|
+
if (opaque) {
|
|
50
|
+
Object.defineProperty(iframe, "contentDocument", { value: null });
|
|
51
|
+
Object.defineProperty(frame, "document", {
|
|
52
|
+
get: () => {
|
|
53
|
+
throw new DOMException("blocked", "SecurityError");
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
const handed = vi.fn(answer);
|
|
58
|
+
closers.push(serveEditingRequests(frame, handed));
|
|
59
|
+
return { iframe, frame, handed };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
describe("previewFrameOpaque", () => {
|
|
63
|
+
it("is true only for a frame whose window refuses a read", () => {
|
|
64
|
+
expect(previewFrameOpaque(document.body.appendChild(document.createElement("iframe")))).toBe(
|
|
65
|
+
false,
|
|
66
|
+
);
|
|
67
|
+
expect(previewFrameOpaque(document.createElement("iframe"))).toBe(false);
|
|
68
|
+
expect(previewFrameOpaque(opaqueFrame().iframe)).toBe(true);
|
|
69
|
+
// A window that answers reads is not opaque, even with no document to hand.
|
|
70
|
+
const readable = document.createElement("iframe");
|
|
71
|
+
Object.defineProperty(readable, "contentWindow", { value: {} });
|
|
72
|
+
expect(previewFrameOpaque(readable)).toBe(false);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe("previewEditingClient", () => {
|
|
77
|
+
it("keeps one client per frame load and opens a fresh one after the frame loads again", async () => {
|
|
78
|
+
const { iframe, handed } = servedFrame(() => ({ waited: false }));
|
|
79
|
+
const first = previewEditingClient(iframe);
|
|
80
|
+
expect(previewEditingClient(iframe)).toBe(first);
|
|
81
|
+
iframe.dispatchEvent(new Event("load"));
|
|
82
|
+
await expect(first.request("wait-for-seek", {})).rejects.toMatchObject({ code: "disposed" });
|
|
83
|
+
const second = previewEditingClient(iframe);
|
|
84
|
+
expect(second).not.toBe(first);
|
|
85
|
+
await expect(second.request("wait-for-seek", {})).resolves.toEqual({ waited: false });
|
|
86
|
+
expect(handed).toHaveBeenCalledOnce();
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("sends an opaque frame's loop start over the port, only when it changes", async () => {
|
|
90
|
+
const seen: unknown[] = [];
|
|
91
|
+
const { iframe } = servedFrame((_kind, params) => {
|
|
92
|
+
seen.push((params as { seconds: unknown }).seconds);
|
|
93
|
+
return { applied: true };
|
|
94
|
+
}, true);
|
|
95
|
+
setFrameLoopStart(iframe, 2);
|
|
96
|
+
setFrameLoopStart(iframe, 2);
|
|
97
|
+
setFrameLoopStart(iframe, null);
|
|
98
|
+
await vi.waitFor(() => expect(seen).toEqual([2, null]));
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("calls a scriptable frame's own loop-start hook and never opens a port", async () => {
|
|
102
|
+
const { iframe, frame, handed } = servedFrame(() => ({ applied: true }));
|
|
103
|
+
const setLoopStart = vi.fn();
|
|
104
|
+
frame.__hf = { setLoopStart };
|
|
105
|
+
setFrameLoopStart(iframe, 2);
|
|
106
|
+
setFrameLoopStart(iframe, 2);
|
|
107
|
+
expect(setLoopStart.mock.calls).toEqual([[2], [2]]);
|
|
108
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
109
|
+
expect(handed).not.toHaveBeenCalled();
|
|
110
|
+
delete frame.__hf;
|
|
111
|
+
expect(() => setFrameLoopStart(iframe, null)).not.toThrow();
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("waits for the runtime's seek to paint, and settles anyway when no runtime answers", async () => {
|
|
115
|
+
const { iframe, handed } = servedFrame(() => ({ waited: true }));
|
|
116
|
+
await previewSeekPainted(iframe);
|
|
117
|
+
expect(handed).toHaveBeenCalledWith("wait-for-seek", {});
|
|
118
|
+
const started = Date.now();
|
|
119
|
+
await previewSeekPainted(opaqueFrame().iframe, 15_000);
|
|
120
|
+
expect(Date.now() - started).toBeLessThan(AGENT_PROBE_MS + 500);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("returns the runtime's manifest only when its reply has the manifest's shape", async () => {
|
|
124
|
+
const manifest = { clips: [], durationInFrames: 30, scenes: [] };
|
|
125
|
+
let reply: unknown = { clipManifest: manifest };
|
|
126
|
+
const { iframe } = servedFrame(() => reply);
|
|
127
|
+
expect(await previewClipManifest(iframe)).toEqual(manifest);
|
|
128
|
+
for (const bad of [
|
|
129
|
+
null,
|
|
130
|
+
{ clipManifest: "x" },
|
|
131
|
+
{ clipManifest: { clips: {}, durationInFrames: 30 } },
|
|
132
|
+
{ clipManifest: { clips: [], durationInFrames: "30" } },
|
|
133
|
+
]) {
|
|
134
|
+
reply = bad;
|
|
135
|
+
expect(await previewClipManifest(iframe)).toBeNull();
|
|
136
|
+
}
|
|
137
|
+
reply = { clipManifest: { clips: [], durationInFrames: Infinity } };
|
|
138
|
+
expect(await previewClipManifest(iframe)).toBeNull();
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
describe("bridge playback for a frame Studio cannot script", () => {
|
|
143
|
+
const refs = () => ({ cache: { current: null }, warned: { current: false } });
|
|
144
|
+
|
|
145
|
+
it("drives the runtime with control messages and never reads the frame's window", () => {
|
|
146
|
+
const { iframe, posted } = opaqueFrame();
|
|
147
|
+
const adapter = resolveFramePlaybackAdapter(iframe, refs());
|
|
148
|
+
expect(adapter).not.toBeNull();
|
|
149
|
+
adapter!.play();
|
|
150
|
+
adapter!.seek(4);
|
|
151
|
+
adapter!.pause();
|
|
152
|
+
expect(posted).toEqual([
|
|
153
|
+
expect.objectContaining({ source: "hf-parent", type: "control", action: "play" }),
|
|
154
|
+
expect.objectContaining({ action: "seek", timeSeconds: 4 }),
|
|
155
|
+
expect.objectContaining({ action: "pause" }),
|
|
156
|
+
]);
|
|
157
|
+
expect(adapter!.getTime()).toBe(4);
|
|
158
|
+
expect(adapter!.isPlaying()).toBe(false);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("keeps a seek until the runtime confirms it, so a state posted before it cannot undo it", () => {
|
|
162
|
+
const { iframe, frame } = opaqueFrame();
|
|
163
|
+
const adapter = bridgePlaybackAdapter(iframe)!;
|
|
164
|
+
const clock = vi.spyOn(performance, "now").mockReturnValue(1_000);
|
|
165
|
+
try {
|
|
166
|
+
adapter.seek(4);
|
|
167
|
+
fromFrame(frame, { type: "state", currentTime: 1, isPlaying: false });
|
|
168
|
+
expect(adapter.getTime()).toBe(4);
|
|
169
|
+
fromFrame(frame, { type: "state", currentTime: 4, isPlaying: false });
|
|
170
|
+
fromFrame(frame, { type: "state", currentTime: 4.5, isPlaying: true });
|
|
171
|
+
expect(adapter.getTime()).toBe(4.5);
|
|
172
|
+
adapter.seek(99);
|
|
173
|
+
clock.mockReturnValue(2_000);
|
|
174
|
+
fromFrame(frame, { type: "state", currentTime: 12, isPlaying: false });
|
|
175
|
+
expect(adapter.getTime()).toBe(12);
|
|
176
|
+
} finally {
|
|
177
|
+
clock.mockRestore();
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("takes a seek the runtime landed on the frame before it, and plays on", () => {
|
|
182
|
+
const { iframe, frame } = opaqueFrame();
|
|
183
|
+
const adapter = bridgePlaybackAdapter(iframe)!;
|
|
184
|
+
const at30fps = runtimeProtocolMetadata(30);
|
|
185
|
+
adapter.seek(4.01);
|
|
186
|
+
fromFrame(frame, { type: "state", currentTime: 4, isPlaying: true, ...at30fps });
|
|
187
|
+
expect(adapter.getTime()).toBe(4);
|
|
188
|
+
expect(adapter.isPlaying()).toBe(true);
|
|
189
|
+
fromFrame(frame, { type: "state", currentTime: 4.3, isPlaying: true, ...at30fps });
|
|
190
|
+
expect(adapter.getTime()).toBe(4.3);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("ignores a post from a window it was not built for, or from an origin its frame cannot have", () => {
|
|
194
|
+
const opaque = opaqueFrame();
|
|
195
|
+
const adapter = bridgePlaybackAdapter(opaque.iframe)!;
|
|
196
|
+
const stranger = opaqueFrame();
|
|
197
|
+
fromFrame(stranger.frame, { type: "state", currentTime: 9, isPlaying: true });
|
|
198
|
+
fromFrame(opaque.frame, { type: "state", currentTime: 8, isPlaying: true }, location.origin);
|
|
199
|
+
fromFrame(
|
|
200
|
+
opaque.frame,
|
|
201
|
+
{ type: "state", currentTime: 7, isPlaying: true },
|
|
202
|
+
"https://example.com",
|
|
203
|
+
);
|
|
204
|
+
expect(adapter.getTime()).toBe(0);
|
|
205
|
+
// A post from a frame nobody registered is dropped, not kept for a later adapter.
|
|
206
|
+
expect(bridgePlaybackAdapter(stranger.iframe)!.getTime()).toBe(0);
|
|
207
|
+
fromFrame(opaque.frame, { type: "state", currentTime: 2, isPlaying: false });
|
|
208
|
+
expect(adapter.getTime()).toBe(2);
|
|
209
|
+
|
|
210
|
+
const sameOrigin = document.createElement("iframe");
|
|
211
|
+
sameOrigin.src = "/preview/index.html";
|
|
212
|
+
const frame = {};
|
|
213
|
+
Object.defineProperty(sameOrigin, "contentWindow", { value: frame });
|
|
214
|
+
watchPreviewFrame(sameOrigin);
|
|
215
|
+
const own = bridgePlaybackAdapter(sameOrigin)!;
|
|
216
|
+
fromFrame(frame, { type: "state", currentTime: 5, isPlaying: false }, "null");
|
|
217
|
+
expect(own.getTime()).toBe(0);
|
|
218
|
+
fromFrame(frame, { type: "state", currentTime: 5, isPlaying: false }, location.origin);
|
|
219
|
+
expect(own.getTime()).toBe(5);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it.each([
|
|
223
|
+
["srcdoc", (f: HTMLIFrameElement) => f.setAttribute("srcdoc", "<p>x</p>"), location.origin],
|
|
224
|
+
[
|
|
225
|
+
"about:blank",
|
|
226
|
+
(f: HTMLIFrameElement) => f.setAttribute("src", "about:blank"),
|
|
227
|
+
location.origin,
|
|
228
|
+
],
|
|
229
|
+
["a data: URL", (f: HTMLIFrameElement) => f.setAttribute("src", "data:text/html,x"), "null"],
|
|
230
|
+
[
|
|
231
|
+
"another origin",
|
|
232
|
+
(f: HTMLIFrameElement) => f.setAttribute("src", "https://a.test/p"),
|
|
233
|
+
"https://a.test",
|
|
234
|
+
],
|
|
235
|
+
])("accepts a frame loading %s only from the origin it has", (_name, load, origin) => {
|
|
236
|
+
const iframe = document.createElement("iframe");
|
|
237
|
+
load(iframe);
|
|
238
|
+
const frame = {};
|
|
239
|
+
Object.defineProperty(iframe, "contentWindow", { value: frame });
|
|
240
|
+
const adapter = bridgePlaybackAdapter(iframe)!;
|
|
241
|
+
const wrong = origin === "null" ? location.origin : "null";
|
|
242
|
+
fromFrame(frame, { type: "state", currentTime: 3, isPlaying: false }, wrong);
|
|
243
|
+
expect(adapter.getTime()).toBe(0);
|
|
244
|
+
fromFrame(frame, { type: "state", currentTime: 3, isPlaying: false }, origin);
|
|
245
|
+
expect(adapter.getTime()).toBe(3);
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it("takes time, play state and duration from its own frame's state and timeline messages", () => {
|
|
249
|
+
const { iframe, frame } = opaqueFrame();
|
|
250
|
+
const other = opaqueFrame();
|
|
251
|
+
const adapter = bridgePlaybackAdapter(iframe)!;
|
|
252
|
+
fromFrame(frame, { type: "timeline", durationSeconds: 12, clips: [] });
|
|
253
|
+
fromFrame(frame, { type: "state", currentTime: 3.5, isPlaying: true });
|
|
254
|
+
fromFrame(other.frame, { type: "state", currentTime: 9, isPlaying: false });
|
|
255
|
+
fromFrame(frame, { type: "state", currentTime: Number.NaN, isPlaying: true });
|
|
256
|
+
expect(adapter.getDuration()).toBe(12);
|
|
257
|
+
expect(adapter.getTime()).toBe(3.5);
|
|
258
|
+
expect(adapter.isPlaying()).toBe(true);
|
|
259
|
+
});
|
|
260
|
+
});
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// Runtime-state reads over a preview frame's editing port; an opaque frame's window is out of reach.
|
|
2
|
+
import {
|
|
3
|
+
createEditingPortClient,
|
|
4
|
+
type EditingParams,
|
|
5
|
+
type EditingPortClient,
|
|
6
|
+
type EditingRequestKind,
|
|
7
|
+
type EditingResult,
|
|
8
|
+
} from "@hyperframes/core/runtime/editing-port";
|
|
9
|
+
import type { ClipManifest, IframeWindow } from "./playbackTypes";
|
|
10
|
+
|
|
11
|
+
const clients = new WeakMap<HTMLIFrameElement, EditingPortClient>();
|
|
12
|
+
const loopStarts = new WeakMap<EditingPortClient, number | null>();
|
|
13
|
+
|
|
14
|
+
/** A frame whose window refuses Studio's reads; one with no window has nothing to reach either way. */
|
|
15
|
+
export function previewFrameOpaque(iframe: HTMLIFrameElement): boolean {
|
|
16
|
+
const win = iframe.contentWindow;
|
|
17
|
+
if (!win) return false;
|
|
18
|
+
try {
|
|
19
|
+
void win.document;
|
|
20
|
+
return false;
|
|
21
|
+
} catch {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** One client per frame load: the frame's next load disposes it. */
|
|
27
|
+
export function previewEditingClient(iframe: HTMLIFrameElement): EditingPortClient {
|
|
28
|
+
const open = clients.get(iframe);
|
|
29
|
+
if (open) return open;
|
|
30
|
+
const client = createEditingPortClient({ iframe });
|
|
31
|
+
clients.set(iframe, client);
|
|
32
|
+
const onLoad = () => {
|
|
33
|
+
client.dispose();
|
|
34
|
+
if (clients.get(iframe) === client) clients.delete(iframe);
|
|
35
|
+
};
|
|
36
|
+
iframe.addEventListener("load", onLoad, { once: true });
|
|
37
|
+
return client;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function setFrameLoopStart(iframe: HTMLIFrameElement, seconds: number | null): void {
|
|
41
|
+
if (!previewFrameOpaque(iframe)) {
|
|
42
|
+
try {
|
|
43
|
+
(iframe.contentWindow as IframeWindow | null)?.__hf?.setLoopStart?.(seconds);
|
|
44
|
+
} catch {}
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const client = previewEditingClient(iframe);
|
|
48
|
+
if (loopStarts.has(client) && loopStarts.get(client) === seconds) return;
|
|
49
|
+
loopStarts.set(client, seconds);
|
|
50
|
+
client.request("set-loop-start", { seconds }).catch(() => {
|
|
51
|
+
if (loopStarts.get(client) === seconds) loopStarts.delete(client);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export const AGENT_PROBE_MS = 1_000;
|
|
56
|
+
|
|
57
|
+
async function agentConnected(client: EditingPortClient): Promise<boolean> {
|
|
58
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
59
|
+
const probe = new Promise<boolean>(
|
|
60
|
+
(resolve) => (timer = setTimeout(resolve, AGENT_PROBE_MS, false)),
|
|
61
|
+
);
|
|
62
|
+
const connected = await Promise.race([
|
|
63
|
+
client.ready.then(
|
|
64
|
+
() => true,
|
|
65
|
+
() => false,
|
|
66
|
+
),
|
|
67
|
+
probe,
|
|
68
|
+
]);
|
|
69
|
+
clearTimeout(timer);
|
|
70
|
+
return connected;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Null when no port comes within AGENT_PROBE_MS (a runtime without the editing agent), or on error. */
|
|
74
|
+
export async function previewRequest<K extends EditingRequestKind>(
|
|
75
|
+
iframe: HTMLIFrameElement,
|
|
76
|
+
kind: K,
|
|
77
|
+
params: EditingParams<K>,
|
|
78
|
+
timeoutMs?: number,
|
|
79
|
+
): Promise<EditingResult<K> | null> {
|
|
80
|
+
const client = previewEditingClient(iframe);
|
|
81
|
+
if (!(await agentConnected(client))) return null;
|
|
82
|
+
return client.request(kind, params, { timeoutMs }).catch(() => null);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export async function previewSeekPainted(iframe: HTMLIFrameElement, timeoutMs?: number) {
|
|
86
|
+
await previewRequest(iframe, "wait-for-seek", {}, timeoutMs);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** The reply is frame data, so its shape is checked. */
|
|
90
|
+
export async function previewClipManifest(iframe: HTMLIFrameElement): Promise<ClipManifest | null> {
|
|
91
|
+
const snapshot = await previewRequest(iframe, "runtime-snapshot", {});
|
|
92
|
+
const manifest: unknown = snapshot?.clipManifest;
|
|
93
|
+
if (typeof manifest !== "object" || manifest === null) return null;
|
|
94
|
+
const { clips, durationInFrames } = manifest as Partial<ClipManifest>;
|
|
95
|
+
if (!Array.isArray(clips) || typeof durationInFrames !== "number") return null;
|
|
96
|
+
if (!Number.isFinite(durationInFrames)) return null;
|
|
97
|
+
return manifest as ClipManifest;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** The runtime's computed `properties` for the element `selector` names; null when it has none. */
|
|
101
|
+
export async function previewComputedStyles(
|
|
102
|
+
iframe: HTMLIFrameElement,
|
|
103
|
+
selector: string,
|
|
104
|
+
properties: string[],
|
|
105
|
+
): Promise<Record<string, string> | null> {
|
|
106
|
+
const reply = await previewRequest(iframe, "computed-styles", { selector, properties });
|
|
107
|
+
const styles: unknown = reply?.styles;
|
|
108
|
+
if (typeof styles !== "object" || styles === null) return null;
|
|
109
|
+
const read: Record<string, string> = {};
|
|
110
|
+
for (const name of properties) {
|
|
111
|
+
const value: unknown = Object.getOwnPropertyDescriptor(styles, name)?.value;
|
|
112
|
+
if (typeof value === "string" && value) read[name] = value;
|
|
113
|
+
}
|
|
114
|
+
return read;
|
|
115
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
|
|
3
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
4
|
+
import type { EditingElementDescription } from "@hyperframes/core/runtime/editing-port";
|
|
5
|
+
import {
|
|
6
|
+
describePreviewElements,
|
|
7
|
+
previewElementSelectorIndex,
|
|
8
|
+
previewElementSourceFile,
|
|
9
|
+
previewElementsAtPoint,
|
|
10
|
+
readPreviewElement,
|
|
11
|
+
type PreviewElement,
|
|
12
|
+
} from "./previewElements";
|
|
13
|
+
import { AGENT_PROBE_MS, previewComputedStyles } from "./previewChannel";
|
|
14
|
+
import {
|
|
15
|
+
getSelectorIndex,
|
|
16
|
+
getSourceFileForElement,
|
|
17
|
+
setCompositionSourceMap,
|
|
18
|
+
} from "../../components/editor/domEditingDom";
|
|
19
|
+
import { detachedFakeIframe, serveEditingRequests } from "../hooks/timelinePlayerTestHarness";
|
|
20
|
+
|
|
21
|
+
const closers: Array<() => void> = [];
|
|
22
|
+
afterEach(() => {
|
|
23
|
+
closers.splice(0).forEach((close) => close());
|
|
24
|
+
setCompositionSourceMap(new Map());
|
|
25
|
+
document.body.innerHTML = "";
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
/** A frame Studio cannot read; `answer` is its runtime, or it has none. */
|
|
29
|
+
function opaqueFrame(answer?: (kind: string, params: unknown) => unknown) {
|
|
30
|
+
const frame: Record<string, unknown> = { postMessage: () => {} };
|
|
31
|
+
const iframe = detachedFakeIframe(frame);
|
|
32
|
+
Object.defineProperty(iframe, "contentDocument", { value: null });
|
|
33
|
+
Object.defineProperty(frame, "document", {
|
|
34
|
+
get: () => {
|
|
35
|
+
throw new DOMException("blocked", "SecurityError");
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
const handed = vi.fn(answer ?? (() => null));
|
|
39
|
+
if (answer) closers.push(serveEditingRequests(frame, handed));
|
|
40
|
+
return { iframe, handed };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const NO_SOURCE = { file: null, compositionId: null, originalCompositionId: null };
|
|
44
|
+
|
|
45
|
+
const LOAD = "0123456789abcdef";
|
|
46
|
+
const h = (n: number) => `${LOAD}:${n}`;
|
|
47
|
+
|
|
48
|
+
function described(n: number, extra: Partial<EditingElementDescription> = {}) {
|
|
49
|
+
return {
|
|
50
|
+
handle: h(n),
|
|
51
|
+
parent: null,
|
|
52
|
+
tag: "div",
|
|
53
|
+
id: null,
|
|
54
|
+
selector: null,
|
|
55
|
+
matchesBefore: null,
|
|
56
|
+
source: NO_SOURCE,
|
|
57
|
+
locked: false,
|
|
58
|
+
attributes: [["data-hf-id", `hf-${n}`]],
|
|
59
|
+
inlineStyles: { left: "4px" },
|
|
60
|
+
text: "",
|
|
61
|
+
children: [{ text: "Hi" }, { handle: h(n + 1) }],
|
|
62
|
+
rect: { x: 1, y: 2, width: 3, height: 4 },
|
|
63
|
+
truncated: false,
|
|
64
|
+
...extra,
|
|
65
|
+
} as EditingElementDescription;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
describe("readPreviewElement", () => {
|
|
69
|
+
it("takes a description with the wire shape and refuses anything off it", () => {
|
|
70
|
+
expect(readPreviewElement(described(1))).toEqual(described(1));
|
|
71
|
+
for (const bad of [
|
|
72
|
+
null,
|
|
73
|
+
[],
|
|
74
|
+
described(1, { handle: 1 as never }),
|
|
75
|
+
described(1, { handle: "1" }),
|
|
76
|
+
described(1, { handle: `${LOAD}:0` }),
|
|
77
|
+
described(1, { handle: `${LOAD.toUpperCase()}:1` }),
|
|
78
|
+
described(1, { parent: "x" }),
|
|
79
|
+
described(1, { tag: 1 as never }),
|
|
80
|
+
described(1, { source: { file: 1 } as never }),
|
|
81
|
+
described(1, { locked: "yes" as never }),
|
|
82
|
+
described(1, { attributes: [["a"]] as never }),
|
|
83
|
+
described(1, { inlineStyles: { left: 4 } as never }),
|
|
84
|
+
described(1, { children: [{ node: {} }] as never }),
|
|
85
|
+
described(1, { matchesBefore: [{ source: NO_SOURCE, count: -1 }] }),
|
|
86
|
+
described(1, { rect: { x: Number.NaN, y: 0, width: 0, height: 0 } }),
|
|
87
|
+
described(1, { truncated: undefined as never }),
|
|
88
|
+
]) {
|
|
89
|
+
expect(readPreviewElement(bad)).toBeNull();
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe("element requests over an opaque frame's port", () => {
|
|
95
|
+
it("asks for the stack under a point and keeps only well-formed entries", async () => {
|
|
96
|
+
const hit = { handle: h(1), tag: "p", rect: { x: 1, y: 2, width: 3, height: 4 } };
|
|
97
|
+
const bad = [
|
|
98
|
+
{ ...hit, handle: "x" },
|
|
99
|
+
{ ...hit, tag: 1 },
|
|
100
|
+
{ ...hit, rect: { x: 1 } },
|
|
101
|
+
];
|
|
102
|
+
const { iframe, handed } = opaqueFrame(() => ({ elements: [hit, ...bad] }));
|
|
103
|
+
expect(await previewElementsAtPoint(iframe, 10, 20)).toEqual([hit]);
|
|
104
|
+
expect(handed).toHaveBeenCalledWith("elements-at-point", { x: 10, y: 20 });
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("describes handles in order, with null for a gone element or a mismatched entry", async () => {
|
|
108
|
+
let reply: unknown = { elements: [described(2), null, described(9)] };
|
|
109
|
+
const { iframe, handed } = opaqueFrame(() => reply);
|
|
110
|
+
const asked = [h(2), h(3), h(4)];
|
|
111
|
+
expect(await describePreviewElements(iframe, asked)).toEqual([described(2), null, null]);
|
|
112
|
+
expect(handed).toHaveBeenCalledWith("describe", { handles: asked });
|
|
113
|
+
reply = { elements: [described(2)] };
|
|
114
|
+
expect(await describePreviewElements(iframe, [h(2), h(3)])).toBeNull();
|
|
115
|
+
handed.mockClear();
|
|
116
|
+
expect(await describePreviewElements(iframe, [])).toBeNull();
|
|
117
|
+
expect(await describePreviewElements(iframe, Array(257).fill(h(1)))).toBeNull();
|
|
118
|
+
expect(handed).not.toHaveBeenCalled();
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("gives up after the agent probe when the frame's runtime has no editing agent", async () => {
|
|
122
|
+
const { iframe } = opaqueFrame();
|
|
123
|
+
const started = Date.now();
|
|
124
|
+
const answers = await Promise.all([
|
|
125
|
+
previewComputedStyles(iframe, "#a", ["color"]),
|
|
126
|
+
previewElementsAtPoint(iframe, 1, 1),
|
|
127
|
+
describePreviewElements(iframe, [h(1)]),
|
|
128
|
+
]);
|
|
129
|
+
expect(answers).toEqual([null, null, null]);
|
|
130
|
+
expect(Date.now() - started).toBeLessThan(AGENT_PROBE_MS + 500);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
describe("source file and selector index from a description", () => {
|
|
135
|
+
it("answers what Studio reads off the frame's own element", () => {
|
|
136
|
+
setCompositionSourceMap(new Map([["card", "cards.html"]]));
|
|
137
|
+
document.body.innerHTML = `
|
|
138
|
+
<div class="tile"></div>
|
|
139
|
+
<div data-composition-file="cards.html"><div class="tile"></div></div>
|
|
140
|
+
<div data-composition-id="card__hf2" data-hf-original-composition-id="card">
|
|
141
|
+
<div class="tile"></div><div class="tile"></div>
|
|
142
|
+
</div>`;
|
|
143
|
+
const target = document.querySelectorAll<HTMLElement>(".tile")[3]!;
|
|
144
|
+
const element = readPreviewElement(
|
|
145
|
+
described(7, {
|
|
146
|
+
selector: ".tile",
|
|
147
|
+
source: { file: null, compositionId: "card__hf2", originalCompositionId: "card" },
|
|
148
|
+
// As the runtime counts the matches before it: one per distinct source.
|
|
149
|
+
matchesBefore: [
|
|
150
|
+
{ source: NO_SOURCE, count: 1 },
|
|
151
|
+
{ source: { ...NO_SOURCE, file: "cards.html" }, count: 1 },
|
|
152
|
+
{
|
|
153
|
+
source: { file: null, compositionId: "card__hf2", originalCompositionId: "card" },
|
|
154
|
+
count: 1,
|
|
155
|
+
},
|
|
156
|
+
],
|
|
157
|
+
}),
|
|
158
|
+
) as PreviewElement;
|
|
159
|
+
const { sourceFile } = getSourceFileForElement(target, "index.html");
|
|
160
|
+
expect(previewElementSourceFile(element, "index.html")).toBe(sourceFile);
|
|
161
|
+
expect(sourceFile).toBe("cards.html");
|
|
162
|
+
const index = getSelectorIndex(document, target, ".tile", sourceFile, "index.html");
|
|
163
|
+
expect(previewElementSelectorIndex(element, "index.html")).toBe(index);
|
|
164
|
+
expect(index).toBe(2);
|
|
165
|
+
expect(previewElementSelectorIndex({ ...element, matchesBefore: null }, null)).toBeUndefined();
|
|
166
|
+
});
|
|
167
|
+
});
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Elements of a preview frame Studio cannot script, by the handles its runtime gives them. A handle
|
|
3
|
+
* names an element for one load of the frame; after the frame loads again, describe it afresh.
|
|
4
|
+
*/
|
|
5
|
+
import {
|
|
6
|
+
EDITING_ELEMENT_HANDLE,
|
|
7
|
+
MAX_DESCRIBE_HANDLES,
|
|
8
|
+
type EditingElementDescription,
|
|
9
|
+
type EditingElementHit,
|
|
10
|
+
type EditingElementSource,
|
|
11
|
+
} from "@hyperframes/core/runtime/editing-port";
|
|
12
|
+
import { resolveElementSourceFile } from "../../components/editor/domEditingDom";
|
|
13
|
+
import { previewRequest } from "./previewChannel";
|
|
14
|
+
|
|
15
|
+
declare const checked: unique symbol;
|
|
16
|
+
/** A description that passed `readPreviewElement`. Only that function makes one. */
|
|
17
|
+
export type PreviewElement = EditingElementDescription & { readonly [checked]: true };
|
|
18
|
+
export type PreviewElementHit = EditingElementHit & { readonly [checked]: true };
|
|
19
|
+
|
|
20
|
+
type Fields = Record<string, unknown>;
|
|
21
|
+
const isFields = (value: unknown): value is Fields =>
|
|
22
|
+
typeof value === "object" && value !== null && !Array.isArray(value);
|
|
23
|
+
const isHandle = (value: unknown) =>
|
|
24
|
+
typeof value === "string" && EDITING_ELEMENT_HANDLE.test(value);
|
|
25
|
+
const isText = (value: unknown) => typeof value === "string";
|
|
26
|
+
const isTextOrNull = (value: unknown) => value === null || typeof value === "string";
|
|
27
|
+
const isCount = (value: unknown) => Number.isSafeInteger(value) && (value as number) >= 0;
|
|
28
|
+
|
|
29
|
+
const isSource = (value: unknown): value is EditingElementSource =>
|
|
30
|
+
isFields(value) &&
|
|
31
|
+
isTextOrNull(value.file) &&
|
|
32
|
+
isTextOrNull(value.compositionId) &&
|
|
33
|
+
isTextOrNull(value.originalCompositionId);
|
|
34
|
+
|
|
35
|
+
const isTextRecord = (value: unknown) => isFields(value) && Object.values(value).every(isText);
|
|
36
|
+
|
|
37
|
+
function hasPlainFields(value: Fields): boolean {
|
|
38
|
+
const { matchesBefore, attributes, children } = value;
|
|
39
|
+
const groupsOk =
|
|
40
|
+
matchesBefore === null ||
|
|
41
|
+
(Array.isArray(matchesBefore) &&
|
|
42
|
+
matchesBefore.every((g) => isFields(g) && isSource(g.source) && isCount(g.count)));
|
|
43
|
+
const attributesOk =
|
|
44
|
+
Array.isArray(attributes) &&
|
|
45
|
+
attributes.every((a) => Array.isArray(a) && a.length === 2 && a.every(isText));
|
|
46
|
+
const childrenOk =
|
|
47
|
+
Array.isArray(children) &&
|
|
48
|
+
children.every((c) => isFields(c) && (isHandle(c.handle) || isText(c.text)));
|
|
49
|
+
return groupsOk && attributesOk && childrenOk && typeof value.truncated === "boolean";
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function readHit(value: unknown): PreviewElementHit | null {
|
|
53
|
+
if (!isFields(value) || !isHandle(value.handle) || !isText(value.tag)) return null;
|
|
54
|
+
const { rect } = value;
|
|
55
|
+
const rectOk = isFields(rect) && [rect.x, rect.y, rect.width, rect.height].every(Number.isFinite);
|
|
56
|
+
return rectOk ? (value as unknown as PreviewElementHit) : null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Frame data checked against `EditingElementDescription`; null when any field is off. */
|
|
60
|
+
export function readPreviewElement(value: unknown): PreviewElement | null {
|
|
61
|
+
if (!readHit(value) || !isFields(value)) return null;
|
|
62
|
+
if (value.parent !== null && !isHandle(value.parent)) return null;
|
|
63
|
+
if (!isTextOrNull(value.id) || !isTextOrNull(value.selector)) return null;
|
|
64
|
+
if (!isSource(value.source) || typeof value.locked !== "boolean") return null;
|
|
65
|
+
if (!isTextRecord(value.inlineStyles) || !isText(value.text)) return null;
|
|
66
|
+
return hasPlainFields(value) ? (value as unknown as PreviewElement) : null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** The elements under a frame viewport point, topmost first; null when the runtime cannot answer. */
|
|
70
|
+
export async function previewElementsAtPoint(
|
|
71
|
+
iframe: HTMLIFrameElement,
|
|
72
|
+
x: number,
|
|
73
|
+
y: number,
|
|
74
|
+
): Promise<PreviewElementHit[] | null> {
|
|
75
|
+
const reply = await previewRequest(iframe, "elements-at-point", { x, y });
|
|
76
|
+
const elements: unknown = reply?.elements;
|
|
77
|
+
if (!Array.isArray(elements)) return null;
|
|
78
|
+
return elements.map(readHit).filter((element) => element !== null);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* One entry per handle, in order: null for an element the frame no longer has. The whole answer is
|
|
83
|
+
* null when the runtime cannot answer, or when there are no handles or more than the runtime takes.
|
|
84
|
+
*/
|
|
85
|
+
export async function describePreviewElements(
|
|
86
|
+
iframe: HTMLIFrameElement,
|
|
87
|
+
handles: readonly string[],
|
|
88
|
+
): Promise<Array<PreviewElement | null> | null> {
|
|
89
|
+
if (!handles.length || handles.length > MAX_DESCRIBE_HANDLES) return null;
|
|
90
|
+
const reply = await previewRequest(iframe, "describe", { handles: [...handles] });
|
|
91
|
+
const elements: unknown = reply?.elements;
|
|
92
|
+
if (!Array.isArray(elements) || elements.length !== handles.length) return null;
|
|
93
|
+
return elements.map((entry, index) => {
|
|
94
|
+
const element = readPreviewElement(entry);
|
|
95
|
+
return element?.handle === handles[index] ? element : null;
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** `getSourceFileForElement` for the frame's own element. */
|
|
100
|
+
export function previewElementSourceFile(
|
|
101
|
+
element: PreviewElement,
|
|
102
|
+
activeCompositionPath: string | null,
|
|
103
|
+
): string {
|
|
104
|
+
return resolveElementSourceFile(element.source, activeCompositionPath);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** `getSelectorIndex` for the frame's own element; undefined when the runtime could not count. */
|
|
108
|
+
export function previewElementSelectorIndex(
|
|
109
|
+
element: PreviewElement,
|
|
110
|
+
activeCompositionPath: string | null,
|
|
111
|
+
): number | undefined {
|
|
112
|
+
if (!element.selector?.startsWith(".") || !element.matchesBefore) return undefined;
|
|
113
|
+
const file = previewElementSourceFile(element, activeCompositionPath);
|
|
114
|
+
return element.matchesBefore
|
|
115
|
+
.filter((group) => resolveElementSourceFile(group.source, activeCompositionPath) === file)
|
|
116
|
+
.reduce((sum, group) => sum + group.count, 0);
|
|
117
|
+
}
|
|
@@ -24,6 +24,18 @@ function swappableIframe(swap: Swap = vi.fn(async () => {})) {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
describe("sceneSwapFor", () => {
|
|
27
|
+
it("returns no swap for an opaque frame, without reading its window", () => {
|
|
28
|
+
const iframe = document.createElement("iframe");
|
|
29
|
+
const blocked = new Proxy({} as Window, {
|
|
30
|
+
get: () => {
|
|
31
|
+
throw new DOMException("blocked", "SecurityError");
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
Object.defineProperty(iframe, "contentWindow", { value: blocked });
|
|
35
|
+
Object.defineProperty(iframe, "contentDocument", { value: null });
|
|
36
|
+
expect(sceneSwapFor(iframe)).toBeNull();
|
|
37
|
+
});
|
|
38
|
+
|
|
27
39
|
it("gives up on a preview download that never finishes, so the caller can reload", async () => {
|
|
28
40
|
vi.useFakeTimers();
|
|
29
41
|
vi.spyOn(globalThis, "fetch").mockImplementation(
|