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

85.71% Statements 24/28
71.43% Branches 15/21
75% Functions 3/4
84.62% Lines 22/26

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 641x     1x         1x 9x   9x 10x 10x     10x                         10x 19x 19x 19x   19x   19x             19x   19x 4x   15x   19x   19x       10x   10x          
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 mutableCell = mutateBlock(cell);
 
                        delete mutableCell.format.width;
 
                        const newCell = createTableCell(
                            cell.spanLeft,
                            cell.spanAbove,
                            cell.isHeader,
                            mutableCell.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);
            }
        }
    }
}