Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions packages/studio/src/components/editor/useMotionPathData.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,78 @@ it("draws an axis the tween leaves alone where GSAP renders it, so the playhead
}
});

/** A frame whose window Studio cannot reach, as a sealed preview's. */
function sealedIframe(): HTMLIFrameElement {
const iframe = document.createElement("iframe");
const blocked = new Proxy(
{},
{
get: () => {
throw new DOMException("blocked", "SecurityError");
},
},
);
Object.defineProperty(iframe, "contentWindow", { value: blocked });
return iframe;
}

// fails without the fix: before a sealed frame's x/y read landed, the path drew its y axis at 0.
it("waits for a sealed frame's x/y before drawing the axis the tween leaves alone", () => {
runtime.read = {
keyframes: [
{ percentage: 66.667, properties: { x: 60 } },
{ percentage: 100, properties: { x: 120 } },
],
};
const iframe = sealedIframe();
let points: string | undefined;
function Probe() {
const ref = useRef(iframe);
points = useMotionPathData(ref, "#box").geometry?.points;
return null;
}
const root = createRoot(document.body.appendChild(document.createElement("div")));
try {
act(() => root.render(<Probe />));
expect(points).toBeUndefined();

runtime.gsapPosition = { x: 60, y: 30 };
act(() => vi.advanceTimersByTime(250));
expect(points).toBe("60,30 120,30");
} finally {
act(() => root.unmount());
}
});

// fails without the fix: a path kept while the read was pending stayed drawn after the selection moved on.
it("drops the kept path when another layer is selected before a sealed read lands", () => {
runtime.read = {
keyframes: [
{ percentage: 66.667, properties: { x: 60 } },
{ percentage: 100, properties: { x: 120 } },
],
};
runtime.gsapPosition = { x: 60, y: 30 };
const iframe = sealedIframe();
let points: string | undefined;
function Probe({ selector }: { selector: string }) {
const ref = useRef(iframe);
points = useMotionPathData(ref, selector).geometry?.points;
return null;
}
const root = createRoot(document.body.appendChild(document.createElement("div")));
try {
act(() => root.render(<Probe selector="#a" />));
expect(points).toBe("60,30 120,30");

runtime.gsapPosition = null;
act(() => root.render(<Probe selector="#b" />));
expect(points).toBeUndefined();
} finally {
act(() => root.unmount());
}
});

it("reads no layout per frame until there is a path or a create ring to draw", () => {
const iframe = document.createElement("iframe");
document.body.append(iframe);
Expand Down
11 changes: 10 additions & 1 deletion packages/studio/src/components/editor/useMotionPathData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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 { useGsapSnapshotVersion } from "../../hooks/gsapFrameSnapshot";
import { elementHome, transformWDivisor } from "@hyperframes/core/runtime/editing-port";
import { isElementVisibleForOverlay } from "./domEditOverlayGeometry";
import {
Expand All @@ -26,6 +27,9 @@ function rectsClose(a: Rect, b: Rect): boolean {

const sealedPlugins = new WeakMap<HTMLIFrameElement, boolean>();

const sealedReadPending = (iframe: HTMLIFrameElement | null) =>
!!iframe && previewFrameOpaque(iframe);

export function hasMotionPathPlugin(iframe: HTMLIFrameElement | null): boolean {
if (iframe && previewFrameOpaque(iframe)) return sealedPlugins.get(iframe) ?? false;
try {
Expand Down Expand Up @@ -84,6 +88,7 @@ export function useMotionPathData(
} {
const [rect, setRect] = useState<Rect | null>(null);
const [geometry, setGeometry] = useState<MotionPathGeometry | null>(null);
const snapshotVersion = useGsapSnapshotVersion();
const resolvedForRef = useRef<string | null>(null);
const geometryResolved = resolvedForRef.current === selector;
const [visibleInPreview, setVisibleInPreview] = useState(true);
Expand Down Expand Up @@ -180,6 +185,10 @@ export function useMotionPathData(
// Position-only: never let a co-located size/scale tween shadow the path.
const read = readRuntimeKeyframes(iframeRef.current, selector, undefined, ["x", "y"]);
const base = read ? readGsapPositionFromIframe(iframeRef.current, selector) : null;
if (read && !base && sealedReadPending(iframeRef.current)) {
if (resolvedForRef.current !== selector) setGeometry(null);
return;
}
const next = buildMotionPathGeometry(read, base ?? undefined);
setGeometry((prev) =>
prev?.kind === next?.kind &&
Expand All @@ -192,7 +201,7 @@ export function useMotionPathData(
recompute();
const id = window.setInterval(recompute, 250);
return () => window.clearInterval(id);
}, [selector, iframeRef]);
}, [selector, iframeRef, snapshotVersion]);

return { rect, geometry, geometryResolved, visibleInPreview, home, pScale };
}
Loading