Skip to content

Build your personal knowledge base with Trilium Notes

text editor/collapsible: use a custom arrow

Adorian Doran386473c36030
diff --git a/packages/ckeditor5-collapsible/src/collapsibleediting.ts b/packages/ckeditor5-collapsible/src/collapsibleediting.ts
index dd651b508d..d1c0997178 100644
--- a/packages/ckeditor5-collapsible/src/collapsibleediting.ts
+++ b/packages/ckeditor5-collapsible/src/collapsibleediting.ts
@@ -70,9 +70,50 @@ export default class CollapsibleEditing extends Plugin {
}
});
- // <summary> conversion (same in both pipelines).
+ // <summary> upcast and data downcast: plain <summary>.
conversion.for("upcast").elementToElement({ view: "summary", model: "summary" });
- conversion.for("downcast").elementToElement({ model: "summary", view: "summary" });
+ conversion.for("dataDowncast").elementToElement({ model: "summary", view: "summary" });
+
+ // <summary> editing downcast: prepend a custom arrow (CKEditor UIElement, so it's
+ // excluded from the data view and not editable). Clicking it manually toggles the
+ // native <details> open attribute — the only way to collapse/expand in the editor.
+ conversion.for("editingDowncast").elementToElement({
+ model: "summary",
+ view: (_modelEl, { writer }) => {
+ const summaryEl = writer.createContainerElement("summary");
+ const arrowEl = writer.createUIElement("span", { class: "trilium-collapsible-arrow" }, function (domDocument) {
+ const span = this.toDomElement(domDocument);
+ // mousedown preventDefault keeps the browser from placing a caret
+ // inside the non-editable UI element.
+ span.addEventListener("mousedown", e => e.preventDefault());
+ span.addEventListener("click", e => {
+ e.stopPropagation();
+ e.preventDefault();
+ const details = span.closest("details");
+ if (details) {
+ details.open = !details.open;
+ }
+ });
+ return span;
+ });
+ writer.insert(writer.createPositionAt(summaryEl, 0), arrowEl);
+ return summaryEl;
+ }
+ });
+
+ // Suppress the native click-to-toggle on <summary> in the editor — only the
+ // custom arrow above is allowed to change the open state. (The data/published
+ // view keeps the native marker and click-to-toggle behavior.)
+ this.listenTo(viewDocument, "click", (_evt, data: any) => {
+ let node = data.target;
+ while (node) {
+ if (node.is?.("element", "summary") && node.parent?.is("element", "details")) {
+ data.preventDefault();
+ return;
+ }
+ node = node.parent;
+ }
+ });
// Helper: is this <details> currently expanded in the DOM? Defaults to true
// when no DOM mapping exists yet (e.g. during early renders).
diff --git a/packages/ckeditor5-collapsible/theme/collapsible.css b/packages/ckeditor5-collapsible/theme/collapsible.css
index 84f02c004d..47ce320fc2 100644
--- a/packages/ckeditor5-collapsible/theme/collapsible.css
+++ b/packages/ckeditor5-collapsible/theme/collapsible.css
@@ -23,11 +23,42 @@
}
/*
- * Editing-only tweaks: while the user is editing we keep the block expanded
- * (the `open` attribute is forced by the editing downcast) and we make the
- * summary behave like a regular editable text block instead of a toggle.
+ * Editor-only tweaks: summary acts like a regular editable text block, the
+ * native disclosure marker is hidden, and the custom arrow (a CKEditor UI
+ * element inserted by the editing downcast) is the only collapse affordance.
+ * The data/published view is untouched.
*/
.ck-editor__editable details.trilium-collapsible > summary {
cursor: text;
+ list-style: none;
+ position: relative;
+ padding-left: 1.5em;
+}
+
+.ck-editor__editable details.trilium-collapsible > summary::-webkit-details-marker {
+ display: none;
+}
+
+/* The arrow lands wherever in DOM the conversion mapper ends up putting it, but
+ * we pin it visually to the left edge of the summary with absolute positioning
+ * so the title text always reads left-to-right with the arrow first. */
+.ck-editor__editable details.trilium-collapsible > summary > .trilium-collapsible-arrow {
+ position: absolute;
+ left: 0;
+ top: 0.25em;
+ cursor: pointer;
+ user-select: none;
+ color: var(--muted-text-color, currentColor);
+ transition: transform 0.15s ease;
+ line-height: 1;
+}
+
+.ck-editor__editable details.trilium-collapsible > summary > .trilium-collapsible-arrow::before {
+ content: "▶";
+ font-size: 0.8em;
+}
+
+.ck-editor__editable details.trilium-collapsible[open] > summary > .trilium-collapsible-arrow {
+ transform: rotate(90deg);
}