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

84.62% Statements 22/26
71.43% Branches 15/21
75% Functions 3/4
83.33% Lines 20/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 601x     1x         1x 8x   8x 9x 9x     9x                         9x 18x 18x 18x             18x   18x 4x   14x   18x   18x       9x   9x          
import { createTableCell, getSelectedCells, mutateBlock } from 'roosterjs-content-model-dom';
import type { ShallowMutableContentModelTable } from 'roosterjs-content-model-types';
 
const MIN_WIDTH = 30;
 
/**
 * @internal
 */
export function splitTableCellHorizontally(table: ShallowMutableContentModelTable) {
    const sel = getSelectedCells(table);
 
    if (sel) {
        for (let colIndex = sel.lastColumn; colIndex >= sel.firstColumn; colIndex--) {
            Iif (
                table.rows.every(
                    (row, rowIndex) =>
                        rowIndex < sel.firstRow ||
                        rowIndex > sel.lastRow ||
                        row.cells[colIndex + 1]?.spanLeft
                )
            ) {
                table.rows.forEach((row, rowIndex) => {
                    mutateBlock(row.cells[colIndex]);
 
                    if (rowIndex >= sel.firstRow && rowIndex <= sel.lastRow) {
                        mutateBlock(row.cells[colIndex + 1]).spanLeft = false;
                    }
                });
            } else {
                table.rows.forEach((row, rowIndex) => {
                    const cell = row.cells[colIndex];
                    Eif (cell) {
                        const newCell = createTableCell(
                            cell.spanLeft,
                            cell.spanAbove,
                            cell.isHeader,
                            cell.format
                        );
 
                        newCell.dataset = { ...cell.dataset };
 
                        if (rowIndex < sel.firstRow || rowIndex > sel.lastRow) {
                            newCell.spanLeft = true;
                        } else {
                            newCell.isSelected = cell.isSelected;
                        }
                        row.cells.splice(colIndex + 1, 0, newCell);
 
                        mutateBlock(row.cells[colIndex]);
                    }
                });
 
                const newWidth = Math.max(table.widths[colIndex] / 2, MIN_WIDTH);
 
                table.widths.splice(colIndex, 1, newWidth, newWidth);
            }
        }
    }
}