From 01618d504aed5f8ac935394b67156a4c43baf5e7 Mon Sep 17 00:00:00 2001 From: James Russo Date: Thu, 12 Nov 2026 10:59:31 +0000 Subject: [PATCH] feat(studio): picker styles and composition size read an opaque preview over its channel (#7602) First slice of moving Studio's selection and overlay readers off the preview frame's DOM. Same-origin frames keep their direct reads; only a frame Studio cannot script takes the new paths. - Runtime editing port: a validated computed-styles request, answered with the same logical opacity Studio reads (moved to core). - Element picker: on an opaque frame, the picked element's computed styles come from the runtime. Picker posts are taken only from an origin the posting frame can have. - Composition rect: an opaque frame's declared size comes from the runtime's stage-size post, mirrored only for registered frames and their origins. Co-authored-by: Claude Opus 5.5 --- .../core/src/runtime/editingAgent.test.ts | 69 +++++++++++++- packages/core/src/runtime/editingAgent.ts | 13 +++ packages/core/src/runtime/editingPort.test.ts | 2 +- packages/core/src/runtime/editingPort.ts | 24 +++++ packages/core/src/studioPreviewMark.ts | 11 +++ .../src/components/editor/domEditingDom.ts | 17 +--- .../editor/useDomEditCompositionRect.ts | 17 +++- .../editor/usePreviewCompositionRect.test.tsx | 39 ++++++++ .../studio/src/hooks/elementPickerStyles.ts | 53 +++++++++++ .../studio/src/hooks/useElementPicker.test.ts | 86 ++++++++++++++++- packages/studio/src/hooks/useElementPicker.ts | 93 ++++++------------- .../studio/src/player/lib/bridgePlayback.ts | 47 +++++++--- .../studio/src/player/lib/previewChannel.ts | 19 ++++ 13 files changed, 388 insertions(+), 102 deletions(-) create mode 100644 packages/studio/src/hooks/elementPickerStyles.ts diff --git a/packages/core/src/runtime/editingAgent.test.ts b/packages/core/src/runtime/editingAgent.test.ts index b3119162fe..4d4dcb8058 100644 --- a/packages/core/src/runtime/editingAgent.test.ts +++ b/packages/core/src/runtime/editingAgent.test.ts @@ -135,7 +135,7 @@ describe("installEditingAgent", () => { ok: true, result: { version: 1, - kinds: ["hello", "runtime-snapshot", "wait-for-seek", "set-loop-start"], + kinds: ["hello", "runtime-snapshot", "wait-for-seek", "set-loop-start", "computed-styles"], }, }); expect((await ask(port, { v: 1, id: 2, kind: "runtime-snapshot", params: {} })).result).toEqual( @@ -175,6 +175,52 @@ describe("installEditingAgent", () => { 15, "invalid-params", ], + [{ v: 1, id: 16, kind: "computed-styles", params: { selector: "p" } }, 16, "invalid-params"], + [ + { v: 1, id: 17, kind: "computed-styles", params: { selector: "", properties: [] } }, + 17, + "invalid-params", + ], + [ + { v: 1, id: 18, kind: "computed-styles", params: { selector: "p", properties: ["color;"] } }, + 18, + "invalid-params", + ], + [ + { v: 1, id: 19, kind: "computed-styles", params: { selector: "p", properties: [1] } }, + 19, + "invalid-params", + ], + [ + { + v: 1, + id: 20, + kind: "computed-styles", + params: { selector: "p", properties: Array(65).fill("color") }, + }, + 20, + "invalid-params", + ], + [ + { + v: 1, + id: 21, + kind: "computed-styles", + params: { selector: "p".repeat(2001), properties: ["color"] }, + }, + 21, + "invalid-params", + ], + [ + { + v: 1, + id: 22, + kind: "computed-styles", + params: { selector: "p", properties: ["color"], extra: 1 }, + }, + 22, + "invalid-params", + ], ])("rejects %j with a typed error", async (request, id, code) => { const setLoopStart = vi.fn(); window.__hf = { setLoopStart }; @@ -183,6 +229,27 @@ describe("installEditingAgent", () => { expect(setLoopStart).not.toHaveBeenCalled(); }); + it("answers computed styles for the element a selector names", async () => { + document.body.innerHTML = `

Hi

+ + `; + const port = connectedPort(); + const ask_ = (id: number, selector: string, properties: string[]) => + ask(port, { v: 1, id, kind: "computed-styles", params: { selector, properties } }); + + expect((await ask_(1, "#title", ["color", "opacity", "--unset"])).result).toEqual({ + styles: { color: "rgb(1, 2, 3)", opacity: "0.5" }, + }); + // A paused video's canvas carries its authored opacity. + expect((await ask_(2, "#clip", ["opacity"])).result).toEqual({ styles: { opacity: "0.25" } }); + expect((await ask_(3, "#missing", ["color"])).result).toEqual({ styles: null }); + expect(await ask_(4, "p[", ["color"])).toMatchObject({ + id: 4, + ok: false, + error: { code: "failed" }, + }); + }); + it("replies failed when a handler rejects or its result cannot be cloned", async () => { window.__hfWaitForSeekCompletion = () => Promise.reject(new Error("seek lost")); const port = connectedPort(); diff --git a/packages/core/src/runtime/editingAgent.ts b/packages/core/src/runtime/editingAgent.ts index c0e3a8c4af..24ae8630ff 100644 --- a/packages/core/src/runtime/editingAgent.ts +++ b/packages/core/src/runtime/editingAgent.ts @@ -20,6 +20,7 @@ import { type EditingResult, } from "./editingPort"; import type { ClipNode } from "./clipTree"; +import { getLogicalOpacity } from "../studioPreviewMark"; type Handlers = { [K in EditingRequestKind]: ( @@ -55,6 +56,18 @@ const HANDLERS: Handlers = { setLoopStart(seconds); return { applied: true }; }, + "computed-styles": ({ selector, properties }) => { + const element = document.querySelector(selector); + if (!element) return { styles: null }; + const computed = window.getComputedStyle(element); + const styles: Record = {}; + for (const name of properties) { + const value = + name === "opacity" ? getLogicalOpacity(element, computed) : computed.getPropertyValue(name); + if (value) styles[name] = value; + } + return { styles }; + }, }; function serve(port: MessagePort): void { diff --git a/packages/core/src/runtime/editingPort.test.ts b/packages/core/src/runtime/editingPort.test.ts index 5f30e0a763..bd9bb8fcb7 100644 --- a/packages/core/src/runtime/editingPort.test.ts +++ b/packages/core/src/runtime/editingPort.test.ts @@ -60,7 +60,7 @@ describe("createEditingPortClient", () => { await client.ready; expect(await client.request("hello", {})).toEqual({ version: 1, - kinds: ["hello", "runtime-snapshot", "wait-for-seek", "set-loop-start"], + kinds: ["hello", "runtime-snapshot", "wait-for-seek", "set-loop-start", "computed-styles"], }); } finally { agent.dispose(); diff --git a/packages/core/src/runtime/editingPort.ts b/packages/core/src/runtime/editingPort.ts index c8c6994e86..f9e57e3126 100644 --- a/packages/core/src/runtime/editingPort.ts +++ b/packages/core/src/runtime/editingPort.ts @@ -23,6 +23,7 @@ export const EDITING_REQUEST_KINDS = [ "runtime-snapshot", "wait-for-seek", "set-loop-start", + "computed-styles", ] as const; export type EditingRequestKind = (typeof EDITING_REQUEST_KINDS)[number]; @@ -48,6 +49,11 @@ export interface EditingRequests { "wait-for-seek": { params: NoParams; result: { waited: boolean } }; /** Where a looping playback wraps to; null stops looping from a custom start. */ "set-loop-start": { params: { seconds: number | null }; result: { applied: boolean } }; + /** `styles` is null when no element matches `selector`; empty values are left out. */ + "computed-styles": { + params: { selector: string; properties: string[] }; + result: { styles: Record | null }; + }; } export type EditingParams = EditingRequests[K]["params"]; export type EditingResult = EditingRequests[K]["result"]; @@ -78,6 +84,10 @@ export function isPlainRecord(value: unknown): value is Record return Object.prototype.toString.call(value) === "[object Object]"; } +const MAX_SELECTOR_LENGTH = 2_000; +const MAX_STYLE_PROPERTIES = 64; +const STYLE_PROPERTY = /^(--)?[a-z][a-z0-9-]*$/; + const noParams = (params: unknown): NoParams | null => params === undefined || (isPlainRecord(params) && Object.keys(params).length === 0) ? {} : null; @@ -95,6 +105,20 @@ const PARAM_PARSERS: { ? { seconds } : null; }, + "computed-styles": (params) => { + if (!isPlainRecord(params) || Object.keys(params).sort().join() !== "properties,selector") { + return null; + } + const { selector, properties } = params; + if (typeof selector !== "string" || !selector || selector.length > MAX_SELECTOR_LENGTH) { + return null; + } + if (!Array.isArray(properties) || properties.length > MAX_STYLE_PROPERTIES) return null; + const names = properties.filter( + (name): name is string => typeof name === "string" && STYLE_PROPERTY.test(name), + ); + return names.length === properties.length ? { selector, properties: names } : null; + }, }; /** Validates one request off the wire. Never throws: anything unusable becomes a typed error. */ diff --git a/packages/core/src/studioPreviewMark.ts b/packages/core/src/studioPreviewMark.ts index 6233d5fd15..101843de72 100644 --- a/packages/core/src/studioPreviewMark.ts +++ b/packages/core/src/studioPreviewMark.ts @@ -6,6 +6,17 @@ export const PREVIEW_VIDEO_FRAME_ATTR = "data-hf-preview-video-frame"; export const PREVIEW_VIDEO_SOURCE_ATTR = "data-hf-preview-video-source"; export const PREVIEW_VIDEO_CANVAS_ATTR = "data-hf-preview-video-canvas"; export const STUDIO_PREVIEW_CAPTURE_MODE = "capture"; + +export function getLogicalOpacity(el: Element, computed: CSSStyleDeclaration): string { + const overlay = el.previousElementSibling; + if ( + el.hasAttribute(PREVIEW_VIDEO_FRAME_ATTR) && + overlay?.localName === "canvas" && + overlay.hasAttribute(PREVIEW_VIDEO_CANVAS_ATTR) + ) + return el.ownerDocument.defaultView?.getComputedStyle(overlay).opacity ?? computed.opacity; + return computed.opacity; +} export const PLAYER_FRAME_ATTR = "data-hf-player-frame"; export function inPlayerFrame(doc: Document): boolean { diff --git a/packages/studio/src/components/editor/domEditingDom.ts b/packages/studio/src/components/editor/domEditingDom.ts index 29a5f435cd..d56cef4cb1 100644 --- a/packages/studio/src/components/editor/domEditingDom.ts +++ b/packages/studio/src/components/editor/domEditingDom.ts @@ -3,10 +3,7 @@ * selector utilities, and composition source resolution. * No imports from other domEditing* modules — safe to import from anywhere. */ -import { - PREVIEW_VIDEO_FRAME_ATTR, - PREVIEW_VIDEO_CANVAS_ATTR, -} from "@hyperframes/core/studio-preview-mark"; +import { getLogicalOpacity } from "@hyperframes/core/studio-preview-mark"; import { COLOR_GRADING_SOURCE_HIDDEN_ATTR } from "@hyperframes/core/color-grading"; import { getSourceScopedSelectorIndex } from "../../utils/sourceScopedSelectorIndex"; import { CURATED_STYLE_PROPERTIES } from "./domEditingTypes"; @@ -38,17 +35,7 @@ export function isEditableTextLeaf(el: HTMLElement): boolean { return isTextBearingTag(el.tagName.toLowerCase()) && el.children.length === 0; } -/** The preview canvas keeps the source's authored opacity while its native pixels are hidden. */ -export function getLogicalOpacity(el: Element, computed: CSSStyleDeclaration): string { - const overlay = el.previousElementSibling; - if ( - el.hasAttribute(PREVIEW_VIDEO_FRAME_ATTR) && - overlay?.localName === "canvas" && - overlay.hasAttribute(PREVIEW_VIDEO_CANVAS_ATTR) - ) - return el.ownerDocument.defaultView?.getComputedStyle(overlay).opacity ?? computed.opacity; - return computed.opacity; -} +export { getLogicalOpacity }; /** Does this node render AT ALL, ignoring what it inherits? Sole owner of the * rule; the walk below only decides which nodes to ask it about. */ diff --git a/packages/studio/src/components/editor/useDomEditCompositionRect.ts b/packages/studio/src/components/editor/useDomEditCompositionRect.ts index 4cac7bb922..9cd853353d 100644 --- a/packages/studio/src/components/editor/useDomEditCompositionRect.ts +++ b/packages/studio/src/components/editor/useDomEditCompositionRect.ts @@ -1,6 +1,8 @@ import { useState, type RefObject } from "react"; import { useMountEffect } from "../../hooks/useMountEffect"; import { subscribeOverlayFrame } from "./overlayFrameLoop"; +import { previewStageSize } from "../../player/lib/bridgePlayback"; +import { previewFrameOpaque } from "../../player/lib/previewChannel"; export interface DomEditCompositionRect { left: number; @@ -23,6 +25,16 @@ function sameRect(a: DomEditCompositionRect, b: DomEditCompositionRect): boolean ); } +function declaredCompositionSize(iframe: HTMLIFrameElement): { width: number; height: number } { + if (previewFrameOpaque(iframe)) return previewStageSize(iframe) ?? { width: NaN, height: NaN }; + const doc = iframe.contentDocument; + const root = doc?.querySelector("[data-composition-id]") ?? doc?.documentElement; + return { + width: Number.parseFloat(root?.getAttribute("data-width") ?? ""), + height: Number.parseFloat(root?.getAttribute("data-height") ?? ""), + }; +} + export function useDomEditCompositionRect({ iframeRef, overlayRef, @@ -50,10 +62,7 @@ export function useDomEditCompositionRect({ const left = iRect.left - oRect.left; const top = iRect.top - oRect.top; if (iRect.width <= 0 || iRect.height <= 0) return; - const doc = iframe.contentDocument; - const root = doc?.querySelector("[data-composition-id]") ?? doc?.documentElement; - const dw = Number.parseFloat(root?.getAttribute("data-width") ?? ""); - const dh = Number.parseFloat(root?.getAttribute("data-height") ?? ""); + const { width: dw, height: dh } = declaredCompositionSize(iframe); const scaleX = dw > 0 ? iRect.width / dw : 1; const scaleY = dh > 0 ? iRect.height / dh : 1; const next = { left, top, width: iRect.width, height: iRect.height, scaleX, scaleY }; diff --git a/packages/studio/src/components/editor/usePreviewCompositionRect.test.tsx b/packages/studio/src/components/editor/usePreviewCompositionRect.test.tsx index e020e98f70..af2cdda965 100644 --- a/packages/studio/src/components/editor/usePreviewCompositionRect.test.tsx +++ b/packages/studio/src/components/editor/usePreviewCompositionRect.test.tsx @@ -9,6 +9,7 @@ import { type PreviewCompositionRect, } from "./usePreviewCompositionRect"; import { usePlayerStore } from "../../player/store/playerStore"; +import { watchPreviewFrame } from "../../player/lib/bridgePlayback"; (globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; @@ -103,4 +104,42 @@ describe("usePreviewCompositionRect", () => { }); probe.unmount(); }); + + it("scales an opaque frame by the size its runtime posts, only from the frame's own origin", async () => { + const frame = {}; + Object.defineProperty(frame, "document", { + get: () => { + throw new DOMException("blocked", "SecurityError"); + }, + }); + const iframe = boxed(document.createElement("iframe"), [110, 70, 960, 540]); + iframe.setAttribute("sandbox", "allow-scripts"); + Object.defineProperty(iframe, "contentWindow", { value: frame }); + Object.defineProperty(iframe, "contentDocument", { value: null }); + watchPreviewFrame(iframe); + const stageSize = (width: number, origin: string) => + window.dispatchEvent( + new MessageEvent("message", { + source: frame as Window, + origin, + data: { source: "hf-preview", type: "stage-size", width, height: 1080 }, + }), + ); + stageSize(1920, "null"); + stageSize(960, window.location.origin); + const probe = mountProbe(); + await act(async () => { + probe.setIframe(iframe); + await settle(); + }); + expect(probe.read()).toEqual({ + left: 100, + top: 50, + width: 960, + height: 540, + scaleX: 0.5, + scaleY: 0.5, + }); + probe.unmount(); + }); }); diff --git a/packages/studio/src/hooks/elementPickerStyles.ts b/packages/studio/src/hooks/elementPickerStyles.ts new file mode 100644 index 0000000000..c59f870a15 --- /dev/null +++ b/packages/studio/src/hooks/elementPickerStyles.ts @@ -0,0 +1,53 @@ +import { getLogicalOpacity } from "../components/editor/domEditingDom"; + +export const PICKED_STYLE_PROPERTIES = [ + "position", + "top", + "left", + "right", + "bottom", + "width", + "height", + "margin-top", + "margin-right", + "margin-bottom", + "margin-left", + "padding-top", + "padding-right", + "padding-bottom", + "padding-left", + "font-size", + "font-weight", + "font-family", + "color", + "background-color", + "background", + "opacity", + "border-radius", + "transform", + "z-index", +]; + +/** Read a subset of computed styles from an element in the iframe */ +export function readComputedStyles( + iframe: HTMLIFrameElement, + selector: string, +): Record { + const styles: Record = {}; + try { + const doc = iframe.contentDocument; + const el = doc?.querySelector(selector); + if (!el) return styles; + const computed = iframe.contentWindow?.getComputedStyle(el); + if (!computed) return styles; + + for (const prop of PICKED_STYLE_PROPERTIES) { + const val = + prop === "opacity" ? getLogicalOpacity(el, computed) : computed.getPropertyValue(prop); + if (val) styles[prop] = val; + } + } catch { + /* cross-origin */ + } + return styles; +} diff --git a/packages/studio/src/hooks/useElementPicker.test.ts b/packages/studio/src/hooks/useElementPicker.test.ts index bd39acf466..05192c119c 100644 --- a/packages/studio/src/hooks/useElementPicker.test.ts +++ b/packages/studio/src/hooks/useElementPicker.test.ts @@ -5,6 +5,7 @@ import { ensureHfIds } from "@hyperframes/parsers/hf-ids"; import { runtimeProtocolMetadata } from "@hyperframes/core/runtime/protocol"; import { afterEach, describe, expect, it, vi } from "vitest"; import { useElementPicker } from "./useElementPicker"; +import { serveEditingRequests } from "../player/hooks/timelinePlayerTestHarness"; // As the host leaves it on disk: hf-ids pinned, no element ids. const SAVED = ensureHfIds(` @@ -20,12 +21,13 @@ afterEach(() => { vi.restoreAllMocks(); }); -// The runtime's element-picked message, posted by `from`. -const picked = (from: Window | null, selector: string) => +// The runtime's element-picked message, posted by `from`, from this page's origin unless told otherwise. +const picked = (from: Window | null, selector: string, origin = window.location.origin) => act(() => { window.dispatchEvent( new MessageEvent("message", { source: from, + origin, data: { source: "hf-preview", type: "element-picked", @@ -36,11 +38,16 @@ const picked = (from: Window | null, selector: string) => ); }); -const pickerMessage = (from: Window | null, data: Record) => +const pickerMessage = ( + from: Window | null, + data: Record, + origin = window.location.origin, +) => act(() => { window.dispatchEvent( new MessageEvent("message", { source: from, + origin, data: { source: "hf-preview", ...data, @@ -483,3 +490,76 @@ describe("a pick across a reload that swaps the preview frame", () => { expect(picker().pickedElement).toBeNull(); }); }); + +describe("messages from the picked frame's origin only", () => { + it("ignores a pick posted by the preview's window from an origin it cannot have", () => { + const { iframe, picker } = mountPicker({ "index.html": SAVED }); + picked(iframe.contentWindow, "h1.foreign", "https://elsewhere.example"); + picked(iframe.contentWindow, "h1.opaque", "null"); + expect(picker().pickedElement?.selector).toBe("h1"); + }); +}); + +describe("a pick in a frame Studio cannot read", () => { + /** A sandboxed frame: its window refuses reads, and its runtime answers editing requests. */ + function opaquePreview(answer: (kind: string, params: unknown) => unknown) { + const frame: Record = {}; + Object.defineProperty(frame, "document", { + get: () => { + throw new DOMException("blocked", "SecurityError"); + }, + }); + const iframe = document.createElement("iframe"); + iframe.setAttribute("sandbox", "allow-scripts"); + Object.defineProperty(iframe, "contentWindow", { value: frame }); + Object.defineProperty(iframe, "contentDocument", { value: null }); + const close = serveEditingRequests(frame, answer); + let api: ReturnType | null = null; + function Harness() { + api = useElementPicker({ current: iframe }); + return null; + } + root = createRoot(document.createElement("div")); + act(() => root?.render(React.createElement(Harness))); + return { frame: frame as unknown as Window, close, picker: () => api! }; + } + + it("takes the picked element's computed styles from its runtime", async () => { + const asked: unknown[] = []; + const { frame, close, picker } = opaquePreview((kind, params) => { + asked.push({ kind, params }); + return { styles: { color: "rgb(1, 2, 3)", opacity: "0.5", injected: "x" } }; + }); + try { + picked(frame, "h1.title", "null"); + expect(picker().pickedElement?.computedStyles).toEqual({}); + await vi.waitFor(() => + expect(picker().pickedElement?.computedStyles).toEqual({ + color: "rgb(1, 2, 3)", + opacity: "0.5", + }), + ); + expect(asked).toEqual([ + { + kind: "computed-styles", + params: { + selector: "h1.title", + properties: expect.arrayContaining(["color", "opacity"]), + }, + }, + ]); + } finally { + close(); + } + }); + + it("ignores a pick from the frame's window that does not come from an opaque origin", () => { + const { frame, close, picker } = opaquePreview(() => ({ styles: {} })); + try { + picked(frame, "h1", window.location.origin); + expect(picker().pickedElement).toBeNull(); + } finally { + close(); + } + }); +}); diff --git a/packages/studio/src/hooks/useElementPicker.ts b/packages/studio/src/hooks/useElementPicker.ts index c054c34943..b466ebd1fa 100644 --- a/packages/studio/src/hooks/useElementPicker.ts +++ b/packages/studio/src/hooks/useElementPicker.ts @@ -12,7 +12,10 @@ import { postRuntimeControlMessage, } from "../player/lib/runtimeProtocol"; import { compositionPathOfPreviewUrl } from "../player/components/CompositionThumbnail"; -import { getLogicalOpacity, getSourceFileForElement } from "../components/editor/domEditingDom"; +import { frameOrigins } from "../player/lib/bridgePlayback"; +import { previewComputedStyles, previewFrameOpaque } from "../player/lib/previewChannel"; +import { PICKED_STYLE_PROPERTIES, readComputedStyles } from "./elementPickerStyles"; +import { getSourceFileForElement } from "../components/editor/domEditingDom"; import { createElementPickerHoverLabel, type ElementPickerHoverInfo, @@ -173,7 +176,19 @@ function useElementPickerSession( setPickedElement(null); }, []); - const windowsThatWerePrimary = useRef(new WeakSet()); + const originsOfWindowsThatWerePrimary = useRef(new WeakMap()); + + const showPicked = useCallback((next: PickedElement, iframe: HTMLIFrameElement) => { + pickedElementRef.current = next; + setPickedElement(next); + if (!previewFrameOpaque(iframe)) return; + void previewComputedStyles(iframe, next.selector, PICKED_STYLE_PROPERTIES).then((styles) => { + if (!styles || pickedElementRef.current !== next) return; + const filled = { ...next, computedStyles: styles }; + pickedElementRef.current = filled; + setPickedElement(filled); + }); + }, []); useEffect(() => { if (!isPickMode) return; @@ -204,13 +219,14 @@ function useElementPickerSession( const activeIframe = getActiveIframe(); if (!activeIframe) return; if (!e.source) return; - if (e.source === iframeRef.current?.contentWindow) - windowsThatWerePrimary.current.add(e.source); - else if ( - e.source !== activeIframe.contentWindow && - !windowsThatWerePrimary.current.has(e.source) - ) - return; + const primary = iframeRef.current; + if (primary && e.source === primary.contentWindow) + originsOfWindowsThatWerePrimary.current.set(e.source, frameOrigins(primary)); + const origins = + e.source === activeIframe.contentWindow + ? frameOrigins(activeIframe) + : originsOfWindowsThatWerePrimary.current.get(e.source); + if (!origins?.includes(e.origin)) return; if (data.type === "element-hovered") { if (e.source !== activeIframe.contentWindow || !isPickModeRef.current) return; @@ -220,9 +236,7 @@ function useElementPickerSession( } if (data.type === "element-picked" && data.elementInfo) { - const next = toPickedElement(data.elementInfo, activeIframe); - pickedElementRef.current = next; - setPickedElement(next); + showPicked(toPickedElement(data.elementInfo, activeIframe), activeIframe); pickStartSelectionRef.current = undefined; isPickModeRef.current = false; clearHover(); @@ -230,11 +244,7 @@ function useElementPickerSession( } else if (data.type === "element-pick-candidates") { // Multiple candidates at click point — pick the first one const el = data.candidates?.[data.selectedIndex ?? 0]; - if (el) { - const next = toPickedElement(el, activeIframe); - pickedElementRef.current = next; - setPickedElement(next); - } + if (el) showPicked(toPickedElement(el, activeIframe), activeIframe); } if (data.type === "pick-mode-cancelled") { @@ -549,52 +559,3 @@ function patchByHfId( const before = path ? files[path] : undefined; return path && before ? { path, before, after: applyPatchByTarget(before, { hfId }, op) } : null; } - -/** Read a subset of computed styles from an element in the iframe */ -function readComputedStyles(iframe: HTMLIFrameElement, selector: string): Record { - const styles: Record = {}; - try { - const doc = iframe.contentDocument; - const el = doc?.querySelector(selector); - if (!el) return styles; - const computed = iframe.contentWindow?.getComputedStyle(el); - if (!computed) return styles; - - const props = [ - "position", - "top", - "left", - "right", - "bottom", - "width", - "height", - "margin-top", - "margin-right", - "margin-bottom", - "margin-left", - "padding-top", - "padding-right", - "padding-bottom", - "padding-left", - "font-size", - "font-weight", - "font-family", - "color", - "background-color", - "background", - "opacity", - "border-radius", - "transform", - "z-index", - ]; - - for (const prop of props) { - const val = - prop === "opacity" ? getLogicalOpacity(el, computed) : computed.getPropertyValue(prop); - if (val) styles[prop] = val; - } - } catch { - /* cross-origin */ - } - return styles; -} diff --git a/packages/studio/src/player/lib/bridgePlayback.ts b/packages/studio/src/player/lib/bridgePlayback.ts index ff9b1eb2a2..d98f2a9c65 100644 --- a/packages/studio/src/player/lib/bridgePlayback.ts +++ b/packages/studio/src/player/lib/bridgePlayback.ts @@ -12,6 +12,8 @@ interface PlaybackMirror { /** A seek the runtime has not confirmed yet, and until when to wait for it. */ seekTo: number | null; seekUntil: number; + /** The composition's declared size from the runtime's `stage-size` post. */ + stageSize: { width: number; height: number } | null; } const SEEK_CONFIRM_MS = 500; @@ -26,7 +28,15 @@ function mirrorOf(iframe: HTMLIFrameElement): PlaybackMirror | null { if (!frame) return null; let mirror = mirrors.get(frame); if (!mirror) { - mirror = { iframe, time: 0, duration: 0, playing: false, seekTo: null, seekUntil: 0 }; + mirror = { + iframe, + time: 0, + duration: 0, + playing: false, + seekTo: null, + seekUntil: 0, + stageSize: null, + }; mirrors.set(frame, mirror); } mirror.iframe = iframe; @@ -38,11 +48,18 @@ export function watchPreviewFrame(iframe: HTMLIFrameElement): void { mirrorOf(iframe); } +/** The composition size a registered frame's runtime last posted. */ +export function previewStageSize( + iframe: HTMLIFrameElement, +): { width: number; height: number } | null { + const frame = iframe.contentWindow; + return (frame && mirrors.get(frame)?.stageSize) ?? null; +} + /** The origins a post from this frame can carry, given its sandbox and what it loads. */ -function frameOrigins(iframe: HTMLIFrameElement): string[] { - if (iframe.hasAttribute("sandbox") && !iframe.sandbox.contains("allow-same-origin")) { - return ["null"]; - } +export function frameOrigins(iframe: HTMLIFrameElement): string[] { + const sandbox = iframe.getAttribute("sandbox")?.toLowerCase().split(/\s+/); + if (sandbox && !sandbox.includes("allow-same-origin")) return ["null"]; const src = iframe.getAttribute("src"); if (iframe.hasAttribute("srcdoc") || !src) return [window.location.origin]; try { @@ -56,6 +73,7 @@ function frameOrigins(iframe: HTMLIFrameElement): string[] { const finiteAtLeastZero = (value: unknown): value is number => typeof value === "number" && Number.isFinite(value) && value >= 0; +const finitePositive = (value: unknown): value is number => finiteAtLeastZero(value) && value > 0; /** The runtime lands a seek on the frame at or before it, so only that frame confirms it. */ function confirmsSeek(currentTime: number, seekTo: number, fps: number | null): boolean { @@ -90,18 +108,23 @@ function mirrorState( mirror.playing = playing; } +function mirrorPost(mirror: PlaybackMirror, message: Record, fps: number | null) { + if (message.type === "timeline" && finiteAtLeastZero(message.durationSeconds)) { + mirror.duration = message.durationSeconds; + } else if (message.type === "state" && finiteAtLeastZero(message.currentTime)) { + mirrorState(mirror, message.currentTime, message.isPlaying === true, fps); + } else if (message.type === "stage-size") { + const { width, height } = message; + if (finitePositive(width) && finitePositive(height)) mirror.stageSize = { width, height }; + } +} + function mirrorRuntimePlayback(event: MessageEvent): void { // Only a frame Studio built, posting from an origin that frame can have. const mirror = event.source ? mirrors.get(event.source) : undefined; if (!mirror || !frameOrigins(mirror.iframe).includes(event.origin)) return; const post = runtimePost(event.data); - if (!post) return; - const { message, fps } = post; - if (message.type === "timeline" && finiteAtLeastZero(message.durationSeconds)) { - mirror.duration = message.durationSeconds; - } else if (message.type === "state" && finiteAtLeastZero(message.currentTime)) { - mirrorState(mirror, message.currentTime, message.isPlaying === true, fps); - } + if (post) mirrorPost(mirror, post.message, post.fps); } if (typeof window !== "undefined") window.addEventListener("message", mirrorRuntimePlayback); diff --git a/packages/studio/src/player/lib/previewChannel.ts b/packages/studio/src/player/lib/previewChannel.ts index cdc0d2dde1..afc54d2af7 100644 --- a/packages/studio/src/player/lib/previewChannel.ts +++ b/packages/studio/src/player/lib/previewChannel.ts @@ -79,3 +79,22 @@ export async function previewClipManifest(iframe: HTMLIFrameElement): Promise | null> { + const reply = await previewEditingClient(iframe) + .request("computed-styles", { selector, properties }) + .catch(() => null); + const styles: unknown = reply?.styles; + if (typeof styles !== "object" || styles === null) return null; + const read: Record = {}; + for (const name of properties) { + const value: unknown = Object.getOwnPropertyDescriptor(styles, name)?.value; + if (typeof value === "string" && value) read[name] = value; + } + return read; +}