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
client/text editor/collapsible lists: tweak arrows
Adorian Doran75928dedd742
diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.cssindex dac1272d33..027ddd8c22 100644--- a/apps/client/src/stylesheets/style.css+++ b/apps/client/src/stylesheets/style.css@@ -2905,6 +2905,17 @@ iframe.print-iframe {tab-size: var(--code-block-tab-width, 4);}+/*+ * COLLAPSIBLE LIST ITEMS+ */++:where(:root) {+ --text-editor-collapsible-list-arrow-color: var(--muted-text-color);+ /* Neutral by design: a translucent grey reads on both light and dark backgrounds. */+ --text-editor-collapsible-list-arrow-hover-background: rgba(128, 128, 128, 0.25);+ --text-editor-collapsible-list-arrow-hover-color: var(--main-text-color);+}+/** TASK STATES*/diff --git a/packages/ckeditor5/src/plugins/collapsible_list_items.spec.ts b/packages/ckeditor5/src/plugins/collapsible_list_items.spec.tsindex 7d5c704fd8..6309e0080b 100644--- a/packages/ckeditor5/src/plugins/collapsible_list_items.spec.ts+++ b/packages/ckeditor5/src/plugins/collapsible_list_items.spec.ts@@ -193,11 +193,7 @@ describe("CollapsibleListItems", () => {return;}- const rect = parentLi.getBoundingClientRect();- const fontSize = parseFloat(getComputedStyle(parentLi).fontSize);- // CSS: inset-inline-start: -2.1em; width/height: 0.4em; top: 0.45em → aim at its centre.- const x = Math.round(rect.left - 1.9 * fontSize);- const y = Math.round(rect.top + 0.65 * fontSize);+ const { x, y } = arrowCentre(parentLi);// Click whatever element actually sits at the arrow's coordinates — the real target,// not a forced <li> — so a mismatch between where the arrow paints and what the@@ -208,6 +204,49 @@ describe("CollapsibleListItems", () => {expect(getBlock(editor, 0).getAttribute(LIST_COLLAPSED_ATTRIBUTE)).toBe(true);});+ it("fills the arrow into a circle while the pointer is on it", async () => {+ const editorElement = getEditorElement(editor);+ editorElement.style.marginLeft = "160px";+ editorElement.style.marginTop = "60px";++ const parentLi = editor.editing.view.getDomRoot()?.querySelector("li");+ expect(parentLi).toBeTruthy();+ if (!parentLi) {+ return;+ }++ const arrow = arrowCentre(parentLi);+ const rect = parentLi.getBoundingClientRect();++ mouseMoveAt(arrow.x, arrow.y);+ expect(parentLi.classList.contains("trilium-list-arrow-hovered")).toBe(true);+ // The arrow is a boxicons glyph, not a hand-drawn shape.+ expect(getComputedStyle(parentLi, "::before").fontFamily).toBe("boxicons");+ // Polled: the circle fades in, so its colour only settles once the transition ends.+ await expect.poll(() => getComputedStyle(parentLi, "::before").backgroundColor)+ .toBe("rgba(128, 128, 128, 0.25)");++ // Over the item's own text the pointer is no longer on the arrow.+ mouseMoveAt(Math.round(rect.left + 5), arrow.y);+ expect(parentLi.classList.contains("trilium-list-arrow-hovered")).toBe(false);+ await expect.poll(() => getComputedStyle(parentLi, "::before").backgroundColor)+ .toBe("rgba(0, 0, 0, 0)");++ // A held button is a drag (selecting text), not a hover.+ mouseMoveAt(arrow.x, arrow.y, 1);+ expect(parentLi.classList.contains("trilium-list-arrow-hovered")).toBe(false);++ // Leaving the editable ends the mouse moves, so the arrow is unlit on the way out —+ // but moving on to another element inside it is left to the mousemove handler.+ mouseMoveAt(arrow.x, arrow.y);+ const otherItem = editor.editing.view.getDomRoot()?.querySelectorAll("li")[1];+ parentLi.dispatchEvent(new MouseEvent("mouseout", { bubbles: true, relatedTarget: otherItem }));+ expect(parentLi.classList.contains("trilium-list-arrow-hovered")).toBe(true);++ parentLi.dispatchEvent(new MouseEvent("mouseout", { bubbles: true, relatedTarget: document.body }));+ expect(parentLi.classList.contains("trilium-list-arrow-hovered")).toBe(false);+ });+it("toggles a to-do list item when its gutter arrow is clicked", () => {// To-do <li>s wrap content in <span class="todo-list__label">, which shifts the// view->model mapping; regression guard for that resolution (the click works for@@ -223,10 +262,7 @@ describe("CollapsibleListItems", () => {return;}- const rect = parentLi.getBoundingClientRect();- const fontSize = parseFloat(getComputedStyle(parentLi).fontSize);- const x = Math.round(rect.left - 1.9 * fontSize);- const y = Math.round(rect.top + 0.65 * fontSize);+ const { x, y } = arrowCentre(parentLi);const hit = document.elementFromPoint(x, y);hit?.dispatchEvent(new MouseEvent("mousedown", { bubbles: true, cancelable: true, clientX: x, clientY: y }));@@ -452,6 +488,28 @@ function pressCtrlEnter(editor: ClassicEditor): void {});}+/**+ * On-screen centre of the arrow drawn next to a list item, read back from the stylesheet rather+ * than restated here: the arrow is a pseudo-element (so it has no rect of its own), but its+ * resolved box is offset from the item, and `translateY(-50%)` puts its centre exactly at `top`.+ * Insets differ per list type — to-do items have no marker to clear — so hardcoding one would+ * make these tests aim at the wrong spot as soon as the CSS moves.+ */+function arrowCentre(item: HTMLElement): { x: number; y: number } {+ const rect = item.getBoundingClientRect();+ const arrow = getComputedStyle(item, "::before");++ return {+ x: Math.round(rect.left + parseFloat(arrow.left) + parseFloat(arrow.width) / 2),+ y: Math.round(rect.top + parseFloat(arrow.top))+ };+}++function mouseMoveAt(clientX: number, clientY: number, buttons = 0): void {+ document.elementFromPoint(clientX, clientY)+ ?.dispatchEvent(new MouseEvent("mousemove", { bubbles: true, clientX, clientY, buttons }));+}+function mouseDownAt(item: HTMLElement, offsetX: number): void {const rect = item.getBoundingClientRect();item.dispatchEvent(new MouseEvent("mousedown", {diff --git a/packages/ckeditor5/src/plugins/collapsible_list_items.ts b/packages/ckeditor5/src/plugins/collapsible_list_items.tsindex 149652e9d1..a3762d6aac 100644--- a/packages/ckeditor5/src/plugins/collapsible_list_items.ts+++ b/packages/ckeditor5/src/plugins/collapsible_list_items.ts@@ -1,11 +1,14 @@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, DomEventObserver, ListEditing, MouseObserver, Plugin, type Editor, type ModelElement, type ModelNode, type ModelWriter, type ViewDocumentDomEventData, type ViewDocumentMouseDownEvent, type ViewDocumentMouseOutEvent, type ViewElement } from "ckeditor5";export const LIST_COLLAPSED_ATTRIBUTE = "listCollapsed";const COLLAPSED_DATA_ATTRIBUTE = "data-trilium-collapsed";+/** Marks the item whose arrow the pointer is on, so the stylesheet can give it a filled circle. */+const ARROW_HOVERED_CLASS = "trilium-list-arrow-hovered";+/*** Allows collapsing/expanding nested list items, similar to outliners such as Dynalist or Roam.*@@ -15,6 +18,8 @@ const COLLAPSED_DATA_ATTRIBUTE = "data-trilium-collapsed";*/export default class CollapsibleListItems extends Plugin {+ private _hoveredArrowItem: ViewElement | null = null;+static get requires() {return [ListEditing] as const;}@@ -47,6 +52,7 @@ export default class CollapsibleListItems extends Plugin {});this._enableToggleOnGutterClick();+ this._enableArrowHoverFeedback();this._enableToggleOnKeystroke();this._enableAutoExpand();}@@ -71,8 +77,7 @@ export default class CollapsibleListItems extends Plugin {/*** 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,- * so a mouse event left of the `<li>` box that still targets the `<li>` is an arrow click.+ * clicked.*/private _enableToggleOnGutterClick() {const editor = this.editor;@@ -81,21 +86,8 @@ export default class CollapsibleListItems extends Plugin {view.addObserver(MouseObserver);this.listenTo<ViewDocumentMouseDownEvent>(view.document, "mousedown", (evt, data) => {- const viewItem = data.target;- if (!viewItem || !viewItem.is("element", "li")) {- return;- }-- const domItem = data.domTarget as HTMLElement;- const rect = domItem.getBoundingClientRect();- const isRtl = domItem.ownerDocument.defaultView?.getComputedStyle(domItem).direction === "rtl";- const isGutterClick = isRtl ? data.domEvent.clientX > rect.right : data.domEvent.clientX < rect.left;- if (!isGutterClick) {- return;- }-- const block = getListBlockFromViewListItem(editor, viewItem);- if (!block || !hasNestedItems(block)) {+ const block = getArrowBlockAtPointer(editor, data);+ if (!block) {return;}@@ -106,6 +98,52 @@ export default class CollapsibleListItems extends Plugin {});}+ /**+ * Fills the arrow into a circle while the pointer rests on it. CSS cannot match a hovered+ * pseudo-element, so the item carries a class instead, set from the same gutter geometry the+ * click handler uses — pointer feedback and click target can therefore never drift apart.+ */+ private _enableArrowHoverFeedback() {+ const editor = this.editor;+ const view = editor.editing.view;++ view.addObserver(MouseMoveObserver);++ this.listenTo<ViewDocumentMouseMoveEvent>(view.document, "mousemove", (_evt, data) => {+ // A held button means a drag (selecting text); leave the view untouched until it ends.+ const onArrow = data.domEvent.buttons === 0 && !!getArrowBlockAtPointer(editor, data);+ this._setArrowHovered(onArrow ? data.target : null);+ });++ // Leaving the editable ends the stream of mouse moves, so the flag is dropped here+ // instead — otherwise an arrow the pointer left sideways would stay lit.+ this.listenTo<ViewDocumentMouseOutEvent>(view.document, "mouseout", (_evt, data) => {+ const domRoot = view.getDomRoot();+ const movedTo = data.domEvent.relatedTarget;+ if (!domRoot || !(movedTo instanceof Node) || !domRoot.contains(movedTo)) {+ this._setArrowHovered(null);+ }+ });+ }++ private _setArrowHovered(viewItem: ViewElement | null) {+ if (viewItem === this._hoveredArrowItem) {+ return;+ }++ const previousItem = this._hoveredArrowItem;+ this._hoveredArrowItem = viewItem;++ this.editor.editing.view.change((writer) => {+ if (previousItem) {+ writer.removeClass(ARROW_HOVERED_CLASS, previousItem);+ }+ if (viewItem) {+ writer.addClass(ARROW_HOVERED_CLASS, viewItem);+ }+ });+ }+/*** Expands collapsed ancestors whenever content would otherwise become (or stay) invisible:* when the selection lands inside a hidden block (arrow keys, find & replace, a backspace@@ -203,6 +241,45 @@ export class ToggleListCollapseCommand extends Command {}+/** `MouseObserver` covers mousedown/up/over/out, but not the moves the hover feedback needs. */+class MouseMoveObserver extends DomEventObserver<"mousemove"> {++ public readonly domEventType = ["mousemove"] as const;++ public onDomEvent(domEvent: MouseEvent) {+ this.fire(domEvent.type, domEvent);+ }++}++type ViewDocumentMouseMoveEvent = {+ name: "mousemove";+ args: [data: ViewDocumentDomEventData<MouseEvent>];+};++/**+ * The collapsible list item whose arrow the pointer is on, if any.+ *+ * The arrow is the item's `::before` pseudo-element, laid out entirely outside the `<li>`'s+ * border box, so it is the only thing that can be hit at an x beyond the box while the event+ * still targets the `<li>` — whichever side the gutter is on (it follows the text direction).+ */+function getArrowBlockAtPointer(editor: Editor, data: ViewDocumentDomEventData<MouseEvent>): ModelElement | null {+ const viewItem = data.target;+ if (!viewItem || !viewItem.is("element", "li")) {+ return null;+ }++ const rect = (data.domTarget as HTMLElement).getBoundingClientRect();+ const { clientX } = data.domEvent;+ if (clientX >= rect.left && clientX <= rect.right) {+ return null;+ }++ const block = getListBlockFromViewListItem(editor, viewItem);+ return block && hasNestedItems(block) ? block : null;+}+function toggleCollapsed(editor: Editor, block: ModelElement): void {const collapse = !block.getAttribute(LIST_COLLAPSED_ATTRIBUTE);diff --git a/packages/ckeditor5/src/theme/collapsible_list_items.css b/packages/ckeditor5/src/theme/collapsible_list_items.cssindex 8d2b934278..a263fb8159 100644--- a/packages/ckeditor5/src/theme/collapsible_list_items.css+++ b/packages/ckeditor5/src/theme/collapsible_list_items.css@@ -10,52 +10,75 @@position: relative;}-/* The arrow, rendered in the gutter left of items that have nested items. Kept clickable even- * while transparent; the plugin detects clicks on it by their position outside the <li> box. */+/* The arrow, rendered in the gutter before items that have nested items: the same boxicons+ * chevron the note tree and the collapsible blocks use. Kept clickable even while transparent;+ * the plugin detects the pointer on it by its position outside the <li> box. */.ck-content.ck-editor__editable li:has(> ul, > ol)::before {- content: "";+ --_arrow-size: 1.2em;+ /* Distance from the item's text edge to the arrow's centre. Together with the size above it+ * fills the gap between the start of the list's indent (2.5em before the text) and the+ * bullet (~1em before it), so the arrow never spills into the page margin — which is only+ * 8px wide on mobile — and never covers the marker. */+ --_arrow-inset: -1.8em;+ --_arrow-rotation: 90deg; /* chevron pointing down: expanded */++ content: "\ea50"; /* lookup code for "chevron-right" in boxicons.css */+ font-family: boxicons;+ /* The glyph is text, so it would otherwise inherit the item's bold/italic styling. Its size+ * stays inherited on purpose: `lh` and `em` below must resolve against the item's metrics. */+ font-weight: normal;+ font-style: normal;+position: absolute;- inset-inline-start: -2.3em;- /* Centre the arrow on the first line: half a line-height, minus half the box, so it- * tracks the text's vertical centre across font sizes and line-heights instead of a- * fixed offset that only looks right near line-height 1.5. */- top: calc(0.5lh - 0.2em);- width: 0.4em;- height: 0.4em;- border-inline-end: 2px solid var(--ck-color-base-text, currentColor);- border-bottom: 2px solid var(--ck-color-base-text, currentColor);- transform: rotate(45deg); /* chevron pointing down: expanded */+ inset-inline-start: calc(var(--_arrow-inset) - var(--_arrow-size) / 2);+ /* Anchored to the centre of the item's first line (half a line-height), then pulled up by+ * half of its own box, so it stays centred whatever the font size and line height are. */+ top: 0.5lh;+ width: var(--_arrow-size);+ height: var(--_arrow-size);++ display: flex;+ align-items: center;+ justify-content: center;++ border-radius: 50%;+ color: var(--text-editor-collapsible-list-arrow-color, currentColor);cursor: pointer;opacity: 0;- transition: opacity 150ms ease, transform 150ms ease;+ transform: translateY(-50%) rotate(var(--_arrow-rotation));+ transition: opacity 150ms ease, transform 150ms ease, background-color 150ms ease, color 150ms ease;}-/* In RTL the logical inline-end border lands on the bottom-left corner instead of the- * bottom-right, so the base rotation would swing the chevron to point left. Rotate the other- * way to keep it pointing down while expanded. */-.ck-content.ck-editor__editable li:has(> ul, > ol):dir(rtl)::before {- transform: rotate(-45deg);-}--/* To-do items show a checkbox instead of a bullet; its `vertical-align: middle` box sits a- * little below the text line's centre, so nudge the arrow down to line up with the checkbox- * rather than the text. The offset is font-size-proportional, hence em (not lh). */+/* To-do items have no marker — their checkbox is an inline box inside the content — so the+ * indent before them is empty and the arrow can sit closer to the text, keeping the gap it+ * leaves the same as the one it leaves a bullet. */.ck-content.ck-editor__editable .todo-list > li:has(> ul, > ol)::before {- top: calc(0.5lh - 0.075em);+ --_arrow-inset: -2.2em;}+/* Hovering the item hints at the arrow; hovering the arrow itself commits to it (below). */.ck-content.ck-editor__editable li:hover::before {- opacity: 0.4;+ opacity: 0.45;}+/* Collapsed items advertise themselves: the arrow is the only sign that content is hidden. */.ck-content.ck-editor__editable li[data-trilium-collapsed="true"]::before {- transform: rotate(-45deg); /* chevron pointing at the text: collapsed */- opacity: 0.7;+ --_arrow-rotation: 0deg; /* chevron pointing at the text: collapsed */+ opacity: 0.75;}.ck-content.ck-editor__editable li[data-trilium-collapsed="true"]:dir(rtl)::before {/* Mirror of the LTR collapsed arrow: points left, toward the leftward-flowing text. */- transform: rotate(45deg);+ --_arrow-rotation: 180deg;+}++/* Pointing at the arrow fills it into a circle, like the icon buttons elsewhere in the app. A+ * pseudo-element cannot be matched by `:hover`, so the plugin flags the item with a class,+ * derived from the same gutter geometry it uses for clicks. */+.ck-content.ck-editor__editable li.trilium-list-arrow-hovered::before {+ opacity: 1;+ color: var(--text-editor-collapsible-list-arrow-hover-color, currentColor);+ background-color: var(--text-editor-collapsible-list-arrow-hover-background, rgba(128, 128, 128, 0.25));}.ck-content.ck-editor__editable li[data-trilium-collapsed="true"] > ul,