mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 11:26:02 +00:00
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
This commit is contained in:
@@ -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 = `
|
||||
<div style="position: absolute; z-index: 4"></div>
|
||||
<div style="position: absolute; z-index: 12; display: none"></div>
|
||||
<section><span style="position: relative; z-index: 7"></span></section>`;
|
||||
expect(maxZIndex()).toEqual({ zIndex: 12 });
|
||||
});
|
||||
|
||||
it("answers 0 for a page with no stacked element or only negative ones", () => {
|
||||
document.body.innerHTML = `<div></div><div style="position: absolute; z-index: -3"></div>`;
|
||||
expect(maxZIndex()).toEqual({ zIndex: 0 });
|
||||
});
|
||||
});
|
||||
@@ -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 };
|
||||
}
|
||||
@@ -197,6 +197,7 @@ describe("installEditingAgent", () => {
|
||||
"assign-audio-group",
|
||||
"gsap-snapshot",
|
||||
"motion-path-home",
|
||||
"max-z-index",
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -90,6 +90,7 @@ describe("createEditingPortClient", () => {
|
||||
"assign-audio-group",
|
||||
"gsap-snapshot",
|
||||
"motion-path-home",
|
||||
"max-z-index",
|
||||
],
|
||||
});
|
||||
} finally {
|
||||
|
||||
@@ -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<K extends EditingRequestKind> = EditingRequests[K]["params"];
|
||||
export type EditingResult<K extends EditingRequestKind> = 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. */
|
||||
|
||||
@@ -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<typeof import("../../player/lib/previewChannel")>();
|
||||
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();
|
||||
});
|
||||
});
|
||||
@@ -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<string | undefined> {
|
||||
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_],
|
||||
);
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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<string, unknown> = {};
|
||||
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(<Harness />);
|
||||
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(<Harness />);
|
||||
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(<Harness />);
|
||||
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([]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<typeof import("../player/lib/previewChannel")>();
|
||||
return { ...actual, previewClipManifest: vi.fn(async () => null) };
|
||||
});
|
||||
|
||||
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
|
||||
|
||||
const SLIDESHOW_HTML = `<html><body><script type="application/hyperframes-slideshow+json">{"slides":[]}</script></body></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<typeof useSlideshowTabState> | 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<ReturnType<typeof previewClipManifest>>);
|
||||
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();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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<string, unknown>;
|
||||
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<HTMLIFrameElement | null>;
|
||||
@@ -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<SceneInfo[]>(() => {
|
||||
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<SceneInfo[]>([]);
|
||||
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 };
|
||||
|
||||
@@ -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<keyof typeof FRAME_STEPS>),
|
||||
"mute",
|
||||
|
||||
@@ -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<typeof import("../player/lib/previewChannel")>();
|
||||
return { ...actual, previewRequest: vi.fn(async () => null) };
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
async function addHud(extra: Partial<Parameters<typeof addBlockToProject>[0]> = {}) {
|
||||
const componentPath = "compositions/camcorder-hud.html";
|
||||
const targetPath = "compositions/scene.html";
|
||||
const files = new Map([
|
||||
[
|
||||
componentPath,
|
||||
`<body><div data-composition-id="camcorder-hud"><div class="hud"></div></div></body>`,
|
||||
],
|
||||
[
|
||||
targetPath,
|
||||
`<main data-composition-id="scene" data-duration="10" data-width="1920" data-height="1080"></main>`,
|
||||
],
|
||||
]);
|
||||
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,
|
||||
`<body><div data-composition-id="camcorder-hud"><div class="hud"></div></div></body>`,
|
||||
],
|
||||
[
|
||||
targetPath,
|
||||
`<main data-composition-id="scene" data-duration="10" data-width="1920" data-height="1080"></main>`,
|
||||
],
|
||||
]);
|
||||
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"');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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<number> {
|
||||
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),
|
||||
|
||||
Reference in New Issue
Block a user