Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/multistate checkboxes: prevent moving away and then restoring the editor focus

Adorian Doran6689cd5be42a
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.ts
index a62d66a6db..ee95419e6c 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
@@ -2,6 +2,7 @@ import { type TaskStateDef } from "@triliumnext/commons";
import { Tooltip } from "bootstrap";
import { _setModelData as setModelData, ClassicEditor, Essentials, HorizontalLine, keyCodes, List, Paragraph, TodoList, type ModelElement } from "ckeditor5";
import { beforeEach, describe, expect, it, vi } from "vitest";
+import { userEvent } from "vitest/browser";
import { createTestEditor } from "../../../test/editor-kit.js";
import TodoListUncheckOnEnter from "../todo_list_uncheck_on_enter.js";
@@ -944,3 +945,45 @@ describe("toggling a checkbox of an editor without a selection of its own", () =
expect(position?.offset).toBe(2);
});
});
+
+describe("clicking a checkbox", () => {
+ it("toggles the item and leaves the focus and the caret in the editable", async () => {
+ const editor = await createEditor();
+ setModelData(editor.model,
+ '<paragraph listIndent="0" listItemId="t-a" listType="todo">Ta[]sk</paragraph>'
+ + '<paragraph listIndent="0" listItemId="t-b" listType="todo">Other</paragraph>');
+ editor.editing.view.focus();
+ const root = editor.editing.view.getDomRoot();
+ const checkboxes = () =>
+ root?.querySelectorAll<HTMLInputElement>('.todo-list__label input[type="checkbox"]');
+ const input = checkboxes()?.[1];
+ expect(root).toBeDefined();
+ expect(input).toBeDefined();
+ if (!root || !input) return;
+ expect(document.activeElement).toBe(root);
+ const focused: EventTarget[] = [];
+ root.addEventListener("focusin", (event) => focused.push(event.target ?? root));
+
+ await userEvent.click(input);
+ expect(getBlock(editor, 1).getAttribute(TODO_LIST_CHECKED_ATTRIBUTE)).toBe(true);
+ expect(focused).toEqual([]);
+ expect(document.activeElement).toBe(root);
+ const position = editor.model.document.selection.getFirstPosition();
+ expect(position?.parent).toBe(getBlock(editor, 0));
+ expect(position?.offset).toBe(2);
+
+ // Only a left-button press on a checkbox of a focused editor keeps the focus.
+ const rightPress = new MouseEvent("mousedown", { button: 2, bubbles: true, cancelable: true });
+ checkboxes()?.[0]?.dispatchEvent(rightPress);
+ const textPress = new MouseEvent("mousedown", { button: 0, bubbles: true, cancelable: true });
+ root.querySelector(".todo-list__label__description")?.dispatchEvent(textPress);
+ root.blur();
+ expect(editor.editing.view.document.isFocused).toBe(false);
+ const blurredPress = new MouseEvent("mousedown",
+ { button: 0, bubbles: true, cancelable: true });
+ checkboxes()?.[0]?.dispatchEvent(blurredPress);
+ expect(rightPress.defaultPrevented).toBe(false);
+ expect(textPress.defaultPrevented).toBe(false);
+ expect(blurredPress.defaultPrevented).toBe(false);
+ });
+});
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.ts
index 0ee8b2b3db..4225051dd7 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
@@ -1,5 +1,5 @@
import { DEFAULT_TASK_STATES, DONE_STATE_NAME, isAnchorState, NONE_STATE_NAME, type TaskStateDef } from "@triliumnext/commons";
-import { Command, ListEditing, Plugin, TodoList, type Editor, type EventInfo, type ModelElement, type UpcastConversionApi, type UpcastConversionData, type UpcastElementEvent, type ViewElement } from "ckeditor5";
+import { Command, ListEditing, MouseObserver, Plugin, TodoList, type Editor, type EventInfo, type ModelElement, type UpcastConversionApi, type UpcastConversionData, type UpcastElementEvent, type ViewDocumentMouseDownEvent, type ViewElement } from "ckeditor5";
import { onTodoRowSplit } from "../todo_list_uncheck_on_enter.js";
import { ContentHintManager, type HintHandle } from "../../content_hint_manager.js";
@@ -121,6 +121,19 @@ export default class TodoListMultistateEditing extends Plugin {
evt.stop();
}, { priority: "high" });
+ // A left-button press on a checkbox keeps the focus in a focused editable.
+ // The click still toggles the checkbox.
+ editor.editing.view.addObserver(MouseObserver);
+ const viewDocument = editor.editing.view.document;
+ this.listenTo<ViewDocumentMouseDownEvent>(viewDocument, "mousedown", (_evt, data) => {
+ const target = data.target;
+ if (viewDocument.isFocused && data.domEvent.button === 0
+ && target.is("element", "input") && target.getAttribute("type") === "checkbox"
+ && target.findAncestor({ classes: "todo-list__label" })) {
+ data.preventDefault();
+ }
+ });
+
editor.keystrokes.set(STATE_CYCLE_SHORTCUT, (_data, cancel) => {
const command = editor.commands.get("setTaskState");
if (!command?.isEnabled) {