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
client/text editor/collapsible blocks: use the new text editor tooltip manager
Adorian Doran1266893af96a
diff --git a/packages/ckeditor5-collapsible/package.json b/packages/ckeditor5-collapsible/package.jsonindex be27ab3d8f..85cf1ad2ea 100644--- a/packages/ckeditor5-collapsible/package.json+++ b/packages/ckeditor5-collapsible/package.json@@ -24,6 +24,9 @@"vite-plugin-svgo": "2.0.0","vitest": "4.1.10"},+ "dependencies": {+ "@triliumnext/ckeditor5-utils": "workspace:*"+ },"peerDependencies": {"bootstrap": "^5.3.0","ckeditor5": "48.2.0"diff --git a/packages/ckeditor5-collapsible/src/collapsible-editing.ts b/packages/ckeditor5-collapsible/src/collapsible-editing.tsindex 75c5b0d5df..5b6a6fcdf1 100644--- a/packages/ckeditor5-collapsible/src/collapsible-editing.ts+++ b/packages/ckeditor5-collapsible/src/collapsible-editing.ts@@ -1,10 +1,24 @@import { Plugin, Enter, Delete, enableViewPlaceholder, getEnvKeystrokeText, type ViewDocumentEnterEvent, type ViewDocumentDeleteEvent, type ViewDocumentArrowKeyEvent } from "ckeditor5";-import { Tooltip } from "bootstrap";+import { EditorTooltipManager, type TooltipHandle } from "@triliumnext/ckeditor5-utils";import BlockDragHandle from "./block-drag-handle.js";import CollapsibleCommand from "./collapsible-command.js";type TranslateFn = (key: string, params?: Record<string, unknown>) => string;+/**+ * Dwell delay before hover or a stationary caret pops a summary/handle tooltip.+ * Long enough that mouse flyovers don't spawn a popup, short enough that+ * intentional attention still produces one.+ */+const TOOLTIP_DWELL_MS = 200;++/**+ * How long a tooltip stays visible without any new event (push, content+ * refresh, caret movement) before it fades itself out. Matches the todo-list+ * multistate plugin's cadence so the two feel consistent.+ */+const TOOLTIP_AUTO_HIDE_MS = 2000;+/*** Schema, conversion and key handling for collapsible blocks.*@@ -37,12 +51,27 @@ export default class CollapsibleEditing extends Plugin {* opened like a freshly-inserted one).*/private readonly preserveOpenOnNextInsert = new Map<any, boolean>();- /** Summary DOM elements that currently have a Bootstrap tooltip attached. */- private readonly summaryTooltips = new Set<HTMLElement>();- /** The summary tooltip we currently have force-shown because the caret is inside it. */- private caretShownTooltip?: HTMLElement;- /** Drag-handle DOM elements that currently have a Bootstrap tooltip attached. */- private readonly handleTooltips = new Set<HTMLElement>();+ /**+ * Shared visibility stack for the summary tooltips (screen-corner hint on+ * each <summary>). One hover handle per rendered summary plus one caret+ * handle push/pop against it — only the top of the stack is on screen at a+ * time. See {@link EditorTooltipManager} for the rationale.+ */+ private summaryTooltipManager?: EditorTooltipManager;+ /** Hover-driven handle per rendered <summary>. Disposed when the summary detaches. */+ private readonly summaryHoverHandles = new Map<HTMLElement, TooltipHandle>();+ /** Caret-driven handle, if the caret is currently inside a <summary>. */+ private summaryCaretHandle?: TooltipHandle;+ /** DOM <summary> the caret handle is currently attached to. */+ private summaryCaretDom?: HTMLElement;+ /**+ * Shared visibility stack for the drag-handle tooltips. Handles use default+ * near-element placement (no `text-editor-content-tooltip` class), so they+ * live in their own manager with its own `tooltipOptions`.+ */+ private handleTooltipManager?: EditorTooltipManager;+ /** Hover-driven handle per rendered drag-handle. Disposed when the handle detaches. */+ private readonly handleHoverHandles = new Map<HTMLElement, TooltipHandle>();public init(): void {this.editor.commands.add("collapsible", new CollapsibleCommand(this.editor));@@ -93,15 +122,21 @@ export default class CollapsibleEditing extends Plugin {clearTimeout(this.autoOpenTimer);this.autoOpenTimer = undefined;}- for (const summary of this.summaryTooltips) {- Tooltip.getInstance(summary)?.dispose();+ for (const handle of this.summaryHoverHandles.values()) {+ handle.dispose();}- this.summaryTooltips.clear();- this.caretShownTooltip = undefined;- for (const handle of this.handleTooltips) {- Tooltip.getInstance(handle)?.dispose();+ this.summaryHoverHandles.clear();+ this.summaryCaretHandle?.dispose();+ this.summaryCaretHandle = undefined;+ this.summaryCaretDom = undefined;+ this.summaryTooltipManager?.destroy();+ this.summaryTooltipManager = undefined;+ for (const handle of this.handleHoverHandles.values()) {+ handle.dispose();}- this.handleTooltips.clear();+ this.handleHoverHandles.clear();+ this.handleTooltipManager?.destroy();+ this.handleTooltipManager = undefined;this.dragHandle?.cancel();this.preserveOpenOnNextInsert.clear();super.destroy();@@ -790,115 +825,117 @@ export default class CollapsibleEditing extends Plugin {}// ------------------------------------------------------------------ // Summary tooltip (Bootstrap, screen-corner hint)+ // Summary tooltip (screen-corner hint) — driven by EditorTooltipManager// -----------------------------------------------------------------/**- * Attach a Bootstrap tooltip to every <summary> DOM element so hovering or- * focusing the title pops up a "click the arrow or press Ctrl+Enter" hint- * in the screen corner (the same UX as the todo-list multistate plugin).+ * Attach a manager-mediated hover tooltip to every <summary> DOM element,+ * plus a caret-driven handle that follows the model selection into and+ * out of summaries. Hover on the summary the caret currently owns is a+ * no-op (same coordination rule as the todo-list multistate plugin), so+ * the two flows can never race the same popup.*/private registerSummaryTooltips() {const editor = this.editor;const t = this.translate();+ const title = t("text-editor.collapsible-tooltip", {+ shortcut: getEnvKeystrokeText("Ctrl+Enter")+ });+ const manager = new EditorTooltipManager({+ tooltipOptions: { customClass: "text-editor-content-tooltip" },+ autoHideAfterMs: TOOLTIP_AUTO_HIDE_MS+ });+ this.summaryTooltipManager = manager;this.listenTo(editor.editing.view, "render", () => {- // 1. Reap tooltips whose summaries CKEditor has detached. Doing this- // FIRST (a) frees the references so GC can collect them and- // (b) keeps `summaryTooltips.size` honest for the fast-path below.- let cleaned = false;- for (const summary of this.summaryTooltips) {+ // Reap handles whose <summary> CKEditor has detached.+ for (const [summary, handle] of this.summaryHoverHandles) {if (!summary.isConnected) {- Tooltip.getInstance(summary)?.dispose();- this.summaryTooltips.delete(summary);- if (this.caretShownTooltip === summary) this.caretShownTooltip = undefined;- cleaned = true;+ handle.dispose();+ this.summaryHoverHandles.delete(summary);+ if (this.summaryCaretDom === summary) {+ this.summaryCaretHandle?.dispose();+ this.summaryCaretHandle = undefined;+ this.summaryCaretDom = undefined;+ }}}- // 2. Collect current summaries from the DOM.- const currentSummaries: HTMLElement[] = [];+ // Wire up hover handles on newly-appeared summaries.this.forEachDomRoot(root => {- for (const s of root.querySelectorAll<HTMLElement>("details.trilium-collapsible > summary")) {- currentSummaries.push(s);+ for (const summary of root.querySelectorAll<HTMLElement>("details.trilium-collapsible > summary")) {+ if (this.summaryHoverHandles.has(summary)) continue;+ const handle = manager.createHandle(summary, title);+ this.summaryHoverHandles.set(summary, handle);+ // Hover on the <summary> whose caret we already own is a no-op:+ // the caret handle owns visibility, and running the dwell + push+ // cycle on top of it only confuses the stack.+ const ownedByCaret = () => this.summaryCaretDom === summary;+ summary.addEventListener("mouseenter", () => {+ if (ownedByCaret()) return;+ handle.showAfter(TOOLTIP_DWELL_MS);+ });+ summary.addEventListener("mouseleave", () => {+ if (ownedByCaret()) return;+ handle.hide();+ });}});-- // 3. Fast-path: nothing was reaped AND the count still matches the- // tracked set — no work to do. This covers the typical keystroke- // render where nothing about the summary set has changed.- if (!cleaned && currentSummaries.length === this.summaryTooltips.size) return;-- // 4. Wire up tooltips on newly-appeared summaries.- for (const summary of currentSummaries) {- if (this.summaryTooltips.has(summary)) continue;- const title = t("text-editor.collapsible-tooltip", {- shortcut: getEnvKeystrokeText("Ctrl+Enter")- });- new Tooltip(summary, { title, customClass: "text-editor-content-tooltip" });- this.summaryTooltips.add(summary);- }});// The caret moving into a <summary> doesn't fire DOM focus (the editable- // root keeps focus), so Bootstrap's focus trigger won't notice. Manually- // show/hide the tooltip on model-selection changes so the hint also- // appears when the user navigates into the title via keyboard or click.+ // root keeps focus), so a hover-only tooltip would miss keyboard-into-summary+ // navigation. Push a caret handle on selection changes to cover it.this.listenTo(editor.model.document.selection, "change:range", () => {const summaryModel = editor.model.document.selection.getFirstPosition()?.findAncestor("summary");const targetDom = summaryModel ? this.getDom<HTMLElement>(summaryModel) : null;- // No-op when the target hasn't changed (selection moves on every keystroke- // while typing in the summary; re-calling show() retriggers the animation).- if (this.caretShownTooltip === targetDom) return;+ // No-op when the target hasn't changed — selection moves on every+ // keystroke while typing in the summary, and rebuilding the handle+ // would restart the auto-hide countdown for no reason.+ if (this.summaryCaretDom === targetDom) return;- if (this.caretShownTooltip) {- Tooltip.getInstance(this.caretShownTooltip)?.hide();- this.caretShownTooltip = undefined;- }+ this.summaryCaretHandle?.dispose();+ this.summaryCaretHandle = undefined;+ this.summaryCaretDom = targetDom ?? undefined;if (targetDom) {- Tooltip.getInstance(targetDom)?.show();- this.caretShownTooltip = targetDom;+ this.summaryCaretHandle = manager.createHandle(targetDom, title);+ this.summaryCaretHandle.show();}});}/**- * Attach a plain Bootstrap tooltip (default near-element placement, no- * screen-corner styling) to each drag handle so hover/focus surfaces the- * "Drag to reposition" hint. Tracked in a Set and reaped on view render- * the same way summary tooltips are.+ * Attach a manager-mediated hover tooltip to each drag handle. Handles+ * use default near-element Bootstrap placement (no screen-corner CSS),+ * so they live in their own manager with plain `tooltipOptions`.*/private registerHandleTooltips() {const editor = this.editor;const t = this.translate();+ const title = t("text-editor.collapsible-handle-tooltip");+ const manager = new EditorTooltipManager({+ autoHideAfterMs: TOOLTIP_AUTO_HIDE_MS+ });+ this.handleTooltipManager = manager;this.listenTo(editor.editing.view, "render", () => {- let cleaned = false;- for (const handle of this.handleTooltips) {- if (!handle.isConnected) {- Tooltip.getInstance(handle)?.dispose();- this.handleTooltips.delete(handle);- cleaned = true;+ for (const [dragHandle, handle] of this.handleHoverHandles) {+ if (!dragHandle.isConnected) {+ handle.dispose();+ this.handleHoverHandles.delete(dragHandle);}}- const current: HTMLElement[] = [];this.forEachDomRoot(root => {- for (const h of root.querySelectorAll<HTMLElement>(".trilium-collapsible-handle")) {- current.push(h);+ for (const dragHandle of root.querySelectorAll<HTMLElement>(".trilium-collapsible-handle")) {+ if (this.handleHoverHandles.has(dragHandle)) continue;+ const handle = manager.createHandle(dragHandle, title);+ this.handleHoverHandles.set(dragHandle, handle);+ dragHandle.addEventListener("mouseenter", () => handle.showAfter(TOOLTIP_DWELL_MS));+ dragHandle.addEventListener("mouseleave", () => handle.hide());}});-- if (!cleaned && current.length === this.handleTooltips.size) return;-- for (const handle of current) {- if (this.handleTooltips.has(handle)) continue;- new Tooltip(handle, {- title: t("text-editor.collapsible-handle-tooltip")- });- this.handleTooltips.add(handle);- }});}diff --git a/packages/ckeditor5-utils/package.json b/packages/ckeditor5-utils/package.jsonnew file mode 100644index 0000000000..a2ae7231d6--- /dev/null+++ b/packages/ckeditor5-utils/package.json@@ -0,0 +1,19 @@+{+ "name": "@triliumnext/ckeditor5-utils",+ "description": "Shared editor-tier primitives (tooltip manager, …) used by @triliumnext/ckeditor5 and its plugin packages.",+ "version": "0.1.0",+ "private": true,+ "type": "module",+ "main": "./src/index.ts",+ "scripts": {+ "test": "vitest"+ },+ "devDependencies": {+ "@vitest/browser": "4.1.10",+ "vitest": "4.1.10",+ "webdriverio": "9.29.1"+ },+ "peerDependencies": {+ "bootstrap": "^5.3.0"+ }+}diff --git a/packages/ckeditor5/src/utils/editor_tooltip_manager.spec.ts b/packages/ckeditor5-utils/src/editor_tooltip_manager.spec.tssimilarity index 100%rename from packages/ckeditor5/src/utils/editor_tooltip_manager.spec.tsrename to packages/ckeditor5-utils/src/editor_tooltip_manager.spec.tsdiff --git a/packages/ckeditor5/src/utils/editor_tooltip_manager.ts b/packages/ckeditor5-utils/src/editor_tooltip_manager.tssimilarity index 100%rename from packages/ckeditor5/src/utils/editor_tooltip_manager.tsrename to packages/ckeditor5-utils/src/editor_tooltip_manager.tsdiff --git a/packages/ckeditor5-utils/src/index.ts b/packages/ckeditor5-utils/src/index.tsnew file mode 100644index 0000000000..a3620b428d--- /dev/null+++ b/packages/ckeditor5-utils/src/index.ts@@ -0,0 +1 @@+export { EditorTooltipManager, type EditorTooltipManagerOptions, type TooltipHandle } from "./editor_tooltip_manager.js";diff --git a/packages/ckeditor5-utils/tsconfig.json b/packages/ckeditor5-utils/tsconfig.jsonnew file mode 100644index 0000000000..c23e61c800--- /dev/null+++ b/packages/ckeditor5-utils/tsconfig.json@@ -0,0 +1,10 @@+{+ "extends": "../../tsconfig.base.json",+ "files": [],+ "include": [],+ "references": [+ {+ "path": "./tsconfig.lib.json"+ }+ ]+}diff --git a/packages/ckeditor5-utils/tsconfig.lib.json b/packages/ckeditor5-utils/tsconfig.lib.jsonnew file mode 100644index 0000000000..0d990d2142--- /dev/null+++ b/packages/ckeditor5-utils/tsconfig.lib.json@@ -0,0 +1,19 @@+{+ "extends": "../../tsconfig.base.json",+ "compilerOptions": {+ "module": "ESNext",+ "moduleResolution": "bundler",+ "rootDir": "src",+ "outDir": "dist",+ "tsBuildInfoFile": "dist/tsconfig.lib.tsbuildinfo",+ "emitDeclarationOnly": true,+ "forceConsistentCasingInFileNames": true,+ "lib": ["DOM", "ES2023"]+ },+ "include": [+ "src/**/*.ts"+ ],+ "exclude": [+ "src/**/*.spec.ts"+ ]+}diff --git a/packages/ckeditor5-utils/vitest.config.ts b/packages/ckeditor5-utils/vitest.config.tsnew file mode 100644index 0000000000..08e0a8c7ff--- /dev/null+++ b/packages/ckeditor5-utils/vitest.config.ts@@ -0,0 +1,32 @@+import { webdriverio } from "@vitest/browser-webdriverio";+import { defineConfig } from "vitest/config";++export default defineConfig({+ test: {+ browser: {+ enabled: true,+ provider: webdriverio(),+ headless: true,+ ui: false,+ instances: [{ browser: "chrome" }]+ },+ include: ["src/**/*.spec.ts"],+ globals: true,+ watch: false,+ reporters: ["default", ["junit", { outputFile: "./test-output/vitest/junit.xml", addFileAttribute: true }]],+ coverage: {+ thresholds: {+ lines: 100,+ functions: 100,+ branches: 100,+ statements: 100+ },+ provider: "v8",+ reportsDirectory: "./test-output/vitest/coverage",+ allowExternal: false,+ include: ["src/**/*.{ts,tsx}"],+ exclude: ["**/*.{test,spec}.{ts,mts,cts,tsx,js,jsx}", "**/*.d.ts", "**/node_modules/**"],+ reporter: ["text", "lcov"]+ }+ }+});diff --git a/packages/ckeditor5/package.json b/packages/ckeditor5/package.jsonindex 5e931baf57..047f3a83bf 100644--- a/packages/ckeditor5/package.json+++ b/packages/ckeditor5/package.json@@ -17,6 +17,7 @@"@triliumnext/commons": "workspace:*","@triliumnext/ckeditor5-admonition": "workspace:*","@triliumnext/ckeditor5-collapsible": "workspace:*",+ "@triliumnext/ckeditor5-utils": "workspace:*","@triliumnext/ckeditor5-footnotes": "workspace:*","@triliumnext/ckeditor5-keyboard-marker": "workspace:*","@triliumnext/ckeditor5-math": "workspace:*",diff --git a/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_editing.ts b/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_editing.tsindex 27e541e669..29a79bdd9e 100644--- a/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_editing.ts+++ b/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_editing.ts@@ -2,7 +2,7 @@ import { DEFAULT_TASK_STATES, DONE_STATE_NAME, formatShortcut, isAnchorState, joimport { Command, env, ListEditing, Plugin, TodoList, type Editor, type ModelElement, type ViewElement } from "ckeditor5";import { onTodoRowSplit } from "../todo_list_uncheck_on_enter.js";-import { EditorTooltipManager, type TooltipHandle } from "../../utils/editor_tooltip_manager.js";+import { EditorTooltipManager, type TooltipHandle } from "@triliumnext/ckeditor5-utils";/*** Dwell delay before a hover or a stationary caret pops the checkbox tooltip.diff --git a/packages/ckeditor5/tsconfig.json b/packages/ckeditor5/tsconfig.jsonindex 443bd1e472..26a45d76cb 100644--- a/packages/ckeditor5/tsconfig.json+++ b/packages/ckeditor5/tsconfig.json@@ -18,6 +18,9 @@{"path": "../ckeditor5-keyboard-marker"},+ {+ "path": "../ckeditor5-utils"+ },{"path": "./tsconfig.lib.json"}diff --git a/packages/ckeditor5/tsconfig.lib.json b/packages/ckeditor5/tsconfig.lib.jsonindex 09c4f6eb8d..ff66415d5c 100644--- a/packages/ckeditor5/tsconfig.lib.json+++ b/packages/ckeditor5/tsconfig.lib.json@@ -38,6 +38,9 @@},{"path": "../ckeditor5-keyboard-marker"+ },+ {+ "path": "../ckeditor5-utils"}]}diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 7e39fb1de4..1dc06c8635 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -1104,6 +1104,9 @@ importers:'@triliumnext/ckeditor5-mermaid':specifier: workspace:*version: link:../ckeditor5-mermaid+ '@triliumnext/ckeditor5-utils':+ specifier: workspace:*+ version: link:../ckeditor5-utils'@triliumnext/commons':specifier: workspace:*version: link:../commons@@ -1162,6 +1165,9 @@ importers:packages/ckeditor5-collapsible:dependencies:+ '@triliumnext/ckeditor5-utils':+ specifier: workspace:*+ version: link:../ckeditor5-utilsbootstrap:specifier: ^5.3.0version: 5.3.8(@popperjs/[email protected])@@ -1333,6 +1339,22 @@ importers:specifier: 9.29.1version: 9.29.1+ packages/ckeditor5-utils:+ dependencies:+ bootstrap:+ specifier: ^5.3.0+ version: 5.3.8(@popperjs/[email protected])+ devDependencies:+ '@vitest/browser':+ specifier: 4.1.10+ version: 4.1.10([email protected](@types/[email protected])([email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])+ vitest:+ specifier: 4.1.10+ version: 4.1.10(@opentelemetry/[email protected])(@types/[email protected])(@vitest/[email protected])(@vitest/[email protected])(@vitest/[email protected])([email protected])([email protected](@noble/[email protected]))([email protected](@types/[email protected])([email protected])([email protected])([email protected])([email protected])([email protected]))+ webdriverio:+ specifier: 9.29.1+ version: 9.29.1+packages/codemirror:dependencies:'@catppuccin/codemirror':