All files / roosterjs-content-model-dom/lib/modelApi/selection getSelectedCells.ts

100% Statements 18/18
100% Branches 8/8
100% Functions 3/3
100% Lines 18/18

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 411x                   1x     131x 131x 131x 131x 131x   131x 258x 612x 193x   193x 100x     193x 100x     193x 193x         131x    
import { hasSelectionInBlockGroup } from '../selection/hasSelectionInBlockGroup';
import type {
    ReadonlyContentModelTable,
    TableSelectionCoordinates,
} from 'roosterjs-content-model-types';
 
/**
 * Get selection coordinates of a table. If there is no selection, return null
 * @param table The table model to get selection from
 */
export function getSelectedCells(
    table: ReadonlyContentModelTable
): TableSelectionCoordinates | null {
    let firstRow = -1;
    let firstColumn = -1;
    let lastRow = -1;
    let lastColumn = -1;
    let hasSelection = false;
 
    table.rows.forEach((row, rowIndex) =>
        row.cells.forEach((cell, colIndex) => {
            if (hasSelectionInBlockGroup(cell)) {
                hasSelection = true;
 
                if (firstRow < 0) {
                    firstRow = rowIndex;
                }
 
                if (firstColumn < 0) {
                    firstColumn = colIndex;
                }
 
                lastRow = Math.max(lastRow, rowIndex);
                lastColumn = Math.max(lastColumn, colIndex);
            }
        })
    );
 
    return hasSelection ? { firstRow, firstColumn, lastRow, lastColumn } : null;
}