From cda94eb0f63cb81ed8ebc806520fb72ce4cb08c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Tue, 17 Nov 2026 16:49:19 +0000 Subject: [PATCH] fix(studio): timeline rows in a sealed preview know their element, source file and audio group (#8078) * test(studio): a sealed preview's timeline rows bind to their elements * fix(studio): timeline rows in a sealed preview know their element, source file and audio group --- .../useTimelineSyncCallbacks.sealed.test.ts | 98 +++++++++++++++++++ .../player/hooks/useTimelineSyncCallbacks.ts | 31 ++++-- 2 files changed, 121 insertions(+), 8 deletions(-) create mode 100644 packages/studio/src/player/hooks/useTimelineSyncCallbacks.sealed.test.ts diff --git a/packages/studio/src/player/hooks/useTimelineSyncCallbacks.sealed.test.ts b/packages/studio/src/player/hooks/useTimelineSyncCallbacks.sealed.test.ts new file mode 100644 index 0000000000..c3838d76de --- /dev/null +++ b/packages/studio/src/player/hooks/useTimelineSyncCallbacks.sealed.test.ts @@ -0,0 +1,98 @@ +// @vitest-environment happy-dom + +import { act, createElement } from "react"; +import { createRoot } from "react-dom/client"; +import { afterEach, expect, it, vi } from "vitest"; +// The frame's real runtime agent, serving the opaque frame from this document as Studio's marked preview page. +import { installMarkedEditingAgent } from "../../../../core/src/runtime/editingAgent.test-helpers"; +import type { TimelineElement } from "../store/playerStore"; +import { detachedFakeIframe } from "./timelinePlayerTestHarness"; +import { useTimelineSyncCallbacks } from "./useTimelineSyncCallbacks"; + +(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; + +const closers: Array<() => void> = []; +afterEach(() => { + closers.splice(0).forEach((close) => close()); + document.body.innerHTML = ""; +}); + +/** A frame Studio cannot read, whose runtime is the real editing agent over this document. */ +function opaqueFrame() { + const frame: Record = {}; + const iframe = detachedFakeIframe(frame); + Object.defineProperty(iframe, "contentDocument", { value: null }); + Object.defineProperty(frame, "document", { + get: () => { + throw new DOMException("blocked", "SecurityError"); + }, + }); + const parent = { + postMessage: (data: unknown, _origin: string, ports: MessagePort[]) => + window.dispatchEvent( + new MessageEvent("message", { source: frame as unknown as Window, data, ports }), + ), + } as unknown as Window; + const agent = installMarkedEditingAgent(parent); + frame.postMessage = (data: unknown) => + window.dispatchEvent(new MessageEvent("message", { source: parent, data })); + closers.push(() => agent.dispose()); + return iframe; +} + +function processWith(iframe: HTMLIFrameElement) { + const sync = vi.fn<(els: TimelineElement[], duration?: number) => void>(); + let callbacks: ReturnType | undefined; + function Harness() { + callbacks = useTimelineSyncCallbacks({ + iframeRef: { current: iframe }, + probeIntervalRef: { current: undefined }, + pendingSeekRef: { current: null }, + isRefreshingRef: { current: false }, + getAdapter: () => null, + syncTimelineElements: sync, + setDuration: () => {}, + setCurrentTime: () => {}, + requestTimelineReady: () => {}, + setIsPlaying: () => {}, + attachIframeShortcutListeners: () => {}, + applyPreviewAudioState: () => {}, + }); + return null; + } + const host = document.createElement("div"); + act(() => createRoot(host).render(createElement(Harness))); + return { sync, process: callbacks!.processTimelineMessage }; +} + +const boxClip = { + id: "box", + label: "box", + start: 0, + duration: 2, + track: 0, + kind: "element" as const, + tagName: "div", + compositionId: null, + parentCompositionId: null, + compositionSrc: null, + assetUrl: null, +}; + +// fails without the fix: a sealed frame's rows had no element behind them, so no id, source file or audio group. +it("binds a sealed preview's timeline row to its element, as an open preview does", async () => { + document.body.innerHTML = `
+
+
`; + const { sync, process } = processWith(opaqueFrame()); + + process({ clips: [boxClip], durationInFrames: 60 }); + + await vi.waitFor(() => expect(sync.mock.calls.at(-1)?.[0][0]?.domId).toBe("box")); + const [row] = sync.mock.calls.at(-1)![0]; + expect(row).toMatchObject({ + key: "index.html#box", + sourceFile: "index.html", + audioGroup: "voice", + }); +}); diff --git a/packages/studio/src/player/hooks/useTimelineSyncCallbacks.ts b/packages/studio/src/player/hooks/useTimelineSyncCallbacks.ts index 7361641288..d0c21fbfa6 100644 --- a/packages/studio/src/player/hooks/useTimelineSyncCallbacks.ts +++ b/packages/studio/src/player/hooks/useTimelineSyncCallbacks.ts @@ -15,6 +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 { useLinkedClipPreferences } from "../../utils/linkedClipPreferences"; import { buildTimelineElementsFromClips, @@ -171,19 +172,13 @@ export function useTimelineSyncCallbacks({ hostOwnsDeadline, }: UseTimelineSyncCallbacksParams) { // Convert a runtime timeline message (from iframe postMessage) into TimelineElements - const processTimelineMessage = useCallback( - (data: RuntimeTimelineMessage) => { - if (!data.clips) return; - - usePlayerStore.getState().setClipManifest(data.clips); - + const applyTimelineMessage = useCallback( + (data: RuntimeTimelineMessage, iframeDoc: Document | null) => { // 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), ); - const iframeDoc = safeContentDocument(iframeRef.current); - try { const parentMap = clipTreeParentMap(iframeRef.current?.contentWindow ?? null); const domClipChildren = collectSubCompositionDomChildren(iframeDoc, data.clips, parentMap); @@ -219,6 +214,26 @@ export function useTimelineSyncCallbacks({ [iframeRef, syncTimelineElements], ); + const timelineReads = useRef(0); + const processTimelineMessage = useCallback( + (data: RuntimeTimelineMessage) => { + if (!data.clips) return; + usePlayerStore.getState().setClipManifest(data.clips); + const iframe = iframeRef.current; + const read = ++timelineReads.current; + if (!iframe || !previewFrameOpaque(iframe)) { + applyTimelineMessage(data, safeContentDocument(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); + }); + }, + [iframeRef, applyTimelineMessage], + ); + const enrichMissingCompositions = useCallback(() => { try { const iframe = iframeRef.current;