fix(studio): the motion path draws and can be created in a sealed preview (#8108)

* test(studio): a sealed preview's motion path finds its layer's home

* fix(studio): the motion path draws and creates in a sealed preview

* test(core): a motion path's home counts a wrapper's 2D and 3D translate

* test(studio): a sealed click places a new motion path from the layer's home at the click

* test(studio): a sealed motion path refuses a bad frame reply and reads the blur root via the port

* fix(studio): a sealed click places a new motion path from the layer's home at the click

* refactor(core): a motion path home reads a matrix's translate in one small helper
This commit is contained in:
Miguel Ángel
2026-11-17 21:58:01 +00:00
committed by GitHub
parent 0b0108f2f6
commit 373d987e32
15 changed files with 517 additions and 69 deletions
@@ -0,0 +1,72 @@
import { afterEach, describe, expect, it } from "vitest";
import { elementHome, motionPathHome, transformWDivisor } from "./motionPathHome";
afterEach(() => {
document.body.innerHTML = "";
Reflect.deleteProperty(window, "MotionPathPlugin");
});
function define(el: HTMLElement, props: Record<string, unknown>) {
for (const [key, value] of Object.entries(props)) Object.defineProperty(el, key, { value });
}
/** A 100x50 layer inside a wrapper inside the composition root; jsdom lays out nothing, so the box is given by hand. */
function layer(wrapperTransform: string) {
document.body.innerHTML = `<div data-composition-id="main"><div id="wrap"><div id="box"></div></div></div>`;
const root = document.querySelector<HTMLElement>("[data-composition-id]")!;
const wrap = document.getElementById("wrap")!;
const box = document.getElementById("box")!;
wrap.style.transform = wrapperTransform;
define(box, {
offsetLeft: 10,
offsetTop: 20,
offsetWidth: 100,
offsetHeight: 50,
offsetParent: wrap,
});
define(wrap, { offsetLeft: 0, offsetTop: 0, offsetParent: root });
return box;
}
describe("elementHome", () => {
it.each([
["no transform", "none", 60, 45],
["a 2D matrix", "matrix(1, 0, 0, 1, 30, 40)", 90, 85],
["a 3D matrix", "matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 50, 60, 0, 1)", 110, 105],
])("adds a wrapper's translate from %s", (_name, transform, x, y) => {
const home = elementHome(layer(transform));
expect({ x: home.x, y: home.y, w: home.w, h: home.h }).toEqual({ x, y, w: 100, h: 50 });
});
});
describe("transformWDivisor", () => {
it("is the element's own m44, and 1 without a 3D transform", () => {
const box = layer("none");
expect(transformWDivisor(box)).toBe(1);
box.style.transform = "matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2)";
expect(transformWDivisor(box)).toBe(2);
});
});
describe("motionPathHome", () => {
it("answers no home for a selector that matches nothing", () => {
expect(motionPathHome("#missing")).toEqual({
home: null,
perspective: 1,
visible: true,
plugin: false,
});
});
it("answers the first match's home and whether the frame loaded MotionPathPlugin", () => {
layer("none");
Object.assign(window, { MotionPathPlugin: {} });
const answer = motionPathHome("#box");
expect(answer.plugin).toBe(true);
expect(answer.home).toMatchObject({ x: 60, y: 45, w: 100, h: 50 });
expect(answer.perspective).toBe(1);
});
});
@@ -1,20 +1,21 @@
import type { MotionPathHome } from "./motionPathGeometry";
import { isHtmlElement } from "../domRealm";
import { isElementVisibleThroughAncestors } from "../editingGeometry";
/** The layer's centre with no animation, its size, and how far its centre moves per px it grows. */
export type MotionPathHome = { x: number; y: number; w: number; h: number; ax: number; ay: number };
const computedTransform = (el: HTMLElement): string =>
el.ownerDocument?.defaultView?.getComputedStyle(el).transform ?? "none";
function matrixTranslate(transform: string): [string | undefined, string | undefined] {
const match = /(matrix3d|matrix)\(([^)]+)\)/.exec(transform);
const values = match?.[2]?.split(",") ?? [];
return match?.[1] === "matrix3d" ? [values[12], values[13]] : [values[4], values[5]];
}
// An element's computed transform translate: a group wrapper GSAP moved carries its offset here.
function transformTranslate(el: HTMLElement): { x: number; y: number } {
const t = el.ownerDocument?.defaultView?.getComputedStyle(el).transform;
if (!t || t === "none") return { x: 0, y: 0 };
const m3 = t.match(/matrix3d\(([^)]+)\)/);
if (m3) {
const v = m3[1].split(",").map(Number);
return { x: v[12] || 0, y: v[13] || 0 };
}
const m = t.match(/matrix\(([^)]+)\)/);
if (m) {
const v = m[1].split(",").map(Number);
return { x: v[4] || 0, y: v[5] || 0 };
}
return { x: 0, y: 0 };
const [x, y] = matrixTranslate(computedTransform(el));
return { x: Number(x) || 0, y: Number(y) || 0 };
}
function cssTranslate(el: HTMLElement): [number, number] {
@@ -73,3 +74,32 @@ export function elementHome(el: HTMLElement): MotionPathHome {
const { ax, ay } = growShares(el, px, py);
return { x, y, w: el.offsetWidth, h: el.offsetHeight, ax, ay };
}
// A depth element (matrix3d m44) renders 1/m44 larger, so its path's offsets travel 1/m44 further on screen.
export function transformWDivisor(el: HTMLElement): number {
const t = el.ownerDocument?.defaultView?.getComputedStyle(el).transform;
if (!t || !t.startsWith("matrix3d(")) return 1;
const v = t.slice("matrix3d(".length, -1).split(",");
const w = Number.parseFloat(v[15] ?? "");
return Number.isFinite(w) && w > 0 ? w : 1;
}
export interface MotionPathFrameHome {
home: MotionPathHome | null;
/** 1/m44 of the element's own transform: how much a depth element magnifies its path. */
perspective: number;
visible: boolean;
plugin: boolean;
}
export function motionPathHome(selector: string): MotionPathFrameHome {
const plugin = Boolean((window as { MotionPathPlugin?: unknown }).MotionPathPlugin);
const element = document.querySelector(selector);
if (!isHtmlElement(element)) return { home: null, perspective: 1, visible: true, plugin };
return {
home: elementHome(element),
perspective: 1 / transformWDivisor(element),
visible: isElementVisibleThroughAncestors(element),
plugin,
};
}
@@ -196,6 +196,7 @@ describe("installEditingAgent", () => {
"set-audio-group-attribute",
"assign-audio-group",
"gsap-snapshot",
"motion-path-home",
],
},
});
@@ -36,6 +36,7 @@ import { restoreAttributes } from "./editing/restoreAttributes";
import { stopWatchingGeometry, watchGeometry } from "./editing/watchGeometry";
import { gestureStart } from "./editingGestureStart";
import { gsapSnapshot } from "./editing/gsapSnapshot";
import { motionPathHome } from "./editing/motionPathHome";
import {
getLogicalOpacity,
markedForStudio,
@@ -212,6 +213,7 @@ const HANDLERS: Handlers = {
"set-audio-group-attribute": setAudioGroupAttribute,
"assign-audio-group": (params) => assignAudioGroup(params, elementForHandle),
"gsap-snapshot": () => gsapSnapshot(),
"motion-path-home": ({ selector }) => motionPathHome(selector),
};
function serve(port: MessagePort): void {
@@ -89,6 +89,7 @@ describe("createEditingPortClient", () => {
"set-audio-group-attribute",
"assign-audio-group",
"gsap-snapshot",
"motion-path-home",
],
});
} finally {
+9
View File
@@ -27,6 +27,7 @@ import {
import type { EditingElementBox, EditingElementGeometry } from "./editingGeometry";
import type { EditingGestureStart } from "./editingGestureStart";
import type { GsapSnapshot } from "./editing/gsapSnapshot";
import type { MotionPathFrameHome } from "./editing/motionPathHome";
import type { EditingLayer } from "./editingLayers";
import type { RuntimeTimelineMessage } from "./types";
import { wallClearTimeout, wallSetTimeout } from "./wallClock";
@@ -166,6 +167,7 @@ export const EDITING_REQUEST_KINDS = [
"set-audio-group-attribute",
"assign-audio-group",
"gsap-snapshot",
"motion-path-home",
] as const;
export type EditingRequestKind = (typeof EDITING_REQUEST_KINDS)[number];
@@ -293,6 +295,7 @@ export interface EditingRequests {
result: { previous: Array<string | null>; changed: boolean };
};
"gsap-snapshot": { params: NoParams; result: GsapSnapshot };
"motion-path-home": { params: { selector: string }; result: MotionPathFrameHome };
}
export type EditingParams<K extends EditingRequestKind> = EditingRequests[K]["params"];
export type EditingResult<K extends EditingRequestKind> = EditingRequests[K]["result"];
@@ -491,6 +494,10 @@ const PARAM_PARSERS: {
"set-audio-group-attribute": parseSetAudioGroupAttribute,
"assign-audio-group": parseAssignAudioGroup,
"gsap-snapshot": noParams,
"motion-path-home": (params) =>
hasKeys(params, "selector") && isSelector(params.selector)
? { selector: params.selector }
: null,
};
/** Validates one request off the wire. Never throws: anything unusable becomes a typed error. */
@@ -695,3 +702,5 @@ export function createEditingPortClient(options: {
},
};
}
export { elementHome, transformWDivisor, type MotionPathHome } from "./editing/motionPathHome";
@@ -0,0 +1,85 @@
// @vitest-environment jsdom
import { act } from "react";
import { createRoot } from "react-dom/client";
import { expect, it, vi } from "vitest";
import { MotionPathOverlay } from "./MotionPathOverlay";
import { usePlayerStore } from "../../player/store/playerStore";
import { usePreviewIframeStore } from "../../player/store/previewIframeStore";
import type { DomEditSelection } from "./domEditing";
import { commitCreatePath } from "./motionPathCommit";
Reflect.set(globalThis, "IS_REACT_ACT_ENVIRONMENT", true);
const POLLED = { x: 0, y: 0, w: 100, h: 50, ax: 0.5, ay: 0.5 };
const AT_CLICK = { x: 500, y: 300, w: 100, h: 50, ax: 0.5, ay: 0.5 };
vi.mock("../../contexts/DomEditContext", () => ({
useDomEditContext: () => ({ selectedGsapAnimations: [] }),
}));
vi.mock("../../player/lib/previewChannel", async (importOriginal) => ({
...(await importOriginal<typeof import("../../player/lib/previewChannel")>()),
previewFrameOpaque: () => true,
}));
vi.mock("./motionPathCommit", async (importOriginal) => ({
...(await importOriginal<typeof import("./motionPathCommit")>()),
commitCreatePath: vi.fn(),
}));
vi.mock("./motionPathSelection", () => ({
selectorFor: () => "#box",
editableAnimationId: () => null,
}));
vi.mock("./useMotionPathData", async (importOriginal) => ({
...(await importOriginal<typeof import("./useMotionPathData")>()),
hasMotionPathPlugin: () => true,
sealedMotionPathHome: vi.fn(async () => ({
home: AT_CLICK,
perspective: 1,
visible: true,
plugin: true,
})),
useMotionPathData: () => ({
rect: { left: 0, top: 0, width: 1920, height: 1080 },
geometry: null,
geometryResolved: true,
visibleInPreview: true,
home: POLLED,
pScale: 1,
}),
}));
// fails without the fix: a sealed click placed the path from the last poll's home, maybe the previous playhead's.
it("places a sealed preview's new path from the layer's home read at the click", async () => {
const host = document.createElement("div");
const surface = document.createElement("div");
surface.setAttribute("data-preview-pan-surface", "");
const frame = document.createElement("iframe");
document.body.append(host, surface, frame);
frame.getBoundingClientRect = () => new DOMRect(0, 0, 1920, 1080);
const root = createRoot(host);
const selection = { element: document.createElement("div") } as unknown as DomEditSelection;
try {
act(() => usePreviewIframeStore.getState().setIframe(frame));
act(() =>
root.render(
<MotionPathOverlay
iframeRef={{ current: frame }}
selection={selection}
compositionSize={{ width: 1920, height: 1080 }}
isPlaying={false}
/>,
),
);
act(() => usePlayerStore.getState().setMotionPathArmed(true));
const press = { bubbles: true, button: 0, clientX: 600, clientY: 400 };
await act(async () => void surface.dispatchEvent(new PointerEvent("pointerdown", press)));
expect(vi.mocked(commitCreatePath).mock.calls[0]?.slice(0, 4)).toEqual(["#box", 0, 100, 100]);
} finally {
act(() => usePlayerStore.getState().setMotionPathArmed(false));
act(() => root.unmount());
usePreviewIframeStore.setState({ iframe: null });
host.remove();
surface.remove();
frame.remove();
}
});
@@ -7,7 +7,12 @@ import { useGsapInteractionFailureTelemetry } from "../../hooks/useGsapInteracti
import { usePlayerStore } from "../../player/store/playerStore";
import { useLivePreviewIframe } from "../../player/store/previewIframeStore";
import { parkPlayheadOnKeyframe } from "../../hooks/gsapDragCommit";
import { nearestPointOnPath, nodeCentre, type MotionNodeRef } from "./motionPathGeometry";
import {
nearestPointOnPath,
nodeCentre,
type MotionNodeRef,
type MotionPathHome,
} from "./motionPathGeometry";
import { editableAnimationId, selectorFor } from "./motionPathSelection";
import { controlForNode, controlUnder, pressControl } from "./motionPathLayerNode";
import { readGsapPositionFromIframe } from "../../hooks/gsapPositionDetection";
@@ -26,8 +31,9 @@ import {
commitRemoveWaypoint,
nodeDropLabel,
} from "./motionPathCommit";
import { elementHome } from "./motionPathHome";
import { hasMotionPathPlugin, transformWDivisor, useMotionPathData } from "./useMotionPathData";
import { elementHome, transformWDivisor } from "@hyperframes/core/runtime/editing-port";
import { hasMotionPathPlugin, sealedMotionPathHome, useMotionPathData } from "./useMotionPathData";
import { previewFrameOpaque } from "../../player/lib/previewChannel";
interface MotionPathOverlayProps {
iframeRef: RefObject<HTMLIFrameElement | null>;
@@ -196,20 +202,28 @@ export const MotionPathOverlay = memo(function MotionPathOverlay({
}
// Resolve the element LIVE from the current iframe document — the selected
// node may be detached after a soft-reload, which would skew home.
const live = frame.contentDocument?.querySelector(createSelector);
if (!isHtmlElement(live)) return;
const sealed = previewFrameOpaque(frame);
const live = sealed ? null : frame.contentDocument?.querySelector(createSelector);
if (!isHtmlElement(live) && !(sealed && home)) return;
e.stopPropagation();
e.preventDefault();
const sc = r.width / compW;
const elHome = elementHome(live);
const t = Math.round(usePlayerStore.getState().currentTime * 100) / 100;
// De-magnify: the click lands on the projected (1/m44-magnified) path, so
// divide the home-relative offset by the perspective factor to recover the
// stored composition offset (inverse of the `* pScale` applied at draw).
const ps = 1 / transformWDivisor(live);
const px = Math.round(((e.clientX - r.left) / sc - elHome.x) / ps);
const py = Math.round(((e.clientY - r.top) / sc - elHome.y) / ps);
const t = Math.round(usePlayerStore.getState().currentTime * 100) / 100;
void commitCreatePath(createSelector, t, px, py, commitMutation);
const place = (elHome: MotionPathHome, ps: number) => {
const px = Math.round(((e.clientX - r.left) / sc - elHome.x) / ps);
const py = Math.round(((e.clientY - r.top) / sc - elHome.y) / ps);
void commitCreatePath(createSelector, t, px, py, commitMutation);
};
if (isHtmlElement(live)) place(elementHome(live), 1 / transformWDivisor(live));
else {
// A frame that cannot answer at the click falls back to the polled home, which may be a seek behind.
void sealedMotionPathHome(frame, createSelector).then((read) =>
place(read?.home ?? home!, read?.home ? read.perspective : pScale),
);
}
setMotionPathArmed(false);
};
surface.addEventListener("pointerdown", onDown, true);
@@ -217,7 +231,7 @@ export const MotionPathOverlay = memo(function MotionPathOverlay({
surface.removeEventListener("pointerdown", onDown, true);
surface.style.cursor = prevCursor;
};
}, [armed, createSelector, compW, iframeRef, commitMutation, setMotionPathArmed]);
}, [armed, createSelector, compW, iframeRef, commitMutation, setMotionPathArmed, home, pScale]);
if (!rect || rect.width <= 0 || !compositionSize || compositionSize.width <= 0) return null;
// Hide the whole overlay (path + create hint) when the element isn't painted —
@@ -0,0 +1,40 @@
// @vitest-environment happy-dom
import { afterEach, expect, it, vi } from "vitest";
const request = vi.hoisted(() => vi.fn());
vi.mock("../../player/lib/previewChannel", async (importOriginal) => ({
...(await importOriginal<typeof import("../../player/lib/previewChannel")>()),
previewRequest: request,
}));
import { sealedMotionPathHome } from "./useMotionPathData";
const HOME = { x: 60, y: 45, w: 100, h: 50, ax: 0.5, ay: 0.5 };
afterEach(() => request.mockReset());
it("holds a well-formed home from the frame", async () => {
request.mockResolvedValue({ home: HOME, perspective: 2, visible: true, plugin: true });
const read = await sealedMotionPathHome(document.createElement("iframe"), "#box");
expect(read).toEqual({ home: HOME, perspective: 2, visible: true, plugin: true });
});
it.each([
["a field that is text", { ...HOME, x: "60" }],
["a field that is not finite", { ...HOME, y: Number.NaN }],
["an endless size", { ...HOME, w: Number.POSITIVE_INFINITY }],
])("counts a home with %s as no home", async (_name, home) => {
request.mockResolvedValue({ home, perspective: Number.NaN, visible: true, plugin: true });
const read = await sealedMotionPathHome(document.createElement("iframe"), "#box");
expect(read).toEqual({ home: null, perspective: 1, visible: true, plugin: true });
});
it("answers nothing for a reply that is off its shape", async () => {
request.mockResolvedValue({ home: HOME, perspective: 1, visible: "yes", plugin: true });
expect(await sealedMotionPathHome(document.createElement("iframe"), "#box")).toBeNull();
});
@@ -0,0 +1,84 @@
// @vitest-environment happy-dom
import { act, createElement, useRef } from "react";
import { createRoot } from "react-dom/client";
import { afterEach, beforeEach, expect, it, vi } from "vitest";
// The frame's real runtime agent, serving the opaque frame from this document.
import { installMarkedEditingAgent } from "../../../../core/src/runtime/editingAgent.test-helpers";
import { detachedFakeIframe } from "../../player/hooks/timelinePlayerTestHarness";
import { usePlayerStore } from "../../player/store/playerStore";
import { resetOverlayFrameLoopForTests } from "./overlayFrameLoop";
import { useMotionPathData } from "./useMotionPathData";
Reflect.set(globalThis, "IS_REACT_ACT_ENVIRONMENT", true);
const closers: Array<() => void> = [];
const originalRaf = window.requestAnimationFrame;
let frames: Array<() => void> = [];
const runFrames = () =>
act(() => {
const batch = frames;
frames = [];
for (const frame of batch) frame();
});
beforeEach(() => {
window.requestAnimationFrame = ((callback: FrameRequestCallback) =>
frames.push(() => callback(performance.now()))) as typeof window.requestAnimationFrame;
// Armed, so the hook measures the layer before there is a path to draw.
usePlayerStore.setState({ previewBooted: true, motionPathArmed: true });
});
afterEach(() => {
closers.splice(0).forEach((close) => close());
resetOverlayFrameLoopForTests();
window.requestAnimationFrame = originalRaf;
usePlayerStore.setState({ previewBooted: false, motionPathArmed: false });
document.body.innerHTML = "";
});
/** A frame Studio cannot read, whose runtime is the real editing agent over this document. */
function opaqueFrame() {
const frame: Record<string, unknown> = {};
const iframe = detachedFakeIframe(frame);
Object.defineProperty(iframe, "contentDocument", { value: null });
Object.defineProperty(frame, "document", {
get: () => {
throw new DOMException("blocked", "SecurityError");
},
});
const parent = {
postMessage: (data: unknown, _origin: string, ports: MessagePort[]) =>
window.dispatchEvent(
new MessageEvent("message", { source: frame as unknown as Window, data, ports }),
),
} as unknown as Window;
const agent = installMarkedEditingAgent(parent);
frame.postMessage = (data: unknown) =>
window.dispatchEvent(new MessageEvent("message", { source: parent, data }));
closers.push(() => agent.dispose());
return iframe;
}
// fails without the fix: the hook read the layer from the frame's document, which a sealed frame keeps.
it("finds a sealed preview's layer home, so its motion path draws from the layer", async () => {
document.body.innerHTML = `<main data-composition-id="main"><div id="box"></div></main>`;
const box = document.getElementById("box")!;
const layout = { offsetLeft: 100, offsetTop: 50, offsetWidth: 200, offsetHeight: 100 };
for (const [key, value] of Object.entries(layout)) Object.defineProperty(box, key, { value });
const iframe = opaqueFrame();
let home: unknown = null;
function Probe() {
const ref = useRef(iframe);
home = useMotionPathData(ref, "#box").home;
return null;
}
const root = createRoot(document.body.appendChild(document.createElement("div")));
try {
act(() => root.render(createElement(Probe)));
await vi.waitFor(() => {
runFrames();
expect(home).toEqual({ x: 200, y: 100, w: 200, h: 100, ax: 0, ay: 0 });
});
} finally {
act(() => root.unmount());
}
});
@@ -3,7 +3,7 @@ import { gsap } from "gsap";
import { act, useRef } from "react";
import { createRoot } from "react-dom/client";
import { afterEach, beforeEach, expect, it, vi } from "vitest";
import { elementHome } from "./motionPathHome";
import { elementHome } from "@hyperframes/core/runtime/editing-port";
import { useMotionPathData } from "./useMotionPathData";
import { resetOverlayFrameLoopForTests } from "./overlayFrameLoop";
import { usePlayerStore } from "../../player/store/playerStore";
@@ -2,32 +2,19 @@ import { useEffect, useRef, useState, type RefObject } from "react";
import { isHtmlElement } from "@hyperframes/core/runtime/dom-realm";
import { readRuntimeKeyframes } from "../../hooks/gsapRuntimeKeyframes";
import { readGsapPositionFromIframe } from "../../hooks/gsapPositionDetection";
import { elementHome, transformWDivisor } from "@hyperframes/core/runtime/editing-port";
import { isElementVisibleForOverlay } from "./domEditOverlayGeometry";
import {
buildMotionPathGeometry,
type MotionPathGeometry,
type MotionPathHome,
} from "./motionPathGeometry";
import { elementHome } from "./motionPathHome";
import { subscribeOverlayFrame } from "./overlayFrameLoop";
import { usePlayerStore } from "../../player/store/playerStore";
import { previewFrameOpaque, previewRequest } from "../../player/lib/previewChannel";
type Rect = { left: number; top: number; width: number; height: number };
// Perspective foreshortening of the element's OWN transform (matrix3d m44). A
// depth element (translateZ toward the viewer) renders 1/m44× larger, so its
// animated x/y offsets travel 1/m44× further on screen than the flat preview
// scale implies. Returns 1 for 2D transforms. The motion path magnifies its
// offset points by 1/m44 (and de-magnifies pointer→offset) so the drawn path and
// its draggable nodes track the projected element instead of drifting off it.
export function transformWDivisor(el: HTMLElement): number {
const t = el.ownerDocument?.defaultView?.getComputedStyle(el).transform;
if (!t || !t.startsWith("matrix3d(")) return 1;
const v = t.slice("matrix3d(".length, -1).split(",");
const w = Number.parseFloat(v[15] ?? "");
return Number.isFinite(w) && w > 0 ? w : 1;
}
function rectsClose(a: Rect, b: Rect): boolean {
return (
Math.abs(a.left - b.left) < 0.5 &&
@@ -37,7 +24,10 @@ function rectsClose(a: Rect, b: Rect): boolean {
);
}
const sealedPlugins = new WeakMap<HTMLIFrameElement, boolean>();
export function hasMotionPathPlugin(iframe: HTMLIFrameElement | null): boolean {
if (iframe && previewFrameOpaque(iframe)) return sealedPlugins.get(iframe) ?? false;
try {
return Boolean(
(iframe?.contentWindow as unknown as { MotionPathPlugin?: unknown })?.MotionPathPlugin,
@@ -47,6 +37,40 @@ export function hasMotionPathPlugin(iframe: HTMLIFrameElement | null): boolean {
}
}
type SealedHome = { visible: boolean; home: MotionPathHome | null; perspective: number };
const sameHome = (a: MotionPathHome, b: MotionPathHome) =>
Math.abs(a.x - b.x) < 0.5 &&
Math.abs(a.y - b.y) < 0.5 &&
a.w === b.w &&
a.h === b.h &&
a.ax === b.ax &&
a.ay === b.ay;
const HOME_FIELDS = ["x", "y", "w", "h", "ax", "ay"] as const;
/** The reply is frame data, so its shape is checked. */
function readHome(value: unknown): MotionPathHome | null {
if (typeof value !== "object" || value === null) return null;
const fields = value as Record<string, unknown>;
const ok = HOME_FIELDS.every((name) => Number.isFinite(fields[name]));
return ok ? (Object.fromEntries(HOME_FIELDS.map((n) => [n, fields[n]])) as MotionPathHome) : null;
}
/** Where a sealed frame's first match of `selector` draws its path from; null when it cannot say. */
export async function sealedMotionPathHome(
iframe: HTMLIFrameElement,
selector: string,
): Promise<(SealedHome & { plugin: boolean }) | null> {
const reply = await previewRequest(iframe, "motion-path-home", { selector });
if (!reply || typeof reply.plugin !== "boolean" || typeof reply.visible !== "boolean")
return null;
sealedPlugins.set(iframe, reply.plugin);
const perspective =
Number.isFinite(reply.perspective) && reply.perspective > 0 ? reply.perspective : 1;
return { visible: reply.visible, home: readHome(reply.home), perspective, plugin: reply.plugin };
}
export function useMotionPathData(
iframeRef: RefObject<HTMLIFrameElement | null>,
selector: string | null,
@@ -91,6 +115,10 @@ export function useMotionPathData(
height: r.height,
};
setRect((prev) => (prev && rectsClose(prev, next) ? prev : next));
if (previewFrameOpaque(el)) {
askSealed(el);
return;
}
let target: Element | null = null;
try {
target = el.contentDocument?.querySelector(selector) ?? null;
@@ -98,29 +126,51 @@ export function useMotionPathData(
/* cross-origin guard */
}
const live = isHtmlElement(target) ? target : null;
const vis = live ? isElementVisibleForOverlay(live) : true;
setVisibleInPreview((prev) => (prev === vis ? prev : vis));
if (live) {
const h = elementHome(live);
setHome((prev) =>
prev &&
Math.abs(prev.x - h.x) < 0.5 &&
Math.abs(prev.y - h.y) < 0.5 &&
prev.w === h.w &&
prev.h === h.h &&
prev.ax === h.ax &&
prev.ay === h.ay
? prev
: h,
);
const ps = 1 / transformWDivisor(live);
setPScale((p) => (Math.abs(p - ps) < 0.001 ? p : ps));
if (!live) apply({ visible: true, home: null, perspective: 1 });
else {
const visible = isElementVisibleForOverlay(live);
apply({ visible, home: elementHome(live), perspective: 1 / transformWDivisor(live) });
}
}
};
return subscribeOverlayFrame(tick);
const apply = ({ visible, home: h, perspective: ps }: SealedHome) => {
setVisibleInPreview((prev) => (prev === visible ? prev : visible));
if (!h) return;
setHome((prev) => (prev && sameHome(prev, h) ? prev : h));
setPScale((p) => (Math.abs(p - ps) < 0.001 ? p : ps));
};
// One request at a time; a tick while it is out skips, the next one after it asks again.
let asking = false;
let stopped = false;
const askSealed = (frame: HTMLIFrameElement) => {
if (asking) return;
asking = true;
void sealedMotionPathHome(frame, selector).then((reply) => {
asking = false;
if (reply && !stopped) apply(reply);
});
};
const stop = subscribeOverlayFrame(tick);
return () => {
stopped = true;
stop();
};
}, [selector, iframeRef, drawn]);
// A sealed frame says whether it has the plugin only when asked; re-render once it has.
const [, notePlugin] = useState(false);
useEffect(() => {
const frame = iframeRef.current;
if (!selector || !frame || !previewFrameOpaque(frame)) return;
let current = true;
void sealedMotionPathHome(frame, selector).then((reply) => {
if (current && reply) notePlugin(reply.plugin);
});
return () => {
current = false;
};
}, [selector, iframeRef]);
useEffect(() => {
if (!selector) {
setGeometry(null);
@@ -0,0 +1,30 @@
// @vitest-environment happy-dom
import { MOTION_BLUR_ATTR } from "@hyperframes/core/motion-blur-settings";
import { expect, it, vi } from "vitest";
const SHUTTER = Object.values(MOTION_BLUR_ATTR)[0]!;
vi.mock("../../player/lib/previewChannel", async (importOriginal) => ({
...(await importOriginal<typeof import("../../player/lib/previewChannel")>()),
previewFrameOpaque: () => true,
}));
vi.mock("../../player/lib/previewElements", () => ({
queryPreviewElements: vi.fn(async () => ({ matches: [{ handle: "0123456789abcdef:1" }] })),
describePreviewElements: vi.fn(async () => [
{
attributes: [
["data-composition-id", "main"],
[SHUTTER, "0.5"],
],
},
]),
}));
import { readPreviewRootAttributes } from "./motionBlurPanelModel";
it("reads a sealed preview's root blur settings through the editing port, and only those", async () => {
const iframe = document.createElement("iframe");
expect(readPreviewRootAttributes(iframe)).toBeNull();
await vi.waitFor(() => expect(readPreviewRootAttributes(iframe)).toEqual({ [SHUTTER]: "0.5" }));
});
@@ -8,6 +8,8 @@ import {
motionBlurAttributeEdits,
readMotionBlurSettings,
} from "@hyperframes/core/motion-blur-settings";
import { previewFrameOpaque } from "../../player/lib/previewChannel";
import { describePreviewElements, queryPreviewElements } from "../../player/lib/previewElements";
export interface CompositionRoot {
id: string;
@@ -50,20 +52,48 @@ export function withEdits(
/** The blur attributes on the live preview's composition root: what actually runs, after an undo or an outside edit too. */
export function readPreviewRootAttributes(iframe: HTMLIFrameElement | null): RootAttributes | null {
if (iframe && previewFrameOpaque(iframe)) {
void askSealedRoot(iframe);
return sealedRoots.get(iframe) ?? null;
}
try {
const root = iframe?.contentDocument?.querySelector("[data-composition-id]");
if (!root) return null;
const found: Record<string, string> = {};
for (const name of Object.values(MOTION_BLUR_ATTR)) {
const value = root.getAttribute(name);
if (value !== null) found[name] = value;
}
return found;
return root ? blurAttributes((name) => root.getAttribute(name)) : null;
} catch {
return null;
}
}
function blurAttributes(read: (name: string) => string | null | undefined): RootAttributes {
const found: Record<string, string> = {};
for (const name of Object.values(MOTION_BLUR_ATTR)) {
const value = read(name);
if (typeof value === "string") found[name] = value;
}
return found;
}
const sealedRoots = new WeakMap<HTMLIFrameElement, RootAttributes>();
const askingRoot = new WeakSet<HTMLIFrameElement>();
/** A sealed frame's root, asked one request at a time; the polled read returns the last answer. */
async function askSealedRoot(iframe: HTMLIFrameElement): Promise<void> {
if (askingRoot.has(iframe)) return;
askingRoot.add(iframe);
try {
const [root] = (await queryPreviewElements(iframe, "[data-composition-id]"))?.matches ?? [];
const [found] = root ? ((await describePreviewElements(iframe, [root.handle])) ?? []) : [];
if (!found) sealedRoots.delete(iframe);
else
sealedRoots.set(
iframe,
blurAttributes((name) => new Map(found.attributes).get(name)),
);
} finally {
askingRoot.delete(iframe);
}
}
export const sameAttributes = (a: RootAttributes | null, b: RootAttributes | null): boolean =>
a === b ||
(a !== null && b !== null && Object.keys({ ...a, ...b }).every((name) => a[name] === b[name]));
@@ -21,7 +21,7 @@ try {
"bun",
[
"build",
"packages/studio/src/components/editor/motionPathHome.ts",
"packages/core/src/runtime/editing/motionPathHome.ts",
"--target",
"browser",
"--outdir",