Skip to content

Commit fb147e4

Browse files
committed
Added edge case handling + tests for columns
1 parent 0caea13 commit fb147e4

3 files changed

Lines changed: 2331 additions & 198 deletions

File tree

packages/core/src/api/blockManipulation/commands/moveBlocks/moveBlocks.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -123,29 +123,14 @@ function updateBlockSelectionFromData(
123123
tr.setSelection(selection);
124124
}
125125

126-
/**
127-
* Replaces any `columnList` blocks with the children of their columns. This is
128-
* done here instead of in `getSelection` as we still need to remove the entire
129-
* `columnList` node but only insert the `blockContainer` nodes inside it.
130-
* @param blocks The blocks to flatten.
131-
*/
126+
// Replaces top-level `column` blocks with their children, as a `column` is not
127+
// a valid block outside a `columnList`. Other blocks are returned as-is.
132128
function flattenColumns(
133129
blocks: Block<any, any, any>[],
134130
): Block<any, any, any>[] {
135-
return blocks
136-
.map((block) => {
137-
if (block.type === "columnList") {
138-
return block.children
139-
.map((column) => flattenColumns(column.children))
140-
.flat();
141-
}
142-
143-
return {
144-
...block,
145-
children: flattenColumns(block.children),
146-
};
147-
})
148-
.flat();
131+
return blocks.flatMap((block) =>
132+
block.type === "column" ? block.children : [block],
133+
);
149134
}
150135

151136
/**
@@ -164,6 +149,21 @@ export function moveBlocks(
164149
placement: "before" | "after",
165150
) {
166151
editor.transact(() => {
152+
// A `columnList` reference can be dissolved by `fixColumnList` when its
153+
// `column`s are removed, leaving its ID invalid for re-insertion. Anchor
154+
// to an adjacent block instead, which is unaffected by the removal.
155+
const refBlock = editor.getBlock(referenceBlock);
156+
if (refBlock?.type === "columnList") {
157+
const adjacent =
158+
placement === "after"
159+
? editor.getNextBlock(refBlock)
160+
: editor.getPrevBlock(refBlock);
161+
if (adjacent) {
162+
referenceBlock = adjacent;
163+
placement = placement === "after" ? "before" : "after";
164+
}
165+
}
166+
167167
editor.removeBlocks(blocks);
168168
editor.insertBlocks(flattenColumns(blocks), referenceBlock, placement);
169169
});

0 commit comments

Comments
 (0)