Skip to content

Build your personal knowledge base with Trilium Notes

fix(ckeditor5): keep to-do checkboxes in collapsible blocks (closes #11544)

Elian Doranb630b26ca5ee
diff --git a/packages/ckeditor5/src/plugins/collapsible/collapsible_editing.ts b/packages/ckeditor5/src/plugins/collapsible/collapsible_editing.ts
index c34268f034..80d8868b7f 100644
--- a/packages/ckeditor5/src/plugins/collapsible/collapsible_editing.ts
+++ b/packages/ckeditor5/src/plugins/collapsible/collapsible_editing.ts
@@ -139,6 +139,7 @@ export default class CollapsibleEditing extends Plugin {
});
this.registerSchema();
this.registerConversion();
+ this.registerListItemRefresh();
this.registerBodyPlaceholder();
this.registerKeyHandlers();
this.registerMergeGuard();
@@ -404,6 +405,42 @@ export default class CollapsibleEditing extends Plugin {
return summary;
}
+ /**
+ * Rebuilds the body's list blocks whenever the collapsible is reconverted, instead of
+ * letting CKEditor reuse their existing view.
+ *
+ * The editing downcast is an `elementToStructure`, so a reconversion re-slots the
+ * surviving children by moving their mapped view element. A to-do item's checkbox is a
+ * UIElement sibling of that element inside <span class="todo-list__label">, so the move
+ * leaves it behind. CKEditor's own opt-out, `isItemBlockInsideStructureSlot()`, walks up
+ * through `ol`/`ul`/`li` only and stops at that label. No-op when the editor has no list
+ * plugin.
+ */
+ private registerListItemRefresh() {
+ if (!this.editor.plugins.has("ListEditing")) return;
+ const model = this.editor.model;
+ // Default priority puts this between ListEditing's "high" handler, whose own refreshes
+ // add insert/remove entries that reconvert a <details>, and the editing controller's
+ // "low" one, which is what then runs the conversion.
+ this.listenTo(model.document, "change:data", () => {
+ const reconverted = new Set<any>();
+ for (const entry of model.document.differ.getChanges()) {
+ // Only a changed child list reconverts the structure, never an attribute.
+ if (entry.type !== "insert" && entry.type !== "remove") continue;
+ if (entry.position.parent.is("element", "details")) {
+ reconverted.add(entry.position.parent);
+ }
+ }
+ for (const details of reconverted) {
+ for (const block of details.getChildren()) {
+ if (block.is("element") && block.hasAttribute("listItemId")) {
+ this.editor.editing.reconvertItem(block);
+ }
+ }
+ }
+ });
+ }
+
// -----------------------------------------------------------------
// DOM/model bridge helpers
// -----------------------------------------------------------------
diff --git a/packages/ckeditor5/src/plugins/collapsible/structure_and_conversion.spec.ts b/packages/ckeditor5/src/plugins/collapsible/structure_and_conversion.spec.ts
index 9789d19de7..094841f487 100644
--- a/packages/ckeditor5/src/plugins/collapsible/structure_and_conversion.spec.ts
+++ b/packages/ckeditor5/src/plugins/collapsible/structure_and_conversion.spec.ts
@@ -1,4 +1,4 @@
-import { _getModelData as getModelData, _setModelData as setModelData, ClassicEditor, Essentials, Paragraph } from "ckeditor5";
+import { _getModelData as getModelData, _setModelData as setModelData, ClassicEditor, Essentials, Heading, Paragraph, TodoList } from "ckeditor5";
import { beforeEach, describe, expect, it } from "vitest";
import { createTestEditor, getEditorElement } from "../../../test/editor-kit.js";
@@ -45,6 +45,117 @@ describe("collapsible structure and conversion", () => {
});
});
+ describe("to-do list in the body", () => {
+ let todoEditor: ClassicEditor;
+
+ /** One entry per rendered `<li>`: whether it still carries its checkbox. */
+ function checkboxes(): boolean[] {
+ todoEditor.editing.view.forceRender();
+ const items = todoEditor.editing.view.getDomRoot()?.querySelectorAll("li") ?? [];
+ return [...items].map((item) => !!item.querySelector("input[type=checkbox]"));
+ }
+
+ beforeEach(async () => {
+ todoEditor = await createTestEditor([Essentials, Paragraph, Heading, TodoList, CollapsibleEditing]);
+ });
+
+ it("keeps every checkbox as body items are added and indented", () => {
+ setModelData(
+ todoEditor.model,
+ "<details open=\"true\"><summary>T</summary>"
+ + "<paragraph listIndent=\"0\" listItemId=\"a\" listType=\"todo\">one[]</paragraph>"
+ + "</details>"
+ );
+
+ for (const label of ["two", "three", "four"]) {
+ todoEditor.execute("enter");
+ todoEditor.execute("insertText", { text: label });
+ }
+ todoEditor.execute("indentList");
+
+ expect(checkboxes()).toEqual([true, true, true, true]);
+ });
+
+ it("keeps every checkbox when an item is split at its first character", () => {
+ setModelData(
+ todoEditor.model,
+ "<details open=\"true\"><summary>T</summary>"
+ + "<paragraph listIndent=\"0\" listItemId=\"a\" listType=\"todo\">one</paragraph>"
+ + "<paragraph listIndent=\"0\" listItemId=\"b\" listType=\"todo\">[]two</paragraph>"
+ + "</details>"
+ );
+
+ // Splitting at offset 0 leaves the item's text behind an empty new item, so the
+ // reconversion has to map a position into a block whose view it just replaced.
+ todoEditor.execute("enter");
+
+ expect(checkboxes()).toEqual([true, true, true]);
+ });
+
+ it("keeps every checkbox when a neighbouring block merges into the list", () => {
+ setModelData(
+ todoEditor.model,
+ "<details open=\"true\"><summary>T</summary>"
+ + "<paragraph listIndent=\"0\" listItemId=\"a\" listType=\"todo\">one</paragraph>"
+ + "<paragraph>[]tail</paragraph><paragraph>keep</paragraph>"
+ + "</details>"
+ );
+ todoEditor.editing.view.forceRender();
+
+ todoEditor.execute("delete");
+
+ expect(checkboxes()).toEqual([true]);
+ });
+
+ it("keeps every checkbox as blocks around the list change", () => {
+ setModelData(
+ todoEditor.model,
+ "<details open=\"true\"><summary>T</summary>"
+ + "<paragraph listIndent=\"0\" listItemId=\"a\" listType=\"todo\">one</paragraph>"
+ + "<paragraph listIndent=\"0\" listItemId=\"b\" listType=\"todo\">two[]</paragraph>"
+ + "</details>"
+ );
+
+ // Leave the list, then build a heading and two paragraphs under it. None of these
+ // changes mentions the list, so nothing but the reconversion itself points at it.
+ todoEditor.execute("enter");
+ todoEditor.execute("todoList");
+ todoEditor.execute("heading", { value: "heading2" });
+ todoEditor.execute("insertText", { text: "Section" });
+ for (const label of ["first", "second"]) {
+ todoEditor.execute("enter");
+ todoEditor.execute("insertText", { text: label });
+ }
+ expect(checkboxes()).toEqual([true, true]);
+
+ todoEditor.model.change((writer) => {
+ const details = todoEditor.model.document.getRoot()?.getChild(0);
+ const last = details?.is("element") ? details.getChild(details.childCount - 1) : null;
+ if (last) {
+ writer.remove(last);
+ }
+ });
+ expect(checkboxes()).toEqual([true, true]);
+
+ // Putting that block back reconverts the collapsible once more.
+ todoEditor.execute("undo");
+ expect(checkboxes()).toEqual([true, true]);
+ });
+
+ it("leaves a to-do list outside any collapsible alone", () => {
+ setModelData(
+ todoEditor.model,
+ "<paragraph listIndent=\"0\" listItemId=\"a\" listType=\"todo\">one[]</paragraph>"
+ + "<details><summary>T</summary><paragraph>body</paragraph></details>"
+ );
+
+ todoEditor.execute("enter");
+ todoEditor.execute("insertText", { text: "two" });
+
+ expect(checkboxes()).toEqual([true, true]);
+ });
+ });
+
describe("structural post-fixer", () => {
it("removes a <summary> inserted outside any collapsible", () => {
setModelData(editor.model, "<paragraph>foo[]</paragraph>");