Skip to content

Build your personal knowledge base with Trilium Notes

fix(ckeditor): new to-do row no longer inherits the checked state on Enter

Elian Doran4d18e296f283
diff --git a/packages/ckeditor5/src/plugins.ts b/packages/ckeditor5/src/plugins.ts
index 3084b9ff1f..f7873782b2 100644
--- a/packages/ckeditor5/src/plugins.ts
+++ b/packages/ckeditor5/src/plugins.ts
@@ -44,6 +44,7 @@ import IncludeNoteBoxSizeDropdown from "./plugins/include_note_box_size_dropdown
import IncludeNoteToolbar from "./plugins/include_note_toolbar.js";
import LinkEmbedToolbar from "./plugins/link_embed_toolbar.js";
import TodoListMultistate from "./plugins/todo_list_multistate/todo_list_multistate.js";
+import TodoListUncheckOnEnter from "./plugins/todo_list_uncheck_on_enter.js";
import CollapsibleListItems from "./plugins/collapsible_list_items.js";
/**
@@ -150,6 +151,7 @@ export const COMMON_PLUGINS: typeof Plugin[] = [
List,
ListProperties,
TodoList,
+ TodoListUncheckOnEnter,
PasteFromOffice,
PictureEditing,
Table,
diff --git a/packages/ckeditor5/src/plugins/todo_list_uncheck_on_enter.spec.ts b/packages/ckeditor5/src/plugins/todo_list_uncheck_on_enter.spec.ts
new file mode 100644
index 0000000000..53dada82c6
--- /dev/null
+++ b/packages/ckeditor5/src/plugins/todo_list_uncheck_on_enter.spec.ts
@@ -0,0 +1,55 @@
+import { _getModelData as getModelData, _setModelData as setModelData, ClassicEditor, Essentials, List, Paragraph, Plugin, TodoList, type ModelElement } from "ckeditor5";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+import { createTestEditor } from "../../test/editor-kit.js";
+import TodoListUncheckOnEnter, { onTodoRowSplit } from "./todo_list_uncheck_on_enter.js";
+
+const TODO_LIST_CHECKED_ATTRIBUTE = "todoListChecked";
+
+function getBlock(editor: ClassicEditor, index: number): ModelElement {
+ const child = editor.model.document.getRoot()?.getChild(index);
+ if (!child || !child.is("element")) {
+ throw new Error(`No element block at index ${index}.`);
+ }
+ return child;
+}
+
+describe("TodoListUncheckOnEnter", () => {
+ let editor: ClassicEditor;
+
+ beforeEach(async () => {
+ editor = await createTestEditor([Essentials, Paragraph, List, TodoList, TodoListUncheckOnEnter]);
+ });
+
+ it("loads the plugin and requires TodoList", () => {
+ expect(editor.plugins.get(TodoListUncheckOnEnter)).toBeInstanceOf(TodoListUncheckOnEnter);
+ expect(TodoListUncheckOnEnter.requires).toContain(TodoList);
+ });
+
+ it("starts a new todo row unchecked when splitting a checked row (#10084)", () => {
+ setModelData(editor.model,
+ '<paragraph listIndent="0" listItemId="t-a" listType="todo" todoListChecked="true">Task[]</paragraph>');
+ editor.execute("enter");
+
+ // The original row keeps its checked state; the new row is unchecked.
+ expect(getBlock(editor, 0).getAttribute(TODO_LIST_CHECKED_ATTRIBUTE)).toBe(true);
+ const newRow = getBlock(editor, 1);
+ expect(newRow.getAttribute("listType")).toBe("todo");
+ expect(newRow.hasAttribute(TODO_LIST_CHECKED_ATTRIBUTE)).toBe(false);
+ });
+
+ it("leaves a non-todo block untouched on Enter", () => {
+ setModelData(editor.model, "<paragraph>Hello[]</paragraph>");
+ editor.execute("enter");
+ expect(getBlock(editor, 1).getAttribute("listType")).toBe(undefined);
+ expect(getModelData(editor.model)).toContain("<paragraph>[]</paragraph>");
+ });
+
+ it("onTodoRowSplit is a no-op when the Enter command is absent", () => {
+ // TodoList always pulls in the Enter feature, so simulate its absence with a stub plugin.
+ const callback = vi.fn();
+ const stub = { editor: { commands: { get: () => undefined } } } as unknown as Plugin;
+ expect(() => onTodoRowSplit(stub, callback)).not.toThrow();
+ expect(callback).not.toHaveBeenCalled();
+ });
+});
diff --git a/packages/ckeditor5/src/plugins/todo_list_uncheck_on_enter.ts b/packages/ckeditor5/src/plugins/todo_list_uncheck_on_enter.ts
new file mode 100644
index 0000000000..3713b2256c
--- /dev/null
+++ b/packages/ckeditor5/src/plugins/todo_list_uncheck_on_enter.ts
@@ -0,0 +1,50 @@
+import { Plugin, TodoList, type ModelElement, type ModelWriter } from "ckeditor5";
+
+const TODO_LIST_CHECKED_ATTRIBUTE = "todoListChecked";
+
+/**
+ * Makes a new to-do item created with <kbd>Enter</kbd> start unchecked.
+ *
+ * Pressing Enter splits the current block, and CKEditor's `writer.split()` copies every block
+ * attribute — including `todoListChecked` — onto the new row, so a new task inherits the checked
+ * state of the row above it (TriliumNext/Trilium#10084). This is stock CKEditor behavior, reported
+ * upstream as ckeditor/ckeditor5#5620 (a confirmed `type:bug`, but closed unfixed as stale in
+ * 2023), and is unrelated to custom task states — hence a standalone plugin alongside `TodoList`.
+ * The custom-state counterpart (clearing `taskState`) lives in `TodoListMultistateEditing` and
+ * reuses the {@link onTodoRowSplit} seam below.
+ */
+export default class TodoListUncheckOnEnter extends Plugin {
+
+ static get requires() {
+ return [TodoList] as const;
+ }
+
+ init() {
+ onTodoRowSplit(this, (writer, block) => {
+ writer.removeAttribute(TODO_LIST_CHECKED_ATTRIBUTE, block);
+ });
+ }
+
+}
+
+/**
+ * Runs `callback` for the to-do row freshly created by an <kbd>Enter</kbd> split, so callers can
+ * reset state the split copied over from the previous row. No-op when the Enter feature is absent.
+ */
+export function onTodoRowSplit(plugin: Plugin, callback: (writer: ModelWriter, block: ModelElement) => void): void {
+ const editor = plugin.editor;
+ const enterCommand = editor.commands.get("enter");
+ if (!enterCommand) {
+ return;
+ }
+
+ plugin.listenTo(enterCommand, "afterExecute", (_evt, data: { writer: ModelWriter }) => {
+ // After a plain Enter the selection collapses into the new row, so this iterates exactly
+ // that block; `getSelectedBlocks()` only yields elements, so no extra narrowing is needed.
+ for (const block of editor.model.document.selection.getSelectedBlocks()) {
+ if (block.getAttribute("listType") === "todo") {
+ callback(data.writer, block);
+ }
+ }
+ });
+}