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

88% Statements 22/25
73.68% Branches 14/19
75% Functions 3/4
87.5% Lines 21/24

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 54 55 56 57 58 59 60 61 62 63 64 65 661x           1x         1x 8x   8x 6x 9x 9x   9x   9x     6x                     9x 9x       18x             18x   18x 4x   14x     18x       9x 9x          
import { createTableCell, getSelectedCells, mutateBlock } from 'roosterjs-content-model-dom';
import type {
    ContentModelTableRow,
    ShallowMutableContentModelTable,
} from 'roosterjs-content-model-types';
 
const MIN_HEIGHT = 22;
 
/**
 * @internal
 */
export function splitTableCellVertically(table: ShallowMutableContentModelTable) {
    const sel = getSelectedCells(table);
 
    if (sel) {
        for (let rowIndex = sel.lastRow; rowIndex >= sel.firstRow; rowIndex--) {
            const row = table.rows[rowIndex];
            const belowRow = table.rows[rowIndex + 1];
 
            row.cells.forEach(mutateBlock);
 
            Iif (
                belowRow?.cells.every(
                    (belowCell, colIndex) =>
                        colIndex < sel.firstColumn ||
                        colIndex > sel.lastColumn ||
                        belowCell.spanAbove
                )
            ) {
                belowRow.cells.forEach((belowCell, colIndex) => {
                    if (colIndex >= sel.firstColumn && colIndex <= sel.lastColumn) {
                        mutateBlock(belowCell).spanAbove = false;
                    }
                });
            } else {
                const newHeight = Math.max((row.height /= 2), MIN_HEIGHT);
                const newRow: ContentModelTableRow = {
                    format: { ...row.format },
                    height: newHeight,
                    cells: row.cells.map((cell, colIndex) => {
                        const newCell = createTableCell(
                            cell.spanLeft,
                            cell.spanAbove,
                            cell.isHeader,
                            cell.format
                        );
 
                        newCell.dataset = { ...cell.dataset };
 
                        if (colIndex < sel.firstColumn || colIndex > sel.lastColumn) {
                            newCell.spanAbove = true;
                        } else {
                            newCell.isSelected = cell.isSelected;
                        }
 
                        return newCell;
                    }),
                };
 
                row.height = newHeight;
                table.rows.splice(rowIndex + 1, 0, newRow);
            }
        }
    }
}