Skip to content

Build your personal knowledge base with Trilium Notes

fix(code): apply the code note theme to an editor created late

Elian Doranc6b4620fec0b
diff --git a/apps/client/src/widgets/type_widgets/code/Code.spec.tsx b/apps/client/src/widgets/type_widgets/code/Code.spec.tsx
index f71ed03ff2..011075fefa 100644
--- a/apps/client/src/widgets/type_widgets/code/Code.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/code/Code.spec.tsx
@@ -1,14 +1,32 @@
// @vitest-environment jsdom
+import VanillaCodeMirror from "@triliumnext/codemirror";
import { render } from "preact";
+import { useEffect, useState } from "preact/hooks";
import { act } from "preact/test-utils";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import FNote from "../../../entities/fnote";
+import options from "../../../services/options";
import search from "../../../services/search";
import { buildNote } from "../../../test/easy-froca";
import { renderInto } from "../../../test/render";
import { ParentComponent } from "../../react/react_utils";
import { EditableCode, ReadOnlyCode } from "./Code";
+import type { CodeMirrorProps } from "./CodeMirror";
+
+// While `deferEditor.on` is set, the editor is created one render after `CodeEditor` mounts, after
+// its effects have run, as in a Mermaid note's split; otherwise the real component renders as is.
+const deferEditor = vi.hoisted(() => ({ on: false }));
+vi.mock("./CodeMirror", async (importOriginal) => {
+ const { default: RealCodeMirror } = await importOriginal<typeof import("./CodeMirror")>();
+ return {
+ default: (props: CodeMirrorProps) => {
+ const [ shown, setShown ] = useState(!deferEditor.on);
+ useEffect(() => setShown(true), []);
+ return shown ? <RealCodeMirror {...props} /> : null;
+ }
+ };
+});
describe("EditableCode", () => {
const parent = { registerHandler() {}, removeHandler() {}, componentId: "c" } as any;
@@ -56,6 +74,36 @@ describe("EditableCode", () => {
});
});
+describe("code note theme", () => {
+ beforeEach(() => {
+ vi.spyOn(search, "searchForNotes").mockResolvedValue([]);
+ options.set("codeNoteThemeMatchesApp", "false");
+ options.set("codeNoteTheme", "default:github-light");
+ });
+
+ afterEach(() => {
+ deferEditor.on = false;
+ vi.restoreAllMocks();
+ });
+
+ it("applies the theme to an editor created after the component's effects have run", async () => {
+ const setTheme = vi.spyOn(VanillaCodeMirror.prototype, "setTheme");
+ deferEditor.on = true;
+ const note = buildNote({ type: "code", mime: "text/plain", title: "T", content: "text" });
+ const parent = { registerHandler() {}, removeHandler() {}, componentId: "c" } as any;
+
+ await act(async () => {
+ renderInto(
+ <ParentComponent.Provider value={parent}>
+ <EditableCode note={note} ntxId="ntx" parentComponent={parent} noteContext={undefined} viewScope={undefined} />
+ </ParentComponent.Provider>
+ );
+ });
+
+ await vi.waitFor(() => expect(setTheme).toHaveBeenCalledWith(expect.objectContaining({ id: "github-light" })));
+ });
+});
+
describe("API log", () => {
const handlers = new Map<string, (data: unknown) => void>();
const parent = {
diff --git a/apps/client/src/widgets/type_widgets/code/Code.tsx b/apps/client/src/widgets/type_widgets/code/Code.tsx
index 91ad219837..24ff0a27d2 100644
--- a/apps/client/src/widgets/type_widgets/code/Code.tsx
+++ b/apps/client/src/widgets/type_widgets/code/Code.tsx
@@ -206,14 +206,10 @@ export function CodeEditor({ ntxId, containerRef: externalContainerRef, editorRe
? (colorScheme === "dark" ? darkTheme : lightTheme)
: codeNoteTheme;
- // React to theme changes.
+ // React to theme changes. `onInitialized` applies it too, for an editor `CodeMirror` creates
+ // after this effect has run.
useEffect(() => {
- if (codeEditorRef.current && effectiveTheme?.startsWith(DEFAULT_PREFIX)) {
- const theme = getThemeById(effectiveTheme.substring(DEFAULT_PREFIX.length));
- if (theme) {
- codeEditorRef.current.setTheme(theme);
- }
- }
+ if (codeEditorRef.current) applyTheme(codeEditorRef.current, effectiveTheme);
}, [ codeEditorRef, effectiveTheme ]);
useTriliumEvent("executeWithCodeEditor", async ({ resolve, ntxId: eventNtxId }) => {
@@ -265,6 +261,7 @@ export function CodeEditor({ ntxId, containerRef: externalContainerRef, editorRe
else if (externalContainerRef) externalContainerRef.current = containerRef.current;
}
if (codeEditorRef.current) {
+ applyTheme(codeEditorRef.current, effectiveTheme);
if (typeof externalEditorRef === "function") externalEditorRef(codeEditorRef.current);
else if (externalEditorRef) externalEditorRef.current = codeEditorRef.current;
}
@@ -273,3 +270,12 @@ export function CodeEditor({ ntxId, containerRef: externalContainerRef, editorRe
}}
/>;
}
+
+/** Applies a `default:`-prefixed code note theme, the only kind the editor knows how to load. */
+function applyTheme(editor: VanillaCodeMirror, effectiveTheme: string | undefined) {
+ if (!effectiveTheme?.startsWith(DEFAULT_PREFIX)) return;
+ const theme = getThemeById(effectiveTheme.substring(DEFAULT_PREFIX.length));
+ if (theme) {
+ void editor.setTheme(theme);
+ }
+}