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
fix(client): rebuild editor after MathLive option changes
Amp585d7eb55385
diff --git a/apps/client/src/widgets/type_widgets/text/CKEditorWithWatchdog.spec.tsx b/apps/client/src/widgets/type_widgets/text/CKEditorWithWatchdog.spec.tsxnew file mode 100644index 0000000000..20871f9a36--- /dev/null+++ b/apps/client/src/widgets/type_widgets/text/CKEditorWithWatchdog.spec.tsx@@ -0,0 +1,95 @@+import type { EditorWatchdog } from "@triliumnext/ckeditor5";+import { createRef, render } from "preact";+import { useRef } from "preact/hooks";+import { act } from "preact/test-utils";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++import CKEditorWithWatchdog, { type CKEditorApi } from "./CKEditorWithWatchdog";++const mocks = vi.hoisted(() => ({+ options: {} as Record<string, string | boolean>,+ create: vi.fn(async () => {}),+ destroy: vi.fn(async () => {})+}));++vi.mock("@triliumnext/ckeditor5", () => {+ class MockEditorWatchdog {+ setCreator() {}+ on() {}+ create() {+ return mocks.create();+ }+ destroy() {+ return mocks.destroy();+ }+ }++ return {+ CKTextEditor: class {},+ ClassicEditor: class {},+ EditorWatchdog: MockEditorWatchdog,+ PopupEditor: class {}+ };+});++vi.mock("../../react/hooks", () => ({+ useKeyboardShortcuts: () => {},+ useLegacyImperativeHandlers: () => {},+ useNoteContext: () => ({+ parentComponent: undefined,+ ntxId: undefined,+ note: undefined,+ notePath: undefined+ }),+ useSyncedRef: (_externalRef: unknown, initialValue: unknown) => useRef(initialValue),+ useTriliumOption: (name: string) => [ String(mocks.options[name] ?? ""), vi.fn() ],+ useTriliumOptionBool: (name: string) => [ mocks.options[name] === true, vi.fn() ]+}));++vi.mock("./ai_quick_actions", () => ({+ useAiMenuFooter: () => undefined,+ useAiQuickActions: () => []+}));++vi.mock("./config", () => ({ buildConfig: vi.fn() }));++let host: HTMLElement;++beforeEach(() => {+ mocks.options = { locale: "en", mathFieldEnabled: true };+ mocks.create.mockClear();+ mocks.destroy.mockClear();+ host = document.body.appendChild(document.createElement("div"));+});++afterEach(() => {+ render(null, host);+ document.body.innerHTML = "";+});++describe("CKEditorWithWatchdog", () => {+ it("rebuilds an open editor when the MathLive option changes", async () => {+ const watchdogRef = createRef<EditorWatchdog>();+ const editorApi = createRef<CKEditorApi>();+ const props = {+ contentLanguage: null,+ watchdogRef,+ onChange: vi.fn(),+ editorApi,+ templates: []+ };++ await act(async () => {+ render(<CKEditorWithWatchdog {...props} />, host);+ });+ await vi.waitFor(() => expect(mocks.create).toHaveBeenCalledTimes(1));++ mocks.options.mathFieldEnabled = false;+ await act(async () => {+ render(<CKEditorWithWatchdog {...props} className="force-rerender" />, host);+ });++ await vi.waitFor(() => expect(mocks.create).toHaveBeenCalledTimes(2));+ expect(mocks.destroy).toHaveBeenCalledTimes(1);+ });+});diff --git a/apps/client/src/widgets/type_widgets/text/CKEditorWithWatchdog.tsx b/apps/client/src/widgets/type_widgets/text/CKEditorWithWatchdog.tsxindex d06842555d..4bb47f3438 100644--- a/apps/client/src/widgets/type_widgets/text/CKEditorWithWatchdog.tsx+++ b/apps/client/src/widgets/type_widgets/text/CKEditorWithWatchdog.tsx@@ -71,9 +71,10 @@ export default function CKEditorWithWatchdog({ containerRef: externalContainerReconst templatesRef = useRef(templates);templatesRef.current = templates;const [ uiLanguage ] = useTriliumOption("locale");- // Read purely as a rebuild trigger: the value is consumed by buildToolbarConfig() via options.get() at- // editor-creation time, so the editor must be recreated when it changes.+ // Read purely as rebuild triggers: config builders consume the values through options.get() at+ // editor-creation time, so the editor must be recreated when either changes.const [ multilineToolbar ] = useTriliumOptionBool("textNoteEditorMultilineToolbar");+ const [ mathFieldEnabled ] = useTriliumOptionBool("mathFieldEnabled");// Rebuild triggers for the same reason, and there is no cheaper option for these: CKEditor bakes// the transformation list at plugin init — `normalizeTransformations` runs once inside// `_enableTransformationWatchers` — so unlike the settings read through a getter (link previews,@@ -343,7 +344,7 @@ export default function CKEditorWithWatchdog({ containerRef: externalContainerRe// makes them apply to an already-open note; it costs the cursor position and undo history,// which is acceptable for a change made deliberately over in the settings.}, [- contentLanguage, uiLanguage, isClassicEditor, multilineToolbar,+ contentLanguage, uiLanguage, isClassicEditor, multilineToolbar, mathFieldEnabled,doubleQuoteStyle, singleQuoteStyle, punctuationReplacements, mathReplacements, symbolReplacements,customReplacements, defaultContentLanguage, htmlSupportEnabled, allowedHtmlTags,aiEnabled, llmProvidersdiff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Math Equations.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Math Equations.htmlindex 0c9a665ec1..d486a9c439 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Math Equations.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Math Equations.html@@ -6,8 +6,9 @@the<img src="1_Math Equations_image.png" width="20" height="15">button from the <a class="reference-link" href="#root/_help_nRhnJkTT8cPs">Formatting toolbar</a> (generallyfound under the <a class="reference-link" href="#root/_help_CohkqWQC1iBv">Insert buttons</a>).</p>-<p>The mathematical expression must be written in the TeX format. There is- no visual editor for the math equations, only a preview. </p>+<p>By default, the math dialog includes a MathLive visual editor and a TeX+ field. Disable <em>MathLive visual editor</em> under Options → Text Notes+ → Features to use only the TeX field and preview.</p><p>Enabling <em>Display mode</em> will render the equation slightly bigger(especially if using big operators such as summation, or fractions) andcenter it. Display mode equations will act as blocks (i.e. like paragraphs,diff --git a/docs/User Guide/User Guide/Note Types/Text/Math Equations.md b/docs/User Guide/User Guide/Note Types/Text/Math Equations.mdindex c8cfa94e5a..171f6107bf 100644--- a/docs/User Guide/User Guide/Note Types/Text/Math Equations.md+++ b/docs/User Guide/User Guide/Note Types/Text/Math Equations.md@@ -3,7 +3,7 @@Within text notes, it's possible to enter mathematical equations using the <img src="1_Math Equations_image.png" width="20" height="15"> button from the <a class="reference-link" href="Formatting%20toolbar.md">Formatting toolbar</a> (generally found under the <a class="reference-link" href="Insert%20buttons.md">Insert buttons</a>).-The mathematical expression must be written in the TeX format. There is no visual editor for the math equations, only a preview.+By default, the math dialog includes a MathLive visual editor and a TeX field. Disable _MathLive visual editor_ under Options → Text Notes → Features to use only the TeX field and preview.Enabling _Display mode_ will render the equation slightly bigger (especially if using big operators such as summation, or fractions) and center it. Display mode equations will act as blocks (i.e. like paragraphs, or tables) and can be inserted for example in lists. Non-display equations can be part of the text.