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
feat(mind-map): give a node's memo the marks a sentence is written with
Elian Doranc1580e58d05e
diff --git a/apps/client/src/widgets/type_widgets/mind_map/NodeMemo.tsx b/apps/client/src/widgets/type_widgets/mind_map/NodeMemo.tsxindex 5fef04131f..6aeaa96a0a 100644--- a/apps/client/src/widgets/type_widgets/mind_map/NodeMemo.tsx+++ b/apps/client/src/widgets/type_widgets/mind_map/NodeMemo.tsx@@ -1,6 +1,6 @@import "./NodeMemo.css";-import { AttributeEditor, CHAT_INPUT_PLUGINS, type CKTextEditor, type EditorConfig } from "@triliumnext/ckeditor5";+import { AttributeEditor, type CKTextEditor, type EditorConfig, MEMO_PLUGINS } from "@triliumnext/ckeditor5";import type { NodeObj } from "mind-elixir";import { useEffect, useLayoutEffect, useRef } from "preact/hooks";@@ -103,18 +103,38 @@ export default function NodeMemo({ selectionKey, memo, readOnly, onCommit }: {* Built where it is used rather than kept still beside the module: the hint is a translated string,* and the catalogue is not yet loaded when this module is. Nothing is spent on rebuilding it — an* editor is raised from the configuration it is handed as it mounts, and never reconfigured.+ *+ * English, as the two other fields built on an {@link AttributeEditor} are (the ribbon's attributes,+ * the chat box): the strings on show are CKEditor's own, and the dictionary they resolve through is+ * fetched, which an editor raised as its field mounts cannot wait for. That leaves the toolbar's+ * tooltips in English wherever the rest of Trilium is not, for all three of them together to answer.*/function buildMemoEditorConfig(): EditorConfig {return {- // The markdown autoformatting the chat box has — quotes, code, lists, links — and no toolbar.- extraPlugins: CHAT_INPUT_PLUGINS,- toolbar: { items: [] },+ extraPlugins: MEMO_PLUGINS,+ toolbar: { items: MEMO_TOOLBAR_ITEMS },placeholder: t("mind-map.memo-placeholder"),licenseKey: "GPL",language: "en"};}+/**+ * What the toolbar over a selection offers. A memo is written in sentences, so what it holds is what+ * a sentence is marked up with and nothing beyond: no headings, which a pane this narrow has no use+ * for, and no colors, which are the map's business rather than the memo's.+ *+ * The toolbar itself is the editor's own. An {@link AttributeEditor} is a `BalloonEditor`, which+ * raises whatever it is configured with over the selection — this list is the whole of the feature,+ * the panel that shows it having been there, empty, all along.+ */+const MEMO_TOOLBAR_ITEMS = [+ "bold", "italic", "strikethrough", "code",+ "|", "link",+ "|", "bulletedList", "numberedList",+ "|", "blockQuote", "codeBlock"+];+/** Held while the selection disagrees, there being no one memo to write over the others. */const MEMO_READ_ONLY_LOCK = "mind-map-node-memo";diff --git a/packages/ckeditor5/src/index.ts b/packages/ckeditor5/src/index.tsindex 421c184f01..fd8d88629a 100644--- a/packages/ckeditor5/src/index.ts+++ b/packages/ckeditor5/src/index.ts@@ -8,7 +8,7 @@ import "./theme/link_embed_form.css";import { COMMON_PLUGINS, CORE_PLUGINS, POPUP_EDITOR_PLUGINS } from "./plugins.js";import { BalloonEditor, DecoupledEditor, FindAndReplaceEditing, FindCommand } from "ckeditor5";export { default as EditorWatchdog } from "./custom_watchdog";-export { CHAT_INPUT_PLUGINS } from "./plugins.js";+export { CHAT_INPUT_PLUGINS, MEMO_PLUGINS } from "./plugins.js";export type { EditorConfig, MentionFeed, MentionFeedObjectItem, ModelNode, ModelPosition, ModelElement, ModelText, WatchdogConfig, WatchdogState } from "ckeditor5";export type { SlashCommandConfig, SlashCommandDefinition } from "./plugins/mention/slash_commands.js";export type { TriliumMentionFeed } from "./plugins/mention/types.js";diff --git a/packages/ckeditor5/src/plugins.spec.ts b/packages/ckeditor5/src/plugins.spec.tsindex 49d2e2c417..be009960f8 100644--- a/packages/ckeditor5/src/plugins.spec.ts+++ b/packages/ckeditor5/src/plugins.spec.ts@@ -1,8 +1,12 @@-import { BlockToolbar } from "ckeditor5";+import { BalloonToolbar, BlockToolbar, Bold, Code, Italic, Strikethrough } from "ckeditor5";import { describe, expect, it } from "vitest";+import { createTestEditor } from "../test/editor-kit.js";+import { AttributeEditor } from "./index.js";import Admonition from "./plugins/admonition/admonition.js";-import { COMMON_PLUGINS, CORE_PLUGINS, POPUP_EDITOR_PLUGINS } from "./plugins.js";+import ItalicAsEmPlugin from "./plugins/italic_as_em.js";+import StrikethroughAsDel from "./plugins/strikethrough_as_del.js";+import { CHAT_INPUT_PLUGINS, COMMON_PLUGINS, CORE_PLUGINS, MEMO_PLUGINS, POPUP_EDITOR_PLUGINS } from "./plugins.js";import CutToNotePlugin from "./plugins/cuttonote.js";import Uploadfileplugin from "./plugins/file_upload/uploadfileplugin.js";import TriliumFormatPainter from "./plugins/format_painter/format_painter.js";@@ -95,4 +99,81 @@ describe("plugin lists", () => {expect(COMMON_PLUGINS).toContain(TriliumFormatPainter);expect(COMMON_PLUGINS).toContain(TriliumSnippets);});++ it("MEMO_PLUGINS marks up a sentence, which the chat input deliberately goes without", () => {+ for (const plugin of CHAT_INPUT_PLUGINS) {+ expect(MEMO_PLUGINS).toContain(plugin);+ }+ for (const plugin of [ Bold, Italic, Strikethrough, Code ]) {+ expect(MEMO_PLUGINS).toContain(plugin);+ // Not added to the set the memo builds on: what is typed in the chat box is turned into+ // markdown before it is sent, and that pass keeps the text of an inline wrapper and+ // drops the wrapper. Bold offered there would be bold lost on its way to the model.+ expect(CHAT_INPUT_PLUGINS).not.toContain(plugin);+ }+ // What writes italics as `<em>` and a strikethrough as `<del>`, as the rest of Trilium+ // stores both; they are downcast converters and do nothing without the marks above.+ expect(MEMO_PLUGINS).toContain(ItalicAsEmPlugin);+ expect(MEMO_PLUGINS).toContain(StrikethroughAsDel);+ });+});++describe("an editor raised on MEMO_PLUGINS", () => {+ /** As the memo is built: the plugins every minimal editor carries, and the memo's own upon them. */+ const memoEditor = () => createTestEditor([ ...CORE_PLUGINS, ...MEMO_PLUGINS ]);++ it("carries every button the memo's toolbar is built from", async () => {+ const editor = await memoEditor();++ // The toolbar of a `BalloonEditor` is raised from the names it is configured with, and a+ // name nothing answers to is dropped with a warning rather than reported. The memo's list+ // (see NodeMemo.tsx) is pinned here so that thinning this set turns the suite red instead.+ for (const item of [+ "bold", "italic", "strikethrough", "code",+ "link", "bulletedList", "numberedList", "blockQuote", "codeBlock"+ ]) {+ expect(editor.ui.componentFactory.has(item)).toBe(true);+ }+ });++ it("registers the markdown spellings of those marks, which follow from the commands", async () => {+ const editor = await memoEditor();++ // Autoformat adds `**bold**`, `_italic_`, `` `code` `` and `~~struck~~` only where the+ // matching command answers, so a set without the marks leaves those spellings inert too —+ // which is what the memo's field did before them.+ for (const command of [ "bold", "italic", "strikethrough", "code" ]) {+ expect(editor.commands.get(command)).toBeDefined();+ }+ });++ it("raises a toolbar over the selection out of the items it is configured with", async () => {+ // What the memo's field relies on, and the whole of how it gets a toolbar: an+ // `AttributeEditor` is a `BalloonEditor`, whose constructor loads `BalloonToolbar` itself+ // and defines `balloonToolbar` from `toolbar`. The field passed an empty list until now,+ // which is the only reason nothing was ever shown.+ const editorElement = document.createElement("div");+ document.body.appendChild(editorElement);+ const editor = await AttributeEditor.create(editorElement, {+ licenseKey: "GPL",+ extraPlugins: MEMO_PLUGINS,+ toolbar: { items: [ "bold", "italic" ] }+ });++ try {+ const balloonToolbar = editor.plugins.get("BalloonToolbar") as BalloonToolbar;+ expect(balloonToolbar.toolbarView.items.length).toBe(2);+ } finally {+ await editor.destroy();+ editorElement.remove();+ }+ });++ it("writes italics and strikethrough the way the rest of Trilium stores them", async () => {+ const editor = await memoEditor();++ editor.setData("<p><i>slanted</i> and <s>struck</s></p>");++ expect(editor.getData()).toBe("<p><em>slanted</em> and <del>struck</del></p>");+ });});diff --git a/packages/ckeditor5/src/plugins.ts b/packages/ckeditor5/src/plugins.tsindex 80b19ae677..b960e39a82 100644--- a/packages/ckeditor5/src/plugins.ts+++ b/packages/ckeditor5/src/plugins.ts@@ -138,6 +138,34 @@ export const CHAT_INPUT_PLUGINS: typeof Plugin[] = [AutoLink];+/**+ * {@link CHAT_INPUT_PLUGINS} and the marks a sentence is written with, for an input whose text is+ * kept rather than sent on — the memo of a mind map node. Added to an {@link AttributeEditor} the+ * same way, and worth a toolbar there: a {@link BalloonEditor} raises one over the selection out of+ * whatever `toolbar` it is configured with.+ *+ * Kept apart from {@link CHAT_INPUT_PLUGINS} rather than added to it, though the chat box would+ * take these as readily: what is typed there is turned into markdown before it is sent, and that+ * pass keeps the text of an inline wrapper and drops the wrapper. Bold offered there would be bold+ * lost on its way to the model.+ *+ * `Autoformat` is what carries most of this: it registers `**bold**`, `_italic_`, `` `code` `` and+ * `~~struck~~` only where the matching command exists, so the marks below are what make those+ * spellings work at all — and the plainer way in, for anyone who would rather type than reach.+ *+ * The two Trilium converters ride along with them: they are what write italics as `<em>` and a+ * strikethrough as `<del>`, which is how the rest of Trilium stores both.+ */+export const MEMO_PLUGINS: typeof Plugin[] = [+ ...CHAT_INPUT_PLUGINS,+ Bold,+ Italic,+ ItalicAsEmPlugin,+ Strikethrough,+ StrikethroughAsDel,+ Code+];+/*** 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.*/