Skip to content

Build your personal knowledge base with Trilium Notes

text editor/collapsible: prevent duplicate empty paragraph when pressing Enter at the end of the summary and no details set yet

Adorian Doranf527b664910d
diff --git a/packages/ckeditor5-collapsible/src/collapsible-editing.ts b/packages/ckeditor5-collapsible/src/collapsible-editing.ts
index 0d47f6cc8a..75c5b0d5df 100644
--- a/packages/ckeditor5-collapsible/src/collapsible-editing.ts
+++ b/packages/ckeditor5-collapsible/src/collapsible-editing.ts
@@ -412,6 +412,16 @@ export default class CollapsibleEditing extends Plugin {
const pos = this.isDetailsOpen(details)
? writer.createPositionAfter(summary)
: writer.createPositionAfter(details);
+ // If the spot already holds an empty paragraph (the body's
+ // placeholder paragraph, or a blank line the user left after
+ // the collapsible), just park the caret in it — stacking a
+ // second identical empty paragraph on top would surprise the
+ // user and require an extra Backspace to clean up.
+ const existing = pos.nodeAfter;
+ if (existing?.is?.("element", "paragraph") && existing.isEmpty) {
+ writer.setSelection(existing, 0);
+ return;
+ }
this.insertParagraphAt(writer, pos);
return;
}
diff --git a/packages/ckeditor5-collapsible/tests/enter-in-summary.ts b/packages/ckeditor5-collapsible/tests/enter-in-summary.ts
new file mode 100644
index 0000000000..11d2539c3a
--- /dev/null
+++ b/packages/ckeditor5-collapsible/tests/enter-in-summary.ts
@@ -0,0 +1,48 @@
+import { ClassicEditor, Essentials, Paragraph, _setModelData as setModelData, _getModelData as getModelData } from "ckeditor5";
+import { afterEach, beforeEach, describe, expect, it } from "vitest";
+import CollapsibleEditing from "../src/collapsible-editing.js";
+
+describe("onEnterInSummary", () => {
+ let domElement: HTMLDivElement;
+ let editor: ClassicEditor;
+ let model: ClassicEditor["model"];
+
+ beforeEach(async () => {
+ domElement = document.createElement("div");
+ document.body.appendChild(domElement);
+ editor = await ClassicEditor.create(domElement, {
+ licenseKey: "GPL",
+ plugins: [Essentials, Paragraph, CollapsibleEditing]
+ });
+ model = editor.model;
+ });
+
+ afterEach(() => {
+ domElement.remove();
+ return editor.destroy();
+ });
+
+ const fireEnter = () => editor.editing.view.document.fire("enter", {
+ preventDefault: () => {},
+ stop: () => {},
+ isSoft: false
+ });
+
+ it("at the end of a title (expanded) parks the caret in the existing empty body paragraph instead of stacking a second one", () => {
+ setModelData(model,
+ "<details><summary>Title[]</summary><paragraph></paragraph></details>"
+ );
+ // Auto-open won't have fired in this synchronous slice, so the details
+ // dom.open is still false. The expanded vs collapsed branch decision is
+ // made via dom.open — force it true to exercise the expanded branch.
+ const detailsDom = editor.editing.view.getDomRoot()!.querySelector("details") as HTMLDetailsElement;
+ detailsDom.open = true;
+
+ fireEnter();
+
+ // Caret moved into the existing empty paragraph; no second one was inserted.
+ expect(getModelData(model)).toBe(
+ "<details><summary>Title</summary><paragraph>[]</paragraph></details>"
+ );
+ });
+});