From 579feb02eefc0b50988634d9d9c44cc02ef14dea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Wed, 18 Nov 2026 13:35:14 +0000 Subject: [PATCH] fix(studio): sealed previews fill scene children, clip parents and first-frame counts (#8180) * test(studio): a sealed scene's children and clip parents fill as an open preview's do * fix(studio): a sealed preview's scene children and clip parents come from the editing port * test(studio): a sealed preview's first-frame event carries its clips and length * fix(studio): a sealed preview's first-frame event counts its clips from the document copy * refactor(studio): drop two comments the names already carry --- ...ePreviewFirstFrameTelemetry.sealed.test.ts | 43 +++++++++++++++++++ .../hooks/usePreviewFirstFrameTelemetry.ts | 18 +++++--- .../useTimelineSyncCallbacks.sealed.test.ts | 39 ++++++++++++++++- .../player/hooks/useTimelineSyncCallbacks.ts | 32 +++++++++----- .../studio/src/player/lib/previewChannel.ts | 12 ++++++ 5 files changed, 124 insertions(+), 20 deletions(-) create mode 100644 packages/studio/src/player/hooks/usePreviewFirstFrameTelemetry.sealed.test.ts diff --git a/packages/studio/src/player/hooks/usePreviewFirstFrameTelemetry.sealed.test.ts b/packages/studio/src/player/hooks/usePreviewFirstFrameTelemetry.sealed.test.ts new file mode 100644 index 0000000000..8ddaacbc5e --- /dev/null +++ b/packages/studio/src/player/hooks/usePreviewFirstFrameTelemetry.sealed.test.ts @@ -0,0 +1,43 @@ +// @vitest-environment happy-dom + +import { act, createElement } from "react"; +import { createRoot } from "react-dom/client"; +import { afterEach, expect, it, vi } from "vitest"; +import { sealedPreviewFrame } from "../lib/sealedPreviewFrame.test-helpers"; +import { usePreviewFirstFrameTelemetry } from "./usePreviewFirstFrameTelemetry"; + +const firstFrames = vi.hoisted(() => [] as Array>); +vi.mock("../../telemetry/events", () => ({ + trackPreviewFirstFrame: (props: Record) => firstFrames.push(props), +})); + +(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; + +afterEach(() => { + document.body.innerHTML = ""; + firstFrames.length = 0; +}); + +// fails without the fix: a sealed frame's document read as null, so the event said 0 clips and 0 seconds. +it("reports a sealed preview's clips and length on its first frame", async () => { + document.body.innerHTML = `
+
+ +
`; + const slot = { gen: 1, role: "live" as const }; + let report: ReturnType | undefined; + function Harness() { + report = usePreviewFirstFrameTelemetry([slot]); + return null; + } + act(() => createRoot(document.createElement("div")).render(createElement(Harness))); + + report!(slot, { iframe: sealedPreviewFrame().iframe, startedAt: performance.now(), loadId: 1 }); + + await vi.waitFor(() => expect(firstFrames).toHaveLength(1)); + expect(firstFrames[0]).toMatchObject({ + composition_seconds: 6, + clip_count: 2, + media_clip_count: 1, + }); +}); diff --git a/packages/studio/src/player/hooks/usePreviewFirstFrameTelemetry.ts b/packages/studio/src/player/hooks/usePreviewFirstFrameTelemetry.ts index afe7a58cfe..b3105e4143 100644 --- a/packages/studio/src/player/hooks/usePreviewFirstFrameTelemetry.ts +++ b/packages/studio/src/player/hooks/usePreviewFirstFrameTelemetry.ts @@ -4,6 +4,7 @@ import { readTimelineDurationFromDocument } from "../lib/timelineElementHelpers" import type { PreviewIframeSlot } from "./useTimelineSyncCallbacks"; import { trackPreviewFirstFrame } from "../../telemetry/events"; import { getBrowserSystemMeta } from "../../telemetry/system"; +import { previewDocumentCopy, previewFrameOpaque } from "../lib/previewChannel"; export function readPreviewComplexity(doc: Document | null | undefined): { clip_count: number; @@ -38,13 +39,16 @@ export function usePreviewFirstFrameTelemetry(previewSlots: PreviewIframeSlot[]) const reportKey = `${slot.role}:${slot.gen}:${loadId}`; if (reportedPaintsRef.current.has(reportKey)) return; reportedPaintsRef.current.add(reportKey); - const complexity = readPreviewComplexity(iframe.contentDocument); - trackPreviewFirstFrame({ - duration_ms: Math.max(0, Math.round(performance.now() - startedAt)), - composition_seconds: readTimelineDurationFromDocument(iframe.contentDocument), - ...complexity, - studio_version: getBrowserSystemMeta().studio_version, - }); + const durationMs = Math.max(0, Math.round(performance.now() - startedAt)); + const report = (doc: Document | null) => + trackPreviewFirstFrame({ + duration_ms: durationMs, + composition_seconds: readTimelineDurationFromDocument(doc), + ...readPreviewComplexity(doc), + studio_version: getBrowserSystemMeta().studio_version, + }); + if (previewFrameOpaque(iframe)) void previewDocumentCopy(iframe).then(report); + else report(iframe.contentDocument); }, [], ); diff --git a/packages/studio/src/player/hooks/useTimelineSyncCallbacks.sealed.test.ts b/packages/studio/src/player/hooks/useTimelineSyncCallbacks.sealed.test.ts index b1e3cb9619..3c616bcd43 100644 --- a/packages/studio/src/player/hooks/useTimelineSyncCallbacks.sealed.test.ts +++ b/packages/studio/src/player/hooks/useTimelineSyncCallbacks.sealed.test.ts @@ -2,8 +2,8 @@ import { act, createElement } from "react"; import { createRoot } from "react-dom/client"; -import { afterEach, expect, it, vi } from "vitest"; -import type { TimelineElement } from "../store/playerStore"; +import { afterEach, expect, it, onTestFinished, vi } from "vitest"; +import { usePlayerStore, type TimelineElement } from "../store/playerStore"; import { useTimelineSyncCallbacks } from "./useTimelineSyncCallbacks"; import { sealedPreviewFrame } from "../lib/sealedPreviewFrame.test-helpers"; @@ -69,3 +69,38 @@ it("binds a sealed preview's timeline row to its element, as an open preview doe audioGroup: "voice", }); }); + +// fails without the fix: reading the frame's `__clipTree` threw, so a sealed scene's children and parents stayed empty. +it("fills a sealed scene's children and the runtime's clip parents", async () => { + document.body.innerHTML = `
+
+

Title

+
+
`; + (window as unknown as { __clipTree: unknown }).__clipTree = { + roots: [ + { id: "scene", parentId: null, children: [{ id: "cue", parentId: "scene", children: [] }] }, + ], + }; + onTestFinished(() => delete (window as unknown as { __clipTree?: unknown }).__clipTree); + usePlayerStore.getState().setClipParentMap(new Map()); + usePlayerStore.getState().setDomClipChildren([]); + const { process } = processWith(sealedPreviewFrame().iframe); + + process({ + clips: [ + { + ...boxClip, + id: "scene", + label: "scene", + kind: "composition", + tagName: "div", + compositionId: "scene", + }, + ], + durationInFrames: 60, + }); + + await vi.waitFor(() => expect(usePlayerStore.getState().clipParentMap.get("cue")).toBe("scene")); + expect(usePlayerStore.getState().domClipChildren.map((child) => child.id)).toContain("title"); +}); diff --git a/packages/studio/src/player/hooks/useTimelineSyncCallbacks.ts b/packages/studio/src/player/hooks/useTimelineSyncCallbacks.ts index d0c21fbfa6..790ea71e29 100644 --- a/packages/studio/src/player/hooks/useTimelineSyncCallbacks.ts +++ b/packages/studio/src/player/hooks/useTimelineSyncCallbacks.ts @@ -15,7 +15,7 @@ import type { PlaybackAdapter, IframeWindow } from "../lib/playbackTypes"; import { readTimelineDurationFromDocument } from "../lib/timelineDOM"; import { buildMissingCompositionElements } from "../lib/timelineIframeHelpers"; import { acceptedRuntimeMessageFps } from "../lib/runtimeProtocol"; -import { previewDocumentCopy, previewFrameOpaque } from "../lib/previewChannel"; +import { previewClipParents, previewDocumentCopy, previewFrameOpaque } from "../lib/previewChannel"; import { useLinkedClipPreferences } from "../../utils/linkedClipPreferences"; import { buildTimelineElementsFromClips, @@ -153,6 +153,15 @@ export function resolveTimelineTotalDuration(input: { ); } +/** A same-origin frame's clip tree, read in place; a frame that refuses the read has none. */ +function openClipParents(iframe: HTMLIFrameElement | null): Map { + try { + return clipTreeParentMap(iframe?.contentWindow ?? null); + } catch { + return new Map(); + } +} + export function useTimelineSyncCallbacks({ iframeRef, probeIntervalRef, @@ -173,14 +182,13 @@ export function useTimelineSyncCallbacks({ }: UseTimelineSyncCallbacksParams) { // Convert a runtime timeline message (from iframe postMessage) into TimelineElements const applyTimelineMessage = useCallback( - (data: RuntimeTimelineMessage, iframeDoc: Document | null) => { + (data: RuntimeTimelineMessage, iframeDoc: Document | null, parentMap: Map) => { // Show root-level clips: no parentCompositionId, OR parent is a "phantom wrapper" const clipCompositionIds = new Set(data.clips.map((c) => c.compositionId).filter(Boolean)); const filtered = data.clips.filter( (clip) => !clip.parentCompositionId || !clipCompositionIds.has(clip.parentCompositionId), ); try { - const parentMap = clipTreeParentMap(iframeRef.current?.contentWindow ?? null); const domClipChildren = collectSubCompositionDomChildren(iframeDoc, data.clips, parentMap); usePlayerStore.getState().setClipParentMap(parentMap); usePlayerStore.getState().setDomClipChildren(domClipChildren); @@ -188,7 +196,7 @@ export function useTimelineSyncCallbacks({ .getState() .setSubCompositionHostState(collectSubCompositionHostState(iframeDoc, data.clips)); } catch { - // cross-origin or __clipTree not available — maps stay empty + // A document the walk cannot read leaves the maps empty. } const els = buildTimelineElementsFromClips(filtered, iframeDoc); @@ -211,7 +219,7 @@ export function useTimelineSyncCallbacks({ syncTimelineElements, ); }, - [iframeRef, syncTimelineElements], + [syncTimelineElements], ); const timelineReads = useRef(0); @@ -222,14 +230,16 @@ export function useTimelineSyncCallbacks({ const iframe = iframeRef.current; const read = ++timelineReads.current; if (!iframe || !previewFrameOpaque(iframe)) { - applyTimelineMessage(data, safeContentDocument(iframe)); + applyTimelineMessage(data, safeContentDocument(iframe), openClipParents(iframe)); return; } - // A sealed frame's rows bind to a copy of its document from the editing port; the newest read wins. - void previewDocumentCopy(iframe).then((doc) => { - if (read === timelineReads.current && iframeRef.current === iframe) - applyTimelineMessage(data, doc); - }); + // A sealed frame's rows bind to its document copy and clip tree from the editing port; the newest read wins. + void Promise.all([previewDocumentCopy(iframe), previewClipParents(iframe)]).then( + ([doc, parents]) => { + if (read === timelineReads.current && iframeRef.current === iframe) + applyTimelineMessage(data, doc, parents); + }, + ); }, [iframeRef, applyTimelineMessage], ); diff --git a/packages/studio/src/player/lib/previewChannel.ts b/packages/studio/src/player/lib/previewChannel.ts index 824ecb571b..2bfddf4f75 100644 --- a/packages/studio/src/player/lib/previewChannel.ts +++ b/packages/studio/src/player/lib/previewChannel.ts @@ -106,6 +106,18 @@ export async function previewClipManifest(iframe: HTMLIFrameElement): Promise> { + const snapshot = await previewRequest(iframe, "runtime-snapshot", {}); + const pairs: unknown = snapshot?.clipParents; + const parents = new Map(); + if (!Array.isArray(pairs)) return parents; + for (const pair of pairs) { + if (Array.isArray(pair) && typeof pair[0] === "string" && typeof pair[1] === "string") + parents.set(pair[0], pair[1]); + } + return parents; +} + export async function previewConsoleErrors( iframe: HTMLIFrameElement, from: number,