Skip to content

Build your personal knowledge base with Trilium Notes

feat(ckeditor5): lazy-load premium features

Elian Dorana5512267c123
diff --git a/apps/client/src/widgets/type_widgets/text/config.ts b/apps/client/src/widgets/type_widgets/text/config.ts
index 4457059273..4066387fc1 100644
--- a/apps/client/src/widgets/type_widgets/text/config.ts
+++ b/apps/client/src/widgets/type_widgets/text/config.ts
@@ -1,15 +1,16 @@
-import { ALLOWED_PROTOCOLS, DISPLAYABLE_LOCALE_IDS, MIME_TYPE_AUTO } from "@triliumnext/commons";
-import { buildExtraCommands, type EditorConfig, getCkLocale, PREMIUM_PLUGINS, TemplateDefinition } from "@triliumnext/ckeditor5";
-import { getHighlightJsNameForMime } from "../../../services/mime_types.js";
-import options from "../../../services/options.js";
-import { ensureMimeTypesForHighlighting, isSyntaxHighlightEnabled } from "../../../services/syntax_highlight.js";
+import { buildExtraCommands, type EditorConfig, getCkLocale, loadPremiumPlugins, TemplateDefinition } from "@triliumnext/ckeditor5";
import emojiDefinitionsUrl from "@triliumnext/ckeditor5/src/emoji_definitions/en.json?url";
+import { ALLOWED_PROTOCOLS, DISPLAYABLE_LOCALE_IDS, MIME_TYPE_AUTO } from "@triliumnext/commons";
+import { normalizeMimeTypeForCKEditor } from "@triliumnext/commons";
+
import { copyTextWithToast } from "../../../services/clipboard_ext.js";
import { t } from "../../../services/i18n.js";
import { getMermaidConfig } from "../../../services/mermaid.js";
-import noteAutocompleteService, { type Suggestion } from "../../../services/note_autocomplete.js";
+import { getHighlightJsNameForMime } from "../../../services/mime_types.js";
import mimeTypesService from "../../../services/mime_types.js";
-import { normalizeMimeTypeForCKEditor } from "@triliumnext/commons";
+import noteAutocompleteService, { type Suggestion } from "../../../services/note_autocomplete.js";
+import options from "../../../services/options.js";
+import { ensureMimeTypesForHighlighting, isSyntaxHighlightEnabled } from "../../../services/syntax_highlight.js";
import { buildToolbarConfig } from "./toolbar.js";
export const OPEN_SOURCE_LICENSE_KEY = "GPL";
@@ -36,7 +37,7 @@ export async function buildConfig(opts: BuildEditorOptions): Promise<EditorConfi
engine: "katex",
outputType: "span", // or script
lazyLoad: async () => {
- (window as any).katex = (await import("../../../services/math.js")).default
+ (window as any).katex = (await import("../../../services/math.js")).default;
},
forceOutputType: false, // forces output to use outputType
enablePreview: true // Enable preview view
@@ -172,7 +173,7 @@ export async function buildConfig(opts: BuildEditorOptions): Promise<EditorConfi
config.language = {
ui: (typeof config.language === "string" ? config.language : "en"),
content: contentLanguage
- }
+ };
}
// Mention customisation.
@@ -195,11 +196,9 @@ export async function buildConfig(opts: BuildEditorOptions): Promise<EditorConfi
};
}
- // Enable premium plugins.
+ // Enable premium plugins dynamically to avoid eager loading.
if (hasPremiumLicense) {
- config.extraPlugins = [
- ...PREMIUM_PLUGINS
- ];
+ config.extraPlugins = await loadPremiumPlugins();
}
return {
@@ -237,7 +236,7 @@ function getLicenseKey() {
}
function getDisabledPlugins() {
- let disabledPlugins: string[] = [];
+ const disabledPlugins: string[] = [];
if (options.get("textNoteEmojiCompletionEnabled") !== "true") {
disabledPlugins.push("EmojiMention");
diff --git a/packages/ckeditor5/src/index.ts b/packages/ckeditor5/src/index.ts
index 7dff348037..7f005b4f74 100644
--- a/packages/ckeditor5/src/index.ts
+++ b/packages/ckeditor5/src/index.ts
@@ -1,11 +1,12 @@
import "ckeditor5/ckeditor5.css";
-import 'ckeditor5-premium-features/ckeditor5-premium-features.css';
+// Premium features CSS loaded dynamically with the plugins
+// import 'ckeditor5-premium-features/ckeditor5-premium-features.css';
import "./theme/code_block_toolbar.css";
import { COMMON_PLUGINS, CORE_PLUGINS, POPUP_EDITOR_PLUGINS } from "./plugins.js";
import { BalloonEditor, DecoupledEditor, FindAndReplaceEditing, FindCommand } from "ckeditor5";
import "./translation_overrides.js";
export { default as EditorWatchdog } from "./custom_watchdog";
-export { PREMIUM_PLUGINS } from "./plugins.js";
+export { loadPremiumPlugins } from "./plugins.js";
export type { EditorConfig, MentionFeed, MentionFeedObjectItem, ModelNode, ModelPosition, ModelElement, ModelText, WatchdogConfig, WatchdogState } from "ckeditor5";
export type { TemplateDefinition } from "ckeditor5-premium-features";
export { default as buildExtraCommands } from "./extra_slash_commands.js";
diff --git a/packages/ckeditor5/src/plugins.ts b/packages/ckeditor5/src/plugins.ts
index c8ca8c8fff..abf51f0869 100644
--- a/packages/ckeditor5/src/plugins.ts
+++ b/packages/ckeditor5/src/plugins.ts
@@ -1,5 +1,6 @@
import { Autoformat, AutoLink, BlockQuote, BlockToolbar, Bold, CKFinderUploadAdapter, Clipboard, Code, CodeBlock, Enter, Font, FontBackgroundColor, FontColor, GeneralHtmlSupport, Heading, HeadingButtonsUI, HorizontalLine, Image, ImageCaption, ImageInline, ImageResize, ImageStyle, ImageToolbar, ImageUpload, Alignment, Indent, IndentBlock, Italic, Link, List, ListProperties, Mention, PageBreak, Paragraph, ParagraphButtonUI, PasteFromOffice, PictureEditing, RemoveFormat, SelectAll, ShiftEnter, SpecialCharacters, SpecialCharactersEssentials, Strikethrough, Style, Subscript, Superscript, Table, TableCaption, TableCellProperties, TableColumnResize, TableProperties, TableSelection, TableToolbar, TextPartLanguage, TextTransformation, TodoList, Typing, Underline, Undo, Bookmark, EmojiMention, EmojiPicker, FindAndReplaceEditing } from "ckeditor5";
-import { SlashCommand, Template, FormatPainter } from "ckeditor5-premium-features";
+// Premium features loaded dynamically to improve initial load time
+// import { SlashCommand, Template, FormatPainter } from "ckeditor5-premium-features";
import type { Plugin } from "ckeditor5";
import CutToNotePlugin from "./plugins/cuttonote.js";
import UploadimagePlugin from "./plugins/uploadimage.js";
@@ -81,13 +82,15 @@ export const CORE_PLUGINS: typeof Plugin[] = [
];
/**
- * Plugins that require a premium CKEditor license key to work.
+ * Dynamically loads plugins that require a premium CKEditor license key.
+ * This avoids loading ~6 seconds of premium features code during initial app startup.
*/
-export const PREMIUM_PLUGINS: typeof Plugin[] = [
- SlashCommand,
- Template,
- FormatPainter
-];
+export async function loadPremiumPlugins(): Promise<(typeof Plugin)[]> {
+ const { SlashCommand, Template, FormatPainter } = await import('ckeditor5-premium-features');
+ // Also load the CSS when premium features are used
+ await import('ckeditor5-premium-features/ckeditor5-premium-features.css');
+ return [SlashCommand, Template, FormatPainter];
+}
/**
* The set of plugins that are required for the editor to work. This is used in normal text editors (floating or fixed toolbar) but not in the attribute editor.