mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-23 11:29:54 +00:00
* fix(player): stop the preview freezing when many clips share one long recording
Brings back #7795, which #7978 took out because the HyperFrames desktop
app stopped re-reading a film after a script edit. The next commit keeps
Studio's preview, and every app that embeds it, on main's behaviour.
This reverts commit 33e1a36a39.
* fix(core): the Studio preview, and apps that embed it, keep main's clip handling
With #7795 the player's frame held clips back on every engine, by its
name, and released a clip by setting its src again. Studio's preview
sits in a player frame too, so it took both, and the HyperFrames desktop
app, which embeds that preview, stopped re-reading a film after a script
edit (#7978).
A page carrying Studio's preview mark now takes the same media decisions
as on main: the player's frame counts there on WebKit only, as before,
and a clip is released by load() with its src dropped. A plain player
embed keeps the hold-back and the new release, which is what stops a
film cut from one long recording freezing in Chrome and Firefox.
163 lines
5.8 KiB
TypeScript
163 lines
5.8 KiB
TypeScript
import { isWebKit } from "./runtime/mediaClock.js";
|
|
|
|
export const STUDIO_PREVIEW_MARK_META = "hyperframes-studio-preview";
|
|
export const PREVIEW_SEEK_HOLD_ATTR = "data-hf-preview-seek-hold";
|
|
export const PREVIEW_VIDEO_FRAME_ATTR = "data-hf-preview-video-frame";
|
|
export const PREVIEW_VIDEO_SOURCE_ATTR = "data-hf-preview-video-source";
|
|
export const PREVIEW_VIDEO_CANVAS_ATTR = "data-hf-preview-video-canvas";
|
|
export const STUDIO_PREVIEW_CAPTURE_MODE = "capture";
|
|
export const PREVIEW_FOR_ANOTHER_SITE_META = "hyperframes-preview-for-another-site";
|
|
|
|
export function servedToAnotherSite(doc: Document): boolean {
|
|
return doc.querySelector(`meta[name="${PREVIEW_FOR_ANOTHER_SITE_META}"]`) !== null;
|
|
}
|
|
|
|
export function markedForStudio(doc: Document): boolean {
|
|
return (
|
|
doc.querySelector(`meta[name="${STUDIO_PREVIEW_MARK_META}"]`) !== null &&
|
|
!servedToAnotherSite(doc)
|
|
);
|
|
}
|
|
|
|
export function getLogicalOpacity(el: Element, computed: CSSStyleDeclaration): string {
|
|
const overlay = el.previousElementSibling;
|
|
if (
|
|
el.hasAttribute(PREVIEW_VIDEO_FRAME_ATTR) &&
|
|
overlay?.localName === "canvas" &&
|
|
overlay.hasAttribute(PREVIEW_VIDEO_CANVAS_ATTR)
|
|
)
|
|
return el.ownerDocument.defaultView?.getComputedStyle(overlay).opacity ?? computed.opacity;
|
|
return computed.opacity;
|
|
}
|
|
export const PLAYER_FRAME_ATTR = "data-hf-player-frame";
|
|
export const PLAYER_FRAME_NAME = "hyperframes-player";
|
|
|
|
export function servedToStudio(doc: Document): boolean {
|
|
return doc.querySelector(`meta[name="${STUDIO_PREVIEW_MARK_META}"]`) !== null;
|
|
}
|
|
|
|
export function inPlayerFrame(doc: Document): boolean {
|
|
const frame = doc.defaultView;
|
|
const marked = frame?.frameElement?.hasAttribute(PLAYER_FRAME_ATTR) === true;
|
|
if (servedToStudio(doc)) return isWebKit() && marked;
|
|
return marked || frame?.name === PLAYER_FRAME_NAME;
|
|
}
|
|
|
|
export function defersMediaIn(doc: Document): boolean {
|
|
return servedToStudio(doc) || inPlayerFrame(doc);
|
|
}
|
|
export function capturePageIn(doc: Document): boolean {
|
|
return (
|
|
doc.querySelector(`meta[name="${STUDIO_PREVIEW_MARK_META}"]`)?.getAttribute("content") ===
|
|
STUDIO_PREVIEW_CAPTURE_MODE
|
|
);
|
|
}
|
|
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";
|
|
export const STUDIO_PREVIEW_ERROR_KINDS = "__hfPreviewErrorKinds";
|
|
export function previewConsoleErrorText(args: readonly unknown[]): string | null {
|
|
const text = args.map((a) => (a instanceof Error ? a.message : String(a))).join(" ");
|
|
return text.includes("favicon") ? null : text;
|
|
}
|
|
|
|
export const refusedPreviewMessage = (status: number) =>
|
|
`The preview page could not be loaded (HTTP ${status}).`;
|
|
export function refusedPreviewStatus(message: string): number | null {
|
|
const status = /^The preview page could not be loaded \(HTTP (\d{3})\)\.$/.exec(message)?.[1];
|
|
return status ? Number(status) : null;
|
|
}
|
|
export const PREVIEW_RASTER_ATTR = "data-hf-preview-raster";
|
|
export const RUNTIME_STAMPED_ATTR = "data-hf-autostamped";
|
|
export const CLIP_HIDDEN_ATTR = "data-hf-clip-hidden";
|
|
export const STUDIO_PREVIEW_FAR_SCENE_ATTR = "data-hf-far-scene";
|
|
export const STUDIO_PREVIEW_PARKED_SCENE_ATTR = "data-hf-parked-scene";
|
|
export const PREVIEW_FILE_SIZE_ATTR = "data-hf-file-size";
|
|
export const PREVIEW_ONLY_ATTRS = [
|
|
PREVIEW_VIDEO_FRAME_ATTR,
|
|
PREVIEW_VIDEO_SOURCE_ATTR,
|
|
PREVIEW_VIDEO_CANVAS_ATTR,
|
|
STUDIO_PREVIEW_UPCOMING_ATTR,
|
|
STUDIO_PREVIEW_FAR_SCENE_ATTR,
|
|
STUDIO_PREVIEW_PARKED_SCENE_ATTR,
|
|
STUDIO_PREVIEW_LAZY_ATTR,
|
|
PREVIEW_RASTER_ATTR,
|
|
RUNTIME_STAMPED_ATTR,
|
|
PREVIEW_FILE_SIZE_ATTR,
|
|
] as const;
|
|
|
|
export const isClipHiddenByRuntime = (el: HTMLElement) =>
|
|
el.hasAttribute(CLIP_HIDDEN_ATTR) && el.style.visibility === "hidden";
|
|
|
|
export function applyAuthoredTiming(
|
|
el: Element,
|
|
attrs: ReadonlyArray<readonly [name: string, value: string]>,
|
|
): void {
|
|
for (const [name, value] of attrs) el.setAttribute(name, value);
|
|
el.removeAttribute(RUNTIME_STAMPED_ATTR);
|
|
}
|
|
|
|
export function dropServiceWorkers(): Promise<void> {
|
|
const workers = typeof navigator === "undefined" ? undefined : navigator.serviceWorker;
|
|
if (!workers) return Promise.resolve();
|
|
return workers
|
|
.getRegistrations()
|
|
.then((all) => Promise.all(all.map((each) => each.unregister())))
|
|
.then(
|
|
() => undefined,
|
|
() => undefined,
|
|
);
|
|
}
|
|
|
|
export function serviceWorkerScriptFetch(headers: {
|
|
serviceWorker?: string | string[];
|
|
fetchDest?: string | string[];
|
|
}): boolean {
|
|
return headers.serviceWorker === "script" || headers.fetchDest === "serviceworker";
|
|
}
|
|
|
|
export interface PreviewTarget {
|
|
hfId?: string;
|
|
domId?: string;
|
|
id: string;
|
|
sourceFile?: string;
|
|
}
|
|
|
|
export function previewElementSourceFile(el: Element): string | undefined {
|
|
const ownerRoot = el.parentElement?.closest("[data-composition-id]");
|
|
return (
|
|
ownerRoot?.getAttribute("data-composition-file") ??
|
|
ownerRoot?.getAttribute("data-composition-src") ??
|
|
undefined
|
|
);
|
|
}
|
|
|
|
export function previewElementFinder(
|
|
doc: Document,
|
|
selector = "[data-hf-id], [id]",
|
|
): (target: PreviewTarget) => Element | null {
|
|
const byKey = new Map<string, Element[]>();
|
|
const add = (key: string, node: Element) => {
|
|
const nodes = byKey.get(key);
|
|
if (nodes) nodes.push(node);
|
|
else byKey.set(key, [node]);
|
|
};
|
|
for (const node of doc.querySelectorAll(selector)) {
|
|
const hfId = node.getAttribute("data-hf-id");
|
|
const id = node.getAttribute("id");
|
|
if (hfId) add(`hf:${hfId}`, node);
|
|
if (id) add(`id:${id}`, node);
|
|
}
|
|
return (target) => {
|
|
const matches = [
|
|
...((target.hfId && byKey.get(`hf:${target.hfId}`)) || []),
|
|
...(byKey.get(`id:${target.domId ?? target.id}`) ?? []),
|
|
];
|
|
return (
|
|
matches.find((node) => previewElementSourceFile(node) === target.sourceFile) ??
|
|
matches[0] ??
|
|
null
|
|
);
|
|
};
|
|
}
|