fix(studio): an undo or redo that adds a sealed page's first edit stays in place (#8462)

A sealed preview only got its seek-reapply script when its file already held a Studio edit, and the script left
a page with none unarmed. The first restore that added one (a redo of a resize, say) was refused by the frame
and fell back to an unawaited reload. Every sealed page now carries the script, armed from the start.
This commit is contained in:
Miguel Ángel
2026-11-22 07:54:54 +00:00
committed by GitHub
parent d37563e35b
commit 2d72e523ff
4 changed files with 65 additions and 9 deletions
@@ -546,6 +546,62 @@ describe("createStudioPositionSeekReapplyScript", () => {
expect(card.style.getPropertyValue("width")).toBe("260px");
});
/** The sealed preview script in a sealed frame whose page loaded with no Studio edit. */
function unmarkedSealedPreview() {
const window = new Window();
window.document.body.innerHTML = '<div id="card"></div>';
const card = window.document.getElementById("card") as unknown as HTMLElement;
Object.defineProperty(window, "parent", { value: SEALED });
const frames: Array<() => void> = [];
const player = { time: 0, getTime: () => player.time };
const built: unknown[] = [];
const gsap = {
timeline: () => ({ fromTo: (el: unknown) => void built.push(el), totalTime: () => {} }),
};
Object.assign(window, {
__hf: {},
__player: player,
__timelines: {},
gsap,
requestAnimationFrame: (callback: () => void) => frames.push(callback),
setTimeout: (callback: () => void) => callback(),
});
runPositionScript(window, { script: createSealedPreviewSeekReapplyScript() });
const moveFrame = () => {
player.time += 1;
frames.splice(0).forEach((callback) => callback());
};
return { window, card, built, moveFrame };
}
// fails without the fix: a sealed page loaded with no Studio edit never started its reapply, so the frame refused the
// first undo or redo that added one (restore-attributes needs a started reapply) and reloaded instead.
it.each([
["box-size", ["data-hf-studio-box-size", "true"], "--hf-studio-width: 200px"],
["rotation", ["data-hf-studio-rotation", "true"], "--hf-studio-rotation: 30deg"],
["path-offset", ["data-hf-studio-path-offset", "true"], "--hf-studio-offset-x: 10px"],
["motion", ["data-hf-studio-motion", '{"start":0,"duration":1,"to":{"x":10}}'], ""],
] as const)(
"keeps a %s mark a restore adds to a sealed page that loaded with none",
(mark, [name, value], style) => {
const { window, card, built, moveFrame } = unmarkedSealedPreview();
expect((window as unknown as Record<string, unknown>)[STUDIO_SEEK_REAPPLY_STARTED]).toBe(
true,
);
card.setAttribute(name, value);
if (style) card.setAttribute("style", style);
moveFrame();
const kept = {
"box-size": () => card.style.getPropertyValue("width") === "200px",
rotation: () => card.style.getPropertyValue("rotate").includes("--hf-studio-rotation"),
"path-offset": () =>
card.style.getPropertyValue("translate").includes("--hf-studio-offset-x"),
motion: () => built.includes(card),
}[mark];
expect(kept()).toBe(true);
},
);
it("follows no frames in a preview Studio can script, nor in a page with no parent", () => {
expect(sealedPreview({ document: {} }).frames).toHaveLength(0);
expect(sealedPreview(null).frames).toHaveLength(0);
@@ -44,7 +44,9 @@ function studioPositionSeekReapplyRuntime(followFrames: boolean): void {
const MOTION_TL_KEY = "studio-motion";
const WRAPPED_PROP = "__hfStudioPositionSeekReapplyWrapped";
const sealedFrameArmsBeforeAnyEdit = followFrames;
if (
!sealedFrameArmsBeforeAnyEdit &&
!document.querySelector("[" + PATH_OFFSET_ATTR + '="true"]') &&
!document.querySelector("[" + BOX_SIZE_ATTR + '="true"]') &&
!document.querySelector("[" + ROTATION_ATTR + '="true"]') &&
@@ -3,10 +3,7 @@ import type { Context, Hono, Next } from "hono";
import { getMimeType } from "./mime.js";
import { fetchAgain } from "./fetchAgain.js";
import { insertBeforeCloseTag } from "@hyperframes/core/compiler";
import {
createSealedPreviewSeekReapplyScript,
htmlHasStudioPositionEdits,
} from "./manualEditsRenderScript.js";
import { createSealedPreviewSeekReapplyScript } from "./manualEditsRenderScript.js";
import {
isProxyWait,
PREVIEW_EXPOSED_HEADERS,
@@ -85,8 +82,7 @@ const PROXY_PREFLIGHT_ANSWER = {
Vary: "Origin",
};
function withStudioEditsReappliedAfterSeeks(html: string): string {
if (!htmlHasStudioPositionEdits(html)) return html;
function withSeekReapplyArmedForAnyEdit(html: string): string {
const tag = `<script>${createSealedPreviewSeekReapplyScript()}\n//# sourceURL=hyperframes://position-seek-reapply</script>`;
return insertBeforeCloseTag(html, "body", tag) ?? `${html}${tag}`;
}
@@ -97,7 +93,7 @@ async function underToken(res: Response, headers: Headers, projectId: string, to
const base = previewBase(projectId);
headers.delete("content-length");
const rebased = (await res.text()).replaceAll(base, `${base}~${token}/`);
const html = withStudioEditsReappliedAfterSeeks(rebased);
const html = withSeekReapplyArmedForAnyEdit(rebased);
return new Response(html, { status: res.status, headers });
}
@@ -210,7 +210,9 @@ describe("an opaque preview frame's loads under its token", () => {
expect(html.replaceAll(tokened, "")).not.toContain("/api/projects/demo/preview/");
});
it("puts Studio's seek reapply last in a sealed document with a Studio edit, and only there", async () => {
// fails without the fix: a sealed document with no Studio edit yet got no reapply, so the first edit an undo or redo
// put back on it could not be kept in place.
it("puts Studio's seek reapply last in every sealed document, edited or not, and in no plain one", async () => {
const reapply = "//# sourceURL=hyperframes://position-seek-reapply</script>";
const sealed = await (await load("comp/sized.html", { "sec-fetch-dest": "iframe" })).text();
expect(sealed).toContain(reapply);
@@ -220,7 +222,7 @@ describe("an opaque preview frame's loads under its token", () => {
});
expect(await plain.text()).not.toContain(reapply);
const unedited = await (await load("comp/scene.html", { "sec-fetch-dest": "iframe" })).text();
expect(unedited).not.toContain(reapply);
expect(unedited).toContain(reapply);
});
it("serves the main preview under the token, re-based", async () => {