Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(timeline): let a zoom write that saves a live drag undo it too

EtienneLescot91299157f882
diff --git a/src/lib/ai-edition/store/useTimeline.test.ts b/src/lib/ai-edition/store/useTimeline.test.ts
index d0a220095..0a671789a 100644
--- a/src/lib/ai-edition/store/useTimeline.test.ts
+++ b/src/lib/ai-edition/store/useTimeline.test.ts
@@ -1380,6 +1380,30 @@ describe("useTimeline undo history", () => {
expect(future).toHaveLength(1);
});
+ // A zoom write that runs while a focus drag is on screen saves the dragged focus too, so the
+ // commit finds nothing left to record. That write's undo step has to reach back to before
+ // the drag, or no Ctrl+Z could ever bring the old focus back.
+ it("undoes a focus drag with the zoom write that saved it", async () => {
+ seed(docWithZoom);
+ const { result } = renderTimeline();
+
+ act(() => result.current.updateZoomFocusLive("zoom_a", { cx: 0.8, cy: 0.2 }));
+ await act(async () => {
+ await result.current.updateZoomDepth("zoom_a", 4);
+ });
+ await act(async () => {
+ await result.current.commitZoomFocus();
+ });
+
+ act(() => {
+ expect(undo()).toBe(true);
+ });
+ expect(useProjectStore.getState().document?.zoomRanges[0]).toMatchObject({
+ depth: 3,
+ focus: { cx: 0.5, cy: 0.5 },
+ });
+ });
+
// Rebase-review finding (queued zoom writes vs. document replacement): a zoom write
// queued behind a still-pending one starts AFTER an undo has restored the document,
// and must not apply its stale patch to the replacement. The in-flight write itself
diff --git a/src/lib/ai-edition/store/useTimeline.ts b/src/lib/ai-edition/store/useTimeline.ts
index 267917ca4..a10af9ce5 100644
--- a/src/lib/ai-edition/store/useTimeline.ts
+++ b/src/lib/ai-edition/store/useTimeline.ts
@@ -661,7 +661,7 @@ export function useTimeline() {
// epoch, so there the stuck save can still land and the block correctly stays.
const unknownZoomSavesRef = useRef<Array<number>>([]);
const queueZoomWrite = useCallback(
- (write: (doc: AxcutDocument) => Promise<boolean>) => {
+ (write: (doc: AxcutDocument, historyBase: AxcutDocument | undefined) => Promise<boolean>) => {
const epoch = currentWriteEpoch();
const projectId = useProjectStore.getState().projectId;
return enqueueZoomWrite(async () => {
@@ -673,7 +673,14 @@ export function useTimeline() {
}
const doc = useProjectStore.getState().document;
if (!doc) return false;
- const save = write(doc);
+ // A focus drag not yet committed is on screen, so this write saves it too, and the
+ // commit will find nothing left to record. Its undo step reaches back to before
+ // the drag instead, or the drag could never be undone.
+ const historyBase =
+ doc === zoomFocusLiveRef.current
+ ? (zoomFocusRollbackRef.current ?? undefined)
+ : undefined;
+ const save = write(doc, historyBase);
const outcome = await saveWithDeadline(save);
if (outcome === "timeout") {
// Unknown, not failed: the write may still land. Refuse later writes into
@@ -700,13 +707,13 @@ export function useTimeline() {
// Reports not-taken for an unknown answer too, so the buttons retry.
const saveZoomPatch = useCallback(
(id: string, patch: Partial<AxcutDocument["zoomRanges"][number]>) =>
- queueZoomWrite((doc) =>
+ queueZoomWrite((doc, historyBase) =>
saveDocument(
{
...doc,
zoomRanges: patchPillById(doc.zoomRanges, id, patch) as AxcutDocument["zoomRanges"],
},
- { history: true },
+ { history: true, historyBase },
),
).then((outcome) => outcome === true),
[queueZoomWrite, saveDocument],
@@ -720,7 +727,7 @@ export function useTimeline() {
(id: string, startMs: number, endMs: number) => {
const s = finiteMs(startMs);
const e = finiteMs(endMs);
- return queueZoomWrite((doc) =>
+ return queueZoomWrite((doc, historyBase) =>
saveDocument(
{
...doc,
@@ -733,7 +740,7 @@ export function useTimeline() {
() => createId("zoom"),
) as AxcutDocument["zoomRanges"],
},
- { history: true },
+ { history: true, historyBase },
),
);
},