Skip to content

Commit c4f0431

Browse files
authored
fix(core): unwrap a dissolving container in place (#3104)
1 parent 4f3ade8 commit c4f0431

4 files changed

Lines changed: 228 additions & 4 deletions

File tree

‎packages/core/src/api/blockManipulation/containers/containers.test.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,70 @@ describe("children repair", () => {
320320
expect(() => editor.prosemirrorState.doc.check()).not.toThrow();
321321
});
322322

323+
// Dissolving is an unwrap, not a rewrite: the survivor's content is the same
324+
// content, in place, so a caret sitting in it stays where the user put it
325+
// (and a collaborator's concurrent edit still maps onto it). Replacing the
326+
// container with freshly built copies of its children would collapse the
327+
// selection onto the edge of the replaced range instead.
328+
it("keeps the caret in the survivor a container unwraps to", () => {
329+
editor.replaceBlocks(editor.document, [
330+
{
331+
type: "pair",
332+
id: "pair-0",
333+
children: [
334+
{ id: "pair-a", type: "paragraph", content: "A" },
335+
{ id: "pair-b", type: "paragraph", content: "Survivor" },
336+
],
337+
},
338+
{ id: "trailing", type: "paragraph", content: "" },
339+
]);
340+
341+
editor.setTextCursorPosition("pair-b", "end");
342+
const offsetBefore = editor.prosemirrorState.selection.$from.parentOffset;
343+
344+
editor.removeBlocks(["pair-a"]);
345+
346+
expect(editor.getBlock("pair-0")).toBeUndefined();
347+
expect(editor.getTextCursorPosition().block.id).toBe("pair-b");
348+
expect(editor.prosemirrorState.selection.$from.parentOffset).toBe(
349+
offsetBefore,
350+
);
351+
});
352+
353+
// The same, for a survivor that is lifted out of two wrappers at once.
354+
it("keeps the caret in a named-only survivor's content", () => {
355+
editor.replaceBlocks(editor.document, [
356+
{
357+
type: "grid",
358+
id: "g-0",
359+
children: [
360+
{
361+
type: "gridCell",
362+
id: "cell-a",
363+
children: [{ id: "cell-a-p", type: "paragraph", content: "A" }],
364+
},
365+
{
366+
type: "gridCell",
367+
id: "cell-b",
368+
children: [{ id: "cell-b-p", type: "paragraph", content: "B" }],
369+
},
370+
],
371+
},
372+
{ id: "trailing", type: "paragraph", content: "" },
373+
]);
374+
375+
editor.setTextCursorPosition("cell-b-p", "end");
376+
const offsetBefore = editor.prosemirrorState.selection.$from.parentOffset;
377+
378+
editor.removeBlocks(["cell-a-p"]);
379+
380+
expect(editor.getBlock("g-0")).toBeUndefined();
381+
expect(editor.getTextCursorPosition().block.id).toBe("cell-b-p");
382+
expect(editor.prosemirrorState.selection.$from.parentOffset).toBe(
383+
offsetBefore,
384+
);
385+
});
386+
323387
// An emptied child of the container is dropped even when the container
324388
// stays at or above `min`: an emptied column disappears rather than
325389
// lingering. The multicolumn e2e snapshots pin the same behavior from the

‎packages/core/src/api/blockManipulation/containers/fixContainer.ts‎

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { Fragment, type Node } from "prosemirror-model";
1+
import { Fragment, type Node, Slice } from "prosemirror-model";
22
import { type Transaction } from "prosemirror-state";
3+
import { ReplaceAroundStep } from "prosemirror-transform";
34

45
import {
56
isContainerNode,
@@ -68,11 +69,11 @@ export function fixContainer(tr: Transaction, containerPos: number) {
6869

6970
const childrenConfig = container.type.spec.blockConfig?.children;
7071
const min = childrenConfig ? (childrenConfig.min ?? 1) : 1;
71-
const survivors: Node[] = [];
72+
const survivors: { node: Node; offset: number }[] = [];
7273
const emptied: { from: number; to: number }[] = [];
7374
container.forEach((child, offset) => {
7475
if (!isEmptyContainerChild(child)) {
75-
survivors.push(child);
76+
survivors.push({ node: child, offset });
7677
} else if (isContainerNode(child.type)) {
7778
const from = containerPos + 1 + offset;
7879
emptied.push({ from, to: from + child.nodeSize });
@@ -90,8 +91,42 @@ export function fixContainer(tr: Transaction, containerPos: number) {
9091

9192
// Too few children left for the container to mean anything, so it is
9293
// replaced by its surviving children.
94+
95+
// A single survivor supplying the whole replacement is an unwrap, and doing
96+
// it as a `ReplaceAroundStep` leaves that content in place in the document:
97+
// positions inside it - a caret, a collaborator's concurrent edit - map
98+
// through it, where deleting and reinserting the same content would collapse
99+
// them onto the edge of the replaced range.
100+
if (survivors.length === 1) {
101+
const [{ node: survivor, offset }] = survivors;
102+
const survivorPos = containerPos + 1 + offset;
103+
// A `namedOnly` survivor is lifted out of two wrappers at once - the column
104+
// and the container - and any other survivor out of just the container.
105+
const gap = isNamedOnly(survivor.type)
106+
? { from: survivorPos + 1, to: survivorPos + survivor.nodeSize - 1 }
107+
: { from: survivorPos, to: survivorPos + survivor.nodeSize };
108+
109+
tr.step(
110+
new ReplaceAroundStep(
111+
containerPos,
112+
containerPos + container.nodeSize,
113+
gap.from,
114+
gap.to,
115+
Slice.empty,
116+
0,
117+
// Not a pure unwrap: emptied siblings sit outside the gap and go with
118+
// the container, which the `structure` check would refuse.
119+
false,
120+
),
121+
);
122+
return;
123+
}
124+
125+
// Several survivors are not one contiguous range once an emptied child sits
126+
// between them, so this path rebuilds them and positions inside them
127+
// collapse. It takes a container with a `min` of 3 or more to reach.
93128
const replacement: Node[] = [];
94-
for (const survivor of survivors) {
129+
for (const { node: survivor } of survivors) {
95130
if (isNamedOnly(survivor.type)) {
96131
// The survivor can't stand on its own either (a column only exists
97132
// inside a column list), so what it holds is what's left.

‎packages/xl-multi-column/src/test/commands/backspace.test.ts‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,115 @@ const threeColumnsWithParagraphAbove = [
8383
},
8484
];
8585

86+
const columnsOnly = [
87+
{
88+
type: "columnList" as const,
89+
children: [
90+
{
91+
type: "column" as const,
92+
children: [
93+
{ id: "col1-para", type: "paragraph" as const, content: "col1" },
94+
],
95+
},
96+
{
97+
type: "column" as const,
98+
children: [
99+
{ id: "col2-para", type: "paragraph" as const, content: "col2" },
100+
],
101+
},
102+
],
103+
},
104+
];
105+
106+
const columnsWithEmptyFirst = [
107+
{
108+
type: "columnList" as const,
109+
children: [
110+
{
111+
type: "column" as const,
112+
children: [
113+
{ id: "empty-para", type: "paragraph" as const, content: "" },
114+
],
115+
},
116+
{
117+
type: "column" as const,
118+
children: [
119+
{ id: "col2-para", type: "paragraph" as const, content: "col2" },
120+
{ id: "col2-trailing", type: "paragraph" as const, content: "" },
121+
],
122+
},
123+
],
124+
},
125+
];
126+
127+
/** Every bit of text in the document, so a fix can't quietly drop content. */
128+
function texts(editor: BlockNoteEditor<any, any, any>): string[] {
129+
const out: string[] = [];
130+
const walk = (blocks: any[]) => {
131+
for (const block of blocks) {
132+
if (Array.isArray(block.content)) {
133+
const text = block.content.map((c: any) => c.text ?? "").join("");
134+
if (text) {
135+
out.push(text);
136+
}
137+
}
138+
walk(block.children ?? []);
139+
}
140+
};
141+
walk(editor.document);
142+
return out.sort();
143+
}
144+
145+
// A column layout with nothing after it, and one whose first column is empty,
146+
// both used to throw out of the keydown handler: there is no following block
147+
// to pull in, and no non-empty first column to merge with. The document is
148+
// left alone (or repaired) instead.
149+
describe("Delete and Backspace at a column layout's edges", () => {
150+
it("Delete at the end of the last column, with the layout ending the document", () => {
151+
const editor = getEditor();
152+
editor.replaceBlocks(editor.document, columnsOnly);
153+
154+
editor.setTextCursorPosition("col2-para", "end");
155+
156+
expect(() => pressDelete(editor)).not.toThrow();
157+
expect(() => editor.prosemirrorState.doc.check()).not.toThrow();
158+
expect(texts(editor)).toEqual(["col1", "col2"]);
159+
});
160+
161+
it("Delete at the start of an empty first column", () => {
162+
const editor = getEditor();
163+
editor.replaceBlocks(editor.document, columnsWithEmptyFirst);
164+
165+
editor.setTextCursorPosition("empty-para", "start");
166+
167+
expect(() => pressDelete(editor)).not.toThrow();
168+
expect(() => editor.prosemirrorState.doc.check()).not.toThrow();
169+
expect(texts(editor)).toEqual(["col2"]);
170+
});
171+
172+
it("Delete at the end of a trailing empty block in the last column", () => {
173+
const editor = getEditor();
174+
editor.replaceBlocks(editor.document, columnsWithEmptyFirst);
175+
176+
editor.setTextCursorPosition("col2-trailing", "end");
177+
178+
expect(() => pressDelete(editor)).not.toThrow();
179+
expect(() => editor.prosemirrorState.doc.check()).not.toThrow();
180+
expect(texts(editor)).toEqual(["col2"]);
181+
});
182+
183+
it("Backspace at the start of a column following an empty column", () => {
184+
const editor = getEditor();
185+
editor.replaceBlocks(editor.document, columnsWithEmptyFirst);
186+
187+
editor.setTextCursorPosition("col2-para", "start");
188+
189+
expect(() => pressBackspace(editor)).not.toThrow();
190+
expect(() => editor.prosemirrorState.doc.check()).not.toThrow();
191+
expect(texts(editor)).toEqual(["col2"]);
192+
});
193+
});
194+
86195
describe("Backspace with multi-column", () => {
87196
// TODO: When migrating to vitest browser mode, replace this test with
88197
// a version that presses Backspace 5 times from offset 5 in "hello world"

‎packages/xl-multi-column/src/test/commands/removeBlocks.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,20 @@ describe("Test removeBlocks", () => {
7979

8080
expect(getEditor().document).toMatchSnapshot();
8181
});
82+
83+
// Removing the other column leaves the column list with one column, so it
84+
// dissolves and the surviving column's blocks are lifted out. That is an
85+
// unwrap of content that never moves, so a caret in it survives untouched.
86+
it("Keeps the cursor in place when removing a column dissolves the columnList", () => {
87+
const editor = getEditor();
88+
editor.setTextCursorPosition("column-paragraph-2", "end");
89+
const offsetBefore = editor.prosemirrorState.selection.$from.parentOffset;
90+
91+
editor.removeBlocks(["column-0"]);
92+
93+
expect(editor.getTextCursorPosition().block.id).toBe("column-paragraph-2");
94+
expect(editor.prosemirrorState.selection.$from.parentOffset).toBe(
95+
offsetBefore,
96+
);
97+
});
8298
});

0 commit comments

Comments
 (0)