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
40 changes: 39 additions & 1 deletion packages/studio/src/hooks/useGestureRecording.sealed.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,21 @@ function startOn(
base: EditingGestureBase | null,
elementEnd?: number,
liveGsap?: Record<string, number>,
emptyTimeline = false,
) {
const iframe = document.createElement("iframe");
document.body.append(iframe);
if (liveGsap)
Object.assign(iframe.contentWindow!, {
gsap: { getProperty: (_element: Element, prop: string) => liveGsap[prop] },
gsap: { getProperty: (_element: Element, prop: string) => liveGsap[prop], set: vi.fn() },
...(emptyTimeline && {
__timelines: { main: { duration: () => 0, seek: vi.fn() } },
__player: { getTime: () => usePlayerStore.getState().currentTime },
}),
});
vi.spyOn(iframe, "getBoundingClientRect").mockReturnValue(new DOMRect(0, 0, 960, 540));
const standIn = document.createElement("div");
standIn.id = "card";
let recording: ReturnType<typeof useGestureRecording> | null = null;
function Harness() {
recording = useGestureRecording();
Expand Down Expand Up @@ -182,3 +188,35 @@ describe("a gesture recording on a sealed preview", () => {
expect([scaledBy(false), scaledBy(true)]).toEqual([1.6, 1.6]);
});
});

describe("a recording on a film whose own timeline is empty", () => {
const AT_REST_GSAP = { x: 0, y: 0, opacity: 1, scaleX: 1 };
const recordMove = (sealed: boolean) => {
ticks = [];
nowMs = 0;
frame.sealed = sealed;
frame.stage = { width: 1920, height: 1080 };
usePlayerStore.setState({ currentTime: 2, duration: 10, requestedSeekTime: null });
const { tick, stop } = startOn(sealed ? AT_REST : null, 2.5, AT_REST_GSAP, !sealed);
const move = (clientX: number, ms: number) => {
document.dispatchEvent(new MouseEvent("pointermove", { bubbles: true, clientX }));
tick(ms);
};
move(0, 0);
move(100, 300);
move(100, 900);
return stop().map(({ time, properties }) => ({ time, properties }));
};

// fails without the fix: unsealed capped the recording at its timeline's own length, 0 here, so every sample
// landed at 0 s, the playhead never moved and the recording saved nothing.
it("records over time unsealed, up to the layer's end within the film", () => {
const samples = recordMove(false);
expect(samples.at(-1)?.time).toBe(0.5);
expect(usePlayerStore.getState().currentTime).toBe(2.5);
});

it("records the same samples sealed and unsealed", () => {
expect(recordMove(false)).toEqual(recordMove(true));
});
});
13 changes: 5 additions & 8 deletions packages/studio/src/hooks/useGestureRecording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function readBasePosition(element: HTMLElement, gsap: IframeGsap | null): BasePo
function connectGsapRuntime(
element: HTMLElement,
iframeEl: HTMLIFrameElement,
elementEndTime: number | undefined,
recordingEnd: number,
): GsapRuntime | null {
try {
const win = iframeEl.contentWindow as Window & {
Expand All @@ -101,14 +101,12 @@ function connectGsapRuntime(
)?.[1] ?? null)
: null;
if (win?.gsap?.set && tl?.seek && element.id) {
const tlDuration = tl.duration();
return {
timeline: tl,
gsap: win.gsap,
element,
startTime: win.__player?.getTime() ?? 0,
maxSeekTime:
elementEndTime != null && elementEndTime < tlDuration ? elementEndTime : tlDuration,
maxSeekTime: recordingEnd,
savedVisibility: element.style.visibility,
savedTranslate: element.style.getPropertyValue("translate"),
putMarkBack: null,
Expand Down Expand Up @@ -354,12 +352,11 @@ export function useGestureRecording() {
r.scale = scale;

// --- Phase 3: Connect to the iframe GSAP runtime ---
r.runtime = connectGsapRuntime(element, iframeEl, elementEndTime);
const { currentTime, duration } = usePlayerStore.getState();
const recordingEnd = Math.min(elementEndTime ?? Infinity, duration);
r.runtime = connectGsapRuntime(element, iframeEl, recordingEnd);
r.drawsOn = sealed ? element : null;
r.playhead = sealed
? { start: currentTime, end: Math.min(elementEndTime ?? Infinity, duration) }
: null;
r.playhead = sealed ? { start: currentTime, end: recordingEnd } : null;
const seekable = r.runtime
? { start: r.runtime.startTime, end: r.runtime.maxSeekTime }
: r.playhead;
Expand Down
Loading