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
text editor/collapsible: various improvements
Adorian Doranb081fbe4be85
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 72eda3492c..1d791992b7 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2678,6 +2678,7 @@"checkbox-tooltip": "Right click for more task states.\nPress {{shortcut}} to cycle between states.","collapsible-body-placeholder": "Type the content here...","collapsible-button-label": "Collapsible block",+ "collapsible-handle-tooltip": "Drag to reposition the collapsible block","collapsible-select-label": "Select collapsible block","collapsible-title-placeholder": "Summary","collapsible-toggle-label": "Toggle collapsible block",diff --git a/packages/ckeditor5-collapsible/src/collapsible-editing.ts b/packages/ckeditor5-collapsible/src/collapsible-editing.tsindex 0fed00ed52..59ada1bf73 100644--- a/packages/ckeditor5-collapsible/src/collapsible-editing.ts+++ b/packages/ckeditor5-collapsible/src/collapsible-editing.ts@@ -41,6 +41,8 @@ export default class CollapsibleEditing extends Plugin {private readonly summaryTooltips = new Set<HTMLElement>();/** The summary tooltip we currently have force-shown because the caret is inside it. */private caretShownTooltip?: HTMLElement;+ /** Drag-handle DOM elements that currently have a Bootstrap tooltip attached. */+ private readonly handleTooltips = new Set<HTMLElement>();public init(): void {this.editor.commands.add("collapsible", new CollapsibleCommand(this.editor));@@ -75,6 +77,7 @@ export default class CollapsibleEditing extends Plugin {this.registerAutoOpenNewDetails();this.registerPostFixers();this.registerSummaryTooltips();+ this.registerHandleTooltips();}public override destroy(): void {@@ -95,6 +98,10 @@ export default class CollapsibleEditing extends Plugin {}this.summaryTooltips.clear();this.caretShownTooltip = undefined;+ for (const handle of this.handleTooltips) {+ Tooltip.getInstance(handle)?.dispose();+ }+ this.handleTooltips.clear();this.dragHandle?.cancel();this.preserveOpenOnNextInsert.clear();super.destroy();@@ -841,6 +848,45 @@ export default class CollapsibleEditing extends Plugin {});}+ /**+ * Attach a plain Bootstrap tooltip (default near-element placement, no+ * screen-corner styling) to each drag handle so hover/focus surfaces the+ * "Drag to reposition" hint. Tracked in a Set and reaped on view render+ * the same way summary tooltips are.+ */+ private registerHandleTooltips() {+ const editor = this.editor;+ const t = this.translate();++ this.listenTo(editor.editing.view, "render", () => {+ let cleaned = false;+ for (const handle of this.handleTooltips) {+ if (!handle.isConnected) {+ Tooltip.getInstance(handle)?.dispose();+ this.handleTooltips.delete(handle);+ cleaned = true;+ }+ }++ const current: HTMLElement[] = [];+ this.forEachDomRoot(root => {+ for (const h of root.querySelectorAll<HTMLElement>(".trilium-collapsible-handle")) {+ current.push(h);+ }+ });++ if (!cleaned && current.length === this.handleTooltips.size) return;++ for (const handle of current) {+ if (this.handleTooltips.has(handle)) continue;+ new Tooltip(handle, {+ title: t("text-editor.collapsible-handle-tooltip")+ });+ this.handleTooltips.add(handle);+ }+ });+ }+// -----------------------------------------------------------------// Model post-fixers// -----------------------------------------------------------------@@ -849,10 +895,41 @@ export default class CollapsibleEditing extends Plugin {const document = this.editor.model.document;document.registerPostFixer(writer => this.structuralPostFixer(writer));document.registerPostFixer(writer => this.summaryInvariantPostFixer(writer));+ document.registerPostFixer(writer => this.bodyExistsPostFixer(writer));document.registerPostFixer(writer => this.gapPostFixer(writer));document.registerPostFixer(writer => this.hiddenBodyPostFixer(writer));}+ /**+ * Every <details> must have at least one body block after its <summary>.+ * Without one the placeholder vanishes and the collapsible looks broken —+ * this happens after Backspace at the start of an empty body, or after+ * onEnterInBody removes the only body paragraph to exit the block. Runs+ * after summaryInvariantPostFixer so the summary is guaranteed to exist+ * before we count children.+ */+ private bodyExistsPostFixer(writer: any): boolean {+ const changes = this.editor.model.document.differ.getChanges();+ const visited = new Set<any>();++ const ensure = (details: any): boolean => {+ if (visited.has(details)) return false;+ visited.add(details);+ if (details.childCount > 1) return false;+ const summary = details.getChild(0);+ if (!summary?.is("element", "summary")) return false;+ writer.insert(writer.createElement("paragraph"), details, "end");+ return true;+ };++ for (const entry of changes) {+ if (entry.type !== "remove") continue;+ const parent = (entry as any).position?.parent;+ if (parent?.is("element", "details") && ensure(parent)) return true;+ }+ return false;+ }+/*** Cleanup invariants after every change:* - <summary> not inside <details> → remove itdiff --git a/packages/ckeditor5-collapsible/theme/collapsible.css b/packages/ckeditor5-collapsible/theme/collapsible.cssindex 34e05a5f05..f4c77521ea 100644--- a/packages/ckeditor5-collapsible/theme/collapsible.css+++ b/packages/ckeditor5-collapsible/theme/collapsible.css@@ -97,8 +97,11 @@/* Body-paragraph placeholder ("Content") is registered on the first body* block. Hide it whenever the body has more than one block — the hint is- * only useful when the collapsible's body is genuinely empty. */- > p.ck-placeholder:not(:last-child)::before {+ * only useful when the body is a single empty <p>. `:has(> :nth-child(3))`+ * matches a <details> with summary + 2+ body children; `:not(:last-child)`+ * would only hide the placeholder on non-last paragraphs, leaving the last+ * one (e.g. the original empty body after Enter-in-summary) still visible. */+ &:has(> :nth-child(3)) > p.ck-placeholder::before {display: none;}diff --git a/packages/ckeditor5/src/plugins/move_block_updown.ts b/packages/ckeditor5/src/plugins/move_block_updown.tsindex 235dbcbbde..4496a317a3 100644--- a/packages/ckeditor5/src/plugins/move_block_updown.ts+++ b/packages/ckeditor5/src/plugins/move_block_updown.ts@@ -120,8 +120,15 @@ abstract class MoveBlockUpDownCommand extends Command {for (const block of blocks) {let el: ModelElement = block;- // Traverse up until the parent is the root ($root) or there is no parent- while (el.parent && el.parent.name !== '$root') {+ // Hoist a caret-in-<summary> to the enclosing <details> so the+ // collapsible's title acts as a handle for the whole block (the+ // schema's `isBlock: true` on <summary> was specifically engineered+ // for this — see packages/ckeditor5-collapsible/src/collapsible-editing.ts).+ // Everything else uses the block as-is, so nested children+ // (collapsible body, admonition body, blockquote children) move+ // within their immediate container instead of escalating to the+ // top-level block.+ if (el.name === 'summary' && el.parent) {el = el.parent as ModelElement;}resolved.push(el);