fix(studio): a sealed preview takes its film's size from the player (#7967)

* fix(studio): a sealed preview takes its film's size from the player

A sealed preview frame refuses Studio's reads, so the size reader found no
document and Studio kept a 16:9 stage and no size for anything placed on
the picture. For a sealed frame it now reads the size its player got from
the runtime, once the player is ready, and the preview reads it again when
the player reports it.

* fix(studio): a sealed preview's size reader keeps its files' comment share

* refactor(studio): the player's size callback reaches its newest owner through one stable call

* fix(studio): a sealed preview reads its size once its film is ready to show, with no new player prop

* test(studio): the preview's mock player loads once, as a frame does
This commit is contained in:
Miguel Ángel
2026-11-18 06:59:47 +00:00
committed by GitHub
parent 12a2f83c49
commit d574ded1ec
5 changed files with 111 additions and 6 deletions
@@ -34,9 +34,8 @@ vi.mock("../../player", async () => {
ref: React.ForwardedRef<HTMLIFrameElement>,
) {
if (!props.suppressLoadingOverlay) livePlayerProps = props;
React.useEffect(() => {
props.onLoad?.();
}, [props]);
const onLoad = React.useRef(props.onLoad);
React.useEffect(() => onLoad.current?.(), []);
React.useState(() => playerMounts.push(props.suppressLoadingOverlay ? "shadow" : "live"));
return React.createElement("div", {
@@ -556,6 +555,22 @@ describe("NLEPreview", () => {
}
});
it("takes a sealed preview's size once its film is ready to show, after the frame has loaded", () => {
const box = { width: 800, height: 600 };
const sealed = renderPreview(undefined, { box, sandboxOrigin: "opaque" });
try {
expect([sealed.stage.style.width, sealed.stage.style.height]).toEqual(["784px", "441px"]);
measured.size = { width: 1080, height: 1920 };
act(() => livePlayerProps.onReadyToShowChange?.(true));
const shape = parseFloat(sealed.stage.style.width) / parseFloat(sealed.stage.style.height);
expect(shape).toBeCloseTo(1080 / 1920, 3);
expect(sealed.stage.style.height).toBe("584px");
} finally {
measured.size = null;
sealed.cleanup();
}
});
it("clips a shadow reload so its own loading overlay cannot paint over the live frame", () => {
const view = renderPreview([
{ gen: 0, role: "live" },
@@ -180,6 +180,15 @@ export const NLEPreview = memo(function NLEPreview({
onCompositionSizeChangeRef.current?.(compositionSize);
}, [compositionSize]);
const showLive = poster.onLiveReadyToShowChange;
const onLiveReadyToShowChange = useCallback(
(ready: boolean) => {
showLive(ready);
if (ready) updateCompositionSizeFromPreview();
},
[showLive, updateCompositionSizeFromPreview],
);
const setPreviewIframeRef = useCallback(
(node: HTMLIFrameElement | null) => {
previewIframeRef.current = node;
@@ -503,7 +512,7 @@ export const NLEPreview = memo(function NLEPreview({
applyInitialZoom();
}}
onCompositionLoadingChange={onCompositionLoadingChange}
onReadyToShowChange={poster.onLiveReadyToShowChange}
onReadyToShowChange={onLiveReadyToShowChange}
onPreviewError={poster.onPreviewError}
onPainted={(details) => reportPreviewFirstFrame(slot, details)}
portrait={portrait}
@@ -101,7 +101,7 @@ function isPreviewPlayerHost(value: unknown): value is PreviewPlayerHost {
return value instanceof HTMLElement;
}
function resolvePreviewPlayerHost(iframe: HTMLIFrameElement): PreviewPlayerHost | null {
export function resolvePreviewPlayerHost(iframe: HTMLIFrameElement): PreviewPlayerHost | null {
const root = iframe.getRootNode();
if (
typeof ShadowRoot !== "undefined" &&
@@ -0,0 +1,61 @@
// @vitest-environment happy-dom
import { afterEach, describe, expect, it } from "vitest";
import { readPreviewCompositionSize } from "./previewCompositionSize";
type Player = HTMLElement & {
ready?: boolean;
compositionWidth?: number;
compositionHeight?: number;
};
/** A frame in a player's shadow root, as <hyperframes-player> holds it; a sealed one refuses its document. */
function frameInPlayer(sealed: boolean, player: Partial<Player>) {
const host = Object.assign(document.createElement("div"), player) as Player;
document.body.append(host);
const iframe = document.createElement("iframe");
host.attachShadow({ mode: "open" }).append(iframe);
if (sealed) {
const refusing = {
get document(): Document {
throw new DOMException("Blocked a frame with origin", "SecurityError");
},
};
Object.defineProperty(iframe, "contentWindow", { get: () => refusing });
Object.defineProperty(iframe, "contentDocument", { get: () => null });
}
return iframe;
}
afterEach(() => document.body.replaceChildren());
describe("readPreviewCompositionSize", () => {
it("reads a sealed frame's size from its player once the player is ready", () => {
const iframe = frameInPlayer(true, {
ready: true,
compositionWidth: 1080,
compositionHeight: 1920,
});
expect(readPreviewCompositionSize(iframe)).toEqual({ width: 1080, height: 1920 });
});
it("reads no size from a sealed frame's player before it is ready, whose numbers are only defaults", () => {
const iframe = frameInPlayer(true, {
ready: false,
compositionWidth: 1920,
compositionHeight: 1080,
});
expect(readPreviewCompositionSize(iframe)).toBeNull();
});
it("reads a frame Studio can script from its document, not its player", () => {
const iframe = frameInPlayer(false, {
ready: true,
compositionWidth: 1920,
compositionHeight: 1080,
});
iframe.contentDocument!.body.innerHTML =
'<div data-composition-id="main" data-width="720" data-height="1280"></div>';
expect(readPreviewCompositionSize(iframe)).toEqual({ width: 720, height: 1280 });
});
});
@@ -1,13 +1,33 @@
import { previewFrameOpaque } from "../player/lib/previewChannel";
import { resolvePreviewPlayerHost } from "../player/lib/timelineIframeHelpers";
export interface PreviewCompositionSize {
width: number;
height: number;
}
/** The root composition's authored size, read from the preview document itself. */
type SizedPlayer = HTMLElement & {
ready?: boolean;
compositionWidth?: number;
compositionHeight?: number;
};
const isPositive = (value: unknown): value is number =>
typeof value === "number" && Number.isFinite(value) && value > 0;
function readyPlayerCompositionSize(iframe: HTMLIFrameElement): PreviewCompositionSize | null {
const player = resolvePreviewPlayerHost(iframe) as SizedPlayer | null;
if (!player?.ready) return null;
const { compositionWidth: width, compositionHeight: height } = player;
return isPositive(width) && isPositive(height) ? { width, height } : null;
}
/** The root composition's authored size, from the preview document, or from its player when the frame is sealed. */
// fallow-ignore-next-line complexity
export function readPreviewCompositionSize(
iframe: HTMLIFrameElement | null,
): PreviewCompositionSize | null {
if (iframe && previewFrameOpaque(iframe)) return readyPlayerCompositionSize(iframe);
try {
const doc = iframe?.contentDocument;
const root =