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
perf(client): keep CKEditor out of the startup module graph
Elian Doran12a34aee22c6
diff --git a/apps/client/src/widgets/dialogs/include_note.tsx b/apps/client/src/widgets/dialogs/include_note.tsxindex 09c4bbef39..c03a4e4327 100644--- a/apps/client/src/widgets/dialogs/include_note.tsx+++ b/apps/client/src/widgets/dialogs/include_note.tsx@@ -9,7 +9,7 @@ import { Suggestion, triggerRecentNotes } from "../../services/note_autocompleteimport tree from "../../services/tree";import froca from "../../services/froca";import { useTriliumEvent, useTriliumOption } from "../react/hooks";-import { type BoxSize, CKEditorApi } from "../type_widgets/text/CKEditorWithWatchdog";+import type { BoxSize, CKEditorApi } from "../type_widgets/text/CKEditorWithWatchdog";export interface IncludeNoteOpts {editorApi: Pick<CKEditorApi, "addIncludeNote" | "addImage">;diff --git a/apps/client/src/widgets/dialogs/markdown_import.tsx b/apps/client/src/widgets/dialogs/markdown_import.tsxindex ea24228da8..a3f53666bb 100644--- a/apps/client/src/widgets/dialogs/markdown_import.tsx+++ b/apps/client/src/widgets/dialogs/markdown_import.tsx@@ -8,7 +8,7 @@ import utils from "../../services/utils";import Button from "../react/Button";import { useTriliumEvent } from "../react/hooks";import Modal from "../react/Modal";-import { CKEditorApi } from "../type_widgets/text/CKEditorWithWatchdog";+import type { CKEditorApi } from "../type_widgets/text/CKEditorWithWatchdog";export interface MarkdownImportOpts {editorApi: CKEditorApi;diff --git a/apps/client/src/widgets/layout/StatusBar.tsx b/apps/client/src/widgets/layout/StatusBar.tsxindex 879c78d6dd..aaf3c7a2fb 100644--- a/apps/client/src/widgets/layout/StatusBar.tsx+++ b/apps/client/src/widgets/layout/StatusBar.tsx@@ -373,6 +373,15 @@ function AttributesPane({ note, noteContext, attributesShown, setAttributesShownconst parentComponent = useContext(ParentComponent);const api = useRef<AttributeEditorImperativeHandlers>(null);+ // The attribute editor pulls in CKEditor, so it is only mounted once the panel has been+ // opened (and stays mounted afterwards so the imperative handlers keep working).+ const [ editorMounted, setEditorMounted ] = useState(false);+ useEffect(() => {+ if (attributesShown) {+ setEditorMounted(true);+ }+ }, [ attributesShown ]);+const context = parentComponent && {componentId: parentComponent.componentId,note,@@ -408,11 +417,11 @@ function AttributesPane({ note, noteContext, attributesShown, setAttributesShown<AutoLinkAttributesTab {...context} /></div>}- <AttributeEditor+ {editorMounted && <AttributeEditor{...context}api={api}ntxId={noteContext.ntxId}- />+ />}</BottomPanel>);}diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsxindex 098edeb0ac..e82ffd9aa1 100644--- a/apps/client/src/widgets/react/hooks.tsx+++ b/apps/client/src/widgets/react/hooks.tsx@@ -1,4 +1,4 @@-import { CKTextEditor } from "@triliumnext/ckeditor5";+import type { CKTextEditor } from "@triliumnext/ckeditor5";import { FilterLabelsByType, KeyboardActionNames, NoteType, OptionNames, RelationNames } from "@triliumnext/commons";import { Tooltip } from "bootstrap";import Mark from "mark.js";diff --git a/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx b/apps/client/src/widgets/ribbon/components/AttributeEditor.tsxindex c8e8570a8d..e2c713c6ce 100644--- a/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx+++ b/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx@@ -1,6 +1,6 @@import "./AttributeEditor.css";-import { AttributeEditor as CKEditorAttributeEditor, MentionFeed, ModelElement, ModelNode, ModelPosition } from "@triliumnext/ckeditor5";+import type { AttributeEditor as CKEditorAttributeEditor, MentionFeed, ModelElement, ModelNode, ModelPosition } from "@triliumnext/ckeditor5";import { AttributeType } from "@triliumnext/commons";import type { Tooltip } from "bootstrap";import { createPortal } from "preact/compat";@@ -108,6 +108,14 @@ export default function AttributeEditor({ api, note, componentId, notePath, ntxIconst editorRef = useRef<CKEditorApi>();const [ locale ] = useTriliumOption("locale");+ // The CKEditor bundle is heavy and this component mounts in always-visible containers+ // (e.g. the status bar), so the editor class is loaded on demand to keep CKEditor out+ // of the startup module graph.+ const [ editorClass, setEditorClass ] = useState<typeof CKEditorAttributeEditor>();+ useEffect(() => {+ void import("@triliumnext/ckeditor5").then((ckeditor) => setEditorClass(() => ckeditor.AttributeEditor));+ }, []);+// Stable config so `useTooltip`'s effect doesn't dispose/recreate the tooltip// on every render (it runs on each keystroke). `focus` shows the help when the// editor is focused; `state` below force-hides it when the attribute-detail@@ -308,11 +316,11 @@ export default function AttributeEditor({ api, note, componentId, notePath, ntxI}}}> <div style="position: relative;">- <CKEditor+ {editorClass && <CKEditorapiRef={editorRef}className="attribute-list-editor"tabIndex={200}- editor={CKEditorAttributeEditor}+ editor={editorClass}currentValue={currentValue}config={{toolbar: { items: [] },@@ -375,7 +383,7 @@ export default function AttributeEditor({ api, note, componentId, notePath, ntxIonBlur={() => save()}onInitialized={() => editorRef.current?.focus()}disableNewlines disableSpellcheck- />+ />}<div className="attribute-editor-buttons">{ needsSaving && <ActionButtondiff --git a/apps/client/src/widgets/sidebar/HighlightsList.tsx b/apps/client/src/widgets/sidebar/HighlightsList.tsxindex 1ba0d9344c..99a7535b52 100644--- a/apps/client/src/widgets/sidebar/HighlightsList.tsx+++ b/apps/client/src/widgets/sidebar/HighlightsList.tsx@@ -1,4 +1,4 @@-import { CKTextEditor, ModelText } from "@triliumnext/ckeditor5";+import type { CKTextEditor, ModelText } from "@triliumnext/ckeditor5";import { createPortal } from "preact/compat";import { useCallback, useEffect, useRef, useState } from "preact/hooks";diff --git a/apps/client/src/widgets/sidebar/RightPanelContainer.tsx b/apps/client/src/widgets/sidebar/RightPanelContainer.tsxindex d8688186e4..75dfe0566f 100644--- a/apps/client/src/widgets/sidebar/RightPanelContainer.tsx+++ b/apps/client/src/widgets/sidebar/RightPanelContainer.tsx@@ -13,6 +13,7 @@ import options from "../../services/options";import { DEFAULT_GUTTER_SIZE } from "../../services/resizer";import Button from "../react/Button";import { useActiveNoteContext, useLegacyWidget, useNoteProperty, useTriliumEvent, useTriliumOptionJson } from "../react/hooks";+import LazyComponent from "../react/LazyComponent";import NoItems from "../react/NoItems";import LegacyRightPanelWidget from "../right_panel_widget";import HighlightsList from "./HighlightsList";@@ -21,7 +22,6 @@ import PdfAttachments from "./pdf/PdfAttachments";import PdfLayers from "./pdf/PdfLayers";import PdfPages from "./pdf/PdfPages";import RightPanelWidget from "./RightPanelWidget";-import SidebarChat from "./SidebarChat";import TableOfContents from "./TableOfContents";const MIN_WIDTH_PERCENT = 5;@@ -99,7 +99,9 @@ function useItems(rightPaneVisible: boolean, widgetsByParent: WidgetsByParent) {enabled: noteType === "text" && highlightsList.length > 0,},{- el: <SidebarChat />,+ // Loaded lazily because the chat pulls in the whole LLM + CKEditor graph,+ // which users without the LLM experimental feature should never download.+ el: <LazyComponent loader={() => import("./SidebarChat.jsx")} />,enabled: noteType !== "llmChat" && isExperimentalFeatureEnabled("llm"),position: 1000},diff --git a/apps/client/src/widgets/sidebar/TableOfContents.tsx b/apps/client/src/widgets/sidebar/TableOfContents.tsxindex 4cdf618db7..ce4b9eea7a 100644--- a/apps/client/src/widgets/sidebar/TableOfContents.tsx+++ b/apps/client/src/widgets/sidebar/TableOfContents.tsx@@ -1,6 +1,6 @@import "./TableOfContents.css";-import { attributeChangeAffectsHeading, CKTextEditor, ModelElement, type ModelNode } from "@triliumnext/ckeditor5";+import type { CKTextEditor, ModelElement, ModelNode } from "@triliumnext/ckeditor5";import clsx from "clsx";import { useCallback, useEffect, useRef, useState } from "preact/hooks";@@ -152,25 +152,38 @@ function EditableTextTableOfContents() {const headings = extractTocFromTextEditor(textEditor);setHeadings(headings);- // React to changes.- const changeCallback = () => {- const changes = textEditor.model.document.differ.getChanges();-- const affectsHeadings = changes.some( change => {- return (- change.type === 'insert' || change.type === 'remove' ||- (change.type === 'attribute' && attributeChangeAffectsHeading(change, textEditor))- );- });- if (affectsHeadings) {- requestAnimationFrame(() => {- setHeadings(extractTocFromTextEditor(textEditor));+ // React to changes. The helper lives in the CKEditor bundle, which is statically heavy+ // but guaranteed to be loaded by now (a text editor instance exists), so resolving it+ // via a dynamic import keeps it out of this component's startup graph.+ let disposed = false;+ let removeListener: (() => void) | undefined;+ void import("@triliumnext/ckeditor5").then(({ attributeChangeAffectsHeading }) => {+ if (disposed) return;++ const changeCallback = () => {+ const changes = textEditor.model.document.differ.getChanges();++ const affectsHeadings = changes.some( change => {+ return (+ change.type === 'insert' || change.type === 'remove' ||+ (change.type === 'attribute' && attributeChangeAffectsHeading(change, textEditor))+ );});- }- };+ if (affectsHeadings) {+ requestAnimationFrame(() => {+ setHeadings(extractTocFromTextEditor(textEditor));+ });+ }+ };- textEditor.model.document.on("change:data", changeCallback);- return () => textEditor.model.document.off("change:data", changeCallback);+ textEditor.model.document.on("change:data", changeCallback);+ removeListener = () => textEditor.model.document.off("change:data", changeCallback);+ });++ return () => {+ disposed = true;+ removeListener?.();+ };}, [ textEditor, note ]);const scrollToHeading = useCallback((heading: CKHeading) => {diff --git a/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.tsx b/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.tsxindex c5e5882746..946c851c86 100644--- a/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.tsx+++ b/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.tsx@@ -1,6 +1,6 @@import "./mobile_editor_toolbar.css";-import { CKTextEditor, ClassicEditor } from "@triliumnext/ckeditor5";+import type { CKTextEditor, ClassicEditor } from "@triliumnext/ckeditor5";import { MutableRef, useCallback, useEffect, useRef, useState } from "preact/hooks";import { isIOS } from "../../../services/utils";