Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import type { BlockNoteEditor } from "../../../../editor/BlockNoteEditor";
import { BlockIdentifier } from "../../../../schema/index.js";
import { getNearestBlockPos } from "../../../getBlockInfoFromPos.js";
import { getNodeById } from "../../../nodeUtil.js";
import { insertBlocks } from "../insertBlocks/insertBlocks.js";
import { removeAndInsertBlocks } from "../replaceBlocks/replaceBlocks.js";

type BlockSelectionData = (
| {
Expand Down Expand Up @@ -148,24 +150,26 @@ export function moveBlocks(
referenceBlock: BlockIdentifier,
placement: "before" | "after",
) {
editor.transact(() => {
// A `columnList` reference can be dissolved by `fixColumnList` when its
// `column`s are removed, leaving its ID invalid for re-insertion. Anchor
// to an adjacent block instead, which is unaffected by the removal.
const refBlock = editor.getBlock(referenceBlock);
if (refBlock?.type === "columnList") {
const adjacent =
placement === "after"
? editor.getNextBlock(refBlock)
: editor.getPrevBlock(refBlock);
if (adjacent) {
referenceBlock = adjacent;
placement = placement === "after" ? "before" : "after";
}
}

editor.removeBlocks(blocks);
editor.insertBlocks(flattenColumns(blocks), referenceBlock, placement);
editor.transact((tr) => {
// Don't fix columns/columnLists in the removal step. Since a move is a
// rearrangement rather than a deletion, columns that it empties out are
// deliberately left as-is instead of being collapsed - this keeps moves
// free of side effects (and reversible by moving back), and matches
// dragging a block out of a column, which doesn't collapse it either.
// Fixing them mid-move also broke the following case:
// <column>
// <paragraph></paragraph>
// <paragraph>Paragraph</paragraph>
// </column>
// When the non-empty block is moved up, the column is seen as empty and
// collapsed in the removal step, so the following insertion fails.
removeAndInsertBlocks(tr, blocks, [], { fixColumns: false });
insertBlocks<any, any, any>(
tr,
flattenColumns(blocks),
referenceBlock,
placement,
);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export function removeAndInsertBlocks<
tr: Transaction,
blocksToRemove: BlockIdentifier[],
blocksToInsert: PartialBlock<BSchema, I, S>[],
options: {
fixColumns?: boolean;
} = {},
): {
insertedBlocks: Block<BSchema, I, S>[];
removedBlocks: Block<BSchema, I, S>[];
Expand Down Expand Up @@ -112,7 +115,12 @@ export function removeAndInsertBlocks<
);
}

columnListPositions.forEach((pos) => fixColumnList(tr, pos));
// Collapses empty columns/columnLists. Callers where the removal isn't a
// deletion can opt out - e.g. `moveBlocks` re-inserts the blocks elsewhere
// and deliberately leaves emptied columns as-is.
if (options.fixColumns !== false) {
columnListPositions.forEach((pos) => fixColumnList(tr, pos));
}

// Converts the nodes created from `blocksToInsert` into full `Block`s.
const insertedBlocks = nodesToInsert.map((node) =>
Expand Down
Loading
Loading