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
fix #10973
Adorian Doranc8fac19263f6
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 292e1e5e66..7596e549d3 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@@ -32,6 +32,17 @@ function getBlock(editor: ClassicEditor, index: number): ModelElement {return child;}+function todoList(...items: string[]): string {+ return `<ul class="todo-list">${items.join("")}</ul>`;+}++/** A saved todo item, in the shape the data downcast produces. */+function todoItem(text: string, state?: string, nested?: string): string {+ const stateAttribute = state ? ` data-trilium-task-state="${state}"` : "";+ return `<li${stateAttribute}><label class="todo-list__label"><input type="checkbox">`+ + `<span class="todo-list__label__description">${text}</span></label>${nested ?? ""}</li>`;+}+function pressCtrlShiftEnter(editor: ClassicEditor): void {editor.keystrokes.press({keyCode: keyCodes.enter,@@ -171,6 +182,23 @@ describe("TodoListMultistateEditing", () => {expect(getBlock(editor, 0).hasAttribute(TASK_STATE_ATTRIBUTE)).toBe(false);});+ it("upcasts a state onto its own item only, never onto nested or sibling items", () => {+ // The list model is flat, so a nested item is a sibling block covered by the+ // parent <li>'s converted range. The state must not spread across it.+ editor.setData(todoList(+ todoItem("Parent", "doing", todoList(todoItem("Child"), todoItem("Sibling", "review"))),+ todoItem("After")));++ expect(getBlock(editor, 0).getAttribute(TASK_STATE_ATTRIBUTE)).toBe("doing");+ expect(getBlock(editor, 1).hasAttribute(TASK_STATE_ATTRIBUTE)).toBe(false);+ expect(getBlock(editor, 2).getAttribute(TASK_STATE_ATTRIBUTE)).toBe("review");+ expect(getBlock(editor, 3).hasAttribute(TASK_STATE_ATTRIBUTE)).toBe(false);++ // A reload therefore round-trips the content unchanged, rather than persisting+ // the parent's state onto its children.+ expect(editor.getData().match(/data-trilium-task-state/g)).toHaveLength(2);+ });+it("cycles through active (non-hidden) states with Ctrl+Shift+Enter", () => {const command = editor.commands.get("setTaskState");// The active cycle is the configured non-hidden custom states: [doing, review].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 6ab2ad8e01..be39fa0c40 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 ModelElement, type ViewElement } from "ckeditor5";+import { Command, ListEditing, Plugin, TodoList, type Editor, type EventInfo, type ModelElement, type UpcastConversionApi, type UpcastConversionData, type UpcastElementEvent, type ViewElement } from "ckeditor5";import { onTodoRowSplit } from "../todo_list_uncheck_on_enter.js";import { ContentHintManager, type HintHandle } from "../../content_hint_manager.js";@@ -161,17 +161,12 @@ export default class TodoListMultistateEditing extends Plugin {}});- editor.conversion.for("upcast").attributeToAttribute({- view: {key: "data-trilium-task-state"},- model: {- key: TASK_STATE_ATTRIBUTE,- value: (viewElement: ViewElement) => {- const value = viewElement.getAttribute("data-trilium-task-state");- return typeof value === "string" && value !== "" && !isAnchorState(value)- ? value- : null;- }- }+ // The state is stored on the `<li>` but upcast from that item's checkbox; see+ // {@link upcastTaskState} for why the obvious `attributeToAttribute` on the `<li>`+ // is wrong. Registered at "low" so upstream's `todoItemInputConverter` has already+ // marked the block as a todo item.+ editor.conversion.for("upcast").add((dispatcher) => {+ dispatcher.on<UpcastElementEvent>("element:input", upcastTaskState, {priority: "low"});});if (hintsEnabled) {@@ -475,6 +470,54 @@ export default class TodoListMultistateEditing extends Plugin {}+/**+ * Upcast `data-trilium-task-state` onto the model block of the todo item that owns the+ * converted checkbox.+ *+ * Anchoring on the checkbox rather than declaring `attributeToAttribute` on the `<li>` is+ * what keeps a state on its own item. That helper applies the model attribute to every+ * top-level node of the `<li>`'s converted range, and the list model is *flat*: a nested+ * item is a sibling block, not a descendant. A parent's range therefore covers its whole+ * subtree, so every nested item without a state of its own inherited the parent's, and+ * because that is a real model attribute, the item-scoped downcast wrote it straight back+ * into the saved content. `modelCursor.parent` at the checkbox is, by construction, the+ * one block that item produced; upstream upcasts `todoListChecked` the same way.+ */+function upcastTaskState(+ _evt: EventInfo,+ data: UpcastConversionData<ViewElement>,+ conversionApi: UpcastConversionApi+): void {+ const block = data.modelCursor.parent;+ // `todoItemInputConverter` runs first and is what marks the block as a todo item.+ if (!block.is("element") || block.getAttribute("listType") !== "todo") {+ return;+ }+ const listItem = findListItemAncestor(data.viewItem);+ if (!listItem) {+ return;+ }+ const value = listItem.getAttribute("data-trilium-task-state");+ if (typeof value !== "string" || value === "" || isAnchorState(value)) {+ return;+ }+ // Consumed so General HTML Support doesn't also carry it as a raw `<li>` attribute.+ conversionApi.consumable.consume(listItem, {attributes: "data-trilium-task-state"});+ conversionApi.writer.setAttribute(TASK_STATE_ATTRIBUTE, value, block);+}++/** Nearest `<li>` ancestor in the view, mirroring {@link readTaskState}'s DOM walk. */+function findListItemAncestor(viewElement: ViewElement): ViewElement | null {+ let ancestor = viewElement.parent;+ while (ancestor && ancestor.is("element")) {+ if (ancestor.is("element", "li")) {+ return ancestor;+ }+ ancestor = ancestor.parent;+ }+ return null;+}+/*** 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