mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-17 15:24:19 +00:00
refactor(studio): one preview mark with a value, preview or capture, instead of a second meta
This commit is contained in:
@@ -8,7 +8,7 @@ import {
|
||||
settleFirstFrameCompositionReadiness,
|
||||
} from "./compositionReadiness.js";
|
||||
import { createRuntimeStartTimeResolver } from "./runtime/startResolver.js";
|
||||
import { DEFER_MEDIA_META, STUDIO_PREVIEW_MARK_META } from "./studioPreviewMark.js";
|
||||
import { STUDIO_PREVIEW_MARK, STUDIO_PREVIEW_MARK_META } from "./studioPreviewMark.js";
|
||||
import { isRuntimeElementVisibleAt } from "./runtime/timeline.js";
|
||||
|
||||
function docWith(bodyHtml: string): Document {
|
||||
@@ -190,7 +190,9 @@ describe("mediaReadinessInput", () => {
|
||||
.appendChild(preview.createElement("meta"))
|
||||
.setAttribute("name", STUDIO_PREVIEW_MARK_META);
|
||||
const capture = docWith(html);
|
||||
capture.head.appendChild(capture.createElement("meta")).setAttribute("name", DEFER_MEDIA_META);
|
||||
const captureMark = capture.head.appendChild(capture.createElement("meta"));
|
||||
captureMark.setAttribute("name", STUDIO_PREVIEW_MARK_META);
|
||||
captureMark.setAttribute("content", STUDIO_PREVIEW_MARK.capture);
|
||||
const plain = docWith(html);
|
||||
const ids = (doc: Document) =>
|
||||
scanPendingCompositionAssets(doc, { scope: "all" }).pendingMedia.map((el) => el.id);
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { HF_COLOR_GRADING_ATTR, serializeHfColorGrading } from "../colorGrading";
|
||||
import {
|
||||
DEFER_MEDIA_META,
|
||||
STUDIO_PREVIEW_MARK,
|
||||
STUDIO_PREVIEW_LAZY_ATTR,
|
||||
STUDIO_PREVIEW_MARK_META,
|
||||
} from "../studioPreviewMark";
|
||||
@@ -378,11 +378,11 @@ describe("runtime entry", () => {
|
||||
const loads: HTMLMediaElement[] = [];
|
||||
const spyLoad = () => {
|
||||
loads.length = 0;
|
||||
vi.spyOn(HTMLMediaElement.prototype, "load").mockImplementation(
|
||||
function (this: HTMLMediaElement) {
|
||||
loads.push(this);
|
||||
},
|
||||
);
|
||||
vi.spyOn(HTMLMediaElement.prototype, "load").mockImplementation(function (
|
||||
this: HTMLMediaElement,
|
||||
) {
|
||||
loads.push(this);
|
||||
});
|
||||
};
|
||||
const loadsOf = (...els: HTMLMediaElement[]) =>
|
||||
els.map((el) => loads.filter((loaded) => loaded === el).length);
|
||||
@@ -393,9 +393,12 @@ describe("runtime entry", () => {
|
||||
|
||||
it("loads a capture build's media only near its moment, while every image of a hidden clip stays shown", async () => {
|
||||
spyLoad();
|
||||
// What a capture build carries instead of the preview mark.
|
||||
// What a capture build carries: the mark, with the capture value.
|
||||
document.head.appendChild(
|
||||
Object.assign(document.createElement("meta"), { name: DEFER_MEDIA_META }),
|
||||
Object.assign(document.createElement("meta"), {
|
||||
name: STUDIO_PREVIEW_MARK_META,
|
||||
content: STUDIO_PREVIEW_MARK.capture,
|
||||
}),
|
||||
);
|
||||
const root = mountRoot();
|
||||
const [now, later] = videos(root, "0", "9");
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import {
|
||||
RUNTIME_STAMPED_ATTR,
|
||||
STUDIO_PREVIEW_MARK_META,
|
||||
STUDIO_PREVIEW_UPCOMING_ATTR,
|
||||
skipsImagesOfHiddenClips,
|
||||
} from "../studioPreviewMark";
|
||||
|
||||
// Hides timed non-media clips from script evaluation until the first visibility pass decides them;
|
||||
@@ -14,7 +14,6 @@ const PREVIEW_HIDE_UNTIL_FIRST_PASS = 'img[loading="lazy"] { display: none !impo
|
||||
const SKIP_ATTR = "data-hf-skip-hidden-images";
|
||||
export const SKIPPED_CLIP = `[data-start]:not(video, audio, img, [${STUDIO_PREVIEW_UPCOMING_ATTR}], [${RUNTIME_STAMPED_ATTR}])[style*="visibility: hidden"]`;
|
||||
const SKIP_HIDDEN_IMAGES = `${SKIPPED_CLIP} img { display: none !important; }`;
|
||||
const STUDIO_PREVIEW_MARK = `meta[name="${STUDIO_PREVIEW_MARK_META}"]`;
|
||||
export const CLIP_HIDDEN_ATTR = "data-hf-clip-hidden";
|
||||
const CONTENTS_ATTR = "data-hf-hidden-clip-contents";
|
||||
const HIDE_HIDDEN_CLIP_CONTENTS = `[${CLIP_HIDDEN_ATTR}][style*="visibility: hidden"] * { visibility: hidden !important; }`;
|
||||
@@ -48,7 +47,7 @@ export function hideTimedClipsUntilFirstPass(): void {
|
||||
if (typeof document === "undefined") return;
|
||||
const parent = document.head ?? document.documentElement;
|
||||
if (!parent) return;
|
||||
const preview = document.querySelector(STUDIO_PREVIEW_MARK) !== null;
|
||||
const preview = skipsImagesOfHiddenClips(document);
|
||||
if (preview && !skipsHiddenImages()) appendStyle(parent, SKIP_ATTR, SKIP_HIDDEN_IMAGES);
|
||||
if (!document.querySelector(`style[${CONTENTS_ATTR}]`))
|
||||
appendStyle(parent, CONTENTS_ATTR, HIDE_HIDDEN_CLIP_CONTENTS);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
export const STUDIO_PREVIEW_MARK_META = "hyperframes-studio-preview";
|
||||
/** Media deferral without the preview's image skipping: a capture's still loads only the media near its moment. */
|
||||
export const DEFER_MEDIA_META = "hyperframes-defer-media";
|
||||
/** The mark's content: Studio's own preview, or a capture build (a still shot right after a seek). */
|
||||
export const STUDIO_PREVIEW_MARK = { preview: "preview", capture: "capture" } as const;
|
||||
export const STUDIO_PREVIEW_UPCOMING_ATTR = "data-hf-upcoming";
|
||||
export const STUDIO_PREVIEW_LAZY_ATTR = "data-hf-preview-lazy";
|
||||
export const STUDIO_PREVIEW_ERRORS = "__hfPreviewErrors";
|
||||
@@ -21,8 +21,14 @@ export function applyAuthoredTiming(
|
||||
el.removeAttribute(RUNTIME_STAMPED_ATTR);
|
||||
}
|
||||
|
||||
/** Whether `doc` loads media only near the playhead: Studio's preview, or a capture build that asks for that alone. */
|
||||
const markOf = (doc: Document) =>
|
||||
doc.querySelector(`meta[name="${STUDIO_PREVIEW_MARK_META}"]`)?.getAttribute("content") ?? null;
|
||||
|
||||
/** Whether `doc` loads media only near the playhead: any marked page, the preview or a capture build. */
|
||||
export const defersMedia = (doc: Document): boolean =>
|
||||
doc.querySelector(
|
||||
`meta[name="${STUDIO_PREVIEW_MARK_META}"], meta[name="${DEFER_MEDIA_META}"]`,
|
||||
) !== null;
|
||||
doc.querySelector(`meta[name="${STUDIO_PREVIEW_MARK_META}"]`) !== null;
|
||||
|
||||
/** Whether `doc` skips hidden clips' images: the preview only (a mark without content is one); a capture keeps
|
||||
* every image eager, so a still right after a seek never shows a scene before its images. */
|
||||
export const skipsImagesOfHiddenClips = (doc: Document): boolean =>
|
||||
defersMedia(doc) && markOf(doc) !== STUDIO_PREVIEW_MARK.capture;
|
||||
|
||||
@@ -23,7 +23,7 @@ import { join, parse } from "node:path";
|
||||
import { ensureHfIds } from "@hyperframes/parsers/hf-ids";
|
||||
import {
|
||||
STUDIO_PREVIEW_ERRORS,
|
||||
DEFER_MEDIA_META,
|
||||
STUDIO_PREVIEW_MARK,
|
||||
STUDIO_PREVIEW_MARK_META,
|
||||
} from "@hyperframes/core/studio-preview-mark";
|
||||
import { AFTER_FONTS_SCRIPT_TYPE } from "@hyperframes/core/compiler";
|
||||
@@ -218,7 +218,7 @@ describe("registerPreviewRoutes", () => {
|
||||
const app = new Hono();
|
||||
registerPreviewRoutes(app, createAdapter(projectDir));
|
||||
const html = await (await app.request("http://localhost/projects/demo/preview")).text();
|
||||
const mark = html.indexOf(`<meta name="${STUDIO_PREVIEW_MARK_META}">`);
|
||||
const mark = html.indexOf(`<meta name="${STUDIO_PREVIEW_MARK_META}" content="preview">`);
|
||||
expect(mark).toBeGreaterThan(-1);
|
||||
expect(mark).toBeLessThan(html.indexOf("/api/runtime.js"));
|
||||
expect(html).toContain("<script data-hf-gsap-fallback>");
|
||||
@@ -282,7 +282,7 @@ describe("registerPreviewRoutes", () => {
|
||||
expect(previewWindow[STUDIO_PREVIEW_ERRORS]).toEqual(["Uncaught Error: GSAP could not load"]);
|
||||
});
|
||||
|
||||
it("serves a later scene's image lazy, and captures every image eager with only media deferred", async () => {
|
||||
it("serves a later scene's image lazy, and captures every image eager with the capture mark", async () => {
|
||||
const projectDir = createProjectDir();
|
||||
const later =
|
||||
'<!DOCTYPE html><html><head></head><body><div data-start="5"><img src="b.png"></div></body></html>';
|
||||
@@ -295,11 +295,12 @@ describe("registerPreviewRoutes", () => {
|
||||
const preview = await (await app.request(url)).text();
|
||||
const capture = await (await app.request(`${url}?${PREVIEW_CAPTURE_PARAM}=1`)).text();
|
||||
expect(preview, path).toMatch(/<img loading="lazy" [^>]*src="b.png">/);
|
||||
expect(preview, path).toContain(STUDIO_PREVIEW_MARK_META);
|
||||
const mark = (content: string) =>
|
||||
`<meta name="${STUDIO_PREVIEW_MARK_META}" content="${content}">`;
|
||||
expect(preview, path).toContain(mark(STUDIO_PREVIEW_MARK.preview));
|
||||
expect(capture, path).not.toContain("loading=");
|
||||
expect(capture, path).not.toContain(STUDIO_PREVIEW_MARK_META);
|
||||
expect(capture, path).toContain(`<meta name="${DEFER_MEDIA_META}">`);
|
||||
expect(preview, path).not.toContain(DEFER_MEDIA_META);
|
||||
expect(capture, path).toContain(mark(STUDIO_PREVIEW_MARK.capture));
|
||||
expect(capture, path).not.toContain(mark(STUDIO_PREVIEW_MARK.preview));
|
||||
expect(capture, path).toContain("<script data-hf-gsap-fallback>");
|
||||
}
|
||||
});
|
||||
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
type BundleOptions,
|
||||
} from "@hyperframes/core/compiler";
|
||||
import {
|
||||
DEFER_MEDIA_META,
|
||||
STUDIO_PREVIEW_MARK,
|
||||
STUDIO_PREVIEW_MARK_META,
|
||||
STUDIO_PREVIEW_ERRORS,
|
||||
} from "@hyperframes/core/studio-preview-mark";
|
||||
@@ -321,7 +321,7 @@ function previewVariablesFromRequest(rawVariables: string | undefined):
|
||||
return { raw: rawVariables, values: parse.values };
|
||||
}
|
||||
|
||||
/** Captures screenshot right after a seek, so they get every image eager and no preview mark; their media still
|
||||
/** Captures screenshot right after a seek, so they get every image eager and the capture mark: their media still
|
||||
* loads only near the moment shot, or a long film's still would load every clip it has. */
|
||||
export const PREVIEW_CAPTURE_PARAM = "hf-capture";
|
||||
|
||||
@@ -329,6 +329,9 @@ export const PREVIEW_CAPTURE_PARAM = "hf-capture";
|
||||
const EARLY_ERRORS_SCRIPT = `<script>(function(){var seen=window.${STUDIO_PREVIEW_ERRORS}=[];
|
||||
addEventListener("error",function(e){seen.push(e.message||String(e))});})();</script>`;
|
||||
|
||||
const previewMark = (content: string) =>
|
||||
`<meta name="${STUDIO_PREVIEW_MARK_META}" content="${content}">`;
|
||||
|
||||
function injectStudioPreviewAugmentations(
|
||||
html: string,
|
||||
adapter: StudioApiAdapter,
|
||||
@@ -337,10 +340,10 @@ function injectStudioPreviewAugmentations(
|
||||
capture: boolean,
|
||||
): string {
|
||||
const marked = capture
|
||||
? injectTagsAtHeadStart(html, `<meta name="${DEFER_MEDIA_META}">`)
|
||||
? injectTagsAtHeadStart(html, previewMark(STUDIO_PREVIEW_MARK.capture))
|
||||
: injectTagsAtHeadStart(
|
||||
lazyPreviewImages(html),
|
||||
`<meta name="${STUDIO_PREVIEW_MARK_META}">${EARLY_ERRORS_SCRIPT}`,
|
||||
`${previewMark(STUDIO_PREVIEW_MARK.preview)}${EARLY_ERRORS_SCRIPT}`,
|
||||
);
|
||||
return injectStudioMotionScript(
|
||||
injectMotionPathPluginIfNeeded(
|
||||
|
||||
Reference in New Issue
Block a user