Skip to content

Build your personal knowledge base with Trilium Notes

text editor/collapsible: make the collapsible blocks collapsed by default

Adorian Doran8bb408c86795
diff --git a/packages/ckeditor5-collapsible/src/collapsiblecommand.ts b/packages/ckeditor5-collapsible/src/collapsiblecommand.ts
index 36197d0426..aed764806e 100644
--- a/packages/ckeditor5-collapsible/src/collapsiblecommand.ts
+++ b/packages/ckeditor5-collapsible/src/collapsiblecommand.ts
@@ -20,6 +20,7 @@ export default class CollapsibleCommand extends Command {
public override execute(): void {
const editor = this.editor;
const model = editor.model;
+ let newDetails: any;
model.change(writer => {
const detailsEl = writer.createElement("details");
@@ -30,10 +31,22 @@ export default class CollapsibleCommand extends Command {
writer.append(paragraphEl, detailsEl);
model.insertContent(detailsEl);
+ newDetails = detailsEl;
// Place the cursor inside the summary so the user can immediately type a title.
writer.setSelection(summaryEl, 0);
});
+
+ // The editing downcast renders <details> closed by default so loaded documents
+ // start collapsed. Open the freshly-inserted one so the user can type into its
+ // body without an extra click. Deferred so the view has had a chance to render.
+ setTimeout(() => {
+ const view = editor.editing.mapper.toViewElement(newDetails);
+ const dom = view ? editor.editing.view.domConverter.viewToDom(view) : null;
+ if (dom instanceof HTMLDetailsElement) {
+ dom.open = true;
+ }
+ }, 0);
}
private _checkEnabled(): boolean {
diff --git a/packages/ckeditor5-collapsible/src/collapsibleediting.ts b/packages/ckeditor5-collapsible/src/collapsibleediting.ts
index a83e3c0c0c..e464197c20 100644
--- a/packages/ckeditor5-collapsible/src/collapsibleediting.ts
+++ b/packages/ckeditor5-collapsible/src/collapsibleediting.ts
@@ -59,14 +59,14 @@ export default class CollapsibleEditing extends Plugin {
}
});
- // <details> editing downcast: force `open` so the user can edit the body.
+ // <details> editing downcast: emit a plain <details class="trilium-collapsible">.
+ // Same as data downcast — collapsibles start collapsed on load. The insert
+ // command explicitly opens the new one after insertion so the user can edit
+ // the body immediately.
conversion.for("editingDowncast").elementToElement({
model: "details",
view: (_modelElement, { writer }) => {
- return writer.createContainerElement("details", {
- class: "trilium-collapsible",
- open: "open"
- });
+ return writer.createContainerElement("details", { class: "trilium-collapsible" });
}
});