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/custom task states: keep the editor tooltip open while the caret is inside a task list item
Adorian Doranf2e5e0d04f49
diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsxindex b9a6921382..0e7ba9dd44 100644--- a/apps/client/src/widgets/react/hooks.tsx+++ b/apps/client/src/widgets/react/hooks.tsx@@ -1111,13 +1111,19 @@ export function useStaticTooltip(elRef: RefObject<Element>, config?: Partial<Tootooltip.dispose();}- // Remove any lingering tooltip popup elements from the DOM. This must stay- // unconditional: delegated (`selector:`) configs spawn per-target Tooltip- // instances whose popups the parent's dispose() does not remove, and cleanups- // run before DOM removal, so a disconnected-only sweep would not catch them.- document- .querySelectorAll(".tooltip")- .forEach(t => t.remove());+ // For delegated (`selector:`) configs, hovered children spawn per-target+ // Tooltip instances whose popups the parent's dispose() does not remove;+ // sweep them here. Non-delegated tooltips clean up their own popup in+ // `tooltip.dispose()`, and a global sweep would wipe unrelated tooltips+ // (e.g. CKEditor plugins' Bootstrap tooltips) every time this hook's+ // effect re-runs — which is what caused the checkbox tooltip in+ // `TodoListMultistateEditing` to vanish whenever the "note saved" badge+ // transitioned states.+ if (config?.selector) {+ document+ .querySelectorAll(".tooltip")+ .forEach(t => t.remove());+ }};}, [ elRef, config ]);}diff --git a/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_editing.spec.ts b/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_editing.spec.tsindex 1d14615fde..cdd051bc02 100644--- a/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_editing.spec.ts+++ b/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_editing.spec.ts@@ -547,4 +547,140 @@ describe("TodoListMultistateEditing", () => {}});});++ describe("caret-driven tooltip visibility", () => {+ // Fixture: a plain paragraph then two todo items. The caret starts in the plain+ // paragraph so we can move it into (and between) the todos to drive the listener.+ const CARET_FIXTURE = '<paragraph>plain[]</paragraph>' ++ '<paragraph listIndent="0" listItemId="t-a" listType="todo">A</paragraph>' ++ '<paragraph listIndent="0" listItemId="t-b" listType="todo">B</paragraph>';++ function inputs(): HTMLInputElement[] {+ const domRoot = editor.editing.view.getDomRoot();+ return Array.from(domRoot?.querySelectorAll<HTMLInputElement>('.todo-list__label input[type="checkbox"]') ?? []);+ }++ function moveCaretTo(blockIndex: number): void {+ editor.model.change((writer) => {+ const block = getBlock(editor, blockIndex);+ writer.setSelection(writer.createPositionAt(block, 0));+ });+ }++ beforeEach(async () => {+ editor = await createEditor({ taskStates: CUSTOM_STATES });+ setModelData(editor.model, CARET_FIXTURE);+ });++ it("shows the tooltip on the todo checkbox when the caret enters its <li>", () => {+ const [checkboxA] = inputs();+ const showSpy = vi.spyOn(Tooltip.getInstance(checkboxA)!, "show");++ moveCaretTo(1); // <paragraph listType="todo"> ("A")++ expect(showSpy).toHaveBeenCalled();+ });++ it("hides the shown tooltip when the caret leaves the todo item", () => {+ const [checkboxA] = inputs();+ moveCaretTo(1);+ const hideSpy = vi.spyOn(Tooltip.getInstance(checkboxA)!, "hide");++ moveCaretTo(0); // back to the plain paragraph++ expect(hideSpy).toHaveBeenCalled();+ });++ it("switches the shown tooltip when the caret moves between two todo items", () => {+ const [checkboxA, checkboxB] = inputs();+ moveCaretTo(1); // enter A+ const hideA = vi.spyOn(Tooltip.getInstance(checkboxA)!, "hide");+ const showB = vi.spyOn(Tooltip.getInstance(checkboxB)!, "show");++ moveCaretTo(2); // enter B++ expect(hideA).toHaveBeenCalled();+ expect(showB).toHaveBeenCalled();+ });++ it("does not re-show the tooltip when the caret moves within the same todo item", () => {+ const [checkboxA] = inputs();+ moveCaretTo(1);+ const tooltipA = Tooltip.getInstance(checkboxA)!;+ const showSpy = vi.spyOn(tooltipA, "show");+ const hideSpy = vi.spyOn(tooltipA, "hide");++ // Move the caret to a different position WITHIN the same todo item.+ editor.model.change((writer) => {+ writer.setSelection(writer.createPositionAt(getBlock(editor, 1), 1));+ });++ expect(showSpy).not.toHaveBeenCalled();+ expect(hideSpy).not.toHaveBeenCalled();+ });++ it("re-shows the tooltip after a state change recreates it while the caret is inside", () => {+ const [checkboxA] = inputs();+ moveCaretTo(1); // caret in todo A+ const showSpy = vi.spyOn(Tooltip.prototype, "show");++ // 'doing' has isCompleted=false → the checkbox input stays the same DOM+ // element, but its tooltip is disposed and recreated by the render listener.+ editor.execute("setTaskState", { state: "doing" });++ // Same input, new tooltip instance, and that new instance was shown.+ const newTooltip = Tooltip.getInstance(checkboxA);+ expect(newTooltip).not.toBeNull();+ expect(showSpy).toHaveBeenCalled();+ showSpy.mockRestore();+ });++ it("recovers cleanly when the checkbox under the caret is replaced by a reconvert", () => {+ const [checkboxA] = inputs();+ moveCaretTo(1);+ expect(Tooltip.getInstance(checkboxA)).not.toBeNull();++ // Toggling the native checkbox reconverts the todo item — the DOM input+ // is replaced. The render listener disposes the old tooltip and its+ // reaper nulls `_caretCheckbox`; `_syncCaretTooltip` then re-derives+ // the target from the caret and re-attaches to the fresh input.+ editor.execute("checkTodoList");+ editor.editing.view.forceRender();++ expect(checkboxA.isConnected).toBe(false);+ // Moving the caret out and back in must not throw on the stale reference.+ expect(() => moveCaretTo(0)).not.toThrow();+ });++ it("returns no target when the caret has no todo ancestor", () => {+ const [checkboxA] = inputs();+ moveCaretTo(1);+ const hideA = vi.spyOn(Tooltip.getInstance(checkboxA)!, "hide");++ moveCaretTo(0); // plain paragraph → no ancestor todo → target = null++ expect(hideA).toHaveBeenCalled();+ });++ it("bails out safely when the position has no parent (defensive branch)", () => {+ const selection = editor.model.document.selection;+ const spy = vi.spyOn(selection, "getFirstPosition")+ .mockReturnValueOnce(null as unknown as ReturnType<typeof selection.getFirstPosition>);+ // Nudge the selection so `change:range` fires with the mocked getFirstPosition.+ editor.model.change((writer) => {+ writer.setSelection(writer.createPositionAt(getBlock(editor, 0), 0));+ });+ spy.mockRestore();+ });++ it("nulls the caret-shown reference on destroy", async () => {+ moveCaretTo(1);+ // No throw expected — destroy must clear internal state cleanly whether+ // or not a caret-shown tooltip is currently tracked.+ await editor.destroy();+ expect(editor.state).toBe("destroyed");+ // Recreate so the shared afterEach's destroy() does not double-destroy.+ editor = await createEditor({ taskStates: CUSTOM_STATES });+ });+ });});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 4d26a1e596..9517a021a0 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@@ -37,6 +37,15 @@ export default class TodoListMultistateEditing extends Plugin {*/private readonly _checkboxTooltips = new Map<HTMLInputElement, string | null>();+ /**+ * The checkbox whose tooltip is currently force-shown because the caret is in+ * its todo item. Tracked so a follow-up selection change can `hide()` the+ * previous tooltip before `show()`ing the next, and so a render that recreates+ * the input can re-attach + re-show without a visible gap (same pattern as+ * {@link CollapsibleEditing#registerSummaryTooltips}).+ */+ private _caretCheckbox: HTMLInputElement | null = null;+init() {const editor = this.editor;const states = getConfiguredTaskStates(editor);@@ -110,14 +119,21 @@ export default class TodoListMultistateEditing extends Plugin {if (!input.isConnected) {Tooltip.getInstance(input)?.dispose();this._checkboxTooltips.delete(input);+ if (this._caretCheckbox === input) {+ this._caretCheckbox = null;+ }}}+ // Track inputs whose tooltip was (re)created this render so the caret+ // sync can re-`show()` them even when the caret target didn't change.+ const recreated = new Set<HTMLInputElement>();for (const input of domRoot.querySelectorAll<HTMLInputElement>(".todo-list__label input[type=\"checkbox\"]")) {const currentState = readTaskState(input);// Skip if the tooltip is already up to date for this state.if (Tooltip.getInstance(input) && this._checkboxTooltips.get(input) === currentState) {continue;}+ const wasTracked = this._checkboxTooltips.has(input);Tooltip.getInstance(input)?.dispose();const title = buildTooltipTitle(input, currentState, stateByName, translate);new Tooltip(input, {@@ -129,10 +145,40 @@ export default class TodoListMultistateEditing extends Plugin {// (with user values interpolated through i18next's default// escaping) so disabling the sanitizer is safe here.sanitize: false,+ // Manual trigger — Bootstrap's default `hover focus` fires+ // `_leave` on stray focusout events (the checkbox has+ // `tabindex="-1"` and can receive programmatic focus during+ // reconversion), which raced our caret-driven `show()` and+ // dismissed the tooltip. Everything visibility-related is+ // driven from `_syncCaretTooltip` and the hover listeners+ // attached in `_attachHoverListeners`.+ trigger: "manual",customClass: "text-editor-content-tooltip"});this._checkboxTooltips.set(input, currentState);+ recreated.add(input);+ // Attach hover listeners once per input — they use dynamic+ // `Tooltip.getInstance(input)` so they keep working after a+ // state-change refresh replaces the tooltip on the same element.+ if (!wasTracked) {+ this._attachHoverListeners(input);+ }}+ // Re-derive the caret target from the model. Covers three cases:+ // a) same input, same tooltip — nothing to do;+ // b) same input, tooltip recreated (state change on the item) —+ // force `show()` so the hint stays up;+ // c) input recreated (Ctrl+Shift+Enter across isCompleted boundary)+ // — old ref was nulled in the reaper above; re-attach to the+ // new input and `show()` its fresh tooltip.+ this._syncCaretTooltip(recreated);+ });++ // Keyboard-only navigation into an <li> doesn't move DOM focus (the editable+ // root keeps it), so Bootstrap's focus trigger never fires. Drive the tooltip+ // manually from the model selection so keyboard users see the hint too.+ this.listenTo(editor.model.document.selection, "change:range", () => {+ this._syncCaretTooltip();});// A new row split off with Enter inherits the previous row's `taskState` (writer.split@@ -206,9 +252,107 @@ export default class TodoListMultistateEditing extends Plugin {Tooltip.getInstance(input)?.dispose();}this._checkboxTooltips.clear();+ this._caretCheckbox = null;super.destroy();}+ /**+ * Attach hover triggers on a freshly-tracked checkbox. Both handlers no-op+ * when the caret is already inside the same todo item — the caret-driven+ * flow already owns the tooltip's visibility there, and letting `show()` /+ * `hide()` run again produced a rebuild-then-tear-down flicker.+ * `Tooltip.getInstance(input)` is resolved lazily so the same closures keep+ * working after a state-change refresh replaces the tooltip on the same+ * DOM element.+ */+ private _attachHoverListeners(input: HTMLInputElement): void {+ const isCaretInThisItem = () => {+ const itemId = input.closest<HTMLElement>("li")?.getAttribute("data-list-item-id");+ return !!itemId && this._isCaretInsideItem(itemId);+ };+ input.addEventListener("mouseenter", () => {+ if (isCaretInThisItem()) {+ return;+ }+ Tooltip.getInstance(input)?.show();+ });+ input.addEventListener("mouseleave", () => {+ if (isCaretInThisItem()) {+ return;+ }+ Tooltip.getInstance(input)?.hide();+ });+ }++ /**+ * Reconcile {@link _caretCheckbox} against the current model caret target and+ * drive the corresponding Bootstrap tooltip's visibility. Called from the+ * selection listener (target may change) AND from the render listener+ * (tooltips may have been (re)created for the current target).+ *+ * `recreated` is the set of inputs whose tooltip was just replaced this render;+ * when the caret target is in that set, `show()` is forced even if the target+ * itself didn't change, so a state-change render keeps the hint visible.+ */+ private _syncCaretTooltip(recreated?: ReadonlySet<HTMLInputElement>): void {+ const target = this._findCaretCheckbox();+ const targetChanged = this._caretCheckbox !== target;+ if (targetChanged && this._caretCheckbox) {+ Tooltip.getInstance(this._caretCheckbox)?.hide();+ }+ this._caretCheckbox = target;+ if (target && (targetChanged || recreated?.has(target))) {+ Tooltip.getInstance(target)?.show();+ }+ }++ /**+ * True when the caret's model position sits inside a list item whose+ * `listItemId` matches. Used by the mouseleave guard to keep the tooltip up+ * while the caret is in the hovered item, without depending on DOM identity.+ */+ private _isCaretInsideItem(itemId: string): boolean {+ const position = this.editor.model.document.selection.getFirstPosition();+ let candidate = position?.parent ?? null;+ while (candidate) {+ if (candidate.is("element") && candidate.getAttribute("listItemId") === itemId) {+ return true;+ }+ candidate = candidate.parent;+ }+ return false;+ }++ /**+ * The <input> DOM checkbox of the todo item the caret is currently inside, or+ * `null` when the caret isn't inside a todo item. Walks up the model to find+ * an ancestor block with `listType == "todo"`, then queries the editing DOM+ * for the matching <li data-list-item-id="…">.+ */+ private _findCaretCheckbox(): HTMLInputElement | null {+ const position = this.editor.model.document.selection.getFirstPosition();+ let node: ModelElement | null = null;+ let candidate = position?.parent ?? null;+ while (candidate) {+ if (candidate.is("element") && candidate.getAttribute("listType") === "todo") {+ node = candidate as ModelElement;+ break;+ }+ candidate = candidate.parent;+ }+ if (!node) {+ return null;+ }+ const itemId = node.getAttribute("listItemId");+ if (typeof itemId !== "string" || !itemId) {+ return null;+ }+ const domRoot = this.editor.editing.view.getDomRoot();+ return domRoot?.querySelector<HTMLInputElement>(+ `li[data-list-item-id="${CSS.escape(itemId)}"] .todo-list__label input[type="checkbox"]`+ ) ?? null;+ }+}type TranslateFn = (key: string, params?: Record<string, unknown>) => string;@@ -217,9 +361,16 @@ type TranslateFn = (key: string, params?: Record<string, unknown>) => string;* The task state applied to the todo item that owns the given checkbox. Anchor* states (`none`/`done`) never carry a `data-trilium-task-state`, so this* returns `null` for them.+ *+ * The lookup must be scoped to the *nearest* <li>, not the nearest <li> that+ * happens to carry the attribute — in nested todo lists the DOM is+ * `<li outer data-trilium-task-state="doing">…<ul><li inner>…</li></ul></li>`,+ * so a filtered `closest("li[data-trilium-task-state]")` on the inner+ * checkbox walks straight past its own (unattributed) <li> and lands on the+ * outer one, wrongly attributing the parent's state to the inner item.*/function readTaskState(input: HTMLInputElement): string | null {- const li = input.closest<HTMLElement>("li[data-trilium-task-state]");+ const li = input.closest<HTMLElement>("li");return li?.getAttribute("data-trilium-task-state") ?? null;}