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
6 changes: 6 additions & 0 deletions packages/core/package-subpaths.json
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,12 @@
"types": "./dist/runtime/editing/patchTween.d.ts",
"environments": ["browser", "bun", "node"]
},
"./runtime/soft-reload": {
"source": "./src/runtime/editing/softReload.ts",
"runtime": "./dist/runtime/editing/softReload.js",
"types": "./dist/runtime/editing/softReload.d.ts",
"environments": ["browser", "bun", "node"]
},
"./runtime/clip-window": {
"source": "./src/runtime/clipWindow.ts",
"runtime": "./dist/runtime/clipWindow.js",
Expand Down
10 changes: 10 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,12 @@
"import": "./src/runtime/editing/patchTween.ts",
"types": "./src/runtime/editing/patchTween.ts"
},
"./runtime/soft-reload": {
"bun": "./src/runtime/editing/softReload.ts",
"node": "./dist/runtime/editing/softReload.js",
"import": "./src/runtime/editing/softReload.ts",
"types": "./src/runtime/editing/softReload.ts"
},
"./runtime/clip-window": {
"bun": "./src/runtime/clipWindow.ts",
"node": "./dist/runtime/clipWindow.js",
Expand Down Expand Up @@ -993,6 +999,10 @@
"import": "./dist/runtime/editing/patchTween.js",
"types": "./dist/runtime/editing/patchTween.d.ts"
},
"./runtime/soft-reload": {
"import": "./dist/runtime/editing/softReload.js",
"types": "./dist/runtime/editing/softReload.d.ts"
},
"./runtime/clip-window": {
"import": "./dist/runtime/clipWindow.js",
"types": "./dist/runtime/clipWindow.d.ts"
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/runtime/editing/patchTween.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// confidently returns false and the caller reloads. Studio runs it on a frame it can reach,
// a sealed frame on itself through the editing port.
import { applyAuthoredInlineOpacity, readStampedAuthoredOpacity } from "../../colorGrading";
import { recordLiveSet } from "./softReload";

export interface SetPatchProps {
x?: number;
Expand Down Expand Up @@ -196,6 +197,7 @@ export function applyGlobalSet(
const numeric = finiteNumericProps(props);
if (typeof target.gsap?.set !== "function" || !el || !numeric) return null;
target.gsap.set(el, numeric);
recordLiveSet(el, numeric);
return el;
}

Expand Down
178 changes: 178 additions & 0 deletions packages/core/src/runtime/editing/softReload.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { parseSoftReload, softReload, type SoftReloadParams } from "./softReload";

type TestWindow = Window & Record<string, unknown>;
const win = window as TestWindow;
const SCRIPT = `var __timelines = {}; __timelines["main"] = 1;`;
const params = (over: Partial<SoftReloadParams> = {}): SoftReloadParams => ({
script: SCRIPT,
time: 1.5,
sets: [],
authored: [],
timing: [],
...over,
});

const killed = vi.fn();
function preview(markup: string) {
document.body.innerHTML = `${markup}<script>${SCRIPT}</script>`;
const seeks: number[] = [];
win.gsap = {
timeline: () => ({}),
set: (targets: HTMLElement[], vars: Record<string, unknown>) => {
if (vars.clearProps) for (const el of targets) el.removeAttribute("style");
},
};
win.__timelines = { main: { kill: killed, clear: () => {} } };
win.__hfForceTimelineRebind = () => {};
win.__player = { seek: (t: number) => seeks.push(t) };
// The sent script registers on a local: the stub registers what a real run would.
const append = document.body.appendChild.bind(document.body);
document.body.appendChild = <T extends Node>(node: T): T => {
if ((node as Node).nodeName === "SCRIPT") (win.__timelines as Record<string, object>).main = {};
return append(node);
};
return seeks;
}

afterEach(() => {
document.body.innerHTML = "";
for (const key of ["gsap", "__timelines", "__hfForceTimelineRebind", "__player"]) delete win[key];
delete (document.body as Partial<HTMLElement>).appendChild;
killed.mockReset();
});

describe("soft-reload", () => {
it("re-runs the sent script and gives a reset element the authored style Studio sent", async () => {
const seeks = preview(
`<div data-composition-id="main"><div id="a" data-hf-id="h1" style="opacity: 0.3; transform: translateX(50px)"></div></div>`,
);
const el = document.getElementById("a")!;
Object.assign(el, { _gsap: {} });

const reply = await softReload(params({ authored: [["hf:h1", "opacity: 0.8"]] }));

expect(reply).toEqual({ result: "applied" });
expect([el.style.opacity, el.style.transform]).toEqual(["0.8", ""]);
expect(killed).toHaveBeenCalledOnce();
const scripts = [...document.querySelectorAll("script")].map((s) => s.textContent);
expect(scripts).toEqual([`(function(){${SCRIPT}\n})();`]);
expect(seeks).toEqual([1.5]);
});

it("restores what a removed standalone set wrote, from the selector and names Studio sent", async () => {
preview(`<div data-composition-id="main"><div id="a" style="left: 10px"></div></div>`);
const el = document.getElementById("a")!;
el.style.backgroundColor = "red";

await softReload(
params({
sets: [{ selector: "#a", props: ["background-color"] }],
authored: [["#a", "left: 10px"]],
}),
);

expect([el.style.backgroundColor, el.style.left]).toEqual(["", "10px"]);
});

it("changes nothing and refuses when a reset element is written in a file Studio did not send", async () => {
preview(
`<div data-composition-id="main" data-composition-file="main.html"><div data-composition-file="card.html"><div id="b"></div></div></div>`,
);
const el = document.getElementById("b")!;
Object.assign(el, { _gsap: {} });
el.style.opacity = "0.3";

const reply = await softReload(params());

expect(reply).toEqual({ result: "cannot-soft-reload" });
expect(killed).not.toHaveBeenCalled();
expect(el.style.opacity).toBe("0.3");
expect(document.querySelector("script")?.textContent).toBe(SCRIPT);
});

it("refuses a script that registers no timeline key, before touching anything", async () => {
preview(`<div data-composition-id="main"></div>`);
expect(await softReload(params({ script: "gsap.to('#a', {x: 1})" }))).toEqual({
result: "cannot-soft-reload",
});
expect(killed).not.toHaveBeenCalled();
});
});

describe("soft-reload of a timing edit", () => {
const MARKUP = `<div data-composition-id="main" data-hf-id="root" data-duration="5"><div id="clip" data-start="1" data-duration="2" data-hf-autostamped></div></div>`;

it("puts the saved timing on the live clip and the film before the rebind derives its windows", async () => {
preview(MARKUP);
const clip = document.getElementById("clip")!;
const root = document.querySelector("[data-composition-id]")!;
const seen: Array<Array<string | null>> = [];
win.__hfForceTimelineRebind = () =>
seen.push([clip.getAttribute("data-start"), root.getAttribute("data-duration")]);

const timing: SoftReloadParams["timing"] = [
["hf:root", [["data-duration", "8"]]],
[
"#clip",
[
["data-start", "4"],
["data-duration", "2"],
],
],
];
expect(await softReload(params({ timing }))).toEqual({ result: "applied" });

expect(seen).toEqual([["4", "8"]]);
expect(clip.hasAttribute("data-hf-autostamped")).toBe(false);
});

it("changes nothing and refuses when a timed element of the saved file is not in the preview", async () => {
preview(MARKUP);
const clip = document.getElementById("clip")!;

const timing: SoftReloadParams["timing"] = [
["#clip", [["data-start", "4"]]],
["#gone", [["data-start", "1"]]],
];
expect(await softReload(params({ timing }))).toEqual({ result: "cannot-soft-reload" });

expect(clip.getAttribute("data-start")).toBe("1");
expect(killed).not.toHaveBeenCalled();
});

it("times the saved file's clip even when a nested file's root repeats its per-file id", async () => {
preview(
`<div data-composition-file="main.html"><div data-composition-id="main" data-hf-id="root">` +
`<div data-composition-file="card.html"><div data-composition-id="card" data-hf-inner-root data-hf-id="shared"></div></div>` +
`<div id="clip" data-hf-id="shared" data-start="1" data-duration="2"></div></div></div>`,
);

const timing: SoftReloadParams["timing"] = [["hf:shared", [["data-start", "4"]]]];
expect(await softReload(params({ timing }))).toEqual({ result: "applied" });

expect(document.getElementById("clip")!.getAttribute("data-start")).toBe("4");
});
});

describe("parseSoftReload", () => {
it("accepts a bounded request", () => {
const ok = params({ sets: [{ selector: "#a", props: ["width"] }], authored: [["hf:1", ""]] });
expect(parseSoftReload(ok)).toEqual(ok);
});

it.each([
["an extra field", { ...params(), keys: ["main"] }],
["an empty script", params({ script: "" })],
["an oversized script", params({ script: "x".repeat(1_000_001) })],
["a negative time", params({ time: -1 })],
["a non-finite time", params({ time: Number.NaN })],
["a set with an extra field", params({ sets: [{ selector: "#a", props: [], x: 1 } as never] })],
["an authored key of no known kind", params({ authored: [["a", ""]] })],
["an authored entry with three parts", params({ authored: [["#a", "", ""] as never] })],
["a timing attribute that is not timing", params({ timing: [["#a", [["style", "x"]]]] })],
["no timing field", { ...params(), timing: undefined }],
])("refuses %s", (_label, request) => {
expect(parseSoftReload(request)).toBeNull();
});
});
Loading
Loading