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
feat(text): toggle list-item collapse with Ctrl+Enter
Elian Dorana0141a47a0d0
diff --git a/packages/ckeditor5/src/plugins/collapsible_list_items.ts b/packages/ckeditor5/src/plugins/collapsible_list_items.tsindex 32f1c0aa89..aec8c9b875 100644--- a/packages/ckeditor5/src/plugins/collapsible_list_items.ts+++ b/packages/ckeditor5/src/plugins/collapsible_list_items.ts@@ -1,6 +1,6 @@import "../theme/collapsible_list_items.css";-import { Command, ListEditing, MouseObserver, Plugin, type Editor, type ModelElement, type ModelNode, type ModelWriter, type ViewDocumentMouseDownEvent, type ViewElement } from "ckeditor5";+import { Command, getCode, ListEditing, MouseObserver, parseKeystroke, Plugin, type Editor, type ModelElement, type ModelNode, type ModelWriter, type ViewDocumentKeyDownEvent, type ViewDocumentMouseDownEvent, type ViewElement } from "ckeditor5";export const LIST_COLLAPSED_ATTRIBUTE = "listCollapsed";@@ -47,9 +47,37 @@ export default class CollapsibleListItems extends Plugin {});this._enableToggleOnGutterClick();+ this._enableToggleOnKeystroke();this._enableAutoExpand();}+ /**+ * Toggles the collapsed state on Ctrl+Enter. To-do items keep the native Ctrl+Enter+ * (check the item), so the handler bails on them; it runs at the highest priority because+ * the native to-do binding stops the event unconditionally on every Ctrl+Enter.+ */+ private _enableToggleOnKeystroke() {+ const editor = this.editor;+ const toggleKeystroke = parseKeystroke("Ctrl+Enter");++ this.listenTo<ViewDocumentKeyDownEvent>(editor.editing.view.document, "keydown", (evt, data) => {+ if (getCode(data) !== toggleKeystroke) {+ return;+ }+ const command = editor.commands.get("toggleListCollapse");+ if (!command?.isEnabled) {+ return;+ }+ const block = getSelectedListBlock(editor);+ if (block?.getAttribute("listType") === "todo") {+ return;+ }+ editor.execute("toggleListCollapse");+ data.preventDefault();+ evt.stop();+ }, {priority: "highest"});+ }+/*** Toggles the collapsed state when the arrow rendered in the gutter next to a list item is* clicked. The arrow is the item's `::before` pseudo-element placed outside its border box,diff --git a/packages/ckeditor5/tests/collapsible_list_items.spec.ts b/packages/ckeditor5/tests/collapsible_list_items.spec.tsindex ef6d9fc5b1..0ef90f9f90 100644--- a/packages/ckeditor5/tests/collapsible_list_items.spec.ts+++ b/packages/ckeditor5/tests/collapsible_list_items.spec.ts@@ -1,4 +1,4 @@-import { _setModelData as setModelData, ClassicEditor, List, Paragraph, Typing, Undo, type ModelElement } from "ckeditor5";+import { _setModelData as setModelData, ClassicEditor, keyCodes, List, Paragraph, TodoList, Typing, Undo, type ModelElement } from "ckeditor5";import { afterEach, beforeEach, describe, expect, it } from "vitest";import CollapsibleListItems, { LIST_COLLAPSED_ATTRIBUTE } from "../src/plugins/collapsible_list_items.js";@@ -20,7 +20,7 @@ describe("CollapsibleListItems", () => {document.body.appendChild(editorElement);editor = await ClassicEditor.create(editorElement, {- plugins: [CollapsibleListItems, List, Paragraph, Typing, Undo],+ plugins: [CollapsibleListItems, List, TodoList, Paragraph, Typing, Undo],licenseKey: "GPL"});@@ -185,6 +185,30 @@ describe("CollapsibleListItems", () => {mouseDownAt(leafItem, -10); // leaf items have nothing to collapseexpect(getBlock(editor, 1).hasAttribute(LIST_COLLAPSED_ATTRIBUTE)).toBe(false);});++ it("toggles via Ctrl+Enter on a collapsible item and ignores leaves", () => {+ pressCtrlEnter(editor); // selection starts in the parent+ expect(getBlock(editor, 0).getAttribute(LIST_COLLAPSED_ATTRIBUTE)).toBe(true);++ pressCtrlEnter(editor);+ expect(getBlock(editor, 0).hasAttribute(LIST_COLLAPSED_ATTRIBUTE)).toBe(false);++ setSelectionIn(editor, 1); // leaf child: nothing to collapse+ pressCtrlEnter(editor);+ expect(getBlock(editor, 1).hasAttribute(LIST_COLLAPSED_ATTRIBUTE)).toBe(false);+ });++ it("leaves Ctrl+Enter to the native to-do check on to-do items", () => {+ setModelData(editor.model,+ '<paragraph listIndent="0" listItemId="t-a" listType="todo">Parent[]</paragraph>' ++ '<paragraph listIndent="1" listItemId="t-b" listType="todo">Child</paragraph>');++ pressCtrlEnter(editor);++ // The collapse toggle must not fire; the native checkTodoList command handles it.+ expect(getBlock(editor, 0).hasAttribute(LIST_COLLAPSED_ATTRIBUTE)).toBe(false);+ expect(getBlock(editor, 0).getAttribute("todoListChecked")).toBe(true);+ });});function getBlock(editor: ClassicEditor, index: number): ModelElement {@@ -201,6 +225,14 @@ function setSelectionIn(editor: ClassicEditor, blockIndex: number): void {});}+function pressCtrlEnter(editor: ClassicEditor): void {+ editor.editing.view.document.fire("keydown", {+ keyCode: keyCodes.enter,+ ctrlKey: true,+ preventDefault: () => {}+ });+}+function mouseDownAt(item: HTMLElement, offsetX: number): void {const rect = item.getBoundingClientRect();item.dispatchEvent(new MouseEvent("mousedown", {