mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 11:26:02 +00:00
fix(studio): the property panel shows a sealed preview's animated values (#8203)
* fix(studio): the property panel shows a sealed preview's animated values * fix(studio): a sealed preview's panel shows rotation and scale from tweens that only turn or grow
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { useGsapSnapshotVersion } from "../../hooks/gsapFrameSnapshot";
|
||||
import { scopedElementKey } from "../../hooks/gsapKeyframeCacheHelpers";
|
||||
import { memo, useMemo, useRef, useState } from "react";
|
||||
import { Move } from "../../icons/SystemIcons";
|
||||
@@ -134,13 +135,14 @@ export const PropertyPanel = memo(function PropertyPanel(props: PropertyPanelPro
|
||||
gsapAnimations?.[0]?.id ??
|
||||
null)
|
||||
: null;
|
||||
const sealedReads = useGsapSnapshotVersion();
|
||||
const gsapRuntimeValues = useMemo(
|
||||
() =>
|
||||
element
|
||||
? readGsapRuntimeValuesForPanel(gsapAnimIdForMemo, gsapAnimations, element, iframeRef)
|
||||
: null,
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps -- iframeRef is stable; currentTime drives re-reads during playback
|
||||
[gsapAnimIdForMemo, gsapAnimations, element, currentTime],
|
||||
[gsapAnimIdForMemo, gsapAnimations, element, currentTime, sealedReads],
|
||||
);
|
||||
const gsapBorderRadius = useMemo(
|
||||
() =>
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { gsap } from "gsap";
|
||||
import { parseGsapScriptAcorn } from "@hyperframes/parsers/gsap-parser-acorn";
|
||||
import { afterEach, expect, it } from "vitest";
|
||||
import { makeSelection } from "../../hooks/domSelectionTestHarness";
|
||||
import { settleGsapSnapshot } from "../../hooks/gsapFrameSnapshot";
|
||||
import { describePreviewElements, queryPreviewElements } from "../../player/lib/previewElements";
|
||||
import { usePlayerStore } from "../../player/store/playerStore";
|
||||
import { sealedPreviewFrame } from "../../player/lib/sealedPreviewFrame.test-helpers";
|
||||
import { standInFor } from "./previewSelection";
|
||||
import { readGsapRuntimeValuesForPanel } from "./propertyPanelHelpers";
|
||||
|
||||
const script = (vars: string) => `var tl = gsap.timeline({ paused: true });
|
||||
tl.to("#box", { ${vars}, duration: 1, ease: "none" }, 0);
|
||||
window.__timelines["main"] = tl;`;
|
||||
|
||||
const mainTimeline = () =>
|
||||
(window as unknown as { __timelines?: Record<string, gsap.core.Timeline> }).__timelines?.main;
|
||||
|
||||
afterEach(() => {
|
||||
mainTimeline()?.kill();
|
||||
document.body.innerHTML = "";
|
||||
for (const name of ["gsap", "__timelines", "__player"]) Reflect.deleteProperty(window, name);
|
||||
usePlayerStore.setState({ currentTime: 0, isPlaying: false });
|
||||
});
|
||||
|
||||
/** The box half way through its tween, behind a frame Studio cannot read, selected by its stand-in. */
|
||||
async function sealedBox(source: string) {
|
||||
document.body.innerHTML = `<div data-composition-id="main"><div id="box"></div></div>`;
|
||||
Object.assign(window, { gsap, __timelines: {}, __player: { getTime: () => 0.5 } });
|
||||
new Function("gsap", "window", source)(gsap, window);
|
||||
mainTimeline()?.seek(0.5);
|
||||
usePlayerStore.setState({ currentTime: 0.5, isPlaying: false });
|
||||
const { iframe } = sealedPreviewFrame();
|
||||
const handle = (await queryPreviewElements(iframe, "#box"))!.matches[0]!.handle;
|
||||
const standIn = standInFor(iframe, (await describePreviewElements(iframe, [handle]))![0]!);
|
||||
const selection = { ...makeSelection("box", standIn), id: "box", handle };
|
||||
await settleGsapSnapshot(iframe);
|
||||
return { iframe, selection, animations: parseGsapScriptAcorn(source).animations };
|
||||
}
|
||||
|
||||
it("shows a sealed preview's GSAP values for the selected layer, read at the playhead", async () => {
|
||||
const { iframe, selection, animations } = await sealedBox(script("x: 200, y: 80"));
|
||||
const values = readGsapRuntimeValuesForPanel(animations[0]!.id, animations, selection, {
|
||||
current: iframe,
|
||||
});
|
||||
expect(values).toMatchObject({ x: 100, y: 40 });
|
||||
});
|
||||
|
||||
it("leaves out GSAP's transform for a sealed layer its tweens do not move", async () => {
|
||||
const { iframe, selection, animations } = await sealedBox(script("opacity: 0"));
|
||||
const values = readGsapRuntimeValuesForPanel(animations[0]!.id, animations, selection, {
|
||||
current: iframe,
|
||||
});
|
||||
expect(values).toEqual({ opacity: 0.5 });
|
||||
});
|
||||
|
||||
it("shows a sealed layer's rotation and scale when its tweens only turn and grow it", async () => {
|
||||
const { iframe, selection, animations } = await sealedBox(script("rotation: 90, scale: 2"));
|
||||
const values = readGsapRuntimeValuesForPanel(animations[0]!.id, animations, selection, {
|
||||
current: iframe,
|
||||
});
|
||||
expect(values).toMatchObject({ rotation: 45, scale: 1.5 });
|
||||
});
|
||||
@@ -9,7 +9,13 @@ import {
|
||||
playbackStartAttributeForElement,
|
||||
readPlaybackStartAttributes,
|
||||
} from "../../player/lib/timelineElementHelpers";
|
||||
import { editsPlainCss, GSAP_TRANSFORM_KEYS } from "../../hooks/gsapRuntimeKeyframes";
|
||||
import {
|
||||
editsPlainCss,
|
||||
GSAP_TRANSFORM_KEYS,
|
||||
sealedGsapRendersTransform,
|
||||
} from "../../hooks/gsapRuntimeKeyframes";
|
||||
import { getIframeGsap } from "../../hooks/gsapShared";
|
||||
import { previewFrameOpaque } from "../../player/lib/previewChannel";
|
||||
|
||||
export type {
|
||||
BackgroundRemovalProgress,
|
||||
@@ -485,18 +491,16 @@ export function readGsapRuntimeValuesForPanel(
|
||||
): Record<string, number> | null {
|
||||
if (!gsapAnimId || gsapAnimations.length === 0) return null;
|
||||
const iframe = previewIframeRef?.current;
|
||||
if (!iframe?.contentWindow) return null;
|
||||
if (!iframe) return null;
|
||||
try {
|
||||
const gsap = (
|
||||
iframe.contentWindow as unknown as {
|
||||
gsap?: { getProperty: (el: Element, prop: string) => number | string };
|
||||
}
|
||||
).gsap;
|
||||
if (!gsap?.getProperty) return null;
|
||||
const el = findPreviewNode(iframe.contentDocument, element);
|
||||
if (!el) return null;
|
||||
const gsap = getIframeGsap(iframe);
|
||||
const sealed = previewFrameOpaque(iframe);
|
||||
const el = sealed ? element.element : findPreviewNode(iframe.contentDocument, element);
|
||||
if (!gsap || !el) return null;
|
||||
const propKeys = collectPanelPropKeys(gsapAnimations);
|
||||
const readsTransform = !editsPlainCss(el, "move");
|
||||
const readsTransform = sealed
|
||||
? sealedGsapRendersTransform(iframe, el)
|
||||
: !editsPlainCss(el, "move");
|
||||
const result: Record<string, number> = {};
|
||||
for (const prop of propKeys) {
|
||||
if (!readsTransform && GSAP_TRANSFORM_KEYS.has(prop)) continue;
|
||||
|
||||
@@ -19,7 +19,7 @@ import { withParsedStart } from "./gsapParsedTween";
|
||||
import { BOX_SIZE_STYLE_PROPS } from "../components/editor/manualEditsDomPatches";
|
||||
import { gsapRendersTransform } from "../components/editor/gsapAnimatesProperty";
|
||||
import { previewFrameOpaque } from "../player/lib/previewChannel";
|
||||
import { frameQuery, frameTimelines } from "./gsapFrameSnapshot";
|
||||
import { frameQuery, frameTarget, frameTimelines } from "./gsapFrameSnapshot";
|
||||
|
||||
/**
|
||||
* A GSAP tween's `vars` object — intentionally open: it mixes channel values
|
||||
@@ -280,11 +280,26 @@ export const GSAP_TRANSFORM_KEYS = new Set(
|
||||
",",
|
||||
),
|
||||
);
|
||||
const TRANSFORM_CHANNELS = [...MOVE_CHANNELS, ...GSAP_TRANSFORM_KEYS];
|
||||
|
||||
/** Sealed `!editsPlainCss(el, "move")`: the frame snapshot's tweens move or transform `el`. */
|
||||
export function sealedGsapRendersTransform(iframe: HTMLIFrameElement, el: Element): boolean {
|
||||
const target = frameTarget(iframe, el);
|
||||
return !!target && timelinesWriteChannels(frameTimelines(iframe), target, TRANSFORM_CHANNELS);
|
||||
}
|
||||
|
||||
/** Whether a live timeline tween or hold writes any of `channels` on `el`. Sync, no fetch. */
|
||||
export function gsapWritesChannels(el: Element, channels: string[]): boolean {
|
||||
const win = el.ownerDocument.defaultView as { __timelines?: Record<string, RuntimeTimeline> };
|
||||
return Object.values(win?.__timelines ?? {}).some((tl) =>
|
||||
return timelinesWriteChannels(win?.__timelines ?? null, el, channels);
|
||||
}
|
||||
|
||||
function timelinesWriteChannels(
|
||||
timelines: Record<string, RuntimeTimeline> | null,
|
||||
el: Element,
|
||||
channels: string[],
|
||||
): boolean {
|
||||
return Object.values(timelines ?? {}).some((tl) =>
|
||||
tweensTargeting(tl, el).some(
|
||||
(tween) =>
|
||||
!!tween.vars &&
|
||||
|
||||
Reference in New Issue
Block a user