Skip to content

Build your personal knowledge base with Trilium Notes

fix(canvas): keep note switches out of the undo store (closes #7148)

Elian Dorand77a53946dd7
diff --git a/apps/client/src/widgets/type_widgets/canvas/persistence.spec.tsx b/apps/client/src/widgets/type_widgets/canvas/persistence.spec.tsx
index 4911b7839b..434612cd97 100644
--- a/apps/client/src/widgets/type_widgets/canvas/persistence.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/canvas/persistence.spec.tsx
@@ -30,7 +30,8 @@ vi.mock("@excalidraw/excalidraw", () => ({
// Mirrors the real behavior closely enough for change tracking: the version of a scene
// is derived from its elements, and an empty scene is always version 0.
getSceneVersion: (elements: FakeElement[]) => elements.reduce((sum, el) => sum + (el.version ?? 0), 0),
- exportToSvg: vi.fn(async () => ({ outerHTML: "<svg/>" }))
+ exportToSvg: vi.fn(async () => ({ outerHTML: "<svg/>" })),
+ CaptureUpdateAction: { IMMEDIATELY: "IMMEDIATELY", NEVER: "NEVER", EVENTUALLY: "EVENTUALLY" }
}));
vi.stubGlobal("logError", vi.fn());
@@ -174,6 +175,9 @@ describe("useCanvasPersistence content loading (#10279)", () => {
expect(updateScene).toHaveBeenCalledTimes(1);
expect(loadedElementIds(updateScene)).toEqual([ "b1" ]);
+ // A note-switch load is scene initialization: it must never enter the undo store,
+ // or undoing the first stroke would restore the previous note's scene (#7148).
+ expect(updateScene.mock.calls[0][0]).toMatchObject({ captureUpdate: "NEVER" });
});
it("stashes a subsequent note's content while the API is unavailable and replays it on arrival", async () => {
diff --git a/apps/client/src/widgets/type_widgets/canvas/persistence.ts b/apps/client/src/widgets/type_widgets/canvas/persistence.ts
index f3a509cc5f..5cc9868c08 100644
--- a/apps/client/src/widgets/type_widgets/canvas/persistence.ts
+++ b/apps/client/src/widgets/type_widgets/canvas/persistence.ts
@@ -1,4 +1,4 @@
-import { exportToSvg, getSceneVersion } from "@excalidraw/excalidraw";
+import { CaptureUpdateAction, exportToSvg, getSceneVersion } from "@excalidraw/excalidraw";
import { ExcalidrawElement, NonDeletedExcalidrawElement } from "@excalidraw/excalidraw/element/types";
import { AppState, BinaryFileData, BinaryFiles, ExcalidrawImperativeAPI, ExcalidrawInitialDataState, ExcalidrawProps, LibraryItem } from "@excalidraw/excalidraw/types";
import { deferred, type DeferredPromise } from "@triliumnext/commons";
@@ -430,7 +430,11 @@ function loadData(api: ExcalidrawImperativeAPI, content: CanvasContent, theme: A
// TODO: Fix type of sceneData
api.updateScene({
elements,
- appState: appState as AppState
+ appState: appState as AppState,
+ // Scene initialization must be excluded from the undo store. The default (EVENTUALLY)
+ // folds this load into the user's next captured action, so undoing their first stroke
+ // would restore the previously displayed note's scene (#7148).
+ captureUpdate: CaptureUpdateAction.NEVER
});
api.addFiles(fileArray);
api.history.clear();
diff --git a/apps/client/src/widgets/type_widgets/canvas/useCanvasNoteDrop.spec.tsx b/apps/client/src/widgets/type_widgets/canvas/useCanvasNoteDrop.spec.tsx
index db3a2a4144..c06bca6ae7 100644
--- a/apps/client/src/widgets/type_widgets/canvas/useCanvasNoteDrop.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/canvas/useCanvasNoteDrop.spec.tsx
@@ -10,7 +10,8 @@ const viewportCoordsToSceneCoords = vi.fn(() => ({ x: 100, y: 200 }));
vi.mock("@excalidraw/excalidraw", () => ({
restoreElements: (...args: unknown[]) => restoreElements(...(args as [unknown[]])),
- viewportCoordsToSceneCoords: () => viewportCoordsToSceneCoords()
+ viewportCoordsToSceneCoords: () => viewportCoordsToSceneCoords(),
+ CaptureUpdateAction: { IMMEDIATELY: "IMMEDIATELY", NEVER: "NEVER", EVENTUALLY: "EVENTUALLY" }
}));
type Handlers = ReturnType<typeof useCanvasNoteDrop>;
@@ -109,9 +110,11 @@ describe("useCanvasNoteDrop", () => {
// The second note is offset by STACK_OFFSET (24) from the first on both axes.
expect(partials[1]).toMatchObject({ x: 124, y: 224, link: "root/bbb" });
- const { elements } = updateScene.mock.calls[0][0];
+ const { elements, captureUpdate } = updateScene.mock.calls[0][0];
expect(elements).toHaveLength(3); // 1 existing + 2 new
expect(elements[0]).toEqual({ id: "existing" });
+ // The drop must be its own undo step (#7148).
+ expect(captureUpdate).toBe("IMMEDIATELY");
});
it("does nothing when read-only", async () => {
diff --git a/apps/client/src/widgets/type_widgets/canvas/useCanvasNoteDrop.ts b/apps/client/src/widgets/type_widgets/canvas/useCanvasNoteDrop.ts
index 6cdb42cc0e..b3a659d511 100644
--- a/apps/client/src/widgets/type_widgets/canvas/useCanvasNoteDrop.ts
+++ b/apps/client/src/widgets/type_widgets/canvas/useCanvasNoteDrop.ts
@@ -1,4 +1,4 @@
-import { restoreElements, viewportCoordsToSceneCoords } from "@excalidraw/excalidraw";
+import { CaptureUpdateAction, restoreElements, viewportCoordsToSceneCoords } from "@excalidraw/excalidraw";
import { ExcalidrawEmbeddableElement } from "@excalidraw/excalidraw/element/types";
import { ExcalidrawImperativeAPI } from "@excalidraw/excalidraw/types";
import { RefObject } from "preact";
@@ -64,7 +64,9 @@ export default function useCanvasNoteDrop(apiRef: RefObject<ExcalidrawImperative
})) as ExcalidrawEmbeddableElement[];
const newElements = restoreElements(partialElements, null);
- api.updateScene({ elements: [...api.getSceneElements(), ...newElements] });
+ // A drop is a user action and must be its own undo step; without IMMEDIATELY it would
+ // only be captured as part of the next action (#7148).
+ api.updateScene({ elements: [...api.getSceneElements(), ...newElements], captureUpdate: CaptureUpdateAction.IMMEDIATELY });
}, [apiRef, isReadOnly]);
return { onDragOverCapture, onDropCapture };