mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 11:26:02 +00:00
fix(studio): a sealed preview keeps Studio's size, move and turn edits after a seek (#8212)
* fix(studio): a sealed preview keeps Studio's size, move and turn edits after a seek * fix(studio): a sealed preview reapplies Studio's edits on each frame it seeks or plays to * fix(studio): build the sealed reapply script from constants, never a caller's value * fix(studio): a sealed preview reapplies Studio's edits after a redraw at the same time * fix(studio): serve the seek reapply only into sealed preview documents * chore(core): shorten the redraw count's doc line
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
import { afterEach, expect, it } from "vitest";
|
||||
import gsap from "gsap";
|
||||
import { initSandboxRuntimeModular } from "./init";
|
||||
import type { RuntimeTimelineLike } from "./types";
|
||||
|
||||
// Importing gsap installs it on window; a test opts in by setting window.gsap itself.
|
||||
delete window.gsap;
|
||||
|
||||
afterEach(() => {
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
|
||||
it("counts every redraw of the page, a seek to the time it already shows included", () => {
|
||||
document.body.innerHTML =
|
||||
`<div id="root" data-composition-id="main" data-root="true" data-start="0" data-duration="4" data-width="640" data-height="360">` +
|
||||
`<div id="box" class="clip" data-start="0" data-duration="4"></div></div>`;
|
||||
const tl = gsap.timeline({ paused: true });
|
||||
tl.fromTo("#box", { width: 200 }, { width: 800, duration: 4, ease: "none" }, 0);
|
||||
window.__timelines = { main: tl as unknown as RuntimeTimelineLike };
|
||||
const parent = window.parent;
|
||||
Object.defineProperty(window, "parent", { configurable: true, value: {} });
|
||||
try {
|
||||
initSandboxRuntimeModular();
|
||||
} finally {
|
||||
Object.defineProperty(window, "parent", { configurable: true, value: parent });
|
||||
}
|
||||
const redraws = () => window.__hf?.redraws ?? 0;
|
||||
|
||||
window.__player?.seek(2);
|
||||
const afterSeek = redraws();
|
||||
window.__player?.seek(2);
|
||||
expect(redraws()).toBe(afterSeek + 1);
|
||||
window.__player?.renderSeek(3);
|
||||
expect(redraws()).toBe(afterSeek + 2);
|
||||
});
|
||||
@@ -5913,6 +5913,8 @@ export function initSandboxRuntimeModular(): void {
|
||||
swallow("runtime.init.transport.adapter", err);
|
||||
}
|
||||
}
|
||||
window.__hf = window.__hf || {};
|
||||
window.__hf.redraws = (window.__hf.redraws ?? 0) + 1;
|
||||
return pageAnimations;
|
||||
}
|
||||
|
||||
|
||||
+2
@@ -83,6 +83,8 @@ declare global {
|
||||
setPausedVideoFrameProvider?: (provider: PausedVideoFrameProvider | null) => void;
|
||||
/** Where Studio's loop wraps to while it plays, or null: the clips due there stay loaded. */
|
||||
setLoopStart?: (seconds: number | null) => void;
|
||||
/** Completed redraws of the page, a redraw at the same time included. */
|
||||
redraws?: number;
|
||||
/** Read-only level taps for the Studio meters: nothing exists until
|
||||
* `start()`, and `stop()` removes every tap. Peaks are linear per channel. */
|
||||
audioMeter?: {
|
||||
|
||||
@@ -148,7 +148,10 @@ import {
|
||||
} from "@hyperframes/core/fonts/embed";
|
||||
import { prepareAnimatedGifInputs } from "./animatedGifPrep.js";
|
||||
import { prepareHeifImageInputs } from "./heifImagePrep.js";
|
||||
import { createStudioPositionSeekReapplyScript } from "@hyperframes/studio-server/manual-edits-render-script";
|
||||
import {
|
||||
createStudioPositionSeekReapplyScript,
|
||||
htmlHasStudioPositionEdits,
|
||||
} from "@hyperframes/studio-server/manual-edits-render-script";
|
||||
import { getPositionEditsRenderScript } from "@hyperframes/core/runtime/position-edits-render";
|
||||
import { defaultLogger, type ProducerLogger } from "../logger.js";
|
||||
import { assertAssetMediaTypeProfile } from "./assetMediaType.js";
|
||||
@@ -2523,14 +2526,7 @@ export async function compileForRender(
|
||||
// GSAP overwrites the `translate` CSS property on every frame seek; this script
|
||||
// re-asserts the CSS custom property var() form after each seek so dragged
|
||||
// positions survive frame-by-frame rendering without a JSON sidecar.
|
||||
const HF_POSITION_ATTRS = [
|
||||
'data-hf-studio-path-offset="true"',
|
||||
'data-hf-studio-box-size="true"',
|
||||
'data-hf-studio-rotation="true"',
|
||||
'data-hf-studio-motion="',
|
||||
];
|
||||
const hasPositionEdits = HF_POSITION_ATTRS.some((attr) => assembledHtml.includes(attr));
|
||||
const htmlWithPositionScript = hasPositionEdits
|
||||
const htmlWithPositionScript = htmlHasStudioPositionEdits(assembledHtml)
|
||||
? (insertBeforeCloseTag(
|
||||
assembledHtml,
|
||||
"body",
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { Window } from "happy-dom";
|
||||
import {
|
||||
createSealedPreviewSeekReapplyScript,
|
||||
createStudioManualEditsRenderBodyScript,
|
||||
createStudioPositionSeekReapplyScript,
|
||||
} from "./manualEditsRenderScript";
|
||||
@@ -390,10 +391,11 @@ describe("createStudioPositionSeekReapplyScript", () => {
|
||||
timers: {
|
||||
setInterval?: typeof globalThis.setInterval;
|
||||
clearInterval?: typeof globalThis.clearInterval;
|
||||
script?: string;
|
||||
} = {},
|
||||
): void {
|
||||
Object.assign(window, { SyntaxError });
|
||||
const script = createStudioPositionSeekReapplyScript();
|
||||
const script = timers.script ?? createStudioPositionSeekReapplyScript();
|
||||
const execute = new Function(
|
||||
"window",
|
||||
"document",
|
||||
@@ -417,6 +419,94 @@ describe("createStudioPositionSeekReapplyScript", () => {
|
||||
);
|
||||
}
|
||||
|
||||
/** A sized card whose seeks clear its size, as a GSAP width tween does. */
|
||||
function sizedCard(window: Window) {
|
||||
window.document.body.innerHTML = `
|
||||
<div id="card"
|
||||
data-hf-studio-box-size="true"
|
||||
style="--hf-studio-width: 200px; --hf-studio-height: 100px; width: 200px; height: 100px">
|
||||
</div>
|
||||
`;
|
||||
const card = window.document.getElementById("card") as unknown as HTMLElement;
|
||||
return { card, clear: () => card.style.removeProperty("width") };
|
||||
}
|
||||
|
||||
it("hands every argument to the seek it wraps", () => {
|
||||
const window = new Window();
|
||||
sizedCard(window);
|
||||
const calls: unknown[][] = [];
|
||||
const hf = { seek: (...args: unknown[]) => void calls.push(args) };
|
||||
(window as unknown as { __hf: typeof hf }).__hf = hf;
|
||||
|
||||
runPositionScript(window);
|
||||
hf.seek(1, { suppressEvents: true });
|
||||
expect(calls).toEqual([[1, { suppressEvents: true }]]);
|
||||
});
|
||||
|
||||
/** The sealed preview script in a frame whose parent is `parent`; frames run when `drawFrame` is called. */
|
||||
function sealedPreview(parent: unknown) {
|
||||
const window = new Window();
|
||||
const { card, clear } = sizedCard(window);
|
||||
if (parent) Object.defineProperty(window, "parent", { value: parent });
|
||||
const frames: Array<() => void> = [];
|
||||
const player = { time: 0, getTime: () => player.time };
|
||||
const main = { at: 0, totalTime: () => main.at };
|
||||
const hf = { redraws: 0 };
|
||||
Object.assign(window, {
|
||||
__hf: hf,
|
||||
__player: player,
|
||||
__timelines: { main },
|
||||
requestAnimationFrame: (callback: () => void) => frames.push(callback),
|
||||
setTimeout: (callback: () => void) => callback(),
|
||||
});
|
||||
runPositionScript(window, { script: createSealedPreviewSeekReapplyScript() });
|
||||
const drawFrame = () => frames.splice(0).forEach((callback) => callback());
|
||||
return { card, clear, player, main, hf, frames, drawFrame };
|
||||
}
|
||||
const SEALED = {
|
||||
get document(): never {
|
||||
throw new Error("SecurityError");
|
||||
},
|
||||
};
|
||||
|
||||
it("puts a sealed preview's edits back on the frame its player or a timeline moved, whatever moved it", () => {
|
||||
const { card, clear, player, main, drawFrame } = sealedPreview(SEALED);
|
||||
drawFrame();
|
||||
player.time = 2;
|
||||
clear();
|
||||
drawFrame();
|
||||
expect(card.style.getPropertyValue("width")).toBe("200px");
|
||||
main.at = 2.5;
|
||||
clear();
|
||||
drawFrame();
|
||||
expect(card.style.getPropertyValue("width")).toBe("200px");
|
||||
});
|
||||
|
||||
it("puts a sealed preview's edits back after the runtime redraws at the time it already shows", () => {
|
||||
const { card, clear, hf, drawFrame } = sealedPreview(SEALED);
|
||||
drawFrame();
|
||||
hf.redraws += 1;
|
||||
clear();
|
||||
drawFrame();
|
||||
expect(card.style.getPropertyValue("width")).toBe("200px");
|
||||
});
|
||||
|
||||
it("does no work on a frame where nothing moved, so a paused preview leaves a gesture's draft alone", () => {
|
||||
const { card, clear, drawFrame } = sealedPreview(SEALED);
|
||||
drawFrame();
|
||||
clear();
|
||||
card.style.setProperty("width", "260px");
|
||||
drawFrame();
|
||||
drawFrame();
|
||||
expect(card.style.getPropertyValue("width")).toBe("260px");
|
||||
});
|
||||
|
||||
it("follows no frames in a preview Studio can script, nor in a page with no parent", () => {
|
||||
expect(sealedPreview({ document: {} }).frames).toHaveLength(0);
|
||||
expect(sealedPreview(null).frames).toHaveLength(0);
|
||||
expect(sealedPreview(SEALED).frames).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("reapplies box-size after seek", () => {
|
||||
const window = new Window();
|
||||
window.document.body.innerHTML = `
|
||||
|
||||
@@ -13,17 +13,29 @@ export function createStudioManualEditsRenderBodyScript(
|
||||
return `(${studioManualEditsRenderRuntime.toString()})(${JSON.stringify(manifestContent)}, ${JSON.stringify(options.activeCompositionPath ?? null)});`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a self-contained IIFE string that re-applies studio position edits
|
||||
* (translate, rotate) after every GSAP seek by querying data attributes baked
|
||||
* into the HTML. Works without a JSON manifest — positions are already inlined
|
||||
* as CSS custom properties on the elements.
|
||||
*/
|
||||
const positionSeekReapplyCall = (followFrames: "true" | "false") =>
|
||||
`(${studioPositionSeekReapplyRuntime.toString()})(${followFrames});`;
|
||||
|
||||
/** A self-contained script that puts the page's committed Studio edits back after every seek. */
|
||||
export function createStudioPositionSeekReapplyScript(): string {
|
||||
return `(${studioPositionSeekReapplyRuntime.toString()})();`;
|
||||
return positionSeekReapplyCall("false");
|
||||
}
|
||||
|
||||
function studioPositionSeekReapplyRuntime(): void {
|
||||
const POSITION_EDIT_MARKS = [
|
||||
'data-hf-studio-path-offset="true"',
|
||||
'data-hf-studio-box-size="true"',
|
||||
'data-hf-studio-rotation="true"',
|
||||
'data-hf-studio-motion="',
|
||||
];
|
||||
export const htmlHasStudioPositionEdits = (html: string) =>
|
||||
POSITION_EDIT_MARKS.some((mark) => html.includes(mark));
|
||||
|
||||
/** The reapply for a preview frame Studio cannot script; Studio wraps one it can script itself. */
|
||||
export function createSealedPreviewSeekReapplyScript(): string {
|
||||
return `(function(){try{if(window.parent===window||window.parent.document)return}catch(_){}${positionSeekReapplyCall("true")}})();`;
|
||||
}
|
||||
|
||||
function studioPositionSeekReapplyRuntime(followFrames: boolean): void {
|
||||
const OFFSET_X_PROP = "--hf-studio-offset-x";
|
||||
const OFFSET_Y_PROP = "--hf-studio-offset-y";
|
||||
const WIDTH_PROP = "--hf-studio-width";
|
||||
@@ -263,10 +275,10 @@ function studioPositionSeekReapplyRuntime(): void {
|
||||
__player?: Record<string, unknown>;
|
||||
};
|
||||
|
||||
const isWrapped = (fn: (time: number) => unknown): boolean =>
|
||||
const isWrapped = (fn: (...args: never[]) => unknown): boolean =>
|
||||
Boolean((fn as unknown as Record<string, unknown>)[WRAPPED_PROP]);
|
||||
|
||||
const markWrapped = (fn: (time: number) => unknown): void => {
|
||||
const markWrapped = (fn: (...args: never[]) => unknown): void => {
|
||||
try {
|
||||
Object.defineProperty(fn, WRAPPED_PROP, {
|
||||
configurable: false,
|
||||
@@ -282,52 +294,42 @@ function studioPositionSeekReapplyRuntime(): void {
|
||||
}
|
||||
};
|
||||
|
||||
const wrapFn = (get: () => unknown, set: (fn: (time: number) => unknown) => void): boolean => {
|
||||
const fn = get();
|
||||
type Seek = (time: number, ...rest: unknown[]) => unknown;
|
||||
type SeekSlot = [owner: "__hf" | "__player", key: string];
|
||||
const SEEKS: SeekSlot[] = [
|
||||
["__hf", "seek"],
|
||||
["__player", "renderSeek"],
|
||||
];
|
||||
|
||||
const wrapFn = ([owner, key]: SeekSlot): boolean => {
|
||||
const fn = runtimeWindow[owner]?.[key];
|
||||
if (typeof fn !== "function") return false;
|
||||
const seek = fn as (time: number) => unknown;
|
||||
const seek = fn as Seek;
|
||||
if (isWrapped(seek)) {
|
||||
reapplyAll();
|
||||
return true;
|
||||
}
|
||||
const wrapped = function (this: unknown, time: number): unknown {
|
||||
const wrapped = function (this: unknown, time: number, ...rest: unknown[]): unknown {
|
||||
lastSeekTime = typeof time === "number" && Number.isFinite(time) ? Math.max(0, time) : 0;
|
||||
const result = seek.call(this, time);
|
||||
const result = seek.call(this, time, ...rest);
|
||||
reapplyAll();
|
||||
return result;
|
||||
};
|
||||
markWrapped(wrapped);
|
||||
set(wrapped);
|
||||
const target = runtimeWindow[owner];
|
||||
if (target) target[key] = wrapped;
|
||||
reapplyAll();
|
||||
return true;
|
||||
};
|
||||
|
||||
const wrapSeekFunctions = (): boolean => {
|
||||
const a = wrapFn(
|
||||
() => runtimeWindow.__hf?.["seek"],
|
||||
(fn) => {
|
||||
if (runtimeWindow.__hf) runtimeWindow.__hf["seek"] = fn;
|
||||
},
|
||||
);
|
||||
const b = wrapFn(
|
||||
() => runtimeWindow.__player?.["renderSeek"],
|
||||
(fn) => {
|
||||
if (runtimeWindow.__player) runtimeWindow.__player["renderSeek"] = fn;
|
||||
},
|
||||
);
|
||||
return a || b;
|
||||
};
|
||||
const wrapSeekFunctions = (): boolean => SEEKS.map(wrapFn).some(Boolean);
|
||||
|
||||
const installSeekTrap = (
|
||||
obj: Record<string, unknown> | undefined,
|
||||
key: string,
|
||||
getter: () => unknown,
|
||||
setter: (fn: (time: number) => unknown) => void,
|
||||
): void => {
|
||||
const installSeekTrap = (slot: SeekSlot): void => {
|
||||
const obj = runtimeWindow[slot[0]];
|
||||
if (!obj) return;
|
||||
try {
|
||||
let current = obj[key];
|
||||
Object.defineProperty(obj, key, {
|
||||
let current = obj[slot[1]];
|
||||
Object.defineProperty(obj, slot[1], {
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
get() {
|
||||
@@ -335,9 +337,7 @@ function studioPositionSeekReapplyRuntime(): void {
|
||||
},
|
||||
set(value: unknown) {
|
||||
current = value;
|
||||
if (typeof value === "function" && !isWrapped(value as (time: number) => unknown)) {
|
||||
wrapFn(getter, setter);
|
||||
}
|
||||
if (typeof value === "function" && !isWrapped(value as Seek)) wrapFn(slot);
|
||||
},
|
||||
});
|
||||
} catch {
|
||||
@@ -352,22 +352,29 @@ function studioPositionSeekReapplyRuntime(): void {
|
||||
}
|
||||
|
||||
wrapSeekFunctions();
|
||||
installSeekTrap(
|
||||
runtimeWindow.__hf,
|
||||
"seek",
|
||||
() => runtimeWindow.__hf?.["seek"],
|
||||
(fn) => {
|
||||
if (runtimeWindow.__hf) runtimeWindow.__hf["seek"] = fn;
|
||||
},
|
||||
);
|
||||
installSeekTrap(
|
||||
runtimeWindow.__player as Record<string, unknown> | undefined,
|
||||
"renderSeek",
|
||||
() => runtimeWindow.__player?.["renderSeek"],
|
||||
(fn) => {
|
||||
if (runtimeWindow.__player) runtimeWindow.__player["renderSeek"] = fn;
|
||||
},
|
||||
);
|
||||
for (const slot of SEEKS) installSeekTrap(slot);
|
||||
// A sealed preview seeks and plays past every hook above: reapply on each frame where time moved.
|
||||
const readNumber = (owner: Record<string, unknown> | undefined, key: string): number => {
|
||||
const fn = owner?.[key];
|
||||
return typeof fn === "function" ? Number(fn.call(owner)) : Number.NaN;
|
||||
};
|
||||
let drawnKey = "";
|
||||
const followFrame = (): void => {
|
||||
const time = readNumber(runtimeWindow.__player, "getTime");
|
||||
const timelines = (runtimeWindow as { __timelines?: Record<string, Record<string, unknown>> })
|
||||
.__timelines;
|
||||
let key = time + "," + String(runtimeWindow.__hf?.["redraws"]);
|
||||
for (const id in timelines)
|
||||
if (id !== MOTION_TL_KEY) key += "," + readNumber(timelines[id], "totalTime");
|
||||
if (key !== drawnKey) {
|
||||
drawnKey = key;
|
||||
lastSeekTime = Number.isFinite(time) ? Math.max(0, time) : lastSeekTime;
|
||||
reapplyAll();
|
||||
}
|
||||
// Asked for after this paint, so the next frame runs it after the runtime's own tick.
|
||||
window.setTimeout(() => window.requestAnimationFrame(followFrame), 0);
|
||||
};
|
||||
if (followFrames) window.requestAnimationFrame(followFrame);
|
||||
let remaining = 120;
|
||||
const interval = setInterval(() => {
|
||||
wrapSeekFunctions();
|
||||
|
||||
@@ -2,6 +2,11 @@ import { createHmac, randomBytes, timingSafeEqual } from "node:crypto";
|
||||
import type { Context, Hono, Next } from "hono";
|
||||
import { getMimeType } from "./mime.js";
|
||||
import { fetchAgain } from "./fetchAgain.js";
|
||||
import { insertBeforeCloseTag } from "@hyperframes/core/compiler";
|
||||
import {
|
||||
createSealedPreviewSeekReapplyScript,
|
||||
htmlHasStudioPositionEdits,
|
||||
} from "./manualEditsRenderScript.js";
|
||||
import {
|
||||
isProxyWait,
|
||||
PREVIEW_EXPOSED_HEADERS,
|
||||
@@ -80,12 +85,19 @@ const PROXY_PREFLIGHT_ANSWER = {
|
||||
Vary: "Origin",
|
||||
};
|
||||
|
||||
function withStudioEditsReappliedAfterSeeks(html: string): string {
|
||||
if (!htmlHasStudioPositionEdits(html)) return html;
|
||||
const tag = `<script>${createSealedPreviewSeekReapplyScript()}\n//# sourceURL=hyperframes://position-seek-reapply</script>`;
|
||||
return insertBeforeCloseTag(html, "body", tag) ?? `${html}${tag}`;
|
||||
}
|
||||
|
||||
async function underToken(res: Response, headers: Headers, projectId: string, token: string) {
|
||||
if (res.status !== 200 || !headers.get("content-type")?.startsWith("text/html"))
|
||||
return new Response(res.body, { status: res.status, headers });
|
||||
const base = previewBase(projectId);
|
||||
headers.delete("content-length");
|
||||
const html = (await res.text()).replaceAll(base, `${base}~${token}/`);
|
||||
const rebased = (await res.text()).replaceAll(base, `${base}~${token}/`);
|
||||
const html = withStudioEditsReappliedAfterSeeks(rebased);
|
||||
return new Response(html, { status: res.status, headers });
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,10 @@ writeFileSync(
|
||||
);
|
||||
writeFileSync(join(projectDir, "fonts", "Brand.woff2"), Buffer.from("wOF2font"));
|
||||
writeFileSync(join(projectDir, "fonts", "Brand.ttf"), Buffer.from("ttf-font"));
|
||||
writeFileSync(
|
||||
join(projectDir, "sized.html"),
|
||||
'<div data-composition-id="sized"><div id="card" data-hf-studio-box-size="true"></div></div>',
|
||||
);
|
||||
writeFileSync(join(projectDir, "scene.js"), "export const scene = 1;");
|
||||
writeFileSync(join(projectDir, "scene.mjs"), "export const scene = 2;");
|
||||
writeFileSync(join(projectDir, "data.json"), '{"secret":true}');
|
||||
@@ -206,6 +210,19 @@ describe("an opaque preview frame's loads under its token", () => {
|
||||
expect(html.replaceAll(tokened, "")).not.toContain("/api/projects/demo/preview/");
|
||||
});
|
||||
|
||||
it("puts Studio's seek reapply last in a sealed document with a Studio edit, and only there", async () => {
|
||||
const reapply = "//# sourceURL=hyperframes://position-seek-reapply</script>";
|
||||
const sealed = await (await load("comp/sized.html", { "sec-fetch-dest": "iframe" })).text();
|
||||
expect(sealed).toContain(reapply);
|
||||
expect(sealed.indexOf(reapply)).toBeGreaterThan(sealed.indexOf('id="card"'));
|
||||
const plain = await api.request("http://localhost/projects/demo/preview/comp/sized.html", {
|
||||
headers: { "sec-fetch-site": "same-origin", "sec-fetch-dest": "iframe" },
|
||||
});
|
||||
expect(await plain.text()).not.toContain(reapply);
|
||||
const unedited = await (await load("comp/scene.html", { "sec-fetch-dest": "iframe" })).text();
|
||||
expect(unedited).not.toContain(reapply);
|
||||
});
|
||||
|
||||
it("serves the main preview under the token, re-based", async () => {
|
||||
const res = await api.request(`http://localhost/projects/demo/preview/~${token}`, {
|
||||
headers: { "sec-fetch-site": "same-origin", "sec-fetch-dest": "iframe" },
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
import { expect, it, vi } from "vitest";
|
||||
import { Window } from "happy-dom";
|
||||
import { createSealedPreviewSeekReapplyScript } from "@hyperframes/studio-server/manual-edits-render-script";
|
||||
import { installStudioManualEditSeekReapply } from "./manualEdits";
|
||||
|
||||
type PreviewWindow = Parameters<typeof installStudioManualEditSeekReapply>[0] & {
|
||||
__player: { seek: (time: number) => void; getTime: () => number };
|
||||
};
|
||||
|
||||
/** A preview frame with a sized card whose seeks clear its width; `parent` is what the frame can read of Studio. */
|
||||
function previewFrame(parent: unknown) {
|
||||
const win = new Window();
|
||||
win.document.body.innerHTML = `<div id="card" data-hf-studio-box-size="true"
|
||||
style="--hf-studio-width: 200px; --hf-studio-height: 100px; width: 200px; height: 100px"></div>`;
|
||||
const card = win.document.getElementById("card") as unknown as HTMLElement;
|
||||
const frame = win as unknown as PreviewWindow;
|
||||
let time = 0;
|
||||
frame.__player = {
|
||||
seek: (to) => {
|
||||
time = to;
|
||||
card.style.removeProperty("width");
|
||||
},
|
||||
getTime: () => time,
|
||||
};
|
||||
const frames: Array<() => void> = [];
|
||||
Object.defineProperty(win, "parent", { value: parent });
|
||||
Object.assign(win, {
|
||||
SyntaxError,
|
||||
requestAnimationFrame: (callback: () => void) => frames.push(callback),
|
||||
setTimeout: (callback: () => void) => callback(),
|
||||
});
|
||||
new Function(
|
||||
"window",
|
||||
"document",
|
||||
"HTMLElement",
|
||||
"DOMMatrix",
|
||||
"setInterval",
|
||||
"clearInterval",
|
||||
createSealedPreviewSeekReapplyScript(),
|
||||
)(
|
||||
win,
|
||||
win.document,
|
||||
win.HTMLElement,
|
||||
globalThis.DOMMatrix,
|
||||
() => 0,
|
||||
() => undefined,
|
||||
);
|
||||
const drawFrame = () => frames.splice(0).forEach((callback) => callback());
|
||||
drawFrame();
|
||||
const widthWrites = vi.spyOn(card.style, "setProperty");
|
||||
const writes = () => widthWrites.mock.calls.filter(([name]) => name === "width").length;
|
||||
return { frame, card, writes, drawFrame };
|
||||
}
|
||||
|
||||
it("reapplies once per seek in a frame Studio scripts: Studio's wrapper, never the frame's script too", () => {
|
||||
const { frame, card, writes, drawFrame } = previewFrame({ document: {} });
|
||||
let studioApplies = 0;
|
||||
installStudioManualEditSeekReapply(frame, () => {
|
||||
studioApplies += 1;
|
||||
});
|
||||
studioApplies = 0;
|
||||
|
||||
frame.__player.seek(1);
|
||||
drawFrame();
|
||||
expect(studioApplies).toBe(1);
|
||||
expect(writes()).toBe(0);
|
||||
expect(card.style.getPropertyValue("width")).toBe("");
|
||||
});
|
||||
|
||||
it("reapplies once per seek in a frame Studio cannot script, on the frame that shows it", () => {
|
||||
const sealed = {
|
||||
get document(): never {
|
||||
throw new Error("SecurityError");
|
||||
},
|
||||
};
|
||||
const { frame, card, writes, drawFrame } = previewFrame(sealed);
|
||||
|
||||
frame.__player.seek(1);
|
||||
drawFrame();
|
||||
drawFrame();
|
||||
expect(writes()).toBe(1);
|
||||
expect(card.style.getPropertyValue("width")).toBe("200px");
|
||||
});
|
||||
Reference in New Issue
Block a user