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

100% Statements 13/13
90% Branches 9/10
100% Functions 1/1
100% Lines 11/11

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 391x                     1x       25x           24x 37x   37x 67x   67x 67x 35x     67x            
import { mutateBlock, setSelection } from 'roosterjs-content-model-dom';
import type {
    ReadonlyContentModelTable,
    TableSelectionCoordinates,
} from 'roosterjs-content-model-types';
 
/**
 * Clear selection of a table.
 * @param table The table model where the selection is to be cleared
 * @param sel The selection coordinates to be cleared
 */
export function clearSelectedCells(
    table: ReadonlyContentModelTable,
    sel: TableSelectionCoordinates
) {
    if (
        sel.firstColumn >= 0 &&
        sel.firstRow >= 0 &&
        sel.lastRow < table.rows.length &&
        sel.lastColumn < table.rows[sel.lastRow].cells.length
    ) {
        for (let i = sel.firstRow; i <= sel.lastRow; i++) {
            const row = table.rows[i];
 
            for (let j = sel.firstColumn; j <= sel.lastColumn; j++) {
                const cell = row.cells[j];
 
                Eif (cell) {
                    if (cell.isSelected) {
                        mutateBlock(cell).isSelected = false;
                    }
 
                    setSelection(cell);
                }
            }
        }
    }
}