Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/canvas: fix some issues
Adorian Doran878e05bce97a
diff --git a/apps/client/src/widgets/type_widgets/canvas/drawing_history.tsx b/apps/client/src/widgets/type_widgets/canvas/drawing_history.tsxindex 7c398bfc6a..0a7518ad65 100644--- a/apps/client/src/widgets/type_widgets/canvas/drawing_history.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/drawing_history.tsx@@ -38,6 +38,8 @@ export interface DrawingMount {attach(api: ExcalidrawImperativeAPI, root: HTMLElement | null, store: DrawingStore): void;/** Records the change of the drawing that Excalidraw ended, if any. */record(): void;+ /** Whether this Excalidraw shows the drawing and no other one in the text note does. */+ isOnlyDrawing(): boolean;/*** Shows `scene`, saved elsewhere, outside the undo history. Before `attach()`, it shows nothing* and returns `false`.@@ -124,6 +126,7 @@ export default class DrawingHistory {record: () => {if (drawing) this.record(drawing);},+ isOnlyDrawing: () => !!drawing && this.drawings.size === 1,show: (scene) => {if (!drawing) return false;showScene(drawing, scene);diff --git a/apps/client/src/widgets/type_widgets/canvas/persistence.spec.tsx b/apps/client/src/widgets/type_widgets/canvas/persistence.spec.tsxindex 42c595277f..0506b1eefd 100644--- a/apps/client/src/widgets/type_widgets/canvas/persistence.spec.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/persistence.spec.tsx@@ -352,22 +352,47 @@ describe("useCanvasPersistence saves", () => {);});- it("holds the save while the pointer is down", async () => {+ it("holds the save while the pointer is down, also a save that waits", async () => {const { note, elements } = buildNoteWithPicture(PICTURE_TIME, PICTURE_TIME);await mount(note);sceneElements = elements;change();+ // A change that waits for its save, then a drag.sceneElements = [ { ...elements[0], version: 99 } ];- change({ cursorButton: "down" });- sceneElements = [ { ...elements[0], version: 100 } ];- change({ cursorButton: "down" });- await waitFor(1500);+ change();+ await waitFor(500);+ for (const version of [ 100, 101, 102, 103, 104, 105 ]) {+ sceneElements = [ { ...elements[0], version } ];+ change({ cursorButton: "down" });+ await waitFor(300);+ }expect(puts).toEqual([]);change({ cursorButton: "up" });await waitFor(1500);expect(puts).toHaveLength(1);+ expect(JSON.parse(puts[0].data.content).elements).toMatchObject([ { version: 105 } ]);+ });++ it("keeps the picture of a save made while it checks the loaded picture", async () => {+ const { note, elements } = buildNoteWithPicture(PICTURE_TIME, PICTURE_TIME);+ let finishCheck = () => {};+ const metadata = new Promise((resolve) => {+ finishCheck = () => resolve({ utcDateModified: "2026-10-27 11:00:00.000Z" });+ });+ note.getMetadata = (() => metadata) as never;+ await mount(note);+ sceneElements = elements;+ change();++ sceneElements = [ { ...elements[0], version: 99 } ];+ change();+ await waitFor(1500);+ finishCheck();+ await waitFor(5000);+ expect(vi.mocked(exportToSvg).mock.calls.map(([ { elements } ]) => elements))+ .toEqual([ sceneElements ]);});it("draws a missing or older picture once the note loads, unless read-only", async () => {@@ -461,7 +486,8 @@ describe("useCanvasDrawingPersistence", () => {async function mount(attachment: FAttachment,editor: ContentEditor | undefined,- options?: Parameters<typeof useCanvasDrawingPersistence>[4]+ options?: Parameters<typeof useCanvasDrawingPersistence>[4],+ target = container) {const api = {getSceneElements: () => sceneElements,@@ -484,7 +510,7 @@ describe("useCanvasDrawingPersistence", () => {options={options}/></ParentComponent.Provider>,- container+ target);});return await (drawingProps?.initialData as Promise<ExcalidrawInitialDataState>);@@ -631,6 +657,73 @@ describe("useCanvasDrawingPersistence", () => {expect(getBlob).toHaveBeenCalledOnce();});+ it("loads the saves of its own editor while another embed shows the drawing", async () => {+ const loaded = [ { id: "r1", type: "rectangle", version: 3 } ];+ const { attachment, getBlob } = buildAttachment({ elements: loaded });+ const host: EmbedHostHistory = {+ push: () => ({ isLatest: () => true }),+ undo: vi.fn(),+ redo: vi.fn(),+ isEnabled: () => false,+ subscribe: () => () => {}+ };+ const editor = {+ ...buildEditor(), componentId: "textNote", history: { host, write: vi.fn() }+ };+ const show = async (target?: HTMLElement) => {+ sceneElements = [];+ await mount(attachment, editor, undefined, target);+ change();+ sceneElements = loaded;+ change();+ getBlob.mockClear();+ };+ await show();+ await reportSave({ attachmentId: "a1" }, "textNote");+ expect(getBlob).not.toHaveBeenCalled();++ // Another embed of the drawing in the same text note.+ const other = document.createElement("div");+ await show(other);+ await reportSave({ attachmentId: "a1" }, "textNote");+ expect(getBlob).toHaveBeenCalledTimes(2);+ act(() => render(null, other));+ });++ it("keeps the picture of a save made while it checks the loaded picture", async () => {+ vi.useFakeTimers();+ const post = vi.spyOn(server, "post").mockResolvedValue(undefined);+ let finishCheck = () => {};+ const pictures = new Promise((resolve) => {+ finishCheck = () => resolve([+ { title: "a1.svg", utcDateModified: "2025-12-31 00:00:00.000Z" }+ ]);+ });+ owner.getAttachmentsByRole = (() => pictures) as never;+ const loaded = [ { id: "r1", type: "rectangle", version: 3 } ];+ const { attachment } = buildAttachment({ elements: loaded });+ const editor = buildEditor();+ await mount(attachment, editor);+ change();+ sceneElements = loaded;+ change();++ sceneElements = [ { ...loaded[0], version: 4 } ];+ change();+ const [ getContent, onSaved ] = editor.scheduleSave.mock.calls[0];+ const saved = getContent();+ vi.mocked(exportToSvg).mockResolvedValueOnce(svgOf("picture"));+ await act(async () => {+ onSaved(saved);+ finishCheck();+ await vi.advanceTimersByTimeAsync(30000);+ });+ expect(vi.mocked(exportToSvg).mock.calls.map(([ { elements } ]) => elements))+ .toEqual([ sceneElements ]);+ expect(post).toHaveBeenCalledOnce();+ post.mockRestore();+ });+it("draws a picture older than the drawing once it loads, if it can save one", async () => {vi.useFakeTimers();const post = vi.spyOn(server, "post").mockResolvedValue(undefined);diff --git a/apps/client/src/widgets/type_widgets/canvas/persistence.ts b/apps/client/src/widgets/type_widgets/canvas/persistence.tsindex 6ebcea228c..a26b7899f2 100644--- a/apps/client/src/widgets/type_widgets/canvas/persistence.ts+++ b/apps/client/src/widgets/type_widgets/canvas/persistence.ts@@ -149,7 +149,8 @@ export default function useCanvasPersistence(note: FNote, noteContext: NoteContelibraryItems: libraryResult.libraryItems});if (canSavePicture) {- void refreshNotePicture(note, { elements, appState: content.appState }, files);+ void refreshNotePicture(note, { elements, appState: content.appState }, files,+ () => currentSceneVersion.current === expectedSceneVersion);}} catch (err) {console.error("Failed to prepare initial canvas data", err);@@ -182,6 +183,11 @@ export default function useCanvasPersistence(note: FNote, noteContext: NoteConteloadData(api, content, theme);+ // Initialize tracking state after loading to prevent redundant updates from initial+ // onChange events+ currentSceneVersion.current = getSceneVersion(api.getSceneElements());+ const loadedSceneVersion = currentSceneVersion.current;+// Images live as attachments (titled with their fileId); fetch them, rebuild their// data URLs and inject them so elements referencing those fileIds render. Legacy notes// that still carry inline images in `content.files` were already loaded by loadData().@@ -197,13 +203,11 @@ export default function useCanvasPersistence(note: FNote, noteContext: NoteContefor (const file of [ ...Object.values(content.files ?? {}), ...files ]) {allFiles[file.id] = file;}- void refreshNotePicture(note, content, allFiles);+ void refreshNotePicture(note, content, allFiles,+ () => currentSceneVersion.current === loadedSceneVersion);}});- // Initialize tracking state after loading to prevent redundant updates from initial onChange events- currentSceneVersion.current = getSceneVersion(api.getSceneElements());-// load the library stateloadLibrary(note).then(({ libraryItems, metadata }) => {if (activeNoteIdRef.current !== loadedNoteId) return; // note switched mid-load@@ -373,11 +377,13 @@ export default function useCanvasPersistence(note: FNote, noteContext: NoteConte}}- // Excalidraw reports each move of a drag: the save waits until the pointer is up.- if (hasChanges && appState.cursorButton !== "down") {+ if (hasChanges) {spacedUpdate.resetUpdateTimer();- spacedUpdate.scheduleUpdate();- currentSceneVersion.current = newSceneVersion;+ // Excalidraw reports each move of a drag: the save waits until the pointer is up.+ if (appState.cursorButton !== "down") {+ spacedUpdate.scheduleUpdate();+ currentSceneVersion.current = newSceneVersion;+ }}},onLibraryChange: (libraryItems) => {@@ -451,7 +457,9 @@ export function useCanvasDrawingPersistence(&& !isLoadedSceneEmptyRef.current;// With unsaved changes, their save schedules the picture.if (editor?.canEdit() && unsavedContent === undefined && !isLoadedSceneEmptyRef.current) {- void refreshDrawingPicture(attachment, blobContent, loaded.files);+ const loadedVersion = currentSceneVersion.current;+ void refreshDrawingPicture(attachment, blobContent, loaded.files,+ () => currentSceneVersion.current === loadedVersion);}return {@@ -545,9 +553,12 @@ export function useCanvasDrawingPersistence(appStateToCompare.current = appState;}- // This drawing already shows the saves that `editor` makes.useTriliumEvent("entitiesReloaded", ({ loadResults }) => {- const isSaved = loadResults.getAttachmentRows(editor?.componentId).some((row) =>+ // While no other Excalidraw of the text note shows the drawing, the saves of `editor` come+ // from this one, which shows them already.+ const isOnlyDrawing = !drawingMount || drawingMount.isOnlyDrawing();+ const rows = loadResults.getAttachmentRows(isOnlyDrawing ? editor?.componentId : undefined);+ const isSaved = rows.some((row) =>row.attachmentId === attachment.attachmentId && !row.isDeleted);if (isSaved) {void loadSavedDrawing();@@ -878,12 +889,22 @@ async function saveNotePicture(noteId: string, scene: PictureScene, files: Binar}}-/** Schedules a picture of `scene`, loaded from `note`, when the saved one is missing or older. */-async function refreshNotePicture(note: FNote, scene: PictureScene, files: BinaryFiles) {+/**+ * Schedules a picture of `scene`, loaded from `note`, when the saved one is missing or older.+ * `isCurrent()` returns `false` once a change replaced the scene, which then gets no picture.+ */+async function refreshNotePicture(+ note: FNote,+ scene: PictureScene,+ files: BinaryFiles,+ isCurrent: () => boolean+) {if (!(scene.elements ?? []).some((element) => !element.isDeleted)) return;try {const picture = await getPicture(note, CANVAS_EXPORT_TITLE);- if (!picture || picture.utcDateModified < (await note.getMetadata()).utcDateModified) {+ const isOutdated = !picture+ || picture.utcDateModified < (await note.getMetadata()).utcDateModified;+ if (isOutdated && isCurrent()) {scheduleNotePicture(note.noteId, scene, files);}} catch (e) {@@ -900,13 +921,22 @@ function scheduleDrawingPicture(drawing: FAttachment, content: string, files: Bi);}-/** Schedules a picture of `drawing`, saved as `content`, when the saved one is missing or older. */-async function refreshDrawingPicture(drawing: FAttachment, content: string, files: BinaryFiles) {+/**+ * Schedules a picture of `drawing`, saved as `content`, when the saved one is missing or older.+ * `isCurrent()` returns `false` once a change replaced the content, which then gets no picture.+ */+async function refreshDrawingPicture(+ drawing: FAttachment,+ content: string,+ files: BinaryFiles,+ isCurrent: () => boolean+) {try {const owner = await froca.getNote(drawing.ownerId);if (!owner) return;const picture = await getPicture(owner, getDrawingPictureTitle(drawing.attachmentId));- if (!picture || picture.utcDateModified < drawing.utcDateModified) {+ const isOutdated = !picture || picture.utcDateModified < drawing.utcDateModified;+ if (isOutdated && isCurrent()) {scheduleDrawingPicture(drawing, content, files);}} catch (e) {diff --git a/apps/client/src/widgets/type_widgets/canvas/picture_schedule.spec.ts b/apps/client/src/widgets/type_widgets/canvas/picture_schedule.spec.tsindex a9a365b7c2..501ce5c0f1 100644--- a/apps/client/src/widgets/type_widgets/canvas/picture_schedule.spec.ts+++ b/apps/client/src/widgets/type_widgets/canvas/picture_schedule.spec.ts@@ -45,18 +45,25 @@ describe("schedulePicture", () => {expect(draw).toHaveBeenCalledOnce();});- it("draws the waiting pictures once the page is hidden", async () => {+ it("draws the waiting pictures once the page is hidden, and new ones at once", async () => {const draw = vi.fn(async () => {});schedulePicture("d1", draw, 30000);const visibility = vi.spyOn(document, "visibilityState", "get").mockReturnValue("hidden");document.dispatchEvent(new Event("visibilitychange"));- visibility.mockRestore();await vi.advanceTimersByTimeAsync(0);expect(draw).toHaveBeenCalledOnce();+ // A save that ends after the page is hidden.+ const later = vi.fn(async () => {});+ schedulePicture("d2", later, 30000);+ await vi.advanceTimersByTimeAsync(0);+ expect(later).toHaveBeenCalledOnce();+ visibility.mockRestore();+await vi.advanceTimersByTimeAsync(30000);expect(draw).toHaveBeenCalledOnce();+ expect(later).toHaveBeenCalledOnce();});it("draws the pictures of a drawing one after the other, also after a failed one", async () => {diff --git a/apps/client/src/widgets/type_widgets/canvas/picture_schedule.ts b/apps/client/src/widgets/type_widgets/canvas/picture_schedule.tsindex ff0b8cfcd4..6e35e4b9cf 100644--- a/apps/client/src/widgets/type_widgets/canvas/picture_schedule.ts+++ b/apps/client/src/widgets/type_widgets/canvas/picture_schedule.ts@@ -16,9 +16,9 @@ let isWatchingPage = false;/*** Draws the picture of the drawing `key` with `draw` once `delay` ms pass without another call for- * `key` and the user pauses their input, or once the page is hidden. A later call replaces the- * `draw` that waits. Drawing blocks the main thread for the whole scene, so it waits until the- * user is idle.+ * `key` and the user pauses their input, or at once while the page is hidden. A later call+ * replaces the `draw` that waits. Drawing blocks the main thread for the whole scene, so it waits+ * until the user is idle.*/export function schedulePicture(key: string, draw: () => Promise<void>, delay: number) {const pending = pendingPictures.get(key);@@ -27,6 +27,9 @@ export function schedulePicture(key: string, draw: () => Promise<void>, delay: n}pendingPictures.set(key, { draw, timer: setTimeout(() => drawWhenIdle(key), delay) });watchPage();+ if (document.visibilityState === "hidden") {+ drawPicture(key);+ }}/** Draws the picture of `key` that waits, or waits until the user's input pauses. */