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

100% Statements 15/15
92.86% Branches 13/14
100% Functions 2/2
100% Lines 14/14

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 451x 1x                     1x       13x 13x   13x 9x 9x 13x 29x   29x             29x   29x   13x                
import { clearSelectedCells } from './clearSelectedCells';
import { createTableCell, getSelectedCells } from 'roosterjs-content-model-dom';
import type {
    ShallowMutableContentModelTable,
    TableHorizontalInsertOperation,
} from 'roosterjs-content-model-types';
 
/**
 * Insert a column to the table
 * @param table The table model where the column is to be inserted
 * @param operation The operation to be performed
 */
export function insertTableColumn(
    table: ShallowMutableContentModelTable,
    operation: TableHorizontalInsertOperation
) {
    const sel = getSelectedCells(table);
    const insertLeft = operation == 'insertLeft';
 
    if (sel) {
        clearSelectedCells(table, sel);
        for (let i = sel?.firstColumn; i <= sel.lastColumn; i++) {
            table.rows.forEach(row => {
                const cell = row.cells[insertLeft ? sel.firstColumn : sel.lastColumn];
 
                const newCell = createTableCell(
                    cell.spanLeft,
                    cell.spanAbove,
                    cell.isHeader,
                    cell.format,
                    cell.dataset
                );
                newCell.isSelected = true;
 
                row.cells.splice(insertLeft ? sel.firstColumn : sel.lastColumn + 1, 0, newCell);
            });
            table.widths.splice(
                insertLeft ? sel.firstColumn : sel.lastColumn + 1,
                0,
                table.widths[insertLeft ? sel.firstColumn : sel.lastColumn]
            );
        }
    }
}