diff --git a/packages/studio/src/utils/gsapSoftReload.ts b/packages/studio/src/utils/gsapSoftReload.ts index 405d9d764d..ac52ba922f 100644 --- a/packages/studio/src/utils/gsapSoftReload.ts +++ b/packages/studio/src/utils/gsapSoftReload.ts @@ -132,6 +132,12 @@ function verifyTimelinesPopulated(win: IframeWindow, targetKeys: string[]): bool return Object.keys(timelines).filter((k) => k !== "__proxied").length > 0; } +// The next tween re-reads the element's transform, folding its CSS translate/rotate/scale afresh. +function markGsapTransformStale(el: Element): void { + const cache = (el as { _gsap?: { uncache?: number } })._gsap; + if (cache) cache.uncache = 1; +} + // GSAP masks a folded CSS translate/rotate/scale with `none`, and a tween writes what it animates // inline; a fresh load has only what the file authors. Opacity has its own restore. function restoreAuthoredStyle( @@ -379,6 +385,8 @@ export function applySoftReload( try { win.gsap.set(allTargets, { clearProps: "all" }); } catch {} + // GSAP's parsed transform outlives clearProps here, so the re-run would not fold the CSS again. + for (const el of allTargets) markGsapTransformStale(el); for (const [el, css] of saved) { const s = el.style; s.cssText = css; diff --git a/packages/studio/src/utils/gsapUndoRestore.browser.test.ts b/packages/studio/src/utils/gsapUndoRestore.browser.test.ts index 3f6ec44c6c..7b23d389e7 100644 --- a/packages/studio/src/utils/gsapUndoRestore.browser.test.ts +++ b/packages/studio/src/utils/gsapUndoRestore.browser.test.ts @@ -14,11 +14,9 @@ const require = createRequire(import.meta.url); const CROP = "inset(0px 40px 0px 0px)"; let browser: Browser; let undoBundle: string; +let softReloadBundle: string; -beforeAll(async () => { - const executablePath = findSystemChrome(); - if (!executablePath) throw new Error("no Chrome found: set HYPERFRAMES_BROWSER_PATH"); - browser = await puppeteer.launch({ executablePath, headless: true, args: ["--no-sandbox"] }); +async function bundle(file: string, name: string): Promise { const out = await build({ configFile: false, logLevel: "silent", @@ -28,68 +26,85 @@ beforeAll(async () => { build: { write: false, minify: false, - lib: { - entry: fileURLToPath(new URL("./gsapUndoRestore.ts", import.meta.url)), - formats: ["iife"], - name: "hfUndo", - }, + lib: { entry: fileURLToPath(new URL(file, import.meta.url)), formats: ["iife"], name }, }, }); - undoBundle = (Array.isArray(out) ? out[0]! : (out as { output: [{ code: string }] })).output[0] - .code; + return (Array.isArray(out) ? out[0]! : (out as { output: [{ code: string }] })).output[0].code; +} + +beforeAll(async () => { + const executablePath = findSystemChrome(); + if (!executablePath) throw new Error("no Chrome found: set HYPERFRAMES_BROWSER_PATH"); + browser = await puppeteer.launch({ executablePath, headless: true, args: ["--no-sandbox"] }); + undoBundle = await bundle("./gsapUndoRestore.ts", "hfUndo"); + softReloadBundle = await bundle("./gsapSoftReload.ts", "hfSoftReload"); }, 60_000); afterAll(() => browser?.close()); +type Spec = { gsap: string; placement: string; rotation: number; time: number }; + +/** The bench's root crop fixture, shown at `spec.time` with the module under test loaded. */ +async function openCase(spec: Spec, moduleBundle: string) { + const dir = join(mkdtempSync(join(tmpdir(), "undo-gsap-")), "case"); + writeFixture({ gesture: "crop", nesting: "root", zoom: 100, ...spec }, dir); + const restored = readFileSync(join(dir, "index.html"), "utf8"); + const page = await browser.newPage(); + await page.setViewport({ width: 1920, height: 1080 }); + await page.setRequestInterception(true); + // Only the fixture's GSAP CDN script is served; anything else is refused, never fetched. + page.on("request", (request) => + request.url().endsWith("/gsap.min.js") + ? request.respond({ body: readFileSync(require.resolve("gsap/dist/gsap.min.js"), "utf8") }) + : request.abort("blockedbyclient"), + ); + await page.setContent(restored, { waitUntil: "load" }); + await page.evaluate(readFileSync(require.resolve("@hyperframes/core/runtime"), "utf8")); + await page.waitForFunction(() => "__player" in window); + await page.evaluate(moduleBundle); + await page.evaluate( + (time) => (window as unknown as { __player: { seek(t: number): void } }).__player.seek(time), + spec.time, + ); + const box = () => + page.evaluate(() => { + const { left, top, width, height } = document + .getElementById("target")! + .getBoundingClientRect(); + return [left, top, width, height].map((n) => Math.round(n * 100) / 100); + }); + return { page, restored, box }; +} + // The first case opens the cold browser's first page, which overran 5 s on a loaded CI runner. it.each([ - { gsap: "hold", placement: "px", rotation: 0 }, - { gsap: "tween", placement: "px", rotation: 0 }, - { gsap: "tween", placement: "xpercent", rotation: 30 }, + { gsap: "hold", placement: "px", rotation: 0, time: 1 }, + { gsap: "tween", placement: "px", rotation: 0, time: 1 }, + { gsap: "tween", placement: "xpercent", rotation: 30, time: 1 }, + { gsap: "keys", placement: "px", rotation: 0, time: 1 }, + { gsap: "keys", placement: "px", rotation: 30, time: 2 }, + { gsap: "scale", placement: "px", rotation: 0, time: 1 }, + { gsap: "spin", placement: "px", rotation: 30, time: 1 }, + { gsap: "fromto", placement: "px", rotation: 0, time: 1 }, ])( - "an undone crop leaves a $gsap $placement r$rotation layer where it was", + "an undone crop leaves a $gsap $placement r$rotation layer at t$time where it was", async (spec) => { - const dir = join(mkdtempSync(join(tmpdir(), "undo-gsap-")), "case"); - writeFixture({ gesture: "crop", nesting: "root", zoom: 100, ...spec }, dir); - const restored = readFileSync(join(dir, "index.html"), "utf8"); + const { page, restored, box } = await openCase(spec, undoBundle); const previous = restored.replace( `
- request.url().endsWith("/gsap.min.js") - ? request.respond({ body: readFileSync(require.resolve("gsap/dist/gsap.min.js"), "utf8") }) - : request.abort("blockedbyclient"), - ); - await page.setContent(restored, { waitUntil: "load" }); - await page.evaluate(readFileSync(require.resolve("@hyperframes/core/runtime"), "utf8")); - await page.waitForFunction(() => "__player" in window); - await page.evaluate(undoBundle); - - const box = () => - page.evaluate(() => { - const { left, top, width, height } = document - .getElementById("target")! - .getBoundingClientRect(); - return [left, top, width, height].map((n) => Math.round(n * 100) / 100); - }); - await page.evaluate(() => - (window as unknown as { __player: { seek(t: number): void } }).__player.seek(1), - ); const before = await box(); const outcome = await page.evaluate( - (files, crop) => { + (files, crop, time) => { document.getElementById("target")!.style.setProperty("clip-path", crop); const undo = (window as unknown as { hfUndo: typeof import("./gsapUndoRestore") }).hfUndo; const iframe = { contentDocument: document, contentWindow: window } as HTMLIFrameElement; - return undo.applyUndoRestoreToPreview(iframe, "index.html", files, 1, () => {}); + return undo.applyUndoRestoreToPreview(iframe, "index.html", files, time, () => {}); }, { "index.html": { previous, restored } }, CROP, + spec.time, ); expect(outcome).toBe("soft"); @@ -98,3 +113,35 @@ it.each([ }, 30_000, ); + +// A drop saves the file and soft-reloads it; the re-run must place an animated layer where it was. +it.each([ + { gsap: "tween", placement: "px", rotation: 0, time: 1 }, + { gsap: "keys", placement: "px", rotation: 30, time: 2 }, + { gsap: "spin", placement: "px", rotation: 0, time: 1 }, +])( + "a soft reload keeps a $gsap $placement r$rotation layer at t$time where it was", + async (spec) => { + const { page, restored, box } = await openCase(spec, softReloadBundle); + const before = await box(); + const outcome = await page.evaluate( + (html, time) => { + const soft = (window as unknown as { hfSoftReload: typeof import("./gsapSoftReload") }) + .hfSoftReload; + const iframe = { contentDocument: document, contentWindow: window } as HTMLIFrameElement; + const script = soft.extractGsapScriptText(html)!; + return soft.applySoftReload(iframe, script, { + currentTimeOverride: time, + authoredHtml: html, + }); + }, + restored, + spec.time, + ); + + expect(outcome).toBe("applied"); + expect(await box()).toEqual(before); + await page.close(); + }, + 30_000, +);