@hyperframes/studio 0.9.59 → 0.9.63

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 (61) hide show
  1. package/dist/assets/{hyperframes-player-bya4hgms.js → hyperframes-player-CUQxAJaE.js} +1 -1
  2. package/dist/assets/{index--dcfSUzI.js → index-D3rdYBVx.js} +1 -1
  3. package/dist/assets/{index-Bxh5lW0C.js → index-DSrTQv-k.js} +204 -204
  4. package/dist/assets/{pausedVideoDecoder-BKym1jA6.js → pausedVideoDecoder-BWllfjF-.js} +1 -1
  5. package/dist/{chunk-G6FJSL7X.js → chunk-JTUKMELR.js} +34 -6
  6. package/dist/{chunk-5EEM5WO4.js → chunk-ZUDVUI7K.js} +11 -1
  7. package/dist/index.d.ts +28 -1
  8. package/dist/index.html +1 -1
  9. package/dist/index.js +1815 -1104
  10. package/dist/player.js +2 -2
  11. package/dist/ui.js +1 -1
  12. package/package.json +8 -8
  13. package/src/captions/components/CaptionOverlay.sealed.test.ts +273 -0
  14. package/src/captions/components/CaptionOverlay.tsx +112 -57
  15. package/src/captions/components/CaptionOverlayUtils.ts +319 -33
  16. package/src/captions/components/CaptionPropertyPanel.tsx +17 -0
  17. package/src/components/editor/LayersPanel.opaque.test.ts +2 -2
  18. package/src/components/editor/previewDrafts.test.ts +2 -2
  19. package/src/components/editor/previewGestureStartRead.test.ts +2 -2
  20. package/src/components/editor/previewLayers.test.ts +2 -2
  21. package/src/components/editor/previewOverlayRects.test.ts +2 -2
  22. package/src/components/editor/previewPointerTargets.test.ts +2 -2
  23. package/src/components/editor/previewSelection.test.ts +2 -2
  24. package/src/components/editor/useColorGradingController.sealed.test.ts +165 -0
  25. package/src/components/editor/useColorGradingController.ts +20 -1
  26. package/src/components/editor/useColorGradingPreviews.ts +127 -43
  27. package/src/components/nle/AudioMeterStrip.tsx +63 -2
  28. package/src/components/nle/NLEPreview.test.ts +24 -0
  29. package/src/components/nle/NLEPreview.tsx +2 -6
  30. package/src/components/nle/audioSealedPreview.test.ts +254 -0
  31. package/src/components/nle/previewStage.ts +11 -1
  32. package/src/components/nle/zLaneGesture.test.ts +67 -0
  33. package/src/components/nle/zLaneGesture.ts +4 -0
  34. package/src/components/panels/VariablesPanel.test.tsx +90 -1
  35. package/src/components/panels/VariablesPanel.tsx +41 -17
  36. package/src/components/panels/renderHandoff.test.ts +86 -0
  37. package/src/components/panels/renderHandoff.ts +28 -0
  38. package/src/components/ui/Menu.test.tsx +77 -0
  39. package/src/components/ui/Menu.tsx +47 -4
  40. package/src/components/ui/Popover.tsx +5 -2
  41. package/src/elementAnimations.test.ts +13 -0
  42. package/src/hooks/timelineAudioGroupCreate.ts +57 -7
  43. package/src/hooks/timelineAudioGroupVolume.ts +110 -34
  44. package/src/hooks/useCaptionDetection.ts +175 -54
  45. package/src/hooks/usePersistentEditHistory.test.ts +84 -0
  46. package/src/hooks/usePersistentEditHistory.ts +36 -3
  47. package/src/index.ts +1 -0
  48. package/src/main.tsx +14 -11
  49. package/src/player/components/Timeline.newTrack.test.tsx +106 -0
  50. package/src/player/components/TimelineLanes.tsx +4 -2
  51. package/src/player/components/TimelineTrackHeader.tsx +12 -4
  52. package/src/player/components/clipMenuAudioItems.tsx +46 -3
  53. package/src/player/components/timelineRowKeys.test.ts +28 -0
  54. package/src/player/components/timelineRowKeys.ts +27 -0
  55. package/src/player/hooks/useShadowPreviewReload.ts +7 -0
  56. package/src/player/hooks/useTimelinePlayer.shadowReload.test.ts +20 -0
  57. package/src/player/lib/previewChannel.ts +10 -0
  58. package/src/utils/gsapUndoRestore.sealed.test.ts +73 -0
  59. package/src/utils/gsapUndoRestore.ts +66 -0
  60. package/src/utils/studioPendingEdits.test.ts +20 -0
  61. package/src/utils/studioPendingEdits.ts +11 -4
@@ -0,0 +1,165 @@
1
+ // @vitest-environment happy-dom
2
+
3
+ import React, { act } from "react";
4
+ import { createRoot } from "react-dom/client";
5
+ import { afterEach, describe, expect, it, vi } from "vitest";
6
+ import { normalizeHfColorGrading } from "@hyperframes/core/color-grading";
7
+ // The frame's real runtime agent, serving the opaque frame from this document.
8
+ import { installMarkedEditingAgent } from "../../../../core/src/runtime/editingAgent.test-helpers";
9
+ import { installReactActEnvironment } from "../../hooks/domSelectionTestHarness";
10
+ import { detachedFakeIframe } from "../../player/hooks/timelinePlayerTestHarness";
11
+ import type { DomEditSelection } from "./domEditing";
12
+ import { useColorGradingController } from "./useColorGradingController";
13
+
14
+ installReactActEnvironment();
15
+
16
+ const closers: Array<() => void> = [];
17
+ afterEach(() => {
18
+ closers.splice(0).forEach((close) => close());
19
+ delete (window as { __hf?: unknown }).__hf;
20
+ document.body.innerHTML = "";
21
+ });
22
+
23
+ /** A frame Studio cannot read, whose runtime is the real editing agent over this document. */
24
+ function opaqueFrame(): HTMLIFrameElement {
25
+ const frame: Record<string, unknown> = {};
26
+ const iframe = detachedFakeIframe(frame);
27
+ Object.defineProperty(iframe, "contentDocument", { value: null });
28
+ Object.defineProperty(frame, "document", {
29
+ get: () => {
30
+ throw new DOMException("blocked", "SecurityError");
31
+ },
32
+ });
33
+ const parent = {
34
+ postMessage: (data: unknown, _origin: string, ports: MessagePort[]) =>
35
+ window.dispatchEvent(
36
+ new MessageEvent("message", { source: frame as unknown as Window, data, ports }),
37
+ ),
38
+ } as unknown as Window;
39
+ const agent = installMarkedEditingAgent(parent);
40
+ frame.postMessage = (data: unknown) =>
41
+ window.dispatchEvent(new MessageEvent("message", { source: parent, data }));
42
+ closers.push(() => agent.dispose());
43
+ return iframe;
44
+ }
45
+
46
+ /** The frame's colour grading runtime, on the window the agent serves. */
47
+ function installFrameGrading() {
48
+ const stopPlayback = vi.fn();
49
+ const grading = {
50
+ getStatus: vi.fn(() => ({ state: "active", message: "Shader active" })),
51
+ renderPreviews: vi.fn(async (_target: unknown, candidates: Array<{ id: string }>) => ({
52
+ width: 160,
53
+ height: 90,
54
+ images: candidates.map(({ id }) => ({
55
+ id,
56
+ dataUrl: id === "neutral" ? "https://example.com/x.png" : `data:image/png;base64,${id}`,
57
+ })),
58
+ })),
59
+ startPreviewPlayback: vi.fn(() => stopPlayback),
60
+ };
61
+ Object.assign(window, { __hf: { colorGrading: grading } });
62
+ return { grading, stopPlayback };
63
+ }
64
+
65
+ const element = {
66
+ element: document.createElement("video"),
67
+ id: "s1-bg",
68
+ hfId: "hf-bg",
69
+ selector: "#s1-bg",
70
+ selectorIndex: 0,
71
+ tagName: "video",
72
+ sourceFile: "index.html",
73
+ dataAttributes: {},
74
+ } as unknown as DomEditSelection;
75
+ const target = { id: "s1-bg", hfId: "hf-bg", selector: "#s1-bg", selectorIndex: 0 };
76
+
77
+ function renderController(iframe: HTMLIFrameElement) {
78
+ let latest: ReturnType<typeof useColorGradingController> | undefined;
79
+ const previewIframeRef = { current: iframe };
80
+ const onSetAttributeLive = vi.fn();
81
+ function Host() {
82
+ latest = useColorGradingController({
83
+ projectId: "proj",
84
+ element,
85
+ previewIframeRef,
86
+ onSetAttributeLive,
87
+ });
88
+ return null;
89
+ }
90
+ const root = createRoot(document.body.appendChild(document.createElement("div")));
91
+ act(() => root.render(React.createElement(Host)));
92
+ closers.unshift(() => act(() => root.unmount()));
93
+ return () => {
94
+ if (!latest) throw new Error("hook did not render");
95
+ return latest;
96
+ };
97
+ }
98
+
99
+ // Each poll flushes React's queued updates first; a check inside one act would never see them.
100
+ const settle = (check: () => void) =>
101
+ vi.waitFor(
102
+ async () => {
103
+ await act(async () => {});
104
+ check();
105
+ },
106
+ { timeout: 3_000 },
107
+ );
108
+
109
+ describe("grade preview sealed", () => {
110
+ it("reads the frame's grading status over the editing port", async () => {
111
+ const { grading } = installFrameGrading();
112
+ const state = renderController(opaqueFrame());
113
+
114
+ await settle(() =>
115
+ expect(state().runtimeStatus).toEqual({ state: "active", message: "Shader active" }),
116
+ );
117
+ expect(grading.getStatus).toHaveBeenCalledWith(target);
118
+ });
119
+
120
+ it("draws preset cards the frame renders, and drops any that are not image data", async () => {
121
+ const { grading } = installFrameGrading();
122
+ const state = renderController(opaqueFrame());
123
+
124
+ act(() => state().requestPresetPreviews());
125
+ await settle(() => expect(state().presetPreviews.images["bright-pop"]).toBeDefined());
126
+
127
+ const [renderedFor, candidates, options] = grading.renderPreviews.mock.calls[0]!;
128
+ expect(renderedFor).toEqual(target);
129
+ expect(candidates).toHaveLength(18);
130
+ expect(options).toMatchObject({ maxDimension: 160 });
131
+ expect(state().presetPreviews).toMatchObject({ width: 160, height: 90 });
132
+ expect(state().presetPreviews.images["bright-pop"]).toBe("data:image/png;base64,bright-pop");
133
+ expect(state().presetPreviews.images.neutral).toBeUndefined();
134
+ });
135
+
136
+ it("plays the hovered card in the frame and stops it when the hover ends", async () => {
137
+ const { grading, stopPlayback } = installFrameGrading();
138
+ const state = renderController(opaqueFrame());
139
+ const look = normalizeHfColorGrading({ preset: "bright-pop", intensity: 1 })!;
140
+
141
+ act(() =>
142
+ state().previewColorGrading(look, { animatedPreview: { kind: "presets", id: "bright-pop" } }),
143
+ );
144
+ await settle(() => expect(grading.renderPreviews).toHaveBeenCalled());
145
+ expect(grading.startPreviewPlayback).toHaveBeenCalledWith(target);
146
+ expect(grading.renderPreviews.mock.calls[0]![2]).toEqual({
147
+ maxDimension: 160,
148
+ useMediaTime: true,
149
+ });
150
+ await settle(() =>
151
+ expect(state().presetPreviews.images["bright-pop"]).toBe("data:image/png;base64,bright-pop"),
152
+ );
153
+
154
+ act(() => state().previewColorGrading(null));
155
+ await settle(() => expect(stopPlayback).toHaveBeenCalledTimes(1));
156
+ });
157
+
158
+ it("captures a graded frame from the frame", async () => {
159
+ installFrameGrading();
160
+ const state = renderController(opaqueFrame());
161
+
162
+ const frame = await act(() => state().captureGradedFrame());
163
+ expect(frame).toEqual({ dataUrl: "data:image/png;base64,capture", width: 160, height: 90 });
164
+ });
165
+ });
@@ -24,6 +24,7 @@ import {
24
24
  postRuntimeControlMessage,
25
25
  } from "../../player/lib/runtimeProtocol";
26
26
  import {
27
+ readSealedColorGradingStatus,
27
28
  useColorGradingPreviews,
28
29
  type ColorGradingCapturedFrame,
29
30
  type ColorGradingPresetPreviews,
@@ -31,6 +32,7 @@ import {
31
32
  } from "./useColorGradingPreviews";
32
33
  import { studioApiFetch } from "../../utils/studioApiFetch";
33
34
  import { authoredSrcPath } from "../../player/components/thumbnailUtils";
35
+ import { previewFrameOpaque } from "../../player/lib/previewChannel";
34
36
 
35
37
  export type { ColorGradingPresetPreviews, ColorGradingPreviewOptions };
36
38
 
@@ -279,8 +281,9 @@ export function useColorGradingController({
279
281
  [element.hfId, element.id, element.selector, element.selectorIndex],
280
282
  );
281
283
 
284
+ const statusReadRef = useRef(0);
282
285
  const refreshRuntimeStatus = useCallback(() => {
283
- setRuntimeStatus(readRuntimeColorGradingStatus(previewIframeRef?.current, target));
286
+ refreshStatus(previewIframeRef?.current, target, statusReadRef, setRuntimeStatus);
284
287
  }, [previewIframeRef, target]);
285
288
 
286
289
  useEffect(() => {
@@ -568,3 +571,19 @@ export function useColorGradingController({
568
571
  },
569
572
  };
570
573
  }
574
+
575
+ function refreshStatus(
576
+ iframe: HTMLIFrameElement | null | undefined,
577
+ target: HfColorGradingTarget,
578
+ reads: { current: number },
579
+ apply: (status: RuntimeColorGradingStatus) => void,
580
+ ): void {
581
+ if (!iframe || !previewFrameOpaque(iframe)) {
582
+ apply(readRuntimeColorGradingStatus(iframe, target));
583
+ return;
584
+ }
585
+ const read = ++reads.current;
586
+ void readSealedColorGradingStatus(iframe, target).then((status) => {
587
+ if (read === reads.current) apply(status);
588
+ });
589
+ }
@@ -1,4 +1,6 @@
1
1
  import { useLivePreviewIframe } from "../../player/store/previewIframeStore";
2
+ import { previewFrameOpaque, previewRequest } from "../../player/lib/previewChannel";
3
+ import type { EditingResult } from "@hyperframes/core/runtime/editing-port";
2
4
  import { useCallback, useEffect, useRef, useState, type RefObject } from "react";
3
5
  import {
4
6
  HF_COLOR_GRADING_ACTIVE_EFFECT_KEYS,
@@ -29,19 +31,23 @@ export interface ColorGradingPreviewOptions {
29
31
  animatedPreview: { kind: ColorGradingPreviewKind; id: string };
30
32
  }
31
33
 
34
+ type PreviewBatch = {
35
+ width: number;
36
+ height: number;
37
+ images: Array<{ id: string; dataUrl: string | null }>;
38
+ };
39
+
32
40
  type RuntimeColorGradingPreview = {
33
41
  renderPreviews: (
34
42
  target: HfColorGradingTarget,
35
43
  candidates: Array<{ id: string; grading: unknown }>,
36
44
  options: { maxDimension: number; useMediaTime?: boolean },
37
- ) => Promise<{
38
- width: number;
39
- height: number;
40
- images: Array<{ id: string; dataUrl: string | null }>;
41
- } | null>;
45
+ ) => Promise<PreviewBatch | null>;
42
46
  startPreviewPlayback?: (target: HfColorGradingTarget) => (() => void) | null;
43
47
  };
44
48
 
49
+ type AnimatedSession = { stopPlayback: () => void; timer: number | null };
50
+
45
51
  type PreviewRequest = {
46
52
  kind: ColorGradingPreviewKind;
47
53
  version: number;
@@ -74,6 +80,79 @@ function readRuntime(
74
80
  }
75
81
  }
76
82
 
83
+ const PORT_PREVIEW_TIMEOUT_MS = 15_000;
84
+
85
+ const isImageData = (value: unknown): value is string =>
86
+ typeof value === "string" && value.startsWith("data:image/");
87
+
88
+ function portBatch(batch: unknown): PreviewBatch | null {
89
+ if (typeof batch !== "object" || batch === null) return null;
90
+ const { width, height, images } = batch as Record<string, unknown>;
91
+ if (!Number.isFinite(width) || !Number.isFinite(height) || !Array.isArray(images)) return null;
92
+ return {
93
+ width: width as number,
94
+ height: height as number,
95
+ images: images.map((image: unknown) => {
96
+ const { id, dataUrl } = (image ?? {}) as { id?: unknown; dataUrl?: unknown };
97
+ return { id: String(id), dataUrl: isImageData(dataUrl) ? dataUrl : null };
98
+ }),
99
+ };
100
+ }
101
+
102
+ function portRuntime(iframe: HTMLIFrameElement): RuntimeColorGradingPreview {
103
+ return {
104
+ renderPreviews: async (target, candidates, { maxDimension, useMediaTime = false }) => {
105
+ const params = { target, candidates, maxDimension, useMediaTime };
106
+ const reply = await previewRequest(
107
+ iframe,
108
+ "color-grading-previews",
109
+ params,
110
+ PORT_PREVIEW_TIMEOUT_MS,
111
+ );
112
+ return portBatch(reply?.batch);
113
+ },
114
+ };
115
+ }
116
+
117
+ function previewRuntime(
118
+ iframe: HTMLIFrameElement | null | undefined,
119
+ ): RuntimeColorGradingPreview | null {
120
+ return iframe && previewFrameOpaque(iframe) ? portRuntime(iframe) : readRuntime(iframe);
121
+ }
122
+
123
+ function playInSealedFrame(
124
+ iframe: HTMLIFrameElement | null | undefined,
125
+ target: HfColorGradingTarget,
126
+ sessionRef: { current: AnimatedSession | null },
127
+ animate: (runtime: RuntimeColorGradingPreview, session: AnimatedSession) => void,
128
+ ): boolean {
129
+ if (!iframe || !previewFrameOpaque(iframe)) return false;
130
+ const stopPlayback = () =>
131
+ void previewRequest(iframe, "color-grading-playback", { target: null });
132
+ const session: AnimatedSession = { stopPlayback, timer: null };
133
+ sessionRef.current = session;
134
+ void previewRequest(iframe, "color-grading-playback", { target }).then((reply) => {
135
+ if (sessionRef.current !== session) return;
136
+ if (reply?.started) animate(portRuntime(iframe), session);
137
+ else sessionRef.current = null;
138
+ });
139
+ return true;
140
+ }
141
+
142
+ type ColorGradingStatus = NonNullable<EditingResult<"color-grading-status">["status"]>;
143
+ const STATUS_STATES = new Set(["missing", "inactive", "pending", "active", "unavailable"]);
144
+
145
+ export async function readSealedColorGradingStatus(
146
+ iframe: HTMLIFrameElement,
147
+ target: HfColorGradingTarget,
148
+ ): Promise<ColorGradingStatus> {
149
+ const reply = await previewRequest(iframe, "color-grading-status", { target });
150
+ const { state, message } = (reply?.status ?? {}) as Partial<Record<string, unknown>>;
151
+ return STATUS_STATES.has(state as string) && typeof message === "string"
152
+ ? { state: state as ColorGradingStatus["state"], message }
153
+ : { state: "pending", message: "Waiting for runtime" };
154
+ }
155
+
77
156
  function previewMaxDimension(): number {
78
157
  return Math.min(320, Math.ceil(160 * Math.max(1, window.devicePixelRatio || 1)));
79
158
  }
@@ -149,7 +228,7 @@ export function useColorGradingPreviews({
149
228
  previews: emptyPreviews(),
150
229
  }));
151
230
  const [request, setRequest] = useState<PreviewRequest | null>(null);
152
- const animatedRef = useRef<{ stopPlayback: () => void; timer: number | null } | null>(null);
231
+ const animatedRef = useRef<AnimatedSession | null>(null);
153
232
  if (state.identityKey !== identityKey) {
154
233
  setState({ identityKey, previews: emptyPreviews() });
155
234
  setRequest(null);
@@ -186,7 +265,7 @@ export function useColorGradingPreviews({
186
265
 
187
266
  const attempt = async () => {
188
267
  if (cancelled || complete || inFlight) return;
189
- const runtime = readRuntime(iframe);
268
+ const runtime = previewRuntime(iframe);
190
269
  if (!runtime) return;
191
270
  inFlight = true;
192
271
  try {
@@ -270,45 +349,50 @@ export function useColorGradingPreviews({
270
349
  stopAnimatedPreview();
271
350
  postColorGrading(next ?? gradingRef.current);
272
351
  if (!next || !options) return;
273
- const runtime = readRuntime(previewIframeRef?.current);
274
- const stopPlayback = runtime?.startPreviewPlayback?.(target);
275
- if (!runtime || !stopPlayback) return;
276
- const session = { stopPlayback, timer: null as number | null };
277
- animatedRef.current = session;
278
- const renderFrame = async () => {
279
- try {
280
- const batch = await runtime.renderPreviews(
281
- target,
282
- [{ id: options.animatedPreview.id, grading: toPreviewColorGrading(next) }],
283
- { maxDimension: previewMaxDimension(), useMediaTime: true },
284
- );
285
- const image = batch?.images[0]?.dataUrl;
286
- if (animatedRef.current !== session || !batch || !image) return;
287
- setState((current) => ({
288
- ...current,
289
- previews: {
290
- ...current.previews,
291
- [options.animatedPreview.kind]: {
292
- ...current.previews[options.animatedPreview.kind],
293
- status: "ready",
294
- images: {
295
- ...current.previews[options.animatedPreview.kind].images,
296
- [options.animatedPreview.id]: image,
352
+ const animate = (runtime: RuntimeColorGradingPreview, session: AnimatedSession) => {
353
+ const renderFrame = async () => {
354
+ try {
355
+ const batch = await runtime.renderPreviews(
356
+ target,
357
+ [{ id: options.animatedPreview.id, grading: toPreviewColorGrading(next) }],
358
+ { maxDimension: previewMaxDimension(), useMediaTime: true },
359
+ );
360
+ const image = batch?.images[0]?.dataUrl;
361
+ if (animatedRef.current !== session || !batch || !image) return;
362
+ setState((current) => ({
363
+ ...current,
364
+ previews: {
365
+ ...current.previews,
366
+ [options.animatedPreview.kind]: {
367
+ ...current.previews[options.animatedPreview.kind],
368
+ status: "ready",
369
+ images: {
370
+ ...current.previews[options.animatedPreview.kind].images,
371
+ [options.animatedPreview.id]: image,
372
+ },
373
+ width: batch.width,
374
+ height: batch.height,
297
375
  },
298
- width: batch.width,
299
- height: batch.height,
300
376
  },
301
- },
302
- }));
303
- } catch {
304
- // A later frame can recover after media/runtime readiness races.
305
- } finally {
306
- if (animatedRef.current === session) {
307
- session.timer = window.setTimeout(() => void renderFrame(), 100);
377
+ }));
378
+ } catch {
379
+ // A later frame can recover after media/runtime readiness races.
380
+ } finally {
381
+ if (animatedRef.current === session) {
382
+ session.timer = window.setTimeout(() => void renderFrame(), 100);
383
+ }
308
384
  }
309
- }
385
+ };
386
+ void renderFrame();
310
387
  };
311
- void renderFrame();
388
+ const iframe = previewIframeRef?.current;
389
+ if (playInSealedFrame(iframe, target, animatedRef, animate)) return;
390
+ const runtime = readRuntime(iframe);
391
+ const stopPlayback = runtime?.startPreviewPlayback?.(target);
392
+ if (!runtime || !stopPlayback) return;
393
+ const session = { stopPlayback, timer: null as number | null };
394
+ animatedRef.current = session;
395
+ animate(runtime, session);
312
396
  },
313
397
  [gradingRef, postColorGrading, previewIframeRef, stopAnimatedPreview, target],
314
398
  );
@@ -336,7 +420,7 @@ export function useColorGradingPreviews({
336
420
  }: {
337
421
  grading?: NormalizedHfColorGrading;
338
422
  } = {}): Promise<ColorGradingCapturedFrame | null> => {
339
- const runtime = readRuntime(previewIframeRef?.current);
423
+ const runtime = previewRuntime(previewIframeRef?.current);
340
424
  if (!runtime) return null;
341
425
  const batch = await runtime.renderPreviews(
342
426
  target,
@@ -19,6 +19,7 @@ import {
19
19
  } from "@hyperframes/core/audio-gain";
20
20
  import type { StereoLevel } from "@hyperframes/core/runtime/levelTap";
21
21
  import { usePlayerStore } from "../../player";
22
+ import { previewFrameOpaque, previewRequest } from "../../player/lib/previewChannel";
22
23
  import { clampNumber } from "../../utils/studioHelpers";
23
24
  import { useAudioMetersVisible } from "../../utils/audioMeterVisibility";
24
25
  import { EXPORT_PEAK_CEILING } from "../../utils/exportPeakCeiling";
@@ -135,6 +136,55 @@ function readHook(iframe: HTMLIFrameElement | null | undefined): AudioMeterHook
135
136
  }
136
137
  }
137
138
 
139
+ const isFields = (value: unknown): value is Record<string, unknown> =>
140
+ typeof value === "object" && value !== null;
141
+ const stereo = (value: unknown): StereoLevel | undefined =>
142
+ isFields(value) && Number.isFinite(value.l) && Number.isFinite(value.r)
143
+ ? { l: value.l as number, r: value.r as number }
144
+ : undefined;
145
+
146
+ function meterLevels(value: unknown): Levels | undefined {
147
+ if (!isFields(value) || !isFields(value.groups)) return undefined;
148
+ const master = stereo(value.master);
149
+ if (!master) return undefined;
150
+ const groups = Object.entries(value.groups).flatMap(([id, level]) => {
151
+ const read = stereo(level);
152
+ return read ? [[id, read] as const] : [];
153
+ });
154
+ return { master, groups: Object.fromEntries(groups) };
155
+ }
156
+
157
+ /** Read interval for a sealed preview's taps: about 30 a second, near the analyser's own window. */
158
+ const SEALED_METER_READ_MS = 33;
159
+
160
+ function createSealedMeter() {
161
+ let frame: HTMLIFrameElement | null = null;
162
+ let levels: Levels | undefined;
163
+ let busy = false;
164
+ let askedAt = -Infinity;
165
+ const stop = () => {
166
+ if (frame) void previewRequest(frame, "audio-meter", { on: false });
167
+ frame = null;
168
+ levels = undefined;
169
+ };
170
+ const read = (iframe: HTMLIFrameElement, now: number): Levels | undefined => {
171
+ if (iframe !== frame) stop();
172
+ frame = iframe;
173
+ if (busy || now - askedAt < SEALED_METER_READ_MS) return levels;
174
+ busy = true;
175
+ askedAt = now;
176
+ void previewRequest(iframe, "audio-meter", { on: true })
177
+ .then((reply) => {
178
+ if (frame === iframe) levels = meterLevels(reply?.levels);
179
+ })
180
+ .finally(() => {
181
+ busy = false;
182
+ });
183
+ return levels;
184
+ };
185
+ return { read, stop };
186
+ }
187
+
138
188
  /** 0 dB is bottom 100% plus 1px down, so the tick stays inside overflow-hidden.
139
189
  * A calc() bottom does not stick as an inline style on Windows happy-dom. */
140
190
  function paintPeak(el: HTMLElement | null, peak: number): void {
@@ -218,10 +268,20 @@ function useMeterLoop(
218
268
  let active: AudioMeterHook | null = null;
219
269
  let last = performance.now();
220
270
  const state = new Map<string | null, Pair>();
271
+ const sealed = createSealedMeter();
272
+ const readLevels = (now: number): Levels | undefined => {
273
+ const iframe = previewIframeRef?.current;
274
+ if (iframe && previewFrameOpaque(iframe)) {
275
+ active = followMeterHook(active, null);
276
+ return sealed.read(iframe, now);
277
+ }
278
+ sealed.stop();
279
+ active = followMeterHook(active, readHook(iframe));
280
+ return active?.read();
281
+ };
221
282
  const tick = (now: number) => {
222
283
  raf = requestAnimationFrame(tick);
223
- active = followMeterHook(active, readHook(previewIframeRef?.current));
224
- const levels = active?.read();
284
+ const levels = readLevels(now);
225
285
  const dt = now - last;
226
286
  last = now;
227
287
  evictGoneMeterState(state, new Set(stripsRef.current.map((s) => s.id)));
@@ -235,6 +295,7 @@ function useMeterLoop(
235
295
  raf = requestAnimationFrame(tick);
236
296
  return () => {
237
297
  cancelAnimationFrame(raf);
298
+ sealed.stop();
238
299
  try {
239
300
  active?.stop();
240
301
  } catch {
@@ -590,6 +590,30 @@ describe("NLEPreview", () => {
590
590
  },
591
591
  );
592
592
 
593
+ // Chrome stops rendering a hidden or clipped cross-origin frame, and a sealed one is cross-origin.
594
+ it.each([
595
+ ["opaque" as const, ["opacity: 0"], ["visibility", "clip-path", "z-index"]],
596
+ [undefined, ["visibility: hidden", "clip-path: inset(100%)"], ["opacity"]],
597
+ ])(
598
+ "keeps a reload's preview out of sight, and a sealed one rendering (%s)",
599
+ (sandboxOrigin, present, absent) => {
600
+ const view = renderPreview(
601
+ [
602
+ { gen: 0, role: "live" },
603
+ { gen: 1, role: "shadow", url: "/api/projects/p/preview?_t=1" },
604
+ ],
605
+ { sandboxOrigin },
606
+ );
607
+ const shadow = [...view.stage.querySelectorAll('[data-testid="mock-player"]')]
608
+ .map((player) => player.getAttribute("style") ?? "")
609
+ .filter((style) => style.includes("pointer-events: none"));
610
+ expect(shadow).toHaveLength(1);
611
+ for (const rule of present) expect(shadow[0]).toContain(rule);
612
+ for (const rule of absent) expect(shadow[0]).not.toContain(rule);
613
+ view.cleanup();
614
+ },
615
+ );
616
+
593
617
  it("covers the live preview with the cached frame-0 poster until it is ready to show", () => {
594
618
  const view = renderPreview();
595
619
  const poster = () =>
@@ -29,11 +29,7 @@ import {
29
29
  } from "./previewZoom";
30
30
  import { RULER_GUTTER_PX, usePreviewGuidesStore } from "../editor/previewGuidesStore";
31
31
  import { PreviewZoomOverlay, usePreviewZoomOverlay } from "./PreviewZoomOverlay";
32
- import {
33
- PREVIEW_STAGE_INSET_PX,
34
- resolvePreviewStageSize,
35
- SHADOW_IFRAME_STYLE,
36
- } from "./previewStage";
32
+ import { PREVIEW_STAGE_INSET_PX, resolvePreviewStageSize, shadowIframeStyle } from "./previewStage";
37
33
  import { usePreviewFirstFrameTelemetry } from "../../player/hooks/usePreviewFirstFrameTelemetry";
38
34
  import { PreviewPoster, usePreviewPoster } from "./PreviewPoster";
39
35
  interface NLEPreviewProps extends Pick<
@@ -534,7 +530,7 @@ export const NLEPreview = memo(function NLEPreview({
534
530
  suppressLoadingOverlay
535
531
  noLoadTimeout
536
532
  sandboxOrigin={sandboxOrigin}
537
- style={SHADOW_IFRAME_STYLE}
533
+ style={shadowIframeStyle(sandboxOrigin)}
538
534
  />
539
535
  ),
540
536
  )}