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
56 changes: 56 additions & 0 deletions packages/studio-server/src/helpers/manualEditsRenderScript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions packages/studio-server/src/helpers/manualEditsRenderScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"]') &&
Expand Down
10 changes: 3 additions & 7 deletions packages/studio-server/src/helpers/opaqueFrameLoads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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}`;
}
Expand All @@ -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 });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 () => {
Expand Down
Loading