Files
hyperframes/packages/studio/src/utils/studioPreviewHelpers.ts
T
James RussoandClaude Opus 5.5 7e04f3cce8 feat(studio): canvas click, hover and timeline select read an opaque preview over its channel (#7637)
* feat(studio): canvas click, hover and timeline select read an opaque preview over its channel

The runtime answers a new pointer-targets request with the canvas pick and its
cycle stack; the hit-test moves to core so Studio picks by the same rule on a
frame it can script. A data: value under the description limit now arrives
whole, so a small data: image is selectable on an opaque frame. Drill-in,
hover, timeline clips known only by clip id, and the stand-in attribute reads
in double-click, ungroup, path-offset and the agent selection context work
there too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(core): raise the core unpacked size budget for the pointer-targets hit-test

The hit-test moves from Studio into core's editing runtime, which core ships
in its runtime bundles; CI measured the unpacked tarball at 4,329,030 bytes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 04:16:20 +00:00

107 lines
3.5 KiB
TypeScript

import {
compositionPoint,
coversComposition,
pointerTarget,
pointerTargets,
} from "@hyperframes/core/runtime/editing-port";
// One canvas hit-test for Studio and the runtime that answers it for a frame Studio cannot script.
export { coversComposition };
/** The element a press at a client point picks in a frame Studio can script, or null. */
export function getPreviewTargetFromPointer(
iframe: HTMLIFrameElement,
clientX: number,
clientY: number,
): HTMLElement | null {
const local = framePointer(iframe, clientX, clientY);
return local && pointerTarget(local.doc, local.point);
}
/** Returns all independently-selectable elements at the pointer (topmost first). */
export function getAllPreviewTargetsFromPointer(
iframe: HTMLIFrameElement,
clientX: number,
clientY: number,
): HTMLElement[] {
const local = framePointer(iframe, clientX, clientY);
return local ? pointerTargets(local.doc, local.point) : [];
}
/** Where a client point falls in `iframe`, as fractions of its box; the frame maps them onto its composition. */
export function framePointFraction(iframe: HTMLIFrameElement, clientX: number, clientY: number) {
const rect = iframe.getBoundingClientRect();
return { x: (clientX - rect.left) / rect.width, y: (clientY - rect.top) / rect.height };
}
function framePointer(iframe: HTMLIFrameElement, clientX: number, clientY: number) {
let doc: Document | null = null;
try {
doc = iframe.contentDocument;
} catch {
return null;
}
if (!doc?.defaultView) return null;
const { x, y } = framePointFraction(iframe, clientX, clientY);
const point = compositionPoint(doc, x, y);
return point && { doc, point };
}
function objectLike(value: unknown): object | null {
return value && (typeof value === "object" || typeof value === "function") ? value : null;
}
function callPlaybackMethod(target: object | null, key: string): void {
const method = target ? Reflect.get(target, key) : null;
if (typeof method !== "function") return;
try {
method.call(target);
} catch {
// Best-effort playback freeze; drag should still work if playback control is unavailable.
}
}
function readPlaybackTime(target: object | null, key: string): number | null {
const method = target ? Reflect.get(target, key) : null;
if (typeof method !== "function") return null;
try {
const value = method.call(target);
return typeof value === "number" && Number.isFinite(value) ? value : null;
} catch {
return null;
}
}
export function pauseStudioPreviewPlayback(iframe: HTMLIFrameElement | null): number | null {
const win = iframe?.contentWindow;
if (!win) return null;
try {
const player = objectLike(Reflect.get(win, "__player"));
const playerPausedTime = readPlaybackTime(player, "getTime");
const playerPause = player ? Reflect.get(player, "pause") : null;
if (typeof playerPause === "function") {
callPlaybackMethod(player, "pause");
return playerPausedTime;
}
let pausedTime: number | null = null;
const timeline = objectLike(Reflect.get(win, "__timeline"));
pausedTime = pausedTime ?? readPlaybackTime(timeline, "time");
callPlaybackMethod(timeline, "pause");
const timelines = objectLike(Reflect.get(win, "__timelines"));
if (timelines) {
for (const value of Object.values(timelines)) {
const timelineRecord = objectLike(value);
pausedTime = pausedTime ?? readPlaybackTime(timelineRecord, "time");
callPlaybackMethod(timelineRecord, "pause");
}
}
return pausedTime;
} catch {
return null;
}
}