Skip to content

Build your personal knowledge base with Trilium Notes

fix(client): refresh the Markdown preview on note switch (closes #11894)

drakeo338f7d64ee12c02
diff --git a/apps/client/src/widgets/type_widgets/code/Code.spec.tsx b/apps/client/src/widgets/type_widgets/code/Code.spec.tsx
new file mode 100644
index 0000000000..11aa1c3aae
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/code/Code.spec.tsx
@@ -0,0 +1,43 @@
+// @vitest-environment jsdom
+import { render } from "preact";
+import { act } from "preact/test-utils";
+import { describe, expect, it, vi } from "vitest";
+
+import { ParentComponent } from "../../react/react_utils";
+import { EditableCode } from "./Code";
+
+function fakeNote(noteId: string, content: string) {
+ return {
+ noteId,
+ type: "code",
+ mime: "text/x-markdown",
+ getLabelValue: () => null,
+ isLabelTruthy: () => false,
+ isMarkdown: () => true,
+ getBlob: async () => ({ content })
+ } as any;
+}
+
+describe("EditableCode", () => {
+ // A preview-only Markdown note keeps its (hidden) editor mounted and relies on this callback to
+ // feed the preview, so it has to fire when a different note's content is loaded.
+ it("reports the content of the new note after a note switch", async () => {
+ const onContentChanged = vi.fn();
+ const parent = { registerHandler() {}, removeHandler() {}, componentId: "c" } as any;
+ const container = document.createElement("div");
+ document.body.appendChild(container);
+
+ const show = (note: any) => render(
+ <ParentComponent.Provider value={parent}>
+ <EditableCode note={note} ntxId="ntx" parentComponent={parent} noteContext={undefined} viewScope={undefined} onContentChanged={onContentChanged} />
+ </ParentComponent.Provider>,
+ container
+ );
+
+ await act(async () => { show(fakeNote("a", "# Note A")); });
+ await vi.waitFor(() => expect(onContentChanged).toHaveBeenCalledWith("# Note A"));
+
+ await act(async () => { show(fakeNote("b", "# Note B")); });
+ await vi.waitFor(() => expect(onContentChanged).toHaveBeenCalledWith("# Note B"));
+ });
+});
diff --git a/apps/client/src/widgets/type_widgets/code/Code.tsx b/apps/client/src/widgets/type_widgets/code/Code.tsx
index 96c3bfdd09..9ac613d4ee 100644
--- a/apps/client/src/widgets/type_widgets/code/Code.tsx
+++ b/apps/client/src/widgets/type_widgets/code/Code.tsx
@@ -115,6 +115,7 @@ export function EditableCode({ note, ntxId, noteContext, debounceUpdate, parentC
const codeEditor = editorRef.current;
if (!codeEditor) return;
codeEditor.setText(content ?? "");
+ onContentChanged?.(content ?? "");
codeEditor.setMimeType(note.mime);
codeEditor.clearHistory();