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

100% Statements 29/29
100% Branches 12/12
100% Functions 1/1
100% Lines 25/25

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 45 46 47 48 49 501x                 1x 15x 15x 15x 52x 13x 6x 6x 8x 8x 20x 20x 9x     9x   11x         7x 7x 9x 9x 23x 23x 11x     11x   12x              
import { getSelectedCells, mutateBlock } from 'roosterjs-content-model-dom';
import type {
    ShallowMutableContentModelTable,
    TableCellShiftOperation,
} from 'roosterjs-content-model-types';
 
/**
 * @internal
 */
export function shiftCells(table: ShallowMutableContentModelTable, value: TableCellShiftOperation) {
    const sel = getSelectedCells(table);
    const rows = table.rows;
    if (sel) {
        const { lastColumn, lastRow, firstColumn, firstRow } = sel;
        if (value == 'shiftCellsLeft') {
            const selectionLength = lastColumn - firstColumn + 1;
            for (let i = firstRow; i <= lastRow; i++) {
                const cellsNumber = rows[i].cells.length;
                for (let j = firstColumn; j < cellsNumber; j++) {
                    const nextCellIndex = j + selectionLength;
                    if (rows[i].cells[nextCellIndex]) {
                        mutateBlock(rows[i].cells[j]).blocks = [
                            ...rows[i].cells[nextCellIndex].blocks,
                        ];
                        mutateBlock(rows[i].cells[nextCellIndex]).blocks = [];
                    } else {
                        mutateBlock(rows[i].cells[j]).blocks = [];
                    }
                }
            }
        } else {
            const selectionLength = lastRow - firstRow + 1;
            for (let j = firstColumn; j <= lastColumn; j++) {
                const cellsNumber = rows.length;
                for (let i = firstRow; i < cellsNumber; i++) {
                    const nextCellIndex = i + selectionLength;
                    if (rows[nextCellIndex]?.cells[j]) {
                        mutateBlock(rows[i].cells[j]).blocks = [
                            ...rows[nextCellIndex].cells[j].blocks,
                        ];
                        mutateBlock(rows[nextCellIndex].cells[j]).blocks = [];
                    } else {
                        mutateBlock(rows[i].cells[j]).blocks = [];
                    }
                }
            }
        }
    }
}