From 80bf6a9ddb8bd24c2039eeb689fd129b6ecac512 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Tue, 17 Nov 2026 23:36:08 +0000 Subject: [PATCH] fix(studio): slideshow scenes, block stacking, drill-down and shortcuts work in a sealed preview (#8118) * fix(studio): slideshow scenes, block stacking, drill-down and shortcuts work in a sealed preview * fix(studio): sealed preview keys act only with the preview focused; frame replies are validated --- .../src/runtime/editing/maxZIndex.test.ts | 21 +++ .../core/src/runtime/editing/maxZIndex.ts | 9 ++ .../core/src/runtime/editingAgent.test.ts | 1 + packages/core/src/runtime/editingAgent.ts | 2 + packages/core/src/runtime/editingPort.test.ts | 1 + packages/core/src/runtime/editingPort.ts | 3 + .../nle/NLEContext.drillDown.test.ts | 85 +++++++++++ .../studio/src/components/nle/NLEContext.tsx | 44 +++--- .../studio/src/hooks/appHotkeysDispatch.ts | 39 +++++- .../useAppHotkeys.previewForwarding.test.tsx | 120 ++++++++++++++++ packages/studio/src/hooks/useAppHotkeys.ts | 37 ++++- .../src/hooks/useSlideshowTabState.test.ts | 50 ++++++- .../studio/src/hooks/useSlideshowTabState.ts | 48 ++++--- .../src/player/hooks/usePlaybackKeyboard.ts | 2 +- .../studio/src/utils/blockInstaller.test.ts | 132 +++++++++++------- packages/studio/src/utils/blockInstaller.ts | 20 +-- 16 files changed, 506 insertions(+), 108 deletions(-) create mode 100644 packages/core/src/runtime/editing/maxZIndex.test.ts create mode 100644 packages/core/src/runtime/editing/maxZIndex.ts create mode 100644 packages/studio/src/components/nle/NLEContext.drillDown.test.ts diff --git a/packages/core/src/runtime/editing/maxZIndex.test.ts b/packages/core/src/runtime/editing/maxZIndex.test.ts new file mode 100644 index 0000000000..d14166ce2e --- /dev/null +++ b/packages/core/src/runtime/editing/maxZIndex.test.ts @@ -0,0 +1,21 @@ +import { afterEach, describe, expect, it } from "vitest"; +import { maxZIndex } from "./maxZIndex"; + +afterEach(() => { + document.body.innerHTML = ""; +}); + +describe("max-z-index", () => { + it("answers the highest z-index on the page, a hidden clip's included", () => { + document.body.innerHTML = ` +
+
+
`; + expect(maxZIndex()).toEqual({ zIndex: 12 }); + }); + + it("answers 0 for a page with no stacked element or only negative ones", () => { + document.body.innerHTML = `
`; + expect(maxZIndex()).toEqual({ zIndex: 0 }); + }); +}); diff --git a/packages/core/src/runtime/editing/maxZIndex.ts b/packages/core/src/runtime/editing/maxZIndex.ts new file mode 100644 index 0000000000..1e49b93db2 --- /dev/null +++ b/packages/core/src/runtime/editing/maxZIndex.ts @@ -0,0 +1,9 @@ +/** The highest z-index on the page, hidden clips included, so a block the parent adds sits above them all. */ +export function maxZIndex(): { zIndex: number } { + let max = 0; + for (const element of Array.from(document.body?.querySelectorAll("*") ?? [])) { + const z = Number.parseInt(getComputedStyle(element).zIndex, 10); + if (Number.isFinite(z) && z > max) max = z; + } + return { zIndex: max }; +} diff --git a/packages/core/src/runtime/editingAgent.test.ts b/packages/core/src/runtime/editingAgent.test.ts index bb33f28f59..982d4d658c 100644 --- a/packages/core/src/runtime/editingAgent.test.ts +++ b/packages/core/src/runtime/editingAgent.test.ts @@ -197,6 +197,7 @@ describe("installEditingAgent", () => { "assign-audio-group", "gsap-snapshot", "motion-path-home", + "max-z-index", ], }, }); diff --git a/packages/core/src/runtime/editingAgent.ts b/packages/core/src/runtime/editingAgent.ts index 0aaeeab2f2..dd34b4fb80 100644 --- a/packages/core/src/runtime/editingAgent.ts +++ b/packages/core/src/runtime/editingAgent.ts @@ -34,6 +34,7 @@ import { layersOf } from "./editingLayers"; import { drawDrafts } from "./editingDraft"; import { restoreAttributes } from "./editing/restoreAttributes"; import { stopWatchingGeometry, watchGeometry } from "./editing/watchGeometry"; +import { maxZIndex } from "./editing/maxZIndex"; import { gestureStart } from "./editingGestureStart"; import { gsapSnapshot } from "./editing/gsapSnapshot"; import { motionPathHome } from "./editing/motionPathHome"; @@ -214,6 +215,7 @@ const HANDLERS: Handlers = { "assign-audio-group": (params) => assignAudioGroup(params, elementForHandle), "gsap-snapshot": () => gsapSnapshot(), "motion-path-home": ({ selector }) => motionPathHome(selector), + "max-z-index": () => maxZIndex(), }; function serve(port: MessagePort): void { diff --git a/packages/core/src/runtime/editingPort.test.ts b/packages/core/src/runtime/editingPort.test.ts index e3f042f3cf..90be87d88f 100644 --- a/packages/core/src/runtime/editingPort.test.ts +++ b/packages/core/src/runtime/editingPort.test.ts @@ -90,6 +90,7 @@ describe("createEditingPortClient", () => { "assign-audio-group", "gsap-snapshot", "motion-path-home", + "max-z-index", ], }); } finally { diff --git a/packages/core/src/runtime/editingPort.ts b/packages/core/src/runtime/editingPort.ts index b87b141ada..49b8f2efb0 100644 --- a/packages/core/src/runtime/editingPort.ts +++ b/packages/core/src/runtime/editingPort.ts @@ -168,6 +168,7 @@ export const EDITING_REQUEST_KINDS = [ "assign-audio-group", "gsap-snapshot", "motion-path-home", + "max-z-index", ] as const; export type EditingRequestKind = (typeof EDITING_REQUEST_KINDS)[number]; @@ -296,6 +297,7 @@ export interface EditingRequests { }; "gsap-snapshot": { params: NoParams; result: GsapSnapshot }; "motion-path-home": { params: { selector: string }; result: MotionPathFrameHome }; + "max-z-index": { params: NoParams; result: { zIndex: number } }; } export type EditingParams = EditingRequests[K]["params"]; export type EditingResult = EditingRequests[K]["result"]; @@ -498,6 +500,7 @@ const PARAM_PARSERS: { hasKeys(params, "selector") && isSelector(params.selector) ? { selector: params.selector } : null, + "max-z-index": noParams, }; /** Validates one request off the wire. Never throws: anything unusable becomes a typed error. */ diff --git a/packages/studio/src/components/nle/NLEContext.drillDown.test.ts b/packages/studio/src/components/nle/NLEContext.drillDown.test.ts new file mode 100644 index 0000000000..394de574c7 --- /dev/null +++ b/packages/studio/src/components/nle/NLEContext.drillDown.test.ts @@ -0,0 +1,85 @@ +// @vitest-environment happy-dom + +import { afterEach, describe, expect, it, vi } from "vitest"; +import { previewRequest } from "../../player/lib/previewChannel"; +import { hostCompositionSrc } from "./NLEContext"; + +vi.mock("../../player/lib/previewChannel", async (importOriginal) => { + const actual = await importOriginal(); + return { ...actual, previewRequest: vi.fn(async () => null) }; +}); + +afterEach(() => vi.mocked(previewRequest).mockReset()); + +const HANDLE = "0123456789abcdef:1"; +const NO_SOURCE = { file: null, compositionId: null, originalCompositionId: null }; + +function sealedPreview(): HTMLIFrameElement { + const iframe = document.createElement("iframe"); + Object.defineProperty(iframe, "contentDocument", { + get() { + throw new DOMException( + "Blocked a frame from accessing a cross-origin frame.", + "SecurityError", + ); + }, + }); + return iframe; +} + +function preview(attributes: unknown) { + vi.mocked(previewRequest).mockImplementation((async (_frame: HTMLIFrameElement, kind: string) => + kind === "query" + ? { matches: [{ handle: HANDLE, source: NO_SOURCE }], truncated: false } + : { + elements: [ + { + handle: HANDLE, + parent: null, + tag: "div", + id: "intro", + selector: null, + matchesBefore: null, + source: NO_SOURCE, + locked: false, + attributes, + inlineStyles: {}, + text: "", + children: [], + rect: { x: 0, y: 0, width: 1, height: 1 }, + truncated: false, + }, + ], + }) as unknown as typeof previewRequest); +} + +describe("hostCompositionSrc", () => { + it("reads a sealed preview's host for the composition over its editing port", async () => { + const iframe = sealedPreview(); + preview([ + ["data-composition-id", "intro"], + ["data-composition-src", "compositions/intro.html"], + ]); + await expect(hostCompositionSrc(iframe, 'in"tro')).resolves.toBe("compositions/intro.html"); + expect(vi.mocked(previewRequest)).toHaveBeenCalledWith(iframe, "query", { + selector: `[data-composition-id="in\\"tro"][data-composition-src]`, + }); + expect(vi.mocked(previewRequest)).toHaveBeenCalledWith(iframe, "describe", { + handles: [HANDLE], + }); + }); + + it("answers nothing for a reply the frame got wrong, rather than passing it on", async () => { + preview([["data-composition-src", { evil: 1 }]]); + await expect(hostCompositionSrc(sealedPreview(), "intro")).resolves.toBeUndefined(); + preview("x"); + await expect(hostCompositionSrc(sealedPreview(), "intro")).resolves.toBeUndefined(); + }); + + it("answers nothing when the preview has no such host or cannot answer", async () => { + vi.mocked(previewRequest).mockResolvedValue(null); + await expect(hostCompositionSrc(sealedPreview(), "intro")).resolves.toBeUndefined(); + vi.mocked(previewRequest).mockResolvedValue({ matches: [], truncated: false }); + await expect(hostCompositionSrc(sealedPreview(), "intro")).resolves.toBeUndefined(); + }); +}); diff --git a/packages/studio/src/components/nle/NLEContext.tsx b/packages/studio/src/components/nle/NLEContext.tsx index 2818650dc8..21f354d9f5 100644 --- a/packages/studio/src/components/nle/NLEContext.tsx +++ b/packages/studio/src/components/nle/NLEContext.tsx @@ -11,6 +11,19 @@ import { useAssetPreviewStore } from "../../utils/assetPreviewStore"; import { createStableContext } from "../../utils/hmrStableContext"; import { studioApiFetch } from "../../utils/studioApiFetch"; import { previewReloadBegun } from "../../player/previewReloading"; +import { describePreviewElements, queryPreviewElements } from "../../player/lib/previewElements"; + +/** The `data-composition-src` of the preview's host for composition `compId`, read over its editing port. */ +export async function hostCompositionSrc( + iframe: HTMLIFrameElement, + compId: string, +): Promise { + const selector = `[data-composition-id="${CSS.escape(compId)}"][data-composition-src]`; + const handle = (await queryPreviewElements(iframe, selector))?.matches[0]?.handle; + if (!handle) return undefined; + const [host] = (await describePreviewElements(iframe, [handle])) ?? []; + return host?.attributes.find(([name]) => name === "data-composition-src")?.[1] || undefined; +} export function shouldDisableTimelineWhileCompositionLoading(compositionLoading: boolean): boolean { return compositionLoading; @@ -171,35 +184,20 @@ export function NLEProvider({ onCompositionChange, }); - // Wrap handleDrillDown to also scan the iframe DOM for data-composition-src + // Drill into a sub-composition: its src from compIdToSrc, else from its host in the preview, else the clip's own. const iframeRef_ = iframeRef; const handleDrillDown = useCallback( (element: TimelineElement) => { if (!element.compositionSrc) return; usePlayerStore.getState().setSelectedElementId(null); - // Check compIdToSrc map first; then scan iframe DOM; then fall through to drillDown const compId = element.id; - let resolvedPath = compIdToSrc.get(compId); - if (!resolvedPath) { - try { - const doc = iframeRef_.current?.contentDocument; - if (doc) { - const host = doc.querySelector( - `[data-composition-id="${CSS.escape(compId)}"][data-composition-src]`, - ); - if (host) { - resolvedPath = host.getAttribute("data-composition-src") || undefined; - } - } - } catch { - /* cross-origin */ - } - } - // Delegate with the resolved compositionSrc (may be same as original) - drillDown({ - id: compId, - compositionSrc: resolvedPath ?? element.compositionSrc, - }); + const known = compIdToSrc.get(compId); + const iframe = iframeRef_.current; + const resolve = + known || !iframe ? Promise.resolve(known) : hostCompositionSrc(iframe, compId); + void resolve.then((resolvedPath) => + drillDown({ id: compId, compositionSrc: resolvedPath ?? element.compositionSrc }), + ); }, [compIdToSrc, drillDown, iframeRef_], ); diff --git a/packages/studio/src/hooks/appHotkeysDispatch.ts b/packages/studio/src/hooks/appHotkeysDispatch.ts index f3a7dd9e30..ee6e27f775 100644 --- a/packages/studio/src/hooks/appHotkeysDispatch.ts +++ b/packages/studio/src/hooks/appHotkeysDispatch.ts @@ -9,7 +9,12 @@ import { shouldIgnoreHistoryShortcut } from "../utils/studioHelpers"; import { canSplitElement } from "../utils/timelineElementSplit"; import { trackStudioEvent } from "../utils/studioTelemetry"; import { razorClaimsKey } from "../player/lib/playbackShortcuts"; -import { getStudioKeys, keyHint, matchesKey } from "../player/components/studioShortcuts"; +import { + getStudioKeys, + keyHint, + matchesKey, + type StudioKeyAction, +} from "../player/components/studioShortcuts"; import { zoomTimelineToSelectedClips } from "../player/components/timelineZoomInput"; import { openAudioGainDialog } from "../player/components/audioGainDialogStore"; import type { LinkShortcutCallbacks } from "./linkShortcuts"; @@ -119,6 +124,38 @@ function dispatchClipboardKey(event: KeyboardEvent, cb: HotkeyCallbacks): boolea return true; } +/** Every action the dispatchers here and dispatchLinkShortcut answer: what a sealed preview forwards to them. */ +export const APP_HOTKEY_ACTIONS = [ + "undo", + "redo", + "redoAlt", + "tabCompositions", + "tabAssets", + "group", + "ungroup", + "copy", + "paste", + "cut", + "duplicate", + "zoomSelection", + "fullscreen", + "split", + "audioGain", + "razorTool", + "selectTool", + "selectLeftward", + "selectRightward", + "cancel", + "deleteSelected", + "deleteSelectedAlt", + "resetKeyframes", + "record", + "linkToggle", + "detachAudio", + "deleteLinked", + "deleteLinkedAlt", +] as const satisfies readonly StudioKeyAction[]; + /** Exported for tests: the shortcuts that act on the editor, each read as a combo at event time. */ export function dispatchModifierKey( event: KeyboardEvent, diff --git a/packages/studio/src/hooks/useAppHotkeys.previewForwarding.test.tsx b/packages/studio/src/hooks/useAppHotkeys.previewForwarding.test.tsx index 139c6fbbde..992f33e14d 100644 --- a/packages/studio/src/hooks/useAppHotkeys.previewForwarding.test.tsx +++ b/packages/studio/src/hooks/useAppHotkeys.previewForwarding.test.tsx @@ -1,5 +1,6 @@ // @vitest-environment happy-dom +import { readFileSync } from "node:fs"; import React, { act, useRef } from "react"; import type { Root } from "react-dom/client"; import { afterEach, describe, expect, it, vi } from "vitest"; @@ -8,6 +9,13 @@ import { usePlayerStore } from "../player/store/playerStore"; import { useAppHotkeys } from "./useAppHotkeys"; import { mountReactHarness } from "./domSelectionTestHarness"; import { trackStudioEvent } from "../utils/studioTelemetry"; +import { APP_HOTKEY_ACTIONS } from "./appHotkeysDispatch"; +import { PLAYBACK_ACTIONS } from "../player/hooks/usePlaybackKeyboard"; +import { DEFAULT_STUDIO_KEYS } from "../player/components/studioShortcuts"; +import { + detachedFakeIframe, + serveEditingRequests, +} from "../player/hooks/timelinePlayerTestHarness"; vi.mock("../utils/studioTelemetry", () => ({ trackStudioEvent: vi.fn() })); @@ -167,4 +175,116 @@ describe("preview iframe hotkey forwarding", () => { expect(add).toHaveBeenCalled(); expect(remove.mock.calls).toEqual(add.mock.calls); }); + + describe("a sealed preview", () => { + const closers: Array<() => void> = []; + afterEach(() => closers.splice(0).forEach((close) => close())); + + // A frame whose document refuses Studio, reached only over the editing port it hands out per connect. + function sealedFrame(focus = { inPreview: true }) { + const frame: Record = {}; + const iframe = detachedFakeIframe(frame); + const hasFocus = vi.spyOn(document, "hasFocus").mockImplementation(() => focus.inPreview); + Object.defineProperty(document, "activeElement", { + configurable: true, + get: () => (focus.inPreview ? iframe : document.body), + }); + closers.push(() => { + hasFocus.mockRestore(); + delete (document as { activeElement?: unknown }).activeElement; + }); + Object.defineProperty(iframe, "contentDocument", { value: null }); + Object.defineProperty(frame, "document", { + get: () => { + throw new DOMException("blocked", "SecurityError"); + }, + }); + const watched: string[][] = []; + const ports: MessagePort[] = []; + closers.push( + serveEditingRequests( + frame, + (kind, params) => { + if (kind !== "watch-keys") return null; + watched.push((params as { combos: string[] }).combos); + return { watching: watched.at(-1)!.length }; + }, + (port) => ports.push(port), + ), + ); + const keyDown = (key: string, code: string) => + ports.at(-1)!.postMessage({ + v: 1, + event: "key", + key: { + type: "keydown", + key, + code, + altKey: false, + ctrlKey: false, + metaKey: false, + shiftKey: false, + repeat: false, + }, + }); + return { iframe, watched, ports, keyDown, hasFocus }; + } + + it("deletes the selection on a Delete pressed inside the preview", async () => { + root = mountReactHarness(); + const { iframe, watched, keyDown } = sealedFrame(); + act(() => sync?.(iframe)); + await vi.waitFor(() => expect(watched).toHaveLength(1)); + expect(new Set(watched[0])).toEqual( + new Set(APP_HOTKEY_ACTIONS.map((action) => DEFAULT_STUDIO_KEYS[action])), + ); + keyDown("Delete", "Delete"); + await vi.waitFor(() => expect(domDelete).toHaveBeenCalledTimes(1)); + }); + + it("ignores a key the preview reports while the person is not in the preview", async () => { + root = mountReactHarness(); + const focus = { inPreview: false }; + const { iframe, watched, keyDown, hasFocus } = sealedFrame(focus); + act(() => sync?.(iframe)); + await vi.waitFor(() => expect(watched).toHaveLength(1)); + keyDown("Delete", "Delete"); + await vi.waitFor(() => expect(hasFocus).toHaveBeenCalledTimes(1)); + focus.inPreview = true; + keyDown("Delete", "Delete"); + await vi.waitFor(() => expect(domDelete).toHaveBeenCalledTimes(1)); + expect(hasFocus).toHaveBeenCalledTimes(2); + }); + + it("stops watching on the preview's next load, from a new port", async () => { + root = mountReactHarness(); + const { iframe, watched, ports } = sealedFrame(); + act(() => sync?.(iframe)); + await vi.waitFor(() => expect(watched).toHaveLength(1)); + act(() => sync?.(iframe)); + await vi.waitFor(() => expect(watched).toHaveLength(3)); + expect(watched.slice(1)).toContainEqual([]); + expect(ports).toHaveLength(2); + }); + + it("watches every action the app's key dispatchers answer, so none is dead in a sealed preview", () => { + const read = (file: string) => readFileSync(new URL(file, import.meta.url), "utf8"); + const answered = new Set([ + ...[...read("./appHotkeysDispatch.ts").matchAll(/matchesKey\(\w+, "(\w+)"\)/g)].map( + (m) => m[1], + ), + ...[...read("./linkShortcuts.ts").matchAll(/\["(\w+)", "\w+"\]/g)].map((m) => m[1]), + ]); + expect(answered.size).toBeGreaterThan(20); + expect(new Set(APP_HOTKEY_ACTIONS)).toEqual(answered); + }); + + it("watches no key the playback keys also watch, so the frame forwards each press once", () => { + const app = new Set(APP_HOTKEY_ACTIONS.map((action) => DEFAULT_STUDIO_KEYS[action])); + const shared = PLAYBACK_ACTIONS.map((action) => DEFAULT_STUDIO_KEYS[action]).filter((combo) => + app.has(combo), + ); + expect(shared).toEqual([]); + }); + }); }); diff --git a/packages/studio/src/hooks/useAppHotkeys.ts b/packages/studio/src/hooks/useAppHotkeys.ts index 1ae64c4c78..0b797b56f8 100644 --- a/packages/studio/src/hooks/useAppHotkeys.ts +++ b/packages/studio/src/hooks/useAppHotkeys.ts @@ -8,13 +8,21 @@ import { applyCaptionModelToIframe, isCaptionPreviewVisible, } from "../captions/components/CaptionOverlayUtils"; -import { type HotkeyCallbacks, dispatchModifierKey, dispatchPlainKey } from "./appHotkeysDispatch"; +import { + APP_HOTKEY_ACTIONS, + type HotkeyCallbacks, + dispatchModifierKey, + dispatchPlainKey, +} from "./appHotkeysDispatch"; import { useEditHistoryActions, type EditHistoryHandle, type UseEditHistoryActionsOptions, } from "./useEditHistoryActions"; import { useStableHandlers } from "./useStableHandlers"; +import { createEditingPortClient } from "@hyperframes/core/runtime/editing-port"; +import { previewFrameOpaque } from "../player/lib/previewChannel"; +import { getStudioKey, subscribeStudioKeys } from "../player/components/studioShortcuts"; function iframeContentWindow(iframe: HTMLIFrameElement | null): Window | null { try { @@ -24,6 +32,29 @@ function iframeContentWindow(iframe: HTMLIFrameElement | null): Window | null { } } +// A port of its own: watch-keys replaces a port's whole list, and playback watches its keys on the shared one. +function forwardSealedFrameKeys(iframe: HTMLIFrameElement, onKeyDown: (e: KeyboardEvent) => void) { + const client = createEditingPortClient({ iframe }); + const watch = (combos: string[]) => void client.request("watch-keys", { combos }).catch(() => {}); + const watchApp = () => watch(APP_HOTKEY_ACTIONS.map(getStudioKey)); + watchApp(); + const stopRebinds = subscribeStudioKeys(watchApp); + // The frame can report keys nobody pressed: act only while the person is in the preview. + const focused = () => + iframe.ownerDocument.hasFocus() && iframe.ownerDocument.activeElement === iframe; + const stopKeys = client.onKey((key) => { + if (key.type === "keydown" && focused()) onKeyDown(new KeyboardEvent(key.type, key)); + }); + return () => { + stopRebinds(); + stopKeys(); + void client + .request("watch-keys", { combos: [] }) + .catch(() => {}) + .finally(() => client.dispose()); + }; +} + function safeAddListener(t: EventTarget | null, type: string, h: EventListener, capture = false) { try { t?.addEventListener(type, h, capture); @@ -247,6 +278,10 @@ export function useAppHotkeys({ (iframe: HTMLIFrameElement | null) => { previewHistoryCleanupRef.current?.(); previewHistoryCleanupRef.current = null; + if (iframe && previewFrameOpaque(iframe)) { + previewHistoryCleanupRef.current = forwardSealedFrameKeys(iframe, handleAppKeyDown); + return; + } const win = iframeContentWindow(iframe); if (!win) return; const appHandler = handleAppKeyDown as EventListener; diff --git a/packages/studio/src/hooks/useSlideshowTabState.test.ts b/packages/studio/src/hooks/useSlideshowTabState.test.ts index 8665f00e17..03d14c9ae3 100644 --- a/packages/studio/src/hooks/useSlideshowTabState.test.ts +++ b/packages/studio/src/hooks/useSlideshowTabState.test.ts @@ -2,9 +2,15 @@ import React, { act } from "react"; import { createRoot } from "react-dom/client"; -import { afterEach, describe, expect, it } from "vitest"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import { previewClipManifest } from "../player/lib/previewChannel"; import { useSlideshowTabState } from "./useSlideshowTabState"; +vi.mock("../player/lib/previewChannel", async (importOriginal) => { + const actual = await importOriginal(); + return { ...actual, previewClipManifest: vi.fn(async () => null) }; +}); + (globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; const SLIDESHOW_HTML = ``; @@ -14,16 +20,21 @@ afterEach(() => { document.body.innerHTML = ""; }); -function renderHook(params: { editingFileContent: string | null | undefined }) { +function renderHook(params: { + editingFileContent: string | null | undefined; + iframe?: HTMLIFrameElement | null; +}) { const host = document.createElement("div"); document.body.append(host); const root = createRoot(host); let current: ReturnType | null = null; + // One ref for every render, as Studio's useRef gives. + const previewIframeRef = { current: params.iframe ?? null }; function Harness() { current = useSlideshowTabState({ editingFileContent: params.editingFileContent, - previewIframeRef: { current: null }, + previewIframeRef, refreshKey: 0, slideshowVisible: false, }); @@ -71,4 +82,37 @@ describe("useSlideshowTabState", () => { expect(harness.getState().isSlideshowComposition).toBe(true); harness.unmount(); }); + + it("lists the scenes of a sealed preview, read over its editing port", async () => { + // A cross-origin frame's window refuses every property read. + const sealedWindow = new Proxy( + {}, + { + get() { + throw new DOMException( + "Blocked a frame from accessing a cross-origin frame.", + "SecurityError", + ); + }, + }, + ); + const iframe = document.createElement("iframe"); + Object.defineProperty(iframe, "contentWindow", { value: sealedWindow }); + vi.mocked(previewClipManifest).mockResolvedValueOnce({ + clips: [], + durationInFrames: 300, + scenes: [ + null, + { id: "s1", label: "Intro", start: 0, duration: 4 }, + { id: 2, label: "Bad id", start: 4, duration: 4 }, + { id: "s3", label: "No time", start: null, duration: 4 }, + ], + } as unknown as Awaited>); + const harness = renderHook({ editingFileContent: SLIDESHOW_HTML, iframe }); + await act(async () => {}); + expect(harness.getState().slideshowScenes).toEqual([ + { id: "s1", label: "Intro", start: 0, duration: 4 }, + ]); + harness.unmount(); + }); }); diff --git a/packages/studio/src/hooks/useSlideshowTabState.ts b/packages/studio/src/hooks/useSlideshowTabState.ts index 6165f032bd..e900536f49 100644 --- a/packages/studio/src/hooks/useSlideshowTabState.ts +++ b/packages/studio/src/hooks/useSlideshowTabState.ts @@ -1,7 +1,7 @@ -import { useMemo, type MutableRefObject } from "react"; +import { useEffect, useMemo, useState, type MutableRefObject } from "react"; import { SLIDESHOW_ISLAND_TYPE, slideshowIslandRegex } from "@hyperframes/core/slideshow"; import type { SceneInfo } from "../components/panels/SlideshowPanel"; -import type { IframeWindow } from "../player/lib/playbackTypes"; +import { previewClipManifest } from "../player/lib/previewChannel"; /** * Derives whether the currently-edited composition is a slideshow (carries @@ -10,6 +10,18 @@ import type { IframeWindow } from "../player/lib/playbackTypes"; * for the Slideshow panel. Extracted from StudioRightPanels to keep that file * under the 600-LOC gate. */ +/** The manifest is frame data: a scene entry must carry a string id and label and finite times. */ +function isSceneEntry(value: unknown): value is SceneInfo { + if (!value || typeof value !== "object") return false; + const s = value as Record; + return ( + typeof s.id === "string" && + typeof s.label === "string" && + Number.isFinite(s.start) && + Number.isFinite(s.duration) + ); +} + export function useSlideshowTabState(params: { editingFileContent: string | null | undefined; previewIframeRef: MutableRefObject; @@ -28,20 +40,24 @@ export function useSlideshowTabState(params: { return slideshowIslandRegex("i").test(editingFileContent); }, [editingFileContent]); - // Derive scene list from the live clip manifest in the preview iframe. - const slideshowScenes = useMemo(() => { - try { - const win = previewIframeRef.current?.contentWindow as IframeWindow | null; - return (win?.__clipManifest?.scenes ?? []).map((s) => ({ - id: s.id, - label: s.label, - start: s.start, - duration: s.duration, - })); - } catch { - return []; - } - // eslint-disable-next-line react-hooks/exhaustive-deps + // The scene list from the preview's live clip manifest, read over its editing port. + const [slideshowScenes, setSlideshowScenes] = useState([]); + useEffect(() => { + const iframe = previewIframeRef.current; + if (!iframe) return setSlideshowScenes([]); + let current = true; + void previewClipManifest(iframe).then((manifest) => { + if (!current) return; + const scenes: unknown[] = Array.isArray(manifest?.scenes) ? manifest.scenes : []; + setSlideshowScenes( + scenes + .filter(isSceneEntry) + .map((s) => ({ id: s.id, label: s.label, start: s.start, duration: s.duration })), + ); + }); + return () => { + current = false; + }; }, [previewIframeRef, slideshowVisible, refreshKey]); return { isSlideshowComposition, slideshowScenes }; diff --git a/packages/studio/src/player/hooks/usePlaybackKeyboard.ts b/packages/studio/src/player/hooks/usePlaybackKeyboard.ts index c8c9c8ff2a..8486cf7a83 100644 --- a/packages/studio/src/player/hooks/usePlaybackKeyboard.ts +++ b/packages/studio/src/player/hooks/usePlaybackKeyboard.ts @@ -45,7 +45,7 @@ const FRAME_STEPS = { frameBackTen: -10, frameForwardTen: 10, } as const; -const PLAYBACK_ACTIONS = [ +export const PLAYBACK_ACTIONS = [ "playPause", ...(Object.keys(FRAME_STEPS) as Array), "mute", diff --git a/packages/studio/src/utils/blockInstaller.test.ts b/packages/studio/src/utils/blockInstaller.test.ts index bf0a3dbdf9..5e42711494 100644 --- a/packages/studio/src/utils/blockInstaller.test.ts +++ b/packages/studio/src/utils/blockInstaller.test.ts @@ -1,64 +1,74 @@ // @vitest-environment happy-dom import { afterEach, describe, expect, it, vi } from "vitest"; +import { previewRequest } from "../player/lib/previewChannel"; import { addBlockToProject } from "./blockInstaller"; +vi.mock("../player/lib/previewChannel", async (importOriginal) => { + const actual = await importOriginal(); + return { ...actual, previewRequest: vi.fn(async () => null) }; +}); + afterEach(() => { vi.unstubAllGlobals(); }); +async function addHud(extra: Partial[0]> = {}) { + const componentPath = "compositions/camcorder-hud.html"; + const targetPath = "compositions/scene.html"; + const files = new Map([ + [ + componentPath, + `
`, + ], + [ + targetPath, + `
`, + ], + ]); + vi.stubGlobal( + "fetch", + vi.fn().mockResolvedValue({ + ok: true, + json: async () => ({ + written: [componentPath], + block: { + name: "camcorder-hud", + title: "Camcorder HUD", + description: "HUD", + type: "hyperframes:block", + files: [], + dimensions: { width: 1920, height: 1080 }, + duration: 4, + }, + }), + }), + ); + const writeProjectFile = vi.fn(async (path: string, content: string) => { + files.set(path, content); + }); + + const result = await addBlockToProject({ + projectId: "project", + blockName: "camcorder-hud", + activeCompPath: targetPath, + placement: { start: 2.5, duration: 4.25, track: 3 }, + timelineElements: [], + readProjectFile: async (path) => files.get(path) ?? "", + writeProjectFile, + recordEdit: vi.fn().mockResolvedValue(undefined), + refreshFileTree: vi.fn().mockResolvedValue(undefined), + reloadPreview: vi.fn(), + showToast: vi.fn(), + ...extra, + }); + return { result, source: files.get(targetPath) }; +} + describe("addBlockToProject", () => { it("uses an explicit selected-media duration and track for a Registry overlay block", async () => { - const componentPath = "compositions/camcorder-hud.html"; - const targetPath = "compositions/scene.html"; - const files = new Map([ - [ - componentPath, - `
`, - ], - [ - targetPath, - `
`, - ], - ]); - vi.stubGlobal( - "fetch", - vi.fn().mockResolvedValue({ - ok: true, - json: async () => ({ - written: [componentPath], - block: { - name: "camcorder-hud", - title: "Camcorder HUD", - description: "HUD", - type: "hyperframes:block", - files: [], - dimensions: { width: 1920, height: 1080 }, - duration: 4, - }, - }), - }), - ); - const writeProjectFile = vi.fn(async (path: string, content: string) => { - files.set(path, content); - }); - - const result = await addBlockToProject({ - projectId: "project", - blockName: "camcorder-hud", - activeCompPath: targetPath, - placement: { start: 2.5, duration: 4.25, track: 3 }, - timelineElements: [], - readProjectFile: async (path) => files.get(path) ?? "", - writeProjectFile, - recordEdit: vi.fn().mockResolvedValue(undefined), - refreshFileTree: vi.fn().mockResolvedValue(undefined), - reloadPreview: vi.fn(), - showToast: vi.fn(), - }); - + const { result, source } = await addHud(); expect(result?.block.name).toBe("camcorder-hud"); - const source = files.get(targetPath); expect(source).toContain('data-composition-src="compositions/camcorder-hud.html"'); expect(source).toContain('data-start="2.5"'); expect(source).toContain('data-duration="4.25"'); @@ -68,4 +78,28 @@ describe("addBlockToProject", () => { expect(result?.hostKey).toBe("compositions/scene.html#camcorder-hud"); expect(source).toContain('id="camcorder-hud"'); }); + + it("stacks the block above everything a sealed preview paints, read over its editing port", async () => { + const iframe = document.createElement("iframe"); + Object.defineProperty(iframe, "contentDocument", { + get() { + throw new DOMException( + "Blocked a frame from accessing a cross-origin frame.", + "SecurityError", + ); + }, + }); + vi.mocked(previewRequest).mockResolvedValueOnce({ zIndex: 41 }); + const { source } = await addHud({ previewIframe: iframe }); + expect(vi.mocked(previewRequest)).toHaveBeenCalledWith(iframe, "max-z-index", {}); + expect(source).toContain("z-index: 42"); + }); + + it("stacks at 1 when the preview's z-index is not a whole number of zero or more", async () => { + for (const zIndex of [-5, 0.5, 1e308]) { + vi.mocked(previewRequest).mockResolvedValueOnce({ zIndex }); + const { source } = await addHud({ previewIframe: document.createElement("iframe") }); + expect(source).toContain('z-index: 1"'); + } + }); }); diff --git a/packages/studio/src/utils/blockInstaller.ts b/packages/studio/src/utils/blockInstaller.ts index 256a8c80c8..79eeee8f95 100644 --- a/packages/studio/src/utils/blockInstaller.ts +++ b/packages/studio/src/utils/blockInstaller.ts @@ -13,20 +13,11 @@ import { serializeStudioFileMutation } from "./studioFileMutationCoordinator"; import { extendRootDurationInSource } from "./rootDuration"; import { deriveTimelineStoreKeyForDomId } from "../player/lib/timelineElementHelpers"; import { studioApiFetch } from "./studioApiFetch"; +import { previewRequest } from "../player/lib/previewChannel"; -function getMaxZIndexFromIframe(iframe: HTMLIFrameElement | null): number { - try { - const doc = iframe?.contentDocument; - if (!doc) return 0; - let max = 0; - for (const el of doc.body.querySelectorAll("*")) { - const z = parseInt(getComputedStyle(el).zIndex, 10); - if (Number.isFinite(z) && z > max) max = z; - } - return max; - } catch { - return 0; - } +async function previewMaxZIndex(iframe: HTMLIFrameElement | null): Promise { + const zIndex = iframe ? (await previewRequest(iframe, "max-z-index", {}))?.zIndex : undefined; + return Number.isSafeInteger(zIndex) && (zIndex as number) >= 0 ? (zIndex as number) : 0; } interface AddBlockOptions { @@ -214,6 +205,7 @@ export async function addBlockToProject( timelineElements: relevantElements, currentTime: opts.currentTime ?? 0, }); + const zIndex = (await previewMaxZIndex(opts.previewIframe ?? null)) + 1; let hostId = ""; const insertHost = (originalContent: string) => { const { width, height } = resolveTimelineAssetCompositionSize(originalContent); @@ -228,7 +220,7 @@ export async function addBlockToProject( height, left: visualPosition ? Math.round(visualPosition.left) : 0, top: visualPosition ? Math.round(visualPosition.top) : 0, - zIndex: getMaxZIndexFromIframe(opts.previewIframe ?? null) + 1, + zIndex, }); return extendRootDurationInSource( insertTimelineAssetIntoSource(originalContent, subComposition),