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/text editor/drawing canvas: fix an issue
Adorian Doran8bdeb9f21199
diff --git a/.claude/skills/developing-content-embeds/SKILL.md b/.claude/skills/developing-content-embeds/SKILL.mdindex 64e9111f87..200a40fd41 100644--- a/.claude/skills/developing-content-embeds/SKILL.md+++ b/.claude/skills/developing-content-embeds/SKILL.md@@ -169,10 +169,10 @@ changes. Three layers, with one recorder per editor in the middle:`isLatest()`. `api.registerAction()` replaces Excalidraw's `undo`/`redo` by name, so its keys,its footer buttons and `CanvasTools` (which clicks `button-undo`) reach the host; thereplacement button copies Excalidraw's `ToolButton` markup. Without a mounted Excalidraw, a step- patches the scene the last one left and saves it through the `write` it attached with; a mount- that loaded before such a step (an undone Editable toggle remounts) gets the scene at attach.- `DrawingPicture` passes each content it loads to `load()`, so a step patches what was saved- elsewhere since, not the old scene. Undo sorts by fractional index when a step sets `index`,+ reads the drawing through the `DrawingStore` it attached with (the unsaved content, else the+ blob, so a save made elsewhere since stays), patches it and writes it back; the steps queue one+ after another. A mount that loaded before such a step (an undone Editable toggle remounts) reads+ it again at attach. Undo sorts by fractional index when a step sets `index`,as Excalidraw renumbers an out-of-order array instead of reordering it.`excalidraw_contract.spec.ts` pins the Excalidraw internals this relies on.- **`EmbedHistories`** (`text/embed_history.ts`, one per `EditableText`) gives a `ContentHistory`diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsxindex 6827b011d7..83b45a64f1 100644--- a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx@@ -23,7 +23,6 @@ import { useEditableEmbed } from "../text/editable_embed";import { CanvasEditor } from "./Canvas";import CanvasDrawingMenu from "./CanvasDrawingMenu";import CanvasEmbedTools, { CanvasTools } from "./CanvasEmbedTools";-import type { DrawingScene } from "./drawing_history";import { type DrawingView, getBoundedView, getCenteredView, getFittedView } from "./drawing_view";import {exportDrawingPicture, loadDrawingFiles, parseContent, useCanvasDrawingPersistence@@ -204,14 +203,12 @@ function DrawingPicture({let isCurrent = true;// Starts on the next frame, so that a drawing whose embed turns out editable renders none.const frame = requestAnimationFrame(async () => {- const loadHistoryScene = editor?.history?.drawing?.load();const content = editor?.getUnsavedContent()?? (await attachment.getBlob())?.content ?? "";const key = `${colorScheme}\n${content}`;if (key === renderedKeyRef.current) return;const rendered = await renderPictureScene(attachment, content, colorScheme === "dark");- loadHistoryScene?.(rendered.drawing);if (isCurrent) {renderedKeyRef.current = key;setScene(rendered);@@ -269,8 +266,6 @@ interface ScenePicture {}interface PictureScene {- /** The drawing that the content saves. */- drawing: DrawingScene;/** The picture of the content, or `null` for an empty drawing. */picture: ScenePicture | null;/** The shapes that are not deleted. */@@ -295,16 +290,14 @@ async function renderPictureScene(const background = viewBackgroundColor && viewBackgroundColor !== "transparent"? viewBackgroundColor: null;- const files = (await loadDrawingFiles(attachment, scene)).files;- const drawing = { elements: scene.elements ?? [], appState: scene.appState ?? {}, files };if (!elements.length) {- return { drawing, picture: null, elements, savedView, background };+ return { picture: null, elements, savedView, background };}const svg = await exportToSvg({elements,appState: { ...scene.appState, exportBackground: false, exportWithDarkMode: isDark },- files,+ files: (await loadDrawingFiles(attachment, scene)).files,exportPadding: 0});const width = Number(svg.getAttribute("width"));@@ -313,7 +306,6 @@ async function renderPictureScene(// above the frames, so it is placed by the bottom right corner of the shapes.const [ , , maxX, maxY ] = getCommonBounds(getRootElements(elements));return {- drawing,picture: { svg, x: maxX - width, y: maxY - height, width, height },elements,savedView,diff --git a/apps/client/src/widgets/type_widgets/canvas/drawing_history.spec.tsx b/apps/client/src/widgets/type_widgets/canvas/drawing_history.spec.tsxindex 5bb9ccef81..c9ddda119d 100644--- a/apps/client/src/widgets/type_widgets/canvas/drawing_history.spec.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/drawing_history.spec.tsx@@ -10,7 +10,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";import type { EmbedHostHistory, HistoryCommand } from "../text/embed_history";import DrawingHistory, {- applyChanges, type ElementChange, getChangedKeys+ applyChanges, type DrawingScene, type DrawingStore, type ElementChange, getChangedKeys} from "./drawing_history";vi.mock("@excalidraw/excalidraw", () => ({@@ -34,7 +34,7 @@ describe("DrawingHistory", () => {const { host, steps } = createHost();const drawing = createDrawing([ RECTANGLE ]);const mount = new DrawingHistory(host).mount();- mount.attach(drawing.api, drawing.root, vi.fn());+ mount.attach(drawing.api, drawing.root, createStore([]));expect([ ...drawing.actions.keys() ]).toEqual([ "undo", "redo" ]);// A drag ends once the pointer is up.@@ -80,7 +80,7 @@ describe("DrawingHistory", () => {const drawing = createDrawing([]);drawing.files.f1 = IMAGE_FILE;const mount = new DrawingHistory(host).mount();- mount.attach(drawing.api, drawing.root, vi.fn());+ mount.attach(drawing.api, drawing.root, createStore([]));drawing.input("drop");drawing.setElements([ IMAGE ]);@@ -102,69 +102,47 @@ describe("DrawingHistory", () => {expect(steps).toHaveLength(2);});- it("changes the drawing while no Excalidraw shows it, and shows that in the next one", () => {- const { host, steps } = createHost();- const history = new DrawingHistory(host);- const write = vi.fn();- const first = createDrawing([ RECTANGLE ]);- const firstMount = history.mount();- firstMount.attach(first.api, first.root, write);- first.input("pointerdown");- first.setElements([ { ...RECTANGLE, x: 10, version: 2 } ]);- firstMount.record();- firstMount.detach();- expect(steps).toHaveLength(1);-- // The next Excalidraw loads the saved drawing before the step applies.- const nextMount = history.mount();- expect(steps[0].undo()).toBe(true);- expect(write).toHaveBeenCalledOnce();- expect(write.mock.calls[0][0].elements).toMatchObject([ { id: "r1", x: 0 } ]);-- const next = createDrawing([ { ...RECTANGLE, x: 10, version: 2 } ]);- nextMount.attach(next.api, next.root, write);- expect(next.getElements()).toMatchObject([ { id: "r1", x: 0 } ]);-- const later = createDrawing([ { ...RECTANGLE, version: 3 } ]);- history.mount().attach(later.api, later.root, write);- expect(later.api.updateScene).not.toHaveBeenCalled();- // A step applies to every Excalidraw that shows the drawing.- steps[0].redo();- expect(next.getElements()[0]).toMatchObject({ x: 10 });- expect(later.getElements()[0]).toMatchObject({ x: 10 });- expect(write).toHaveBeenCalledOnce();- });-- it("applies a step to the drawing as saved elsewhere while no Excalidraw shows it", () => {- const { host, steps } = createHost();- const history = new DrawingHistory(host);- const write = vi.fn();- const drawing = createDrawing([ RECTANGLE ]);- const mount = history.mount();- mount.attach(drawing.api, drawing.root, write);- drawing.input("pointerdown");- drawing.setElements([ { ...RECTANGLE, x: 10, version: 2 } ]);- mount.record();- mount.detach();-- history.load()({- elements: [ { ...RECTANGLE, x: 10, version: 2 }, ELLIPSE ], appState: {}, files: {}+ it("changes the drawing as saved while no Excalidraw shows it, and shows that in the next one",+ async () => {+ const { host, steps } = createHost();+ const history = new DrawingHistory(host);+ const moved = { ...RECTANGLE, x: 10, version: 2 };+ const store = createStore([ RECTANGLE ]);+ const first = createDrawing([ RECTANGLE ]);+ const firstMount = history.mount();+ firstMount.attach(first.api, first.root, store);+ first.input("pointerdown");+ first.setElements([ moved ]);+ firstMount.record();+ firstMount.detach();+ expect(steps).toHaveLength(1);++ // Saved elsewhere once Excalidraw went away, and loaded by the next one.+ store.setElements([ moved, ELLIPSE ]);+ const nextMount = history.mount();+ expect(steps[0].undo()).toBe(true);+ await vi.waitFor(() => expect(store.write).toHaveBeenCalledOnce());+ expect(store.getElements()).toMatchObject([ { id: "r1", x: 0 }, { id: "e1" } ]);++ const next = createDrawing([ moved, ELLIPSE ]);+ nextMount.attach(next.api, next.root, store);+ await vi.waitFor(() => expect(next.getElements())+ .toMatchObject([ { id: "r1", x: 0 }, { id: "e1" } ]));++ const later = createDrawing([ { ...RECTANGLE, version: 3 }, ELLIPSE ]);+ history.mount().attach(later.api, later.root, store);+ expect(later.api.updateScene).not.toHaveBeenCalled();+ // A step applies to every Excalidraw that shows the drawing.+ steps[0].redo();+ expect(next.getElements()[0]).toMatchObject({ x: 10 });+ expect(later.getElements()[0]).toMatchObject({ x: 10 });+ expect(store.write).toHaveBeenCalledOnce();});- steps[0].undo();- expect(write.mock.calls[0][0].elements).toMatchObject([ { id: "r1", x: 0 }, { id: "e1" } ]);-- // A load that started before the step is older than the drawing the step leaves.- const staleLoad = history.load();- steps[0].redo();- staleLoad({ elements: [ RECTANGLE ], appState: {}, files: {} });- steps[0].undo();- expect(write.mock.calls[2][0].elements).toMatchObject([ { id: "r1", x: 0 }, { id: "e1" } ]);- });it("undoes and redoes the note with the keys and the buttons of Excalidraw", () => {const { host, setEnabled } = createHost();const drawing = createDrawing([]);- new DrawingHistory(host).mount().attach(drawing.api, drawing.root, vi.fn());+ new DrawingHistory(host).mount().attach(drawing.api, drawing.root, createStore([]));const undo = drawing.actions.get("undo");const redo = drawing.actions.get("redo");expect(undo && redo).toBeTruthy();@@ -286,6 +264,24 @@ function createHost() {};}+/** The saved drawing, which shows `elements`. */+function createStore(elements: ExcalidrawElement[]) {+ let saved: DrawingScene = { elements, appState: {}, files: {} };+ const store = {+ read: vi.fn(async () => saved),+ write: vi.fn((scene: DrawingScene) => {+ saved = scene;+ })+ } satisfies DrawingStore;+ return {+ ...store,+ getElements: () => saved.elements,+ setElements: (next: ExcalidrawElement[]) => {+ saved = { ...saved, elements: next };+ }+ };+}+/** An Excalidraw that shows `elements`, in a root with its focusable container. */function createDrawing(elements: ExcalidrawElement[]) {let scene = elements;diff --git a/apps/client/src/widgets/type_widgets/canvas/drawing_history.tsx b/apps/client/src/widgets/type_widgets/canvas/drawing_history.tsxindex b37439a4af..1ad332d8a9 100644--- a/apps/client/src/widgets/type_widgets/canvas/drawing_history.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/drawing_history.tsx@@ -21,18 +21,21 @@ export interface DrawingScene {files: BinaryFiles;}+/** Reads and saves the drawing while no Excalidraw shows it. */+export interface DrawingStore {+ /** The drawing as saved, with the changes that wait for a save. */+ read(): Promise<DrawingScene>;+ write(scene: DrawingScene): void;+}+/** An Excalidraw that shows the drawing, from the moment it starts to load the drawing. */export interface DrawingMount {/*** Records the changes of `api` from now on, once it shows the loaded drawing. Input inside- * `root` marks the changes that the user makes. `write` saves the drawing once no Excalidraw- * shows it.+ * `root` marks the changes that the user makes. `store` reads and saves the drawing once no+ * Excalidraw shows it.*/- attach(- api: ExcalidrawImperativeAPI,- root: HTMLElement | null,- write: (scene: DrawingScene) => void- ): void;+ attach(api: ExcalidrawImperativeAPI, root: HTMLElement | null, store: DrawingStore): void;/** Records the change of the drawing that Excalidraw ended, if any. */record(): void;/** Stops recording, before Excalidraw unmounts. */@@ -86,18 +89,15 @@ export const HISTORY_ICON_PATHS: Record<HistoryCommand, string> = {* The undo history of a canvas drawing, kept on the undo stack of the text note that embeds it.* Each Excalidraw that shows the drawing records a step once the user ends a change, and its Undo* and Redo act on the last change of the note. A step changes every Excalidraw that shows the- * drawing, or the drawing through the last `write` while none does.+ * drawing, or the drawing as saved, through the last `DrawingStore`, while none does.*/export default class DrawingHistory {/** Counts the steps applied, so that an Excalidraw that loaded before one shows it. */private revision = 0;private readonly drawings = new Set<AttachedDrawing>();- /**- * The drawing of the last Excalidraw that went away, or as it saved since, for the steps- * applied without one.- */- private scene: DrawingScene | undefined;- private write: ((scene: DrawingScene) => void) | undefined;+ private store: DrawingStore | undefined;+ /** The steps applied through `store`, which read the drawing one after another. */+ private storeUpdates = Promise.resolve();private lastStep: RecordedStep | undefined;constructor(private readonly host: EmbedHostHistory) {}@@ -107,8 +107,8 @@ export default class DrawingHistory {const revision = this.revision;let drawing: AttachedDrawing | undefined;return {- attach: (api, root, write) => {- drawing = this.attach(api, root, write, revision);+ attach: (api, root, store) => {+ drawing = this.attach(api, root, store, revision);},record: () => {if (drawing) this.record(drawing);@@ -120,42 +120,18 @@ export default class DrawingHistory {};}- /**- * Takes the drawing as its content saves it, which loads now, for the steps applied while no- * Excalidraw shows it. A step applied before the returned call ends the load in vain.- */- load(): (scene: DrawingScene) => void {- const revision = this.revision;- return (scene) => {- if (revision === this.revision && !this.drawings.size) {- this.scene = scene;- }- };- }-private attach(api: ExcalidrawImperativeAPI,root: HTMLElement | null,- write: (scene: DrawingScene) => void,+ store: DrawingStore,revision: number) {- const scene = revision === this.revision ? undefined : this.getLatestScene();- const background = scene?.appState.viewBackgroundColor- ?? api.getAppState().viewBackgroundColor;- if (scene) {- addMissingFiles(api, scene.files);- api.updateScene({- elements: scene.elements,- appState: { viewBackgroundColor: background },- captureUpdate: CaptureUpdateAction.NEVER- });- }-+ const [ other ] = this.drawings;const drawing: AttachedDrawing = {api,root,committed: new Map(),- background,+ background: api.getAppState().viewBackgroundColor,hasInput: false,stopListening: () => {}};@@ -179,16 +155,38 @@ export default class DrawingHistory {}this.drawings.add(drawing);- this.write = write;+ this.store = store;+ // Excalidraw loaded the drawing before a step changed it.+ if (revision !== this.revision) {+ if (other) {+ showScene(drawing, getDrawingScene(other.api));+ } else {+ this.showStoredScene(drawing, store);+ }+ }return drawing;}private detach(drawing: AttachedDrawing) {drawing.stopListening();this.drawings.delete(drawing);- if (!this.drawings.size) {- this.scene = getDrawingScene(drawing.api);- }+ }++ /** Shows the drawing of `store` in `drawing` once the steps applied through it saved. */+ private showStoredScene(drawing: AttachedDrawing, store: DrawingStore) {+ const revision = this.revision;+ this.queueStoreUpdate(async () => {+ const scene = await store.read();+ if (revision === this.revision && this.drawings.has(drawing)) {+ showScene(drawing, scene);+ }+ });+ }++ private queueStoreUpdate(update: () => Promise<void>) {+ this.storeUpdates = this.storeUpdates.then(update).catch((error: unknown) => {+ console.error("Unable to update the canvas drawing", error);+ });}/**@@ -236,9 +234,12 @@ export default class DrawingHistory {queueMicrotask(() => {first.root?.querySelector<HTMLElement>(".excalidraw-container")?.focus();});- } else if (this.scene) {- this.scene = applyToScene(this.scene, step, isUndo);- this.write?.(this.scene);+ } else if (this.store) {+ // Applies to the drawing as saved, which can have changed elsewhere since.+ const { store } = this;+ this.queueStoreUpdate(async () => {+ store.write(applyToScene(await store.read(), step, isUndo));+ });} else {return false;}@@ -246,11 +247,6 @@ export default class DrawingHistory {this.revision++;return true;}-- private getLatestScene() {- const [ drawing ] = this.drawings;- return drawing ? getDrawingScene(drawing.api) : this.scene;- }}/** The history of the drawing of `history`, which the first call creates. */@@ -365,6 +361,20 @@ function applyToDrawing(drawing: AttachedDrawing, step: DrawingStep, isUndo: boodrawing.hasInput = false;}+/** Shows `scene` in `drawing`, outside the undo history. */+function showScene(drawing: AttachedDrawing, scene: DrawingScene) {+ const { api } = drawing;+ const background = scene.appState.viewBackgroundColor ?? drawing.background;+ addMissingFiles(api, scene.files);+ api.updateScene({+ elements: scene.elements,+ appState: { viewBackgroundColor: background },+ captureUpdate: CaptureUpdateAction.NEVER+ });+ commitElements(drawing);+ drawing.background = background;+}+function applyToScene(scene: DrawingScene, step: DrawingStep, isUndo: boolean): DrawingScene {const background = step.background?.[isUndo ? 0 : 1];return {diff --git a/apps/client/src/widgets/type_widgets/canvas/persistence.spec.tsx b/apps/client/src/widgets/type_widgets/canvas/persistence.spec.tsxindex 6e8daf0791..c5ae41f8a3 100644--- a/apps/client/src/widgets/type_widgets/canvas/persistence.spec.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/persistence.spec.tsx@@ -391,7 +391,7 @@ describe("useCanvasDrawingPersistence", () => {it("keeps its changes on the undo stack of the note, also once Excalidraw is gone", async () => {const loaded = [ { id: "r1", type: "rectangle", version: 3, x: 0 } ];- const { attachment } = buildAttachment({ elements: loaded });+ const { attachment, getBlob } = buildAttachment({ elements: loaded });const steps: HostHistoryStep[] = [];const host: EmbedHostHistory = {push: (step) => {@@ -418,9 +418,14 @@ describe("useCanvasDrawingPersistence", () => {act(() => render(null, container));editor.scheduleSave.mockClear();+ // Saved elsewhere since Excalidraw went away.+ const added = { id: "e1", type: "ellipse", version: 1 };+ getBlob.mockResolvedValue({ content: JSON.stringify({ elements: [ moved, added ] }) });expect(steps[0].undo()).toBe(true);+ await vi.waitFor(() => expect(editor.scheduleSave).toHaveBeenCalledOnce());const [ getContent ] = editor.scheduleSave.mock.calls[0];- expect(JSON.parse(getContent()).elements).toMatchObject([ { id: "r1", x: 0 } ]);+ expect(JSON.parse(getContent()).elements)+ .toMatchObject([ { id: "r1", x: 0 }, { id: "e1" } ]);});it("loads the images it refers to from its note and saves them by reference", async () => {diff --git a/apps/client/src/widgets/type_widgets/canvas/persistence.ts b/apps/client/src/widgets/type_widgets/canvas/persistence.tsindex 5c12d42086..4f524a199e 100644--- a/apps/client/src/widgets/type_widgets/canvas/persistence.ts+++ b/apps/client/src/widgets/type_widgets/canvas/persistence.ts@@ -435,10 +435,13 @@ export function useCanvasDrawingPersistence(const sceneVersion = getSceneVersion(api.getSceneElements());if (!isSceneShownRef.current && (isLoadedSceneEmptyRef.current || sceneVersion)) {isSceneShownRef.current = true;- drawingMount?.attach(api, rootRef?.current ?? null, (scene) => editor?.scheduleSave(- () => serializeDrawing(scene, referencedFileIds.current),- (content) => saveDrawingPicture(attachment, content, scene.files)- ));+ drawingMount?.attach(api, rootRef?.current ?? null, {+ read: () => readDrawing(attachment, editor),+ write: (scene) => editor?.scheduleSave(+ () => serializeDrawing(scene, referencedFileIds.current),+ (content) => saveDrawingPicture(attachment, content, scene.files)+ )+ });onSceneShown?.();}if (!editor) return;@@ -641,6 +644,17 @@ function markBitmaps(picture: SVGSVGElement) {}}+/** The drawing that `attachment` saves, with the changes that `editor` has yet to save. */+async function readDrawing(+ attachment: FAttachment,+ editor: ContentEditor | undefined+): Promise<DrawingScene> {+ const content = parseContent(+ editor?.getUnsavedContent() ?? (await attachment.getBlob())?.content ?? "", attachment);+ const { files } = await loadDrawingFiles(attachment, content);+ return { elements: content.elements ?? [], appState: content.appState ?? {}, files };+}+/*** The content of a canvas drawing. Unlike a canvas note, a drawing keeps its images inline, except* the ones it refers to by file ID.