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

100% Statements 14/14
100% Branches 6/6
100% Functions 2/2
100% Lines 13/13

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 441x 1x                     1x       14x 14x   14x 10x   10x 14x   14x     28x             28x 28x              
import { clearSelectedCells } from './clearSelectedCells';
import { createTableCell, getSelectedCells } from 'roosterjs-content-model-dom';
import type {
    ShallowMutableContentModelTable,
    TableVerticalInsertOperation,
} from 'roosterjs-content-model-types';
 
/**
 * Insert a row to the table
 * @param table The table model where the row is to be inserted
 * @param operation The operation to be performed
 */
export function insertTableRow(
    table: ShallowMutableContentModelTable,
    operation: TableVerticalInsertOperation
) {
    const sel = getSelectedCells(table);
    const insertAbove = operation == 'insertAbove';
 
    if (sel) {
        clearSelectedCells(table, sel);
 
        for (let i = sel.firstRow; i <= sel.lastRow; i++) {
            const sourceRow = table.rows[insertAbove ? sel.firstRow : sel.lastRow];
 
            table.rows.splice(insertAbove ? sel.firstRow : sel.lastRow + 1, 0, {
                format: { ...sourceRow.format },
                cells: sourceRow.cells.map(cell => {
                    const newCell = createTableCell(
                        cell.spanLeft,
                        cell.spanAbove,
                        cell.isHeader,
                        cell.format,
                        cell.dataset
                    );
                    newCell.isSelected = true;
                    return newCell;
                }),
                height: sourceRow.height,
            });
        }
    }
}