Skip to content

Build your personal knowledge base with Trilium Notes

fix(text): keep a checkbox toggle off the note's first image

Elian Doran498bc09cfb5b
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 0b45d4048e..a62d66a6db 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
@@ -1,6 +1,6 @@
import { type TaskStateDef } from "@triliumnext/commons";
import { Tooltip } from "bootstrap";
-import { _setModelData as setModelData, ClassicEditor, Essentials, keyCodes, List, Paragraph, TodoList, type ModelElement } from "ckeditor5";
+import { _setModelData as setModelData, ClassicEditor, Essentials, HorizontalLine, keyCodes, List, Paragraph, TodoList, type ModelElement } from "ckeditor5";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createTestEditor } from "../../../test/editor-kit.js";
@@ -901,3 +901,46 @@ describe("TodoListMultistateEditing", () => {
});
});
});
+
+describe("toggling a checkbox of an editor without a selection of its own", () => {
+ async function createEditorWithObjectFirst(): Promise<ClassicEditor> {
+ const editor = await createTestEditor([Essentials, Paragraph, HorizontalLine, List, TodoList, TodoListMultistateEditing]);
+ setModelData(editor.model, "<horizontalLine></horizontalLine>"
+ + '<paragraph listIndent="0" listItemId="t-a" listType="todo">Task</paragraph>'
+ + '<paragraph listIndent="0" listItemId="t-b" listType="todo">Other</paragraph>');
+ return editor;
+ }
+
+ function toggle(editor: ClassicEditor, index: number) {
+ const inputs = editor.editing.view.getDomRoot()?.querySelectorAll<HTMLInputElement>('.todo-list__label input[type="checkbox"]');
+ const input = inputs?.[index];
+ expect(input).toBeDefined();
+ if (!input) return;
+ input.checked = !input.checked;
+ input.dispatchEvent(new Event("change", { bubbles: true }));
+ }
+
+ it("leaves the selection in the toggled item rather than on the object ahead of the list", async () => {
+ const editor = await createEditorWithObjectFirst();
+ editor.model.change((writer) => writer.setSelection(null));
+ const selection = editor.model.document.selection;
+ expect(selection.hasOwnRange).toBe(false);
+ expect(selection.getSelectedElement()?.name).toBe("horizontalLine");
+
+ toggle(editor, 1);
+ expect(getBlock(editor, 2).getAttribute(TODO_LIST_CHECKED_ATTRIBUTE)).toBe(true);
+ expect(selection.getSelectedElement()).toBeNull();
+ expect(selection.getFirstPosition()?.parent).toBe(getBlock(editor, 2));
+ });
+
+ it("restores a selection of its own, as upstream does", async () => {
+ const editor = await createEditorWithObjectFirst();
+ editor.model.change((writer) => writer.setSelection(getBlock(editor, 1), 2));
+
+ toggle(editor, 1);
+ expect(getBlock(editor, 2).getAttribute(TODO_LIST_CHECKED_ATTRIBUTE)).toBe(true);
+ const position = editor.model.document.selection.getFirstPosition();
+ expect(position?.parent).toBe(getBlock(editor, 1));
+ expect(position?.offset).toBe(2);
+ });
+});
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 8a32af2942..0ee8b2b3db 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
@@ -106,6 +106,21 @@ export default class TodoListMultistateEditing extends Plugin {
editor.commands.add("setTaskState", new SetTaskStateCommand(editor));
+ // Upstream's handler restores the selection it found, which turns the default selection of
+ // an editor that never had the focus into a real one, on an image at the start of the note.
+ this.listenTo(editor.editing.view.document, "todoCheckboxChange", (evt, data: { target: ViewElement }) => {
+ if (editor.model.document.selection.hasOwnRange) {
+ return;
+ }
+ const { mapper, view } = editor.editing;
+ const block = mapper.toModelPosition(view.createPositionAfter(data.target)).parent;
+ editor.model.change((writer) => {
+ writer.setSelection(writer.createPositionAt(block, "end"));
+ editor.execute("checkTodoList");
+ });
+ evt.stop();
+ }, { priority: "high" });
+
editor.keystrokes.set(STATE_CYCLE_SHORTCUT, (_data, cancel) => {
const command = editor.commands.get("setTaskState");
if (!command?.isEnabled) {