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 Doran65d33456dd0d
diff --git a/.claude/skills/developing-content-embeds/SKILL.md b/.claude/skills/developing-content-embeds/SKILL.mdindex bca6cd5781..e4010fbc60 100644--- a/.claude/skills/developing-content-embeds/SKILL.md+++ b/.claude/skills/developing-content-embeds/SKILL.md@@ -171,7 +171,7 @@ changes. Three layers, with one recorder per editor in the middle:replacement button copies Excalidraw's `ToolButton` markup. Without a mounted Excalidraw, a stepreads the drawing through the `DrawingStore` it attached with (the unsaved content, else theblob, so a save made elsewhere since stays), patches it and writes it back; the steps queue one- after another and count in `revision` once written. They also patch an Excalidraw that attached+ after another and count in `revision` once written; a step on a live canvas waits behind them. They also patch an Excalidraw that attachedduring the read (patches are idempotent), and a mount that loaded before a written step (anundone Editable toggle remounts) reads the drawing 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.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 b2524da1d9..cb64bd1e24 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@@ -206,6 +206,33 @@ describe("DrawingHistory", () => {await vi.waitFor(() => expect(next.getElements()).toMatchObject([ { x: 0, y: 0 } ]));});+ it("applies the steps in order while one waits for the saved drawing", 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();+ store.setElements([ moved ]);++ const read = deferScene();+ store.read.mockImplementationOnce(() => read.promise);+ steps[0].undo();+ const nextMount = history.mount();+ const next = createDrawing([ moved ]);+ nextMount.attach(next.api, next.root, store);+ steps[0].redo();+ read.resolve({ elements: [ moved ], appState: {}, files: {} });+ await vi.waitFor(() => expect(store.write).toHaveBeenCalledOnce());+ await vi.waitFor(() => expect(next.api.updateScene).toHaveBeenCalledTimes(2));+ expect(next.getElements()).toMatchObject([ { x: 10 } ]);+ });+it("undoes and redoes the note with the keys and the buttons of Excalidraw", () => {const { host, setEnabled } = createHost();const drawing = createDrawing([]);diff --git a/apps/client/src/widgets/type_widgets/canvas/drawing_history.tsx b/apps/client/src/widgets/type_widgets/canvas/drawing_history.tsxindex d744c13c84..e4befb44b2 100644--- a/apps/client/src/widgets/type_widgets/canvas/drawing_history.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/drawing_history.tsx@@ -101,6 +101,7 @@ export default class DrawingHistory {private store: DrawingStore | undefined;/** The steps applied through `store`, which read the drawing one after another. */private storeUpdates = Promise.resolve();+ private pendingStoreUpdates = 0;private lastStep: RecordedStep | undefined;constructor(private readonly host: EmbedHostHistory) {}@@ -187,8 +188,11 @@ export default class DrawingHistory {}private queueStoreUpdate(update: () => Promise<void>) {+ this.pendingStoreUpdates++;this.storeUpdates = this.storeUpdates.then(update).catch((error: unknown) => {console.error("Unable to update the canvas drawing", error);+ }).finally(() => {+ this.pendingStoreUpdates--;});}@@ -229,22 +233,28 @@ export default class DrawingHistory {private apply(step: DrawingStep, isUndo: boolean) {const [ first ] = this.drawings;- if (first) {+ if (!first && !this.store) {+ return false;+ }++ if (first && !this.pendingStoreUpdates) {this.applyToDrawings(step, isUndo);// Deferred until after the Undo button of the toolbar focuses the text editor.queueMicrotask(() => {first.root?.querySelector<HTMLElement>(".excalidraw-container")?.focus();});- } else if (this.store) {- // Applies to the drawing as saved, which can have changed elsewhere since.- const { store } = this;+ } else {+ // Waits for the steps before it, which read the drawing.+ const isShown = !!first;this.queueStoreUpdate(async () => {- store.write(applyToScene(await store.read(), step, isUndo));+ const { store } = this;+ if (store && (!isShown || !this.drawings.size)) {+ // Applies to the drawing as saved, which can have changed elsewhere since.+ store.write(applyToScene(await store.read(), step, isUndo));+ }// An Excalidraw that attached meanwhile loaded the drawing before the step.this.applyToDrawings(step, isUndo);});- } else {- return false;}return true;}