Skip to content

Build your personal knowledge base with Trilium Notes

fix(mermaid): let the text editor's Undo reach a diagram edit still held back

Elian Doran3ada46b3ff31
diff --git a/.claude/skills/developing-content-embeds/SKILL.md b/.claude/skills/developing-content-embeds/SKILL.md
index 23024099f0..6cd05ae630 100644
--- a/.claude/skills/developing-content-embeds/SKILL.md
+++ b/.claude/skills/developing-content-embeds/SKILL.md
@@ -171,7 +171,9 @@ in-place drawings replaced the code blocks the boxes need.
source and `setData()` then dropped the embed, whose later write went to the graveyard. The
editor hands each held write to `InlineSourceSaves` (`text/attachment_saves.ts`), which marks the
note changed, and `getData()` in `EditableText` flushes it first — as `AttachmentSaves` does for
- attachments.
+ attachments. The text editor's Undo and Redo flush it too (`flushBeforeHistory()`): otherwise
+ Undo reverted the write before it, the embed ignored that while it held newer text, and the held
+ write then landed after the Undo and cleared Redo.
- **Non-interactive rendering must be finished when it returns.** A presentation slide snapshots
`getRenderedContent()` and disposes it at once, so a mounted component that draws in an effect
leaves it blank. Without `options.interactive`, draw a picture before returning
diff --git a/apps/client/src/widgets/type_widgets/text/EditableText.tsx b/apps/client/src/widgets/type_widgets/text/EditableText.tsx
index dc43061a26..70cc96000c 100644
--- a/apps/client/src/widgets/type_widgets/text/EditableText.tsx
+++ b/apps/client/src/widgets/type_widgets/text/EditableText.tsx
@@ -664,6 +664,7 @@ export default function EditableText({
editor.plugins.get("FileUploadEditing")
.on<FileUploadEvent>("upload", showFileUploadProgress);
+ inlineSourceSaves.flushBeforeHistory(editor);
if (block !== undefined) {
editor.plugins.get("BlockReferenceEditing").editRange();
diff --git a/apps/client/src/widgets/type_widgets/text/attachment_saves.spec.ts b/apps/client/src/widgets/type_widgets/text/attachment_saves.spec.ts
index 5c409bb037..1f1dbed2a3 100644
--- a/apps/client/src/widgets/type_widgets/text/attachment_saves.spec.ts
+++ b/apps/client/src/widgets/type_widgets/text/attachment_saves.spec.ts
@@ -1,3 +1,5 @@
+import { AttributeEditor } from "@triliumnext/ckeditor5";
+import { _setModelData } from "ckeditor5";
import { h, render } from "preact";
import { act } from "preact/test-utils";
import { describe, expect, it, vi } from "vitest";
@@ -8,7 +10,9 @@ import type FAttachment from "../../../entities/fattachment";
import type FNote from "../../../entities/fnote";
import type { AttachmentEditor, NoteEditor } from "../../../services/content_renderer";
import { ParentComponent } from "../../react/react_utils";
-import AttachmentSaves, { NoteSaves, useAttachmentEditor, useNoteEditor } from "./attachment_saves";
+import AttachmentSaves, {
+ InlineSourceSaves, NoteSaves, useAttachmentEditor, useNoteEditor
+} from "./attachment_saves";
const serverPost = vi.hoisted(() => vi.fn(async () => undefined));
const serverPut = vi.hoisted(() => vi.fn(async () => undefined));
@@ -291,3 +295,40 @@ describe("useNoteEditor", () => {
"notes/code1/data", { content: "second" }, component.componentId);
});
});
+
+describe("InlineSourceSaves", () => {
+ it("makes the held writes before the text editor undoes or redoes, as a step of their own", async () => {
+ const element = document.createElement("div");
+ document.body.append(element);
+ const editor = await AttributeEditor.create(element, { licenseKey: "GPL" });
+ const type = (text: string) => editor.model.change((writer) => {
+ editor.model.insertContent(writer.createText(text));
+ });
+ const saves = new InlineSourceSaves(() => {});
+ saves.flushBeforeHistory(editor);
+ // Holds a write that, like an inline source, leaves `saves` once made.
+ const hold = (text: string) => {
+ const write = () => {
+ saves.delete(write);
+ type(text);
+ };
+ saves.add(write);
+ };
+
+ _setModelData(editor.model, "<paragraph>a[]</paragraph>");
+ type("b");
+ hold("c");
+ editor.execute("undo");
+ expect(editor.getData()).toBe("<p>ab</p>");
+
+ editor.execute("redo");
+ expect(editor.getData()).toBe("<p>abc</p>");
+
+ hold("d");
+ editor.execute("redo");
+ expect(editor.getData()).toBe("<p>abcd</p>");
+
+ await editor.destroy();
+ element.remove();
+ });
+});
diff --git a/apps/client/src/widgets/type_widgets/text/attachment_saves.ts b/apps/client/src/widgets/type_widgets/text/attachment_saves.ts
index 00b5c70722..b2830aed6a 100644
--- a/apps/client/src/widgets/type_widgets/text/attachment_saves.ts
+++ b/apps/client/src/widgets/type_widgets/text/attachment_saves.ts
@@ -1,3 +1,4 @@
+import type { CKTextEditor } from "@triliumnext/ckeditor5";
import { useCallback, useContext, useEffect, useRef, useState } from "preact/hooks";
import type { default as NoteContext, SaveState } from "../../../components/note_context";
@@ -250,6 +251,17 @@ export class InlineSourceSaves {
write();
}
}
+
+ /**
+ * Makes the held writes before `editor` undoes or redoes, so that a held write is a step of
+ * the undo history rather than a change made after it.
+ */
+ flushBeforeHistory(editor: Pick<CKTextEditor, "commands">) {
+ for (const name of [ "undo", "redo" ]) {
+ // Runs before the command checks `isEnabled`, which a held write can turn on.
+ editor.commands.get(name)?.on("execute", () => this.flush(), { priority: "highest" });
+ }
+ }
}
/**