All files / roosterjs-content-model-api/lib/modelApi/table mergeTableRow.ts

100% Statements 20/20
76.92% Branches 20/26
100% Functions 1/1
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 541x 1x                 1x       10x 10x   10x 6x   6x 6x 10x   10x                   10x   10x   10x 12x 12x     10x   10x 10x              
import { canMergeCells } from './canMergeCells';
import { getSelectedCells, mutateBlock } from 'roosterjs-content-model-dom';
import type {
    ShallowMutableContentModelTable,
    TableVerticalMergeOperation,
} from 'roosterjs-content-model-types';
 
/**
 * @internal
 */
export function mergeTableRow(
    table: ShallowMutableContentModelTable,
    operation: TableVerticalMergeOperation
) {
    const sel = getSelectedCells(table);
    const mergeAbove = operation == 'mergeAbove';
 
    if (sel) {
        const mergingRowIndex = mergeAbove ? sel.firstRow : sel.lastRow + 1;
 
        Eif (mergingRowIndex > 0 && mergingRowIndex < table.rows.length) {
            for (let colIndex = sel.firstColumn; colIndex <= sel.lastColumn; colIndex++) {
                const cell = table.rows[mergingRowIndex].cells[colIndex];
 
                Eif (
                    cell &&
                    canMergeCells(
                        table.rows,
                        mergingRowIndex - 1,
                        colIndex,
                        mergingRowIndex,
                        colIndex
                    )
                ) {
                    mutateBlock(cell).spanAbove = true;
 
                    let newSelectedRow = mergingRowIndex;
 
                    while (table.rows[newSelectedRow]?.cells[colIndex]?.spanAbove) {
                        mutateBlock(table.rows[newSelectedRow].cells[colIndex]);
                        newSelectedRow--;
                    }
 
                    const newCell = table.rows[newSelectedRow]?.cells[colIndex];
 
                    Eif (newCell) {
                        mutateBlock(newCell).isSelected = true;
                    }
                }
            }
        }
    }
}