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
72 changes: 67 additions & 5 deletions packages/core/src/runtime/editing/patchTween.sceneRest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ describe("patch-tweens on a scene that rests", () => {
]);
});

it("answers not patched when the patched set sits in a child timeline that already ended", () => {
it("draws the patched set in a child timeline that already ended, and answers patched", () => {
load(1, (a) => a.add(gsap.timeline().set("#a", { x: 40 }, 0), 0));
expect(patchA(300)).toEqual({ patched: false });
expect(shownX("#a")).toBe(40);
expect(patchA(300)).toEqual({ patched: true });
expect(shownX("#a")).toBe(300);
});

it("answers patched for a keyframe-step edit once the tween that replaced it renders", () => {
Expand All @@ -92,10 +92,72 @@ describe("patch-tweens on a scene that rests", () => {
expect(shownX("#a")).toBe(90);
});

it("answers not patched when nothing draws the scene it changed", () => {
it("draws the patch itself when no player seeks the scene it changed", () => {
load(1);
delete window.__player;
expect(patchA(300)).toEqual({ patched: false });
expect(patchA(300)).toEqual({ patched: true });
expect(shownX("#a")).toBe(300);
});
});

// fails without the fix: a Studio preview's seek paints a tick later, so the frame answered not patched while
// the patched hold painted next tick under the drag's drawn offset (the 312 px flash on the edit bench).
describe("patch-tweens when the player's seek paints a tick later, as a Studio preview's does", () => {
beforeEach(() => {
resetRuntimeFixtureDom();
window.gsap = gsap as unknown as typeof window.gsap;
});
afterEach(() => {
window.__hfRuntimeTeardown?.();
vi.restoreAllMocks();
delete window.__player;
delete window.__playerReady;
delete window.__hf;
});

it("draws a patched root hold in the patch's own task, and answers patched", () => {
document.body.innerHTML =
`<div data-composition-id="main" data-root="true" data-duration="10">` +
`<div id="target"></div><div id="other"></div></div>`;
const main = gsap.timeline({ paused: true });
main.to("#other", { x: 120, duration: 10 }, 0).set("#target", { x: 200, y: 126 }, 0);
window.__timelines = { main } as unknown as Record<string, RuntimeTimelineLike>;
initSandboxRuntimeModular();
window.__player?.seek(1.5);
vi.spyOn(window.__player!, "seek").mockImplementation(() => undefined);

const reply = patchTweens({
patches: [{ selector: "#target", change: { kind: "set", props: { x: -60, y: -47 } } }],
seek: true,
});

expect(reply).toEqual({ patched: true });
expect([gsap.getProperty("#target", "x"), gsap.getProperty("#target", "y")]).toEqual([
-60, -47,
]);
});

it("draws a patched hold on a yoyo timeline's second pass without rewinding it to the first", () => {
document.body.innerHTML =
`<div data-composition-id="main" data-root="true" data-duration="10">` +
`<div id="target"></div><div id="other"></div></div>`;
const pass = gsap.timeline({ repeat: 1, yoyo: true });
pass.set("#target", { x: 200 }, 0).to("#other", { x: 120, duration: 1 }, 0);
const main = gsap.timeline({ paused: true }).to({}, { duration: 10 }, 0).add(pass, 0);
window.__timelines = { main } as unknown as Record<string, RuntimeTimelineLike>;
initSandboxRuntimeModular();
window.__player?.seek(1.25);
vi.spyOn(window.__player!, "seek").mockImplementation(() => undefined);
const secondPass = pass.totalTime();
expect(secondPass).toBeGreaterThan(1);

const reply = patchTweens({
patches: [{ selector: "#target", change: { kind: "set", props: { x: -60 } } }],
seek: true,
});

expect(reply).toEqual({ patched: true });
expect([gsap.getProperty("#target", "x"), pass.totalTime()]).toEqual([-60, secondPass]);
});
});

Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/runtime/editing/patchTween.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { applyAuthoredInlineOpacity, readStampedAuthoredOpacity } from "../../co
import { recordLiveSet, swapWaiting } from "./softReload";
import { drawDrafts } from "../editingDraft";
import { markRestingScenesForRedraw } from "../sceneRest";
import { redrawGsapTimelineAt } from "../adapters/gsap";
import type { RuntimeTimelineLike } from "../types";
import { parseDraftEnd, type EditingDraftEnd } from "../editingDraftEnd";

export interface SetPatchProps {
Expand Down Expand Up @@ -343,6 +345,15 @@ const renderedOnceReached = (tween: PatchableTween): boolean => {
return !reached || Boolean((tween as GsapInitFlag)._initted);
};

function drawWhatTheSeekLeft(tweens: readonly PatchableTween[]): void {
for (const tween of tweens) {
const parent = tween.parent as RuntimeTimelineLike | undefined;
const time = parent?.totalTime?.();
if (parent && typeof time === "number" && !renderedOnceReached(tween))
redrawGsapTimelineAt(parent, time);
}
}

function timelinesAbove(tween: PatchableTween): object[] {
const chain: object[] = [];
for (let parent = tween.parent; parent; parent = (parent as PatchableTween).parent)
Expand Down Expand Up @@ -507,6 +518,7 @@ export function patchTweens({ patches, seek, endsDrafts }: PatchTweensParams): {
const defer = !(seek && index === renderAt);
return !patchTween(target, selector, change, undefined, defer, patchedTweens);
});
if (seek) drawWhatTheSeekLeft(patchedTweens);
const shown = !seek || patchedTweens.every(renderedOnceReached);
const patched = missed === undefined && shown;
if (patched && endsDrafts) drawDrafts(endsDrafts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { gsap } from "gsap";
import { parseGsapScriptAcorn } from "@hyperframes/parsers/gsap-parser-acorn";
import { afterEach, expect, it, vi } from "vitest";
import { afterEach, describe, expect, it, vi } from "vitest";
import { makeSelection } from "../../hooks/domSelectionTestHarness";
import { freezeDragStamp } from "../../hooks/draggedGsapPosition";
import { settleGsapSnapshot } from "../../hooks/gsapFrameSnapshot";
Expand All @@ -19,7 +19,7 @@ import {
endManualOffsetDragMembers,
type ManualOffsetDragMember,
} from "./manualOffsetDrag";
import { drawPreviewDraft, endPreviewDraft } from "./previewDrafts";
import { armPreviewDraftEnd, drawPreviewDraft, endPreviewDraft } from "./previewDrafts";
import { patchSealedPreview } from "../../hooks/sealedTweenPatch";
import { takePreviewTicket } from "../../utils/sealedPreviewLedger";
import { previewAnswer, previewRequest } from "../../player/lib/previewChannel";
Expand Down Expand Up @@ -526,6 +526,78 @@ it("a save the frame cannot apply leaves the end to go alone, so the draft still
expect(draftEnds()).toEqual([expect.objectContaining({ end: "keep-size" })]);
});

const savedEnds = () =>
(patchRequests() as unknown as Array<[unknown, unknown, { endsDrafts?: unknown[] }]>).at(-1)?.[2]
.endsDrafts;
const regrab = (x: number) => (standIn: Element) =>
drawPreviewDraft(standIn, { translate: { x, y: 0 } });
const box = () => document.getElementById("box")!;

describe("a newer gesture that takes the element before the older save lands", () => {
// fails without the fix: the older save took the end of the draft the regrab now drew on, and its patch
// cleared the regrab's translate for a frame (the mid-drag flash of seqheldtwice-tlhold-px on the edit bench).
it("leaves the regrab's draft alone when the older save lands", async () => {
mount(SCRIPT);
await dragBoxOnSealedFrame(SCRIPT, undefined, regrab(7), async (iframe) => {
await savePosition({ kind: "global-set", props: { x: 110, y: 40 } })(iframe);
expect(savedEnds()).toBeUndefined();
expect(drawnTranslate(box())[0]).not.toBe(0);
});
});

it("puts a cancelled regrab back where the older save left it, not where the older drag began", async () => {
mount(SCRIPT);
await dragBoxOnSealedFrame(SCRIPT, undefined, regrab(7), async (iframe, standIn) => {
await savePosition({ kind: "global-set", props: { x: 110, y: 40 } })(iframe);
endPreviewDraft(standIn, "restore");
await vi.waitFor(() => expect(box().style.translate).toBe(""));
expect(drawnAt(box())).toEqual({ x: 110, y: 40 });
});
});

it("leaves the regrab's draft alone when the older save fails", async () => {
mount(SCRIPT);
await dragBoxOnSealedFrame(SCRIPT, undefined, regrab(7), async (iframe) => {
await drawsSettled();
vi.mocked(previewRequest).mockClear();
vi.mocked(previewAnswer).mockClear();
const ticket = takePreviewTicket(iframe, {
scriptText: SCRIPT,
snapshot: null,
attributes: false,
timing: false,
});
const patch = {
selector: "#gone",
change: { kind: "global-set" as const, props: { x: 1, y: 1 } },
};
patchSealedPreview(ticket, [patch], true, () => undefined);
await vi.waitFor(() => expect(patchRequests()).toHaveLength(1));
await vi.mocked(previewAnswer).mock.results.at(-1)!.value;
await drawsSettled();
expect(draftEnds()).toEqual([]);
expect(drawnTranslate(box())[0]).not.toBe(0);
});
});

it("ends only the newest gesture's draft when three saves land out of order", async () => {
mount(SCRIPT);
await dragBoxOnSealedFrame(SCRIPT, undefined, regrab(7), async (iframe, standIn) => {
armPreviewDraftEnd(standIn, "#box");
regrab(9)(standIn);
await savePosition({ kind: "global-set", props: { x: 105, y: 40 } })(iframe);
expect(savedEnds()).toBeUndefined();
await savePosition({ kind: "global-set", props: { x: 103, y: 40 } })(iframe);
expect(savedEnds()).toBeUndefined();
expect(drawnTranslate(box())[0]).not.toBe(0);
armPreviewDraftEnd(standIn, "#box");
await savePosition({ kind: "global-set", props: { x: 110, y: 40 } })(iframe);
expect(savedEnds()).toEqual([expect.objectContaining({ end: "keep-size" })]);
expect([box().style.translate, drawnAt(box())]).toEqual(["", { x: 110, y: 40 }]);
});
});
});

it("a failed save's restore still takes the draft back", async () => {
mount(SCRIPT);
await dragBoxOnSealedFrame(SCRIPT, undefined, (standIn) => endPreviewDraft(standIn, "restore"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,9 @@ describe("gesture drafts on an opaque frame", () => {
const member = dragMember(element);
applyManualOffsetDragDraft(member, 10, 0);
await settled();
armPreviewDraftEnd(element, "#card");
vi.mocked(previewRequest).mockClear();
applyManualOffsetDragDraft(member, 12, 0);
armPreviewDraftEnd(element, "#card");
const ends = takePreviewDraftEnds(iframe, ["#card"]);
// without the flush the draw still waits, so the end can't ride the save and a late draw could undo it
expect(draftRequests()).toHaveLength(1);
Expand Down
10 changes: 3 additions & 7 deletions packages/studio/src/components/editor/previewDrafts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,9 @@ export function drawPreviewDraft(element: Element, part: Partial<Drawn>): void {
const target = standIns.get(element);
if (!target) return;
const queue = queueFor(target.iframe);
const entry: Entry = {
translate: null,
size: null,
gesture: {},
...queue.drawn.get(target.handle),
...part,
};
const prior = queue.drawn.get(target.handle);
const entry: Entry = { translate: null, size: null, gesture: {}, ...prior, ...part };
if (prior?.saving) delete entry.saving;
queue.drawn.set(target.handle, entry);
const { translate, size, rotate } = entry;
const op = { handle: target.handle, translate, size, ...("rotate" in entry && { rotate }) };
Expand Down
Loading