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
refactor(mind-map): replace the node menu plugin with our own panel
Elian Doran2fee0fadba2c
diff --git a/apps/client/package.json b/apps/client/package.jsonindex fbeebb53dd..01b2585973 100644--- a/apps/client/package.json+++ b/apps/client/package.json@@ -27,7 +27,6 @@"@fullcalendar/timegrid": "6.1.21","@maplibre/maplibre-gl-leaflet": "0.1.3","@mermaid-js/layout-elk": "0.2.2",- "@mind-elixir/node-menu": "5.0.1","@preact/signals": "2.10.0","@triliumnext/ckeditor5": "workspace:*","@triliumnext/codemirror": "workspace:*",diff --git a/apps/client/src/types-lib.d.ts b/apps/client/src/types-lib.d.tsindex 029b78c01e..4309462a63 100644--- a/apps/client/src/types-lib.d.ts+++ b/apps/client/src/types-lib.d.ts@@ -28,10 +28,6 @@ declare module "draggabilly" {}}-declare module "@mind-elixir/node-menu" {- export default mindmap;-}-declare module "katex/contrib/auto-render" {var renderMathInElement: (element: HTMLElement, options: {trust: boolean;diff --git a/apps/client/src/widgets/type_widgets/MindMap.css b/apps/client/src/widgets/type_widgets/MindMap.cssindex 480666d677..80664e7f08 100644--- a/apps/client/src/widgets/type_widgets/MindMap.css+++ b/apps/client/src/widgets/type_widgets/MindMap.css@@ -7,120 +7,15 @@height: 100%;}-.map-container .node-menu {+/* Hosts our own floating UI on top of the map, without interfering with the DOM managed by Mind Elixir. */+.mind-map-container .mind-map-overlay {position: absolute;- top: 60px;- inset-inline-end: 20px;- bottom: 80px;- overflow: auto;- background: var(--panel-bgcolor);- color: var(--main-color);- border-radius: 5px;- box-shadow: 0 1px 2px #0003;- width: 240px;- box-sizing: border-box;- padding: 0 15px 15px;- transition: .3s all+ inset: 0;+ pointer-events: none;}-.map-container .node-menu.close {- height: 29px;- width: 46px;- overflow: hidden-}--.map-container .node-menu .button-container {- padding: 3px 0;- direction: rtl-}--.map-container .node-menu #nm-tag {- margin-top: 20px-}--.map-container .node-menu .nm-fontsize-container {- display: flex;- justify-content: space-around;- margin-bottom: 20px-}--.map-container .node-menu .nm-fontsize-container div {- height: 36px;- width: 36px;- display: flex;- align-items: center;- justify-content: center;- box-shadow: 0 1px 2px #0003;- background-color: #fff;- color: tomato;- border-radius: 100%-}--.map-container .node-menu .nm-fontcolor-container {- margin-bottom: 10px-}--.map-container .node-menu input,-.map-container .node-menu textarea {- background: var(--input-background-color);- border: 1px solid var(--panel-border-color);- border-radius: var(--bs-border-radius);- color: var(--main-color);- padding: 5px;- margin: 10px 0;- width: 100%;- box-sizing: border-box;-}--.map-container .node-menu textarea {- resize: none-}--.map-container .node-menu .split6 {- display: inline-block;- width: 16.66%;- margin-bottom: 5px-}--.map-container .node-menu .palette {- border-radius: 100%;- width: 21px;- height: 21px;- border: 1px solid #edf1f2;- margin: auto-}--.map-container .node-menu .nmenu-selected,-.map-container .node-menu .palette:hover {- box-shadow: tomato 0 0 0 2px;- background-color: #c7e9fa-}--.map-container .node-menu .size-selected {- background-color: tomato !important;- border-color: tomato;- fill: #fff;- color: #fff-}--.map-container .node-menu .size-selected svg {- color: #fff-}--.map-container .node-menu .bof {- text-align: center-}--.map-container .node-menu .bof span {- display: inline-block;- font-size: 14px;- border-radius: 4px;- padding: 2px 5px-}--.map-container .node-menu .bof .selected {- background-color: tomato;- color: #fff+.mind-map-container .mind-map-overlay > * {+ pointer-events: auto;}.mind-map-container:fullscreen {diff --git a/apps/client/src/widgets/type_widgets/MindMap.spec.ts b/apps/client/src/widgets/type_widgets/MindMap.spec.tsindex 8a0219459b..9724c2b253 100644--- a/apps/client/src/widgets/type_widgets/MindMap.spec.ts+++ b/apps/client/src/widgets/type_widgets/MindMap.spec.ts@@ -2,11 +2,7 @@// DOMPurify relies on browser-faithful DOM traversal (NodeIterator); happy-dom// mishandles it and strips valid markup (surfaced by dompurify 3.4.8). Run the// sanitization-dependent specs under jsdom, which matches real-browser behavior.-import { describe, expect, it, vi } from "vitest";--// The node-menu plugin touches the DOM at import time, which fails under happy-dom-// and is irrelevant to the pure sanitization logic under test here.-vi.mock("@mind-elixir/node-menu", () => ({ default: {} }));+import { describe, expect, it } from "vitest";import { sanitizeMindMapData } from "./MindMap.js";diff --git a/apps/client/src/widgets/type_widgets/MindMap.tsx b/apps/client/src/widgets/type_widgets/MindMap.tsxindex 111c150c5b..7c6036ad1e 100644--- a/apps/client/src/widgets/type_widgets/MindMap.tsx+++ b/apps/client/src/widgets/type_widgets/MindMap.tsx@@ -1,15 +1,13 @@import "mind-elixir/style";-import "@mind-elixir/node-menu/dist/style.css";import "./MindMap.css";-// allow node-menu plugin css to be bundled by webpack-import nodeMenu from "@mind-elixir/node-menu";import { NOTE_TYPE_IMAGE_ATTACHMENTS } from "@triliumnext/commons";import { t } from "i18next";-import { DARK_THEME, default as VanillaMindElixir, MindElixirData, MindElixirInstance, Operation, THEME as LIGHT_THEME } from "mind-elixir";+import { DARK_THEME, default as VanillaMindElixir, MindElixirData, MindElixirInstance, NodeObj, Operation, THEME as LIGHT_THEME } from "mind-elixir";import type { LangPack } from "mind-elixir/i18n";-import { HTMLAttributes, RefObject } from "preact";-import { useCallback, useEffect, useRef } from "preact/hooks";+import { ComponentChildren, HTMLAttributes, RefObject } from "preact";+import { createPortal } from "preact/compat";+import { useCallback, useEffect, useRef, useState } from "preact/hooks";import { sanitizeNoteContentHtml } from "../../services/sanitize_content";import toast from "../../services/toast";@@ -17,6 +15,7 @@ import utils from "../../services/utils";import { useColorScheme, useEditorSpacedUpdate, useEffectiveReadOnly, useSyncedRef, useTriliumEvent, useTriliumEvents, useTriliumOption } from "../react/hooks";import { refToJQuerySelector } from "../react/react_utils";import { renderMindMapPreviewSvg } from "./helpers/mind_map_export";+import MindMapNodePanel from "./MindMapNodePanel";import { TypeWidgetProps } from "./type_widget";const NEW_TOPIC_NAME = "";@@ -68,10 +67,13 @@ function sanitizeMindMapNode(value: unknown): void {interface MindElixirProps {apiRef?: RefObject<MindElixirInstance>;+ /** Rendered in an overlay on top of the map, outside of the DOM managed by Mind Elixir. */+ children?: ComponentChildren;containerProps?: Omit<HTMLAttributes<HTMLDivElement>, "ref">;containerRef?: RefObject<HTMLDivElement>;editable: boolean;onChange?: () => void;+ onSelectionChange?: (selectedNodes: NodeObj[]) => void;}function buildMindElixirLangPack(): LangPack {@@ -95,6 +97,7 @@ export default function MindMap({ note, ntxId, noteContext }: TypeWidgetProps) {const apiRef = useRef<MindElixirInstance>(null);const containerRef = useRef<HTMLDivElement>(null);const isReadOnly = useEffectiveReadOnly(note, noteContext);+ const [ selectedNodes, setSelectedNodes ] = useState<NodeObj[]>([]);const spacedUpdate = useEditorSpacedUpdate({@@ -178,21 +181,27 @@ export default function MindMap({ note, ntxId, noteContext }: TypeWidgetProps) {containerRef={containerRef}apiRef={apiRef}onChange={() => spacedUpdate.scheduleUpdate()}+ onSelectionChange={setSelectedNodes}editable={!isReadOnly}containerProps={{className: "mind-map-container",onKeyDown}}- />+ >+ {!isReadOnly && selectedNodes.length > 0 && <MindMapNodePanel />}+ </MindElixir>);}-function MindElixir({ containerRef: externalContainerRef, containerProps, apiRef: externalApiRef, onChange, editable }: MindElixirProps) {+function MindElixir({ children, containerRef: externalContainerRef, containerProps, apiRef: externalApiRef, onChange, onSelectionChange, editable }: MindElixirProps) {const containerRef = useSyncedRef<HTMLDivElement>(externalContainerRef, null);const apiRef = useRef<MindElixirInstance>(null);const [ locale ] = useTriliumOption("locale");const colorScheme = useColorScheme();const defaultColorScheme = useRef(colorScheme);+ const [ overlayEl ] = useState(createOverlayElement);+ // Incremented every time a new instance is built, so that listeners get rebound onto its event bus.+ const [ instanceVersion, setInstanceVersion ] = useState(0);function reinitialize() {if (!containerRef.current) return;@@ -204,14 +213,16 @@ function MindElixir({ containerRef: externalContainerRef, containerProps, apiReftheme: defaultColorScheme.current === "dark" ? DARK_THEME : LIGHT_THEME});- if (editable) {- mind.install(nodeMenu);- }-apiRef.current = mind;if (externalApiRef) {externalApiRef.current = mind;}++ // Mind Elixir wipes the element it is given, so the overlay has to be reattached+ // after every initialization. It goes outside `mind.container` on purpose, since+ // that one scrolls along with the map.+ containerRef.current.appendChild(overlayEl);+ setInstanceVersion((version) => version + 1);}useEffect(() => {@@ -262,7 +273,34 @@ function MindElixir({ containerRef: externalContainerRef, containerProps, apiRef};}, [ onChange ]);+ // Selection listener. The events only carry the nodes that were added to or removed from the+ // selection, so the full selection is read back from the instance instead.+ useEffect(() => {+ const mind = apiRef.current;+ if (!onSelectionChange || !mind) return;++ const reportSelection = () => onSelectionChange(mind.currentNodes.map(({ nodeObj }) => nodeObj));++ reportSelection();+ mind.bus.addListener("selectNodes", reportSelection);+ mind.bus.addListener("unselectNodes", reportSelection);++ return () => {+ mind.bus.removeListener("selectNodes", reportSelection);+ mind.bus.removeListener("unselectNodes", reportSelection);+ };+ }, [ onSelectionChange, instanceVersion ]);+return (- <div ref={containerRef} {...containerProps} />+ <>+ <div ref={containerRef} {...containerProps} />+ {createPortal(<>{children}</>, overlayEl)}+ </>);}++function createOverlayElement() {+ const overlayEl = document.createElement("div");+ overlayEl.className = "mind-map-overlay";+ return overlayEl;+}diff --git a/apps/client/src/widgets/type_widgets/MindMapNodePanel.css b/apps/client/src/widgets/type_widgets/MindMapNodePanel.cssnew file mode 100644index 0000000000..3389724e60--- /dev/null+++ b/apps/client/src/widgets/type_widgets/MindMapNodePanel.css@@ -0,0 +1,21 @@+.mind-map-node-panel {+ position: absolute;+ /* Leaves room for the floating buttons of the note. */+ top: 60px;+ inset-inline-end: 20px;+ width: 240px;+ max-height: calc(100% - 100px);+ overflow: auto;+ box-sizing: border-box;+ padding: 15px;+ background: var(--menu-background-color);+ color: var(--menu-text-color);+ border: 1px solid var(--dropdown-border-color);+ border-radius: 8px;+ box-shadow: 0 2px 8px #0003;+ backdrop-filter: blur(10px);+}++.mind-map-container:fullscreen .mind-map-node-panel {+ top: calc(20px + env(safe-area-inset-top));+}diff --git a/apps/client/src/widgets/type_widgets/MindMapNodePanel.tsx b/apps/client/src/widgets/type_widgets/MindMapNodePanel.tsxnew file mode 100644index 0000000000..dbad7dc7d5--- /dev/null+++ b/apps/client/src/widgets/type_widgets/MindMapNodePanel.tsx@@ -0,0 +1,18 @@+import "./MindMapNodePanel.css";++/**+ * Floating panel displayed over a mind map while at least one node is selected, meant to host the+ * formatting controls for the selection.+ */+export default function MindMapNodePanel() {+ return (+ <div+ className="mind-map-node-panel"+ /* Keep interactions inside the panel from reaching the map underneath. */+ onMouseDown={(e) => e.stopPropagation()}+ onKeyDown={(e) => e.stopPropagation()}+ >+ Hello world+ </div>+ );+}diff --git a/apps/standalone/package.json b/apps/standalone/package.jsonindex ac6fd52f83..a62e438e25 100644--- a/apps/standalone/package.json+++ b/apps/standalone/package.json@@ -23,7 +23,6 @@"@fullcalendar/timegrid": "6.1.21","@maplibre/maplibre-gl-leaflet": "0.1.3","@mermaid-js/layout-elk": "0.2.2",- "@mind-elixir/node-menu": "5.0.1","@popperjs/core": "2.11.8","@preact/signals": "2.10.0","@sqlite.org/sqlite-wasm": "3.51.1-build2",diff --git a/packages/trilium-e2e/src/note_types/mindmap.spec.ts b/packages/trilium-e2e/src/note_types/mindmap.spec.tsindex 9b3ca19070..ffcb28097a 100644--- a/packages/trilium-e2e/src/note_types/mindmap.spec.ts+++ b/packages/trilium-e2e/src/note_types/mindmap.spec.ts@@ -16,7 +16,7 @@ test("displays note settings", async ({ page, context }) => {await app.goto();await app.goToNoteInNewTab("Sample mindmap");- await app.currentNoteSplit.getByText("Hello world").click({ force: true });- const nodeMenu = app.currentNoteSplit.locator(".node-menu");- await expect(nodeMenu).toBeVisible();+ await app.currentNoteSplit.locator("me-tpc").filter({ hasText: "Hello world" }).click({ force: true });+ const nodePanel = app.currentNoteSplit.locator(".mind-map-node-panel");+ await expect(nodePanel).toBeVisible();});diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 9ccefd7701..a3e60147cb 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -223,9 +223,6 @@ importers:'@mermaid-js/layout-elk':specifier: 0.2.2version: 0.2.2([email protected])- '@mind-elixir/node-menu':- specifier: 5.0.1- version: 5.0.1([email protected])'@preact/signals':specifier: 2.10.0version: 2.10.0([email protected])@@ -857,9 +854,6 @@ importers:'@mermaid-js/layout-elk':specifier: 0.2.2version: 0.2.2([email protected])- '@mind-elixir/node-menu':- specifier: 5.0.1- version: 5.0.1([email protected])'@popperjs/core':specifier: 2.11.8version: 2.11.8@@ -3585,11 +3579,6 @@ packages:'@mermaid-js/[email protected]':resolution: {integrity: sha512-oYPyv8A4As1yH5Bx+04iQEQxXuIQDe0GKCNSRgao6z8AM9jixXIfP0vsppRLvGf+nKIOb9/LdpWA4YuJiVvESA==}- '@mind-elixir/[email protected]':- resolution: {integrity: sha512-K5+DiduGqEEPd0IejxW7eMqafOajMby4sA8IEt+iUvNjeBCboGVng9M7ZIk6aTp/MhVYTKLZvV+FPXFnGzohmA==}- peerDependencies:- mind-elixir: '>5.0.0'-'@mixmark-io/[email protected]':resolution: {integrity: sha512-Y28PR25bHXUg88kCV7nivXrP2Nj2RueZ3/l/jdx6J9f8J4nsEGcgX0Qe6lt7Pa+J79+kPiJU3LguR6O/6zrLOw==}@@ -15512,10 +15501,6 @@ snapshots:dependencies:'@chevrotain/types': 11.1.2- '@mind-elixir/[email protected]([email protected])':- dependencies:- mind-elixir: 5.14.0-'@mixmark-io/[email protected]': {}