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): put a focus back only over a write of its own epoch

EtienneLescot0caf60155e2e
diff --git a/src/lib/ai-edition/store/useTimeline.test.ts b/src/lib/ai-edition/store/useTimeline.test.ts
index cd510dc83..787f07996 100644
--- a/src/lib/ai-edition/store/useTimeline.test.ts
+++ b/src/lib/ai-edition/store/useTimeline.test.ts
@@ -1353,6 +1353,33 @@ describe("useTimeline undo history", () => {
expect(useProjectStore.getState().document?.zoomRanges[0]?.depth).toBe(3);
});
+ // The commit's focus is put back only over a write of its own epoch. A drag abandoned
+ // (`ZoomFocusOverlay` unmounts when the focus mode flips to auto) and then undone must stay
+ // undone when a bare commit (`endDrag` with nothing in front of it) comes later.
+ it("does not put an abandoned drag's focus back after an undo", async () => {
+ seed(docWithZoom);
+ const { result } = renderTimeline();
+
+ await act(async () => {
+ await result.current.updateZoomDepth("zoom_a", 4);
+ });
+ act(() => result.current.updateZoomFocusLive("zoom_a", { cx: 0.8, cy: 0.2 }));
+ act(() => {
+ expect(undo()).toBe(true);
+ });
+ expect(future).toHaveLength(1);
+
+ await act(async () => {
+ await result.current.commitZoomFocus();
+ });
+
+ expect(useProjectStore.getState().document?.zoomRanges[0]?.focus).toEqual({
+ cx: 0.5,
+ cy: 0.5,
+ });
+ expect(future).toHaveLength(1);
+ });
+
// 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 259d15edc..25c37e82e 100644
--- a/src/lib/ai-edition/store/useTimeline.ts
+++ b/src/lib/ai-edition/store/useTimeline.ts
@@ -135,8 +135,13 @@ export function useTimeline() {
// was never written.
const zoomFocusRollbackRef = useRef<AxcutDocument | null>(null);
const zoomFocusLiveRef = useRef<AxcutDocument | null>(null);
- // And the focus drag's last value, which its commit puts back if a zoom write lands over it.
- const zoomFocusEditRef = useRef<{ id: string; focus: { cx: number; cy: number } } | null>(null);
+ // And the focus drag's last value, which its commit puts back if a zoom write lands over it,
+ // with the write epoch it was made in.
+ const zoomFocusEditRef = useRef<{
+ id: string;
+ focus: { cx: number; cy: number };
+ epoch: number;
+ } | null>(null);
const annotationRollbackRef = useRef<AxcutDocument | null>(null);
const annotationLiveRef = useRef<AxcutDocument | null>(null);
@@ -752,7 +757,11 @@ export function useTimeline() {
// behind when the commit failed and rolled the document back to that very
// document — a Ctrl+Z that visibly did nothing, with `future` already wiped.
if (zoomFocusLiveRef.current !== doc) zoomFocusRollbackRef.current = doc;
- const edit = { id, focus: { cx: finiteFraction(focus.cx), cy: finiteFraction(focus.cy) } };
+ const edit = {
+ id,
+ focus: { cx: finiteFraction(focus.cx), cy: finiteFraction(focus.cy) },
+ epoch: currentWriteEpoch(),
+ };
const next: AxcutDocument = {
...doc,
zoomRanges: patchPillById(doc.zoomRanges, id, {
@@ -774,7 +783,10 @@ export function useTimeline() {
// place of the dragged one.
const live = zoomFocusLiveRef.current;
const rollback = zoomFocusRollbackRef.current;
- const edit = zoomFocusEditRef.current;
+ // A drag abandoned before an undo or a replacement is not this commit's to put back: the
+ // document it was made on is gone, and restoring its focus would undo the undo.
+ const pendingEdit = zoomFocusEditRef.current;
+ const edit = pendingEdit?.epoch === currentWriteEpoch() ? pendingEdit : null;
zoomFocusRollbackRef.current = null;
zoomFocusLiveRef.current = null;
zoomFocusEditRef.current = null;