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
fix(collapsible): reveal a closed block when content merges into it
Elian Dorandb1908a0d98e
diff --git a/packages/ckeditor5/src/plugins/collapsible/collapsible_editing.ts b/packages/ckeditor5/src/plugins/collapsible/collapsible_editing.tsindex 50716cd380..14dbad6e79 100644--- a/packages/ckeditor5/src/plugins/collapsible/collapsible_editing.ts+++ b/packages/ckeditor5/src/plugins/collapsible/collapsible_editing.ts@@ -142,6 +142,7 @@ export default class CollapsibleEditing extends Plugin {this.registerBodyPlaceholder();this.registerKeyHandlers();this.registerMergeGuard();+ this.registerHiddenMergeReveal();this.registerClickHandler();this.registerDomListeners();this.registerPostFixers();@@ -859,6 +860,46 @@ export default class CollapsibleEditing extends Plugin {}, { priority: "high" });}+ /**+ * Expand a collapsed collapsible that a deletion is about to merge content into.+ *+ * Backspace at the start of the line below a closed block merges that line into the+ * last body block, which is hidden: the text leaves the screen with no sign of where+ * it went. Opening the block first keeps the result in view. The expansion runs on the+ * deletion's own batch, so a single Ctrl+Z takes back both the merge and the reveal —+ * the same reasoning as the mid-title split in+ * {@link CollapsibleEditing#onEnterInSummary}.+ *+ * Scoped to deletions that actually carry something in. A blank line below the block+ * merges nothing, so it is removed with the block left closed, and Backspace on a blank+ * line keeps meaning "drop this line" rather than "open the block below".+ *+ * Registered after {@link CollapsibleEditing#registerMergeGuard} so it sees the options+ * that guard rewrites: `leaveUnmerged` means nothing is folded in and nothing needs+ * revealing.+ */+ private registerHiddenMergeReveal() {+ this.listenTo(this.editor.model, "deleteContent", (_evt, args: any[]) => {+ const [selection, options] = args;+ if (options?.leaveUnmerged) return;+ const range = selection.getFirstRange();+ /* v8 ignore next -- deleteContent bails on a collapsed selection before this fires */+ if (!range) return;+ // Content is folded in only when the range starts inside a collapsible and ends+ // outside it — the direction CKEditor merges left.+ const details = range.start.findAncestor("details");+ if (!details || this.isDetailsOpen(details)) return;+ if (range.end.getAncestors().includes(details)) return;+ // Whatever trails the range's end is what gets carried across. Nothing trailing,+ // nothing to see.+ if (range.end.isAtEnd) return;++ // A nested change joins the batch already open around `deleteContent`, keeping+ // the reveal on the same undo step as the merge.+ this.editor.model.change(writer => writer.setAttribute(OPEN_ATTRIBUTE, true, details));+ }, { priority: "high" });+ }+/** Backspace at start of an empty summary unwraps the collapsible. */private onBackspaceInEmptySummary(evt: any, data: any) {const selection = this.editor.model.document.selection;diff --git a/packages/ckeditor5/src/plugins/collapsible/delete_across_boundary.spec.ts b/packages/ckeditor5/src/plugins/collapsible/delete_across_boundary.spec.tsindex 43510450c6..1b060540c2 100644--- a/packages/ckeditor5/src/plugins/collapsible/delete_across_boundary.spec.ts+++ b/packages/ckeditor5/src/plugins/collapsible/delete_across_boundary.spec.ts@@ -283,19 +283,72 @@ describe("collapsible: deleting across the boundary", () => {expect(renderedCollapsible()).toEqual({ title: "Title", body: "onetwo" });});- it("merges a line that has content instead of dropping the body", async () => {+ it("expands the block when the line below it has content to merge in", async () => {setModelData(editor.model, `${CLOSED}<paragraph>[]after</paragraph>`);editor.editing.view.focus();await userEvent.keyboard("{Backspace}");+ // Merging "after" into a hidden block would take it off screen with no sign of+ // where it went, so the collapsible opens and the result stays in view.expect(getModelData(editor.model)).toBe(- "<details><summary>Title[]</summary>" +- "<paragraph>one</paragraph><paragraph>twoafter</paragraph>" ++ "<details open=\"true\"><summary>Title</summary>" ++ "<paragraph>one</paragraph><paragraph>two[]after</paragraph>" ++ "</details>"+ );+ expect(renderedCollapsible()).toEqual({ title: "Title", body: "onetwoafter" });+ });++ it("takes back the merge and the reveal on one undo", async () => {+ setModelData(editor.model, `${CLOSED}<paragraph>[]after</paragraph>`);+ editor.editing.view.focus();++ await userEvent.keyboard("{Backspace}");+ editor.execute("undo");++ // The reveal rides the deletion's batch, so the block is collapsed again rather+ // than left open over restored content.+ expect(getModelData(editor.model)).toBe(+ `<details><summary>Title[]</summary>${BODY}</details><paragraph>after</paragraph>`+ );+ });++ it("expands a nested collapsed block that content merges into", async () => {+ setModelData(editor.model,+ "<details open=\"true\"><summary>Outer</summary>" ++ `<details><summary>Inner</summary>${BODY}</details>` ++ "<paragraph>[]x</paragraph>" ++ "</details>");+ editor.editing.view.focus();++ await userEvent.keyboard("{Backspace}");++ expect(getModelData(editor.model)).toBe(+ "<details open=\"true\"><summary>Outer</summary>" ++ "<details open=\"true\"><summary>Inner</summary>" ++ "<paragraph>one</paragraph><paragraph>two[]x</paragraph>" ++ "</details>" +"</details>");});+ it("leaves the block closed when the merge guard cancels the merge", () => {+ // The range runs out of one collapsed block and into the next, so the guard sets+ // `leaveUnmerged` and nothing is folded into the first — there is nothing to reveal.+ setModelData(editor.model,+ "<details><summary>A</summary><paragraph>[x</paragraph></details>" ++ "<details><summary>B</summary><paragraph>y]z</paragraph></details>");++ fireDeleteSelection(editor.model.document.selection.getFirstRange());++ // A keeps its collapsed state; the caret lands in its title because the emptied+ // body is hidden. B's title went with the selected range, as it always has.+ expect(getModelData(editor.model)).toBe(+ "<details><summary>A[]</summary><paragraph></paragraph></details>" ++ "<details><summary></summary><paragraph>z</paragraph></details>"+ );+ });+it("protects a collapsed collapsible nested in an open one", async () => {setModelData(editor.model,"<details open=\"true\"><summary>Outer</summary>" +