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/content embed: fix an issue
Adorian Doran319b8b82b51e
diff --git a/.claude/skills/developing-content-embeds/SKILL.md b/.claude/skills/developing-content-embeds/SKILL.mdindex 122b45504c..682dd60cb3 100644--- a/.claude/skills/developing-content-embeds/SKILL.md+++ b/.claude/skills/developing-content-embeds/SKILL.md@@ -164,8 +164,11 @@ text editor's undo stack, in order with the text's own changes. Three layers:attaches it while mounted, read-only included.Content loaded from elsewhere goes through `view.loadText()`, which stays out of the history; a-`setText()` (re-indent) is a step. Another editor joins by pushing its own steps to `EmbedHistory`-through the same `host`. Included text notes and drawing canvases keep their own history.+`setText()` (re-indent) is a step. The `content` prop of `CodeEmbedEditor` loads, so a change made+while read-only (the re-indent stand-in in `CodeEmbed.tsx`) calls `setText()` on the read-only view,+after loading the unsaved content of the other embeds into it, or Undo skips it or reverts their+edits. Another editor joins by pushing its own steps to `EmbedHistory` through the same `host`.+Included text notes and drawing canvases keep their own history.## Recipe: a new source embed (a diagram language)diff --git a/apps/client/src/widgets/type_widgets/code/CodeEmbed.spec.tsx b/apps/client/src/widgets/type_widgets/code/CodeEmbed.spec.tsxindex 09331ca7ec..3c3e42ddd2 100644--- a/apps/client/src/widgets/type_widgets/code/CodeEmbed.spec.tsx+++ b/apps/client/src/widgets/type_widgets/code/CodeEmbed.spec.tsx@@ -301,13 +301,18 @@ describe("CodeEmbed", () => {await vi.waitFor(() => expect(closedView.state.doc.toString()).toBe("# if a:\n b"));});- it("re-indents the unsaved changes that another embed of the same note made", async () => {+ it("re-indents another embed's unsaved changes as a step of the note's undo", async () => {options.set("codeNoteTabWidth", "4");options.set("codeNoteIndentWithTabs", "false");options.set("codeNotesMimeTypes", JSON.stringify([ "text/x-python" ]));vi.spyOn(contextMenu, "show").mockResolvedValue(undefined);+ const element = document.createElement("div");+ document.body.append(element);+ const textEditor = await AttributeEditor.create(element, {+ licenseKey: "GPL", extraPlugins: [ EmbedHistory ]+ });const note = buildCodeNote("if a:\n b");- const editor = buildSavingEditor(note);+ const editor = buildSavingEditor(note, new EmbedHistories(() => textEditor));const { figure: editing } = await mount(note, editor, { isEditable: true });const { figure: previewing } = await mount(note, editor);const view = await findView(editing);@@ -315,6 +320,11 @@ describe("CodeEmbed", () => {await selectMenuItem(previewing, "tabWidth", "status_bar.tab_width_style_tabs", true);expect(editor.getUnsavedContent()).toBe("# if a:\n\tb");+ act(() => textEditor.execute("undo"));+ expect(editor.getUnsavedContent()).toBe("# if a:\n b");++ await textEditor.destroy();+ element.remove();});it("keeps the language and indentation of a file in its embed, set from its toolbar", async () => {diff --git a/apps/client/src/widgets/type_widgets/code/CodeEmbed.tsx b/apps/client/src/widgets/type_widgets/code/CodeEmbed.tsxindex dd411b57e4..92246ca582 100644--- a/apps/client/src/widgets/type_widgets/code/CodeEmbed.tsx+++ b/apps/client/src/widgets/type_widgets/code/CodeEmbed.tsx@@ -73,6 +73,13 @@ export default function CodeEmbed({const getCodeText = (): CodeText => (isEditing ? viewRef.current : null) ?? {getText: () => editor?.getUnsavedContent() ?? content,setText: (text) => {+ // Through the read-only editor, the change is a step of the shared undo history.+ const view = viewRef.current;+ const unsavedContent = editor?.getUnsavedContent() ?? content;+ if (view && view.getText() !== unsavedContent) {+ view.loadText(unsavedContent);+ }+ view?.setText(text);editor?.scheduleSave(() => text);setContent(text);}