All files / roosterjs-content-model-core/lib/coreApi/setDOMSelection toggleTableSelection.ts

100% Statements 12/12
100% Branches 8/8
100% Functions 1/1
100% Lines 12/12

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 341x 1x                   1x 8x   8x 6x 4x   10x 2x 2x       2x         2x        
import { parseTableCells } from 'roosterjs-content-model-dom';
import { removeTableCellsStyle, setTableCellsStyle } from './setTableCellsStyle';
 
import type { EditorCore, TableCellCoordinate } from 'roosterjs-content-model-types';
 
/**
 * @internal
 * Toggle table selection styles on/off
 * @param core The EditorCore object
 * @param isHiding True to hide the table selection background, false to show it
 */
export function toggleTableSelection(core: EditorCore, isHiding: boolean) {
    const selection = core.selection.selection;
 
    if (selection?.type === 'table') {
        if (isHiding) {
            removeTableCellsStyle(core);
        } else {
            const { table, firstColumn, firstRow, lastColumn, lastRow } = selection;
            const parsedTable = parseTableCells(table);
            const firstCell: TableCellCoordinate = {
                row: Math.min(firstRow, lastRow),
                col: Math.min(firstColumn, lastColumn),
            };
            const lastCell: TableCellCoordinate = {
                row: Math.max(firstRow, lastRow),
                col: Math.max(firstColumn, lastColumn),
            };
 
            setTableCellsStyle(core, table, parsedTable, firstCell, lastCell);
        }
    }
}