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

100% Statements 50/50
85.71% Branches 18/21
100% Functions 3/3
100% Lines 34/34

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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 881x                               1x 15x 15x 15x 7x 7x   14x 14x 1x   13x         15x             2x 2x 4x 4x 4x     2x 2x               1x           17x 22x 22x 5x 5x 10x           10x   5x     22x 26x 26x 24x   26x   22x      
import {
    createTableCell,
    createTableRow,
    trimModelForSelection,
} from 'roosterjs-content-model-dom';
import type {
    ContentModelBlock,
    ContentModelTable,
    ContentModelTableCellFormat,
    IEditor,
    ReadonlyContentModelTable,
} from 'roosterjs-content-model-types';
 
/**
 * @internal
 */
export function getSelectedContentForTable(editor: IEditor): ContentModelBlock[][] {
    const selectedRows: ContentModelBlock[][] = [];
    const selection = editor.getDOMSelection();
    if (selection && (selection?.type !== 'range' || !selection.range.collapsed)) {
        const selectedModel = editor.getContentModelCopy('disconnected');
        trimModelForSelection(selectedModel, selection);
 
        for (const block of selectedModel.blocks) {
            if (block.blockType === 'Table') {
                extractTableCellsContent(block, selectedRows);
            } else {
                selectedRows.push([block as ContentModelBlock]);
            }
        }
    }
 
    return selectedRows;
}
 
function extractTableCellsContent(
    table: ReadonlyContentModelTable,
    selectedRows: ContentModelBlock[][]
) {
    for (const row of table.rows) {
        const rowBlocks: ContentModelBlock[] = [];
        for (const cell of row.cells) {
            Eif (!cell.spanLeft && !cell.spanAbove) {
                rowBlocks.push(...(cell.blocks as ContentModelBlock[]));
            }
        }
        Eif (rowBlocks.length > 0) {
            selectedRows.push(rowBlocks);
        }
    }
}
 
/**
 * @internal
 */
export function insertTableContent(
    table: ContentModelTable,
    contentRows: ContentModelBlock[][],
    colNumber: number,
    customCellFormat?: ContentModelTableCellFormat
) {
    let rowIndex = 0;
    for (const rowBlocks of contentRows) {
        if (!table.rows[rowIndex]) {
            const row = createTableRow();
            for (let i = 0; i < colNumber; i++) {
                const cell = createTableCell(
                    undefined /*spanLeftOrColSpan */,
                    undefined /*spanAboveOrRowSpan */,
                    undefined /* isHeader */,
                    customCellFormat
                );
                row.cells.push(cell);
            }
            table.rows.push(row);
        }
 
        let cellIndex = 0;
        for (const block of rowBlocks) {
            if (cellIndex < table.rows[rowIndex].cells.length) {
                table.rows[rowIndex].cells[cellIndex].blocks = [block];
            }
            cellIndex++;
        }
        rowIndex++;
    }
}