fix: Split cell should work without accurate selection (#11256)

closes #11248
This commit is contained in:
Tom Moor
2026-01-23 23:29:54 -05:00
committed by GitHub
parent 07514cb692
commit 3118721b21
2 changed files with 57 additions and 3 deletions
+52 -2
View File
@@ -742,12 +742,62 @@ export function deleteCellSelection(
}
/**
* A command that splits a cell and collapses the selection.
* A command that splits the first merged cell found in the selection and
* collapses the selection. Works with both single cell and multi-cell selections.
*
* @returns The command
*/
export function splitCellAndCollapse(): Command {
return chainTransactions(splitCell, collapseSelection());
return (state, dispatch) => {
if (!isInTable(state)) {
return false;
}
const { selection } = state;
// Handle CellSelection (including RowSelection and ColumnSelection which extend it)
if (
selection instanceof CellSelection ||
selection instanceof RowSelection ||
selection instanceof ColumnSelection
) {
// Find the first merged cell in the selection
let mergedCellPos: number | null = null;
selection.forEachCell((cell, pos) => {
if (
mergedCellPos === null &&
(cell.attrs.colspan > 1 || cell.attrs.rowspan > 1)
) {
mergedCellPos = pos;
}
});
// If no merged cell found, nothing to split
if (mergedCellPos === null) {
return false;
}
if (dispatch) {
// Create a CellSelection for the merged cell and apply splitCell
const $cell = state.doc.resolve(mergedCellPos);
const cellSelection = new CellSelection($cell);
const stateWithCellSelection = state.apply(
state.tr.setSelection(cellSelection)
);
// Apply splitCell and collapse
chainTransactions(splitCell, collapseSelection())(
stateWithCellSelection,
dispatch
);
}
return true;
}
// Fallback to standard splitCell for non-cell selections
return chainTransactions(splitCell, collapseSelection())(state, dispatch);
};
}
/**
+5 -1
View File
@@ -313,7 +313,11 @@ export function isMultipleCellSelection(state: EditorState): boolean {
*/
export function isMergedCellSelection(state: EditorState): boolean {
const { selection } = state;
if (selection instanceof CellSelection) {
if (
selection instanceof CellSelection ||
selection instanceof RowSelection ||
selection instanceof ColumnSelection
) {
// Check if any cell in the selection has a colspan or rowspan > 1
let hasMergedCells = false;
selection.forEachCell((cell) => {