@hyperframes/studio 0.9.65 → 0.9.66

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.
Files changed (54) hide show
  1. package/dist/assets/hyperframes-player-VGx6ow_e.js +573 -0
  2. package/dist/assets/{index-tvLrzMBv.js → index-B8Xt_Oyw.js} +180 -180
  3. package/dist/assets/{index-CAjz3b83.js → index-BH18TdX9.js} +1 -1
  4. package/dist/assets/{pausedVideoDecoder-xC5IGkfB.js → pausedVideoDecoder-Bx3fGNkU.js} +1 -1
  5. package/dist/{chunk-VDDEXNVX.js → chunk-ESTBKVCB.js} +8 -0
  6. package/dist/index.html +1 -1
  7. package/dist/index.js +355 -142
  8. package/dist/player.js +1 -1
  9. package/package.json +8 -8
  10. package/src/captions/components/CaptionOverlay.sealed.test.ts +2 -25
  11. package/src/components/editor/LayersPanel.opaque.test.ts +4 -29
  12. package/src/components/editor/domEditOverlayCrop.ts +78 -15
  13. package/src/components/editor/domEditOverlayCropFrameLift.test.ts +64 -0
  14. package/src/components/editor/gsapMoveSealedPreview.test.ts +3 -23
  15. package/src/components/editor/previewDrafts.test.ts +13 -39
  16. package/src/components/editor/previewDrafts.ts +2 -0
  17. package/src/components/editor/previewGestureStartRead.test.ts +59 -21
  18. package/src/components/editor/previewGestureStartRead.ts +9 -0
  19. package/src/components/editor/previewLayers.test.ts +3 -20
  20. package/src/components/editor/previewOverlayRects.test.ts +3 -20
  21. package/src/components/editor/previewPointerTargets.test.ts +3 -20
  22. package/src/components/editor/previewSelection.test.ts +51 -35
  23. package/src/components/editor/previewSelection.ts +17 -0
  24. package/src/components/editor/useColorGradingController.sealed.test.ts +5 -30
  25. package/src/components/editor/useMotionPathData.sealed.test.ts +2 -29
  26. package/src/components/nle/NLEContext.drillDown.test.ts +85 -0
  27. package/src/components/nle/NLEContext.tsx +21 -23
  28. package/src/components/nle/NLEPreview.test.ts +18 -3
  29. package/src/components/nle/NLEPreview.tsx +10 -1
  30. package/src/components/nle/audioSealedPreview.test.ts +5 -32
  31. package/src/hooks/appHotkeysDispatch.ts +38 -1
  32. package/src/hooks/domEditCommits.sealed.test.ts +191 -0
  33. package/src/hooks/domStyleCommit.ts +18 -7
  34. package/src/hooks/gsapResizeIntercept.ts +7 -2
  35. package/src/hooks/useAppHotkeys.previewForwarding.test.tsx +134 -0
  36. package/src/hooks/useAppHotkeys.ts +36 -1
  37. package/src/hooks/useDomEditAttributeCommits.ts +43 -19
  38. package/src/hooks/useEnableKeyframes.sealed.test.ts +2 -29
  39. package/src/hooks/useSlideshowTabState.test.ts +47 -3
  40. package/src/hooks/useSlideshowTabState.ts +32 -16
  41. package/src/player/hooks/timelinePlayerTestHarness.ts +2 -1
  42. package/src/player/hooks/usePlaybackKeyboard.ts +1 -1
  43. package/src/player/hooks/useTimelineSyncCallbacks.sealed.test.ts +2 -29
  44. package/src/player/lib/previewChannel.test.ts +10 -0
  45. package/src/player/lib/previewChannel.ts +9 -0
  46. package/src/player/lib/previewElements.test.ts +34 -1
  47. package/src/player/lib/previewElements.ts +99 -16
  48. package/src/player/lib/sealedPreviewFrame.test-helpers.ts +38 -0
  49. package/src/player/lib/timelineIframeHelpers.ts +1 -1
  50. package/src/utils/blockInstaller.test.ts +82 -48
  51. package/src/utils/blockInstaller.ts +6 -14
  52. package/src/utils/previewCompositionSize.test.ts +61 -0
  53. package/src/utils/previewCompositionSize.ts +21 -1
  54. package/dist/assets/hyperframes-player-Dscc6yHD.js +0 -573
@@ -1,5 +1,6 @@
1
1
  // @vitest-environment happy-dom
2
2
 
3
+ import { readFileSync } from "node:fs";
3
4
  import React, { act, useRef } from "react";
4
5
  import type { Root } from "react-dom/client";
5
6
  import { afterEach, describe, expect, it, vi } from "vitest";
@@ -8,6 +9,14 @@ import { usePlayerStore } from "../player/store/playerStore";
8
9
  import { useAppHotkeys } from "./useAppHotkeys";
9
10
  import { mountReactHarness } from "./domSelectionTestHarness";
10
11
  import { trackStudioEvent } from "../utils/studioTelemetry";
12
+ import { APP_HOTKEY_ACTIONS } from "./appHotkeysDispatch";
13
+ import { PLAYBACK_ACTIONS } from "../player/hooks/usePlaybackKeyboard";
14
+ import { DEFAULT_STUDIO_KEYS } from "../player/components/studioShortcuts";
15
+ import {
16
+ detachedFakeIframe,
17
+ serveEditingRequests,
18
+ } from "../player/hooks/timelinePlayerTestHarness";
19
+ import { previewRequest } from "../player/lib/previewChannel";
11
20
 
12
21
  vi.mock("../utils/studioTelemetry", () => ({ trackStudioEvent: vi.fn() }));
13
22
 
@@ -167,4 +176,129 @@ describe("preview iframe hotkey forwarding", () => {
167
176
  expect(add).toHaveBeenCalled();
168
177
  expect(remove.mock.calls).toEqual(add.mock.calls);
169
178
  });
179
+
180
+ describe("a sealed preview", () => {
181
+ const closers: Array<() => void> = [];
182
+ afterEach(() => closers.splice(0).forEach((close) => close()));
183
+
184
+ // A frame whose document refuses Studio, reached only over the editing port it hands out per connect.
185
+ function sealedFrame(focus = { inPreview: true }) {
186
+ const frame: Record<string, unknown> = {};
187
+ const iframe = detachedFakeIframe(frame);
188
+ const hasFocus = vi.spyOn(document, "hasFocus").mockImplementation(() => focus.inPreview);
189
+ Object.defineProperty(document, "activeElement", {
190
+ configurable: true,
191
+ get: () => (focus.inPreview ? iframe : document.body),
192
+ });
193
+ closers.push(() => {
194
+ hasFocus.mockRestore();
195
+ delete (document as { activeElement?: unknown }).activeElement;
196
+ });
197
+ Object.defineProperty(iframe, "contentDocument", { value: null });
198
+ Object.defineProperty(frame, "document", {
199
+ get: () => {
200
+ throw new DOMException("blocked", "SecurityError");
201
+ },
202
+ });
203
+ const watched: string[][] = [];
204
+ const ports: MessagePort[] = [];
205
+ closers.push(
206
+ serveEditingRequests(
207
+ frame,
208
+ (kind, params) => {
209
+ if (kind === "max-z-index") return { zIndex: 7 };
210
+ if (kind !== "watch-keys") return null;
211
+ watched.push((params as { combos: string[] }).combos);
212
+ return { watching: watched.at(-1)!.length };
213
+ },
214
+ (port) => ports.push(port),
215
+ ),
216
+ );
217
+ const keyDown = (key: string, code: string) =>
218
+ ports.at(-1)!.postMessage({
219
+ v: 1,
220
+ event: "key",
221
+ key: {
222
+ type: "keydown",
223
+ key,
224
+ code,
225
+ altKey: false,
226
+ ctrlKey: false,
227
+ metaKey: false,
228
+ shiftKey: false,
229
+ repeat: false,
230
+ },
231
+ });
232
+ return { iframe, watched, ports, keyDown, hasFocus };
233
+ }
234
+
235
+ it("leaves Studio's own reads of the preview answered while it forwards keys", async () => {
236
+ root = mountReactHarness(<Harness />);
237
+ const { iframe, watched } = sealedFrame();
238
+ let read: Promise<unknown> | undefined;
239
+ act(() => {
240
+ sync?.(iframe);
241
+ read = previewRequest(iframe, "max-z-index", {}, 1_000);
242
+ });
243
+ await vi.waitFor(() => expect(watched).toHaveLength(1));
244
+ await expect(read).resolves.toEqual({ zIndex: 7 });
245
+ });
246
+
247
+ it("deletes the selection on a Delete pressed inside the preview", async () => {
248
+ root = mountReactHarness(<Harness />);
249
+ const { iframe, watched, keyDown } = sealedFrame();
250
+ act(() => sync?.(iframe));
251
+ await vi.waitFor(() => expect(watched).toHaveLength(1));
252
+ expect(new Set(watched[0])).toEqual(
253
+ new Set(APP_HOTKEY_ACTIONS.map((action) => DEFAULT_STUDIO_KEYS[action])),
254
+ );
255
+ keyDown("Delete", "Delete");
256
+ await vi.waitFor(() => expect(domDelete).toHaveBeenCalledTimes(1));
257
+ });
258
+
259
+ it("ignores a key the preview reports while the person is not in the preview", async () => {
260
+ root = mountReactHarness(<Harness />);
261
+ const focus = { inPreview: false };
262
+ const { iframe, watched, keyDown, hasFocus } = sealedFrame(focus);
263
+ act(() => sync?.(iframe));
264
+ await vi.waitFor(() => expect(watched).toHaveLength(1));
265
+ keyDown("Delete", "Delete");
266
+ await vi.waitFor(() => expect(hasFocus).toHaveBeenCalledTimes(1));
267
+ focus.inPreview = true;
268
+ keyDown("Delete", "Delete");
269
+ await vi.waitFor(() => expect(domDelete).toHaveBeenCalledTimes(1));
270
+ expect(hasFocus).toHaveBeenCalledTimes(2);
271
+ });
272
+
273
+ it("stops watching on the preview's next load, from a new port", async () => {
274
+ root = mountReactHarness(<Harness />);
275
+ const { iframe, watched, ports } = sealedFrame();
276
+ act(() => sync?.(iframe));
277
+ await vi.waitFor(() => expect(watched).toHaveLength(1));
278
+ act(() => sync?.(iframe));
279
+ await vi.waitFor(() => expect(watched).toHaveLength(3));
280
+ expect(watched.slice(1)).toContainEqual([]);
281
+ expect(ports).toHaveLength(2);
282
+ });
283
+
284
+ it("watches every action the app's key dispatchers answer, so none is dead in a sealed preview", () => {
285
+ const read = (file: string) => readFileSync(new URL(file, import.meta.url), "utf8");
286
+ const answered = new Set([
287
+ ...[...read("./appHotkeysDispatch.ts").matchAll(/matchesKey\(\w+, "(\w+)"\)/g)].map(
288
+ (m) => m[1],
289
+ ),
290
+ ...[...read("./linkShortcuts.ts").matchAll(/\["(\w+)", "\w+"\]/g)].map((m) => m[1]),
291
+ ]);
292
+ expect(answered.size).toBeGreaterThan(20);
293
+ expect(new Set(APP_HOTKEY_ACTIONS)).toEqual(answered);
294
+ });
295
+
296
+ it("watches no key the playback keys also watch, so the frame forwards each press once", () => {
297
+ const app = new Set(APP_HOTKEY_ACTIONS.map((action) => DEFAULT_STUDIO_KEYS[action]));
298
+ const shared = PLAYBACK_ACTIONS.map((action) => DEFAULT_STUDIO_KEYS[action]).filter((combo) =>
299
+ app.has(combo),
300
+ );
301
+ expect(shared).toEqual([]);
302
+ });
303
+ });
170
304
  });
@@ -8,13 +8,21 @@ import {
8
8
  applyCaptionModelToIframe,
9
9
  isCaptionPreviewVisible,
10
10
  } from "../captions/components/CaptionOverlayUtils";
11
- import { type HotkeyCallbacks, dispatchModifierKey, dispatchPlainKey } from "./appHotkeysDispatch";
11
+ import {
12
+ APP_HOTKEY_ACTIONS,
13
+ type HotkeyCallbacks,
14
+ dispatchModifierKey,
15
+ dispatchPlainKey,
16
+ } from "./appHotkeysDispatch";
12
17
  import {
13
18
  useEditHistoryActions,
14
19
  type EditHistoryHandle,
15
20
  type UseEditHistoryActionsOptions,
16
21
  } from "./useEditHistoryActions";
17
22
  import { useStableHandlers } from "./useStableHandlers";
23
+ import { createEditingPortClient } from "@hyperframes/core/runtime/editing-port";
24
+ import { previewFrameOpaque } from "../player/lib/previewChannel";
25
+ import { getStudioKey, subscribeStudioKeys } from "../player/components/studioShortcuts";
18
26
 
19
27
  function iframeContentWindow(iframe: HTMLIFrameElement | null): Window | null {
20
28
  try {
@@ -24,6 +32,29 @@ function iframeContentWindow(iframe: HTMLIFrameElement | null): Window | null {
24
32
  }
25
33
  }
26
34
 
35
+ // A port of its own: watch-keys replaces a port's whole list, and playback watches its keys on the shared one.
36
+ function forwardSealedFrameKeys(iframe: HTMLIFrameElement, onKeyDown: (e: KeyboardEvent) => void) {
37
+ const client = createEditingPortClient({ iframe });
38
+ const watch = (combos: string[]) => void client.request("watch-keys", { combos }).catch(() => {});
39
+ const watchApp = () => watch(APP_HOTKEY_ACTIONS.map(getStudioKey));
40
+ watchApp();
41
+ const stopRebinds = subscribeStudioKeys(watchApp);
42
+ // The frame can report keys nobody pressed: act only while the person is in the preview.
43
+ const focused = () =>
44
+ iframe.ownerDocument.hasFocus() && iframe.ownerDocument.activeElement === iframe;
45
+ const stopKeys = client.onKey((key) => {
46
+ if (key.type === "keydown" && focused()) onKeyDown(new KeyboardEvent(key.type, key));
47
+ });
48
+ return () => {
49
+ stopRebinds();
50
+ stopKeys();
51
+ void client
52
+ .request("watch-keys", { combos: [] })
53
+ .catch(() => {})
54
+ .finally(() => client.dispose());
55
+ };
56
+ }
57
+
27
58
  function safeAddListener(t: EventTarget | null, type: string, h: EventListener, capture = false) {
28
59
  try {
29
60
  t?.addEventListener(type, h, capture);
@@ -247,6 +278,10 @@ export function useAppHotkeys({
247
278
  (iframe: HTMLIFrameElement | null) => {
248
279
  previewHistoryCleanupRef.current?.();
249
280
  previewHistoryCleanupRef.current = null;
281
+ if (iframe && previewFrameOpaque(iframe)) {
282
+ previewHistoryCleanupRef.current = forwardSealedFrameKeys(iframe, handleAppKeyDown);
283
+ return;
284
+ }
250
285
  const win = iframeContentWindow(iframe);
251
286
  if (!win) return;
252
287
  const appHandler = handleAppKeyDown as EventListener;
@@ -14,6 +14,8 @@ import { bumpDomEditCommitMapVersion, runDomEditCommit } from "./domEditCommitRu
14
14
  import { syncStoredAutomationFromPreview } from "../player/lib/automationStoreSync";
15
15
  import { HF_AUDIO_GROUP_ATTR, HF_AUDIO_GROUP_TAG } from "@hyperframes/core/audio-groups";
16
16
  import { invalidateGroupInfoCache } from "../player/lib/timelineGroupInfo";
17
+ import { editSealedSelection } from "../components/editor/previewSelection";
18
+ import { afterSealedDraw, type PreviewElementEdit } from "../player/lib/previewElements";
17
19
 
18
20
  // ── Types ──
19
21
 
@@ -161,6 +163,7 @@ export function useDomEditAttributeCommits({
161
163
  const op: PatchOperation = { type: "attribute", property: attr, value };
162
164
  let editedElement: HTMLElement | null = null;
163
165
  let previousValue: string | null = null;
166
+ let sealedEdit: PreviewElementEdit | null = null;
164
167
 
165
168
  await runDomEditCommit({
166
169
  capture: () => {
@@ -175,20 +178,27 @@ export function useDomEditAttributeCommits({
175
178
  previousValue = captured.previousValue;
176
179
  },
177
180
  apply: () => {
178
- if (!editedElement) return;
179
181
  const nextValue = value === null || value === "" ? null : value;
182
+ if (!editedElement) {
183
+ const attributes: Array<[string, string | null]> = [[fullAttr, nextValue]];
184
+ sealedEdit = editSealedSelection(iframe, domEditSelection, { attributes });
185
+ return;
186
+ }
180
187
  setOrRemovePreviewAttribute(editedElement, fullAttr, nextValue);
181
188
  },
182
189
  persist: options.previewOnly
183
190
  ? async () => {}
184
191
  : () =>
185
- persistDomEditOperations(domEditSelection, [op], {
186
- label: options.label,
187
- coalesceKey: commitKey,
188
- skipRefresh: options.skipRefresh,
189
- }),
192
+ afterSealedDraw(sealedEdit, (drawn) =>
193
+ persistDomEditOperations(domEditSelection, [op], {
194
+ label: options.label,
195
+ coalesceKey: commitKey,
196
+ skipRefresh: options.skipRefresh && drawn,
197
+ }),
198
+ ),
190
199
  shouldRevert: () => isLatestCommit(),
191
200
  revert: () => {
201
+ sealedEdit?.restore();
192
202
  if (!editedElement) return;
193
203
  setOrRemovePreviewAttribute(editedElement, fullAttr, previousValue);
194
204
  },
@@ -256,6 +266,7 @@ export function useDomEditAttributeCommits({
256
266
  commitKey,
257
267
  );
258
268
  let captured: CapturedMultiAttributeElement | null = null;
269
+ let sealedEdit: PreviewElementEdit | null = null;
259
270
  let landed = false;
260
271
 
261
272
  await runDomEditCommit({
@@ -268,21 +279,31 @@ export function useDomEditAttributeCommits({
268
279
  );
269
280
  },
270
281
  apply: () => {
271
- if (!captured) return;
282
+ if (!captured) {
283
+ const attributes = entries.map(({ fullAttr, value }): [string, string | null] => [
284
+ fullAttr,
285
+ value,
286
+ ]);
287
+ sealedEdit = editSealedSelection(iframe, selection, { attributes });
288
+ return;
289
+ }
272
290
  for (const entry of entries) {
273
291
  setOrRemovePreviewAttribute(captured.element, entry.fullAttr, entry.value);
274
292
  }
275
293
  },
276
294
  persist: () =>
277
- persistDomEditOperations(selection, ops, {
278
- label: options.label,
279
- coalesceKey: commitKey,
280
- skipRefresh: options.skipRefresh,
281
- prepareContent: options.prepareContent,
282
- }),
295
+ afterSealedDraw(sealedEdit, (drawn) =>
296
+ persistDomEditOperations(selection, ops, {
297
+ label: options.label,
298
+ coalesceKey: commitKey,
299
+ skipRefresh: options.skipRefresh && drawn,
300
+ prepareContent: options.prepareContent,
301
+ }),
302
+ ),
283
303
  shouldRevert: (error) =>
284
304
  isLatestCommit() && !(error instanceof DomEditPersistPreparedWriteError),
285
305
  revert: () => {
306
+ sealedEdit?.restore();
286
307
  if (!captured) return;
287
308
  for (const entry of entries) {
288
309
  setOrRemovePreviewAttribute(
@@ -413,6 +434,7 @@ export function useDomEditAttributeCommits({
413
434
  const op: PatchOperation = { type: "html-attribute", property: attr, value };
414
435
  let editedElement: HTMLElement | null = null;
415
436
  let previousValue: string | null = null;
437
+ let sealedEdit: PreviewElementEdit | null = null;
416
438
 
417
439
  await runDomEditCommit({
418
440
  capture: () => {
@@ -427,12 +449,13 @@ export function useDomEditAttributeCommits({
427
449
  previousValue = captured.previousValue;
428
450
  },
429
451
  apply: () => {
430
- if (!editedElement) return;
431
- setOrRemovePreviewAttribute(
432
- editedElement,
433
- attr,
434
- resolveOptimisticAttributeValue(attr, value),
435
- );
452
+ const optimistic = resolveOptimisticAttributeValue(attr, value);
453
+ if (!editedElement) {
454
+ const attributes: Array<[string, string | null]> = [[attr, optimistic]];
455
+ sealedEdit = editSealedSelection(iframe, domEditSelection, { attributes });
456
+ return;
457
+ }
458
+ setOrRemovePreviewAttribute(editedElement, attr, optimistic);
436
459
  },
437
460
  persist: () =>
438
461
  persistDomEditOperations(domEditSelection, [op], {
@@ -442,6 +465,7 @@ export function useDomEditAttributeCommits({
442
465
  }),
443
466
  shouldRevert: () => isLatestCommit(),
444
467
  revert: () => {
468
+ sealedEdit?.restore();
445
469
  if (!editedElement) return;
446
470
  setOrRemovePreviewAttribute(editedElement, attr, previousValue);
447
471
  },
@@ -2,46 +2,19 @@
2
2
 
3
3
  import { afterEach, expect, it, vi } from "vitest";
4
4
  import type { GsapAnimation } from "@hyperframes/core/gsap-parser";
5
- // The frame's real runtime agent, serving the opaque frame from this document as Studio's marked preview page.
6
- import { installMarkedEditingAgent } from "../../../core/src/runtime/editingAgent.test-helpers";
7
- import { detachedFakeIframe } from "../player/hooks/timelinePlayerTestHarness";
8
5
  import { describePreviewElements, queryPreviewElements } from "../player/lib/previewElements";
9
6
  import { usePlayerStore } from "../player/store/playerStore";
10
7
  import { standInFor } from "../components/editor/previewSelection";
11
8
  import type { DomEditSelection } from "../components/editor/domEditingTypes";
12
9
  import { applyKeyframeAtPlayhead, type EnableKeyframesSession } from "./useEnableKeyframes";
10
+ import { sealedPreviewFrame } from "../player/lib/sealedPreviewFrame.test-helpers";
13
11
 
14
- const closers: Array<() => void> = [];
15
12
  afterEach(() => {
16
- closers.splice(0).forEach((close) => close());
17
13
  usePlayerStore.setState({ currentTime: 0, isPlaying: false });
18
14
  document.body.innerHTML = "";
19
15
  for (const name of ["gsap", "__timelines", "__player"]) Reflect.deleteProperty(window, name);
20
16
  });
21
17
 
22
- /** A frame Studio cannot read, whose runtime is the real editing agent over this document. */
23
- function opaqueFrame() {
24
- const frame: Record<string, unknown> = {};
25
- const iframe = detachedFakeIframe(frame);
26
- Object.defineProperty(iframe, "contentDocument", { value: null });
27
- Object.defineProperty(frame, "document", {
28
- get: () => {
29
- throw new DOMException("blocked", "SecurityError");
30
- },
31
- });
32
- const parent = {
33
- postMessage: (data: unknown, _origin: string, ports: MessagePort[]) =>
34
- window.dispatchEvent(
35
- new MessageEvent("message", { source: frame as unknown as Window, data, ports }),
36
- ),
37
- } as unknown as Window;
38
- const agent = installMarkedEditingAgent(parent);
39
- frame.postMessage = (data: unknown) =>
40
- window.dispatchEvent(new MessageEvent("message", { source: parent, data }));
41
- closers.push(() => agent.dispose());
42
- return iframe;
43
- }
44
-
45
18
  /** The frame's GSAP: one keyframed tween moving #box, paused half way where GSAP has it at 120, 40. */
46
19
  function frameGsapAt(seconds: number) {
47
20
  const box = document.getElementById("box")!;
@@ -81,7 +54,7 @@ it("adds a keyframe at the playhead with the sealed preview's GSAP values there"
81
54
  document.body.innerHTML = `<main data-composition-id="main"><div id="box"></div></main>`;
82
55
  frameGsapAt(0.5);
83
56
  usePlayerStore.setState({ currentTime: 0.5, isPlaying: false });
84
- const iframe = opaqueFrame();
57
+ const iframe = sealedPreviewFrame().iframe;
85
58
  const [match] = (await queryPreviewElements(iframe, "#box"))!.matches;
86
59
  const [described] = (await describePreviewElements(iframe, [match!.handle]))!;
87
60
  const sel = { id: "box", selector: "#box", element: standInFor(iframe, described!) };
@@ -2,9 +2,15 @@
2
2
 
3
3
  import React, { act } from "react";
4
4
  import { createRoot } from "react-dom/client";
5
- import { afterEach, describe, expect, it } from "vitest";
5
+ import { afterEach, describe, expect, it, vi } from "vitest";
6
+ import { previewClipManifest } from "../player/lib/previewChannel";
6
7
  import { useSlideshowTabState } from "./useSlideshowTabState";
7
8
 
9
+ vi.mock("../player/lib/previewChannel", async (importOriginal) => {
10
+ const actual = await importOriginal<typeof import("../player/lib/previewChannel")>();
11
+ return { ...actual, previewClipManifest: vi.fn(async () => null) };
12
+ });
13
+
8
14
  (globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
9
15
 
10
16
  const SLIDESHOW_HTML = `<html><body><script type="application/hyperframes-slideshow+json">{"slides":[]}</script></body></html>`;
@@ -14,16 +20,21 @@ afterEach(() => {
14
20
  document.body.innerHTML = "";
15
21
  });
16
22
 
17
- function renderHook(params: { editingFileContent: string | null | undefined }) {
23
+ function renderHook(params: {
24
+ editingFileContent: string | null | undefined;
25
+ iframe?: HTMLIFrameElement | null;
26
+ }) {
18
27
  const host = document.createElement("div");
19
28
  document.body.append(host);
20
29
  const root = createRoot(host);
21
30
  let current: ReturnType<typeof useSlideshowTabState> | null = null;
31
+ // One ref for every render, as Studio's useRef gives.
32
+ const previewIframeRef = { current: params.iframe ?? null };
22
33
 
23
34
  function Harness() {
24
35
  current = useSlideshowTabState({
25
36
  editingFileContent: params.editingFileContent,
26
- previewIframeRef: { current: null },
37
+ previewIframeRef,
27
38
  refreshKey: 0,
28
39
  slideshowVisible: false,
29
40
  });
@@ -71,4 +82,37 @@ describe("useSlideshowTabState", () => {
71
82
  expect(harness.getState().isSlideshowComposition).toBe(true);
72
83
  harness.unmount();
73
84
  });
85
+
86
+ it("lists the scenes of a sealed preview, read over its editing port", async () => {
87
+ // A cross-origin frame's window refuses every property read.
88
+ const sealedWindow = new Proxy(
89
+ {},
90
+ {
91
+ get() {
92
+ throw new DOMException(
93
+ "Blocked a frame from accessing a cross-origin frame.",
94
+ "SecurityError",
95
+ );
96
+ },
97
+ },
98
+ );
99
+ const iframe = document.createElement("iframe");
100
+ Object.defineProperty(iframe, "contentWindow", { value: sealedWindow });
101
+ vi.mocked(previewClipManifest).mockResolvedValueOnce({
102
+ clips: [],
103
+ durationInFrames: 300,
104
+ scenes: [
105
+ null,
106
+ { id: "s1", label: "Intro", start: 0, duration: 4 },
107
+ { id: 2, label: "Bad id", start: 4, duration: 4 },
108
+ { id: "s3", label: "No time", start: null, duration: 4 },
109
+ ],
110
+ } as unknown as Awaited<ReturnType<typeof previewClipManifest>>);
111
+ const harness = renderHook({ editingFileContent: SLIDESHOW_HTML, iframe });
112
+ await act(async () => {});
113
+ expect(harness.getState().slideshowScenes).toEqual([
114
+ { id: "s1", label: "Intro", start: 0, duration: 4 },
115
+ ]);
116
+ harness.unmount();
117
+ });
74
118
  });
@@ -1,7 +1,7 @@
1
- import { useMemo, type MutableRefObject } from "react";
1
+ import { useEffect, useMemo, useState, type MutableRefObject } from "react";
2
2
  import { SLIDESHOW_ISLAND_TYPE, slideshowIslandRegex } from "@hyperframes/core/slideshow";
3
3
  import type { SceneInfo } from "../components/panels/SlideshowPanel";
4
- import type { IframeWindow } from "../player/lib/playbackTypes";
4
+ import { previewClipManifest } from "../player/lib/previewChannel";
5
5
 
6
6
  /**
7
7
  * Derives whether the currently-edited composition is a slideshow (carries
@@ -10,6 +10,18 @@ import type { IframeWindow } from "../player/lib/playbackTypes";
10
10
  * for the Slideshow panel. Extracted from StudioRightPanels to keep that file
11
11
  * under the 600-LOC gate.
12
12
  */
13
+ /** The manifest is frame data: a scene entry must carry a string id and label and finite times. */
14
+ function isSceneEntry(value: unknown): value is SceneInfo {
15
+ if (!value || typeof value !== "object") return false;
16
+ const s = value as Record<string, unknown>;
17
+ return (
18
+ typeof s.id === "string" &&
19
+ typeof s.label === "string" &&
20
+ Number.isFinite(s.start) &&
21
+ Number.isFinite(s.duration)
22
+ );
23
+ }
24
+
13
25
  export function useSlideshowTabState(params: {
14
26
  editingFileContent: string | null | undefined;
15
27
  previewIframeRef: MutableRefObject<HTMLIFrameElement | null>;
@@ -28,20 +40,24 @@ export function useSlideshowTabState(params: {
28
40
  return slideshowIslandRegex("i").test(editingFileContent);
29
41
  }, [editingFileContent]);
30
42
 
31
- // Derive scene list from the live clip manifest in the preview iframe.
32
- const slideshowScenes = useMemo<SceneInfo[]>(() => {
33
- try {
34
- const win = previewIframeRef.current?.contentWindow as IframeWindow | null;
35
- return (win?.__clipManifest?.scenes ?? []).map((s) => ({
36
- id: s.id,
37
- label: s.label,
38
- start: s.start,
39
- duration: s.duration,
40
- }));
41
- } catch {
42
- return [];
43
- }
44
- // eslint-disable-next-line react-hooks/exhaustive-deps
43
+ // The scene list from the preview's live clip manifest, read over its editing port.
44
+ const [slideshowScenes, setSlideshowScenes] = useState<SceneInfo[]>([]);
45
+ useEffect(() => {
46
+ const iframe = previewIframeRef.current;
47
+ if (!iframe) return setSlideshowScenes([]);
48
+ let current = true;
49
+ void previewClipManifest(iframe).then((manifest) => {
50
+ if (!current) return;
51
+ const scenes: unknown[] = Array.isArray(manifest?.scenes) ? manifest.scenes : [];
52
+ setSlideshowScenes(
53
+ scenes
54
+ .filter(isSceneEntry)
55
+ .map((s) => ({ id: s.id, label: s.label, start: s.start, duration: s.duration })),
56
+ );
57
+ });
58
+ return () => {
59
+ current = false;
60
+ };
45
61
  }, [previewIframeRef, slideshowVisible, refreshKey]);
46
62
 
47
63
  return { isSlideshowComposition, slideshowScenes };
@@ -171,7 +171,8 @@ export function serveEditingRequests(
171
171
  };
172
172
  const handoff = { source: "hf-preview", type: "editing-port", v: 1 };
173
173
  const source = frame as unknown as Window;
174
- window.dispatchEvent(new MessageEvent("message", { source, data: handoff, ports: [port2] }));
174
+ const init = { source, data: handoff, ports: [port2] };
175
+ setTimeout(() => window.dispatchEvent(new MessageEvent("message", init)));
175
176
  };
176
177
  return () => ports.forEach((port) => port.close());
177
178
  }
@@ -45,7 +45,7 @@ const FRAME_STEPS = {
45
45
  frameBackTen: -10,
46
46
  frameForwardTen: 10,
47
47
  } as const;
48
- const PLAYBACK_ACTIONS = [
48
+ export const PLAYBACK_ACTIONS = [
49
49
  "playPause",
50
50
  ...(Object.keys(FRAME_STEPS) as Array<keyof typeof FRAME_STEPS>),
51
51
  "mute",
@@ -3,43 +3,16 @@
3
3
  import { act, createElement } from "react";
4
4
  import { createRoot } from "react-dom/client";
5
5
  import { afterEach, expect, it, vi } from "vitest";
6
- // The frame's real runtime agent, serving the opaque frame from this document as Studio's marked preview page.
7
- import { installMarkedEditingAgent } from "../../../../core/src/runtime/editingAgent.test-helpers";
8
6
  import type { TimelineElement } from "../store/playerStore";
9
- import { detachedFakeIframe } from "./timelinePlayerTestHarness";
10
7
  import { useTimelineSyncCallbacks } from "./useTimelineSyncCallbacks";
8
+ import { sealedPreviewFrame } from "../lib/sealedPreviewFrame.test-helpers";
11
9
 
12
10
  (globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
13
11
 
14
- const closers: Array<() => void> = [];
15
12
  afterEach(() => {
16
- closers.splice(0).forEach((close) => close());
17
13
  document.body.innerHTML = "";
18
14
  });
19
15
 
20
- /** A frame Studio cannot read, whose runtime is the real editing agent over this document. */
21
- function opaqueFrame() {
22
- const frame: Record<string, unknown> = {};
23
- const iframe = detachedFakeIframe(frame);
24
- Object.defineProperty(iframe, "contentDocument", { value: null });
25
- Object.defineProperty(frame, "document", {
26
- get: () => {
27
- throw new DOMException("blocked", "SecurityError");
28
- },
29
- });
30
- const parent = {
31
- postMessage: (data: unknown, _origin: string, ports: MessagePort[]) =>
32
- window.dispatchEvent(
33
- new MessageEvent("message", { source: frame as unknown as Window, data, ports }),
34
- ),
35
- } as unknown as Window;
36
- const agent = installMarkedEditingAgent(parent);
37
- frame.postMessage = (data: unknown) =>
38
- window.dispatchEvent(new MessageEvent("message", { source: parent, data }));
39
- closers.push(() => agent.dispose());
40
- return iframe;
41
- }
42
-
43
16
  function processWith(iframe: HTMLIFrameElement) {
44
17
  const sync = vi.fn<(els: TimelineElement[], duration?: number) => void>();
45
18
  let callbacks: ReturnType<typeof useTimelineSyncCallbacks> | undefined;
@@ -84,7 +57,7 @@ it("binds a sealed preview's timeline row to its element, as an open preview doe
84
57
  document.body.innerHTML = `<div data-composition-id="main" data-composition-file="index.html">
85
58
  <div id="box" class="clip" data-start="0" data-duration="2" data-audio-group="voice"></div>
86
59
  </div>`;
87
- const { sync, process } = processWith(opaqueFrame());
60
+ const { sync, process } = processWith(sealedPreviewFrame().iframe);
88
61
 
89
62
  process({ clips: [boxClip], durationInFrames: 60 });
90
63
 
@@ -120,6 +120,16 @@ describe("previewEditingClient", () => {
120
120
  expect(Date.now() - started).toBeLessThan(AGENT_PROBE_MS + 500);
121
121
  });
122
122
 
123
+ it("answers a frame whose runtime never came at once, rather than after another probe", async () => {
124
+ const { iframe } = opaqueFrame();
125
+ const started = Date.now();
126
+ await previewSeekPainted(iframe, 15_000);
127
+ expect(Date.now() - started).toBeGreaterThanOrEqual(AGENT_PROBE_MS - 50);
128
+ const again = Date.now();
129
+ await previewSeekPainted(iframe, 15_000);
130
+ expect(Date.now() - again).toBeLessThan(100);
131
+ });
132
+
123
133
  it("returns the runtime's manifest only when its reply has the manifest's shape", async () => {
124
134
  const manifest = { clips: [], durationInFrames: 30, scenes: [] };
125
135
  let reply: unknown = { clipManifest: manifest };
@@ -54,7 +54,15 @@ export function setFrameLoopStart(iframe: HTMLIFrameElement, seconds: number | n
54
54
 
55
55
  export const AGENT_PROBE_MS = 1_000;
56
56
 
57
+ const agents = new WeakMap<EditingPortClient, boolean>();
58
+
57
59
  async function agentConnected(client: EditingPortClient): Promise<boolean> {
60
+ const known = agents.get(client);
61
+ if (known !== undefined) return known;
62
+ client.ready.then(
63
+ () => agents.set(client, true),
64
+ () => agents.set(client, false),
65
+ );
58
66
  let timer: ReturnType<typeof setTimeout> | undefined;
59
67
  const probe = new Promise<boolean>(
60
68
  (resolve) => (timer = setTimeout(resolve, AGENT_PROBE_MS, false)),
@@ -67,6 +75,7 @@ async function agentConnected(client: EditingPortClient): Promise<boolean> {
67
75
  probe,
68
76
  ]);
69
77
  clearTimeout(timer);
78
+ if (!agents.has(client)) agents.set(client, connected);
70
79
  return connected;
71
80
  }
72
81