fix(studio): a hand move in a sealed preview saves as the same CSS translate as unsealed (#8731)

This commit is contained in:
Miguel Ángel
2026-11-24 17:34:30 +00:00
committed by GitHub
parent d975475cd5
commit d44c847ca7
4 changed files with 93 additions and 5 deletions
@@ -1,6 +1,7 @@
import { afterEach, describe, expect, it } from "vitest";
import { gsap as realGsap } from "gsap";
import { handleFor } from "../editingHandles";
import { gestureStart } from "../editingGestureStart";
import { gsapSnapshot } from "./gsapSnapshot";
import { MAX_SNAPSHOT_TEXT, MAX_SNAPSHOT_TWEENS } from "./gsapSnapshotLimits";
@@ -150,4 +151,17 @@ describe("gsapSnapshot", () => {
expect(snapshot.timelines[0]!.tweens).toHaveLength(1);
expect(snapshot.targets[0]!.values.x).toBeCloseTo(600);
});
it("leaves a CSS-moved element's position to CSS when only its opacity is tweened", async () => {
document.body.innerHTML = `<h1 id="title" style="translate: 0 -200px">Title</h1>`;
const title = document.getElementById("title")!;
const tl = realGsap.timeline({ paused: true });
tl.to("#title", { opacity: 0.5, duration: 10 }, 0);
tl.seek(0);
Object.assign(window, { gsap: realGsap, __timelines: { main: tl } });
await gsapSnapshot();
expect(gestureStart([handleFor(title)]).elements[0]!.gsap).toBeNull();
});
});
@@ -1,6 +1,7 @@
// The frame's GSAP tweens as plain data, for a parent that cannot reach the frame's window.
import type { EditingElementHandle } from "../editingElement";
import { handleFor } from "../editingHandles";
import { MOVES, rendersTransform } from "../editingGestureStart";
import { isPlainRecord } from "../editingPort";
import { isElementNode } from "../domRealm";
import {
@@ -165,10 +166,14 @@ class SnapshotReader {
return [...this.channels].map(([element, channels]) => {
if (channels.size > MAX_SNAPSHOT_CHANNELS) throw new SnapshotBound("channels");
const values: Record<string, number> = {};
for (const channel of channels) {
const read = (channel: string) => {
const value = Number(gsap?.getProperty?.(element, channel));
if (Number.isFinite(value)) values[channel] = value;
}
};
for (const channel of channels) read(channel);
const gsapOwnsPosition =
rendersTransform(element) || MOVES.some((channel) => channels.has(channel));
if (gsapOwnsPosition) for (const axis of ["x", "y"]) if (!(axis in values)) read(axis);
const className = element.getAttribute("class") ?? "";
if (className.length > MAX_SNAPSHOT_TEXT) throw new SnapshotBound("class names");
return { handle: handleFor(element), id: capped(element.id), className, values };
@@ -181,7 +186,7 @@ class SnapshotReader {
parsed: GsapSnapshotTween["parsed"],
parts: GsapSnapshotTween[],
) {
const channels = this.channels.get(element) ?? new Set(["x", "y"]);
const channels = this.channels.get(element) ?? new Set<string>();
this.channels.set(element, channels);
const steps = isPlainRecord(vars) ? [vars, ...stepsOf(vars.keyframes)] : [];
for (const step of steps)
@@ -125,7 +125,7 @@ type GsapWindow = {
// The channels Studio's move route and drag commit read as GSAP moving an element.
const POSITION = ["x", "y", "xPercent", "yPercent", "left", "top", "translateX", "translateY"];
const MOVES = [...POSITION, "motionPath"];
export const MOVES = [...POSITION, "motionPath"];
const TURNS = [
"rotation",
"rotationX",
@@ -162,7 +162,7 @@ function tweensOn(element: Element): GsapTween[] {
);
}
const rendersTransform = (element: Element) =>
export const rendersTransform = (element: Element) =>
!!(element as { _gsap?: { renderTransform?: unknown } })._gsap?.renderTransform;
export type EditingGestureBase = {
@@ -0,0 +1,69 @@
// Real Chrome: a CSS-placed element whose only tween is its opacity still moves by CSS once Studio has read GSAP.
import { readFileSync } from "node:fs";
import { createRequire } from "node:module";
import type { Browser } from "puppeteer-core";
import { afterAll, beforeAll, expect, it, vi } from "vitest";
import { closeTestChrome, launchTestChrome } from "../../tests/chromeTestUtils";
import { buildStudioBundle, openPreview, type Studio } from "../../tests/sealedPreviewHarness";
vi.setConfig({ testTimeout: 60_000, hookTimeout: 60_000 });
const require = createRequire(import.meta.url);
const MODES = process.env.HF_PARITY_MODE ? [process.env.HF_PARITY_MODE] : ["unsealed", "sealed"];
type Reads = Studio & {
settleGsapSnapshot: typeof import("./gsapFrameSnapshot").settleGsapSnapshot;
previewGestureStart: typeof import("../player/lib/previewElements").previewGestureStart;
};
let browser: Browser;
let studioBundle: string;
beforeAll(async () => {
browser = await launchTestChrome();
studioBundle = await buildStudioBundle([
`export { settleGsapSnapshot } from "./hooks/gsapFrameSnapshot";`,
`export { previewGestureStart } from "./player/lib/previewElements";`,
]);
});
afterAll(() => closeTestChrome(browser));
function previewHtml(): string {
const gsap = readFileSync(require.resolve("gsap/dist/gsap.min.js"), "utf8");
const runtime = readFileSync(require.resolve("@hyperframes/core/runtime"), "utf8");
return `<!doctype html><html><head><meta charset="UTF-8" />
<meta name="hyperframes-studio-preview" content="1" />
<style>html, body { margin: 0; width: 1920px; height: 1080px; overflow: hidden }
#title { position: absolute; left: 600px; top: 500px; translate: 0 -200px }</style>
</head><body>
<div id="root" data-composition-id="main" data-start="0" data-duration="4" data-width="1920" data-height="1080">
<h1 id="title" class="clip" data-start="0" data-duration="4" data-track-index="1">Title</h1>
</div>
<script>${gsap}</script>
<script>
window.__timelines = window.__timelines || {};
var tl = gsap.timeline({ paused: true });
tl.to("#title", { opacity: 0.5, duration: 4 }, 0);
window.__timelines.main = tl;
</script>
<script>${runtime}</script></body></html>`;
}
// fails without the fix: the sealed read asked GSAP for x, GSAP took over the translate and the move saved as gsap.set.
it.each(MODES)("a move after a GSAP read still saves as a CSS translate (%s)", async (mode) => {
const preview = await openPreview(browser, studioBundle, previewHtml(), mode);
await preview.seek(1);
const start = await preview.page.evaluate(async () => {
const studio = (window as unknown as { hfStudio: Reads }).hfStudio;
const iframe = document.querySelector("iframe")!;
studio.usePlayerStore.setState({ currentTime: 1, isPlaying: false });
await studio.settleGsapSnapshot(iframe);
const query = await studio.previewRequest(iframe, "query", { selector: "#title" });
const handle = (query as { matches: Array<{ handle: string }> } | null)?.matches[0]?.handle;
return (await studio.previewGestureStart(iframe, [handle!]))?.elements[0];
});
expect(start).toMatchObject({ gsap: null });
const drawn = await preview.frame.evaluate(
() => getComputedStyle(document.getElementById("title")!).translate,
);
expect(drawn).toBe("0px -200px");
await preview.page.close();
});