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

92.31% Statements 24/26
61.11% Branches 11/18
85.71% Functions 6/7
91.67% Lines 22/24

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 861x                             1x                 1x                 1x       4x 12x             1x       4x 12x   12x 12x 12x 12x                 8x   8x 6x 12x 24x 24x   24x 24x   24x                    
import {
    getSelectedCells,
    mutateBlock,
    updateTableCellMetadata,
} from 'roosterjs-content-model-dom';
import type {
    ShallowMutableContentModelTable,
    ShallowMutableContentModelTableCell,
    TableCellHorizontalAlignOperation,
    TableCellVerticalAlignOperation,
} from 'roosterjs-content-model-types';
 
const TextAlignValueMap: Partial<Record<
    TableCellHorizontalAlignOperation,
    'start' | 'center' | 'end'
>> = {
    alignCellLeft: 'start',
    alignCellCenter: 'center',
    alignCellRight: 'end',
};
 
const VerticalAlignValueMap: Partial<Record<
    TableCellVerticalAlignOperation,
    'top' | 'middle' | 'bottom'
>> = {
    alignCellTop: 'top',
    alignCellMiddle: 'middle',
    alignCellBottom: 'bottom',
};
 
/**
 * @internal
 */
export function alignTableCellHorizontally(
    table: ShallowMutableContentModelTable,
    operation: TableCellHorizontalAlignOperation
) {
    alignTableCellInternal(table, cell => {
        cell.format.textAlign = TextAlignValueMap[operation];
    });
}
 
/**
 * @internal
 */
export function alignTableCellVertically(
    table: ShallowMutableContentModelTable,
    operation: TableCellVerticalAlignOperation
) {
    alignTableCellInternal(table, cell => {
        cell.format.verticalAlign = VerticalAlignValueMap[operation];
 
        updateTableCellMetadata(cell, metadata => {
            metadata = metadata || {};
            metadata.vAlignOverride = true;
            return metadata;
        });
    });
}
 
function alignTableCellInternal(
    table: ShallowMutableContentModelTable,
    callback: (cell: ShallowMutableContentModelTableCell) => void
) {
    const sel = getSelectedCells(table);
 
    if (sel) {
        for (let rowIndex = sel.firstRow; rowIndex <= sel.lastRow; rowIndex++) {
            for (let colIndex = sel.firstColumn; colIndex <= sel.lastColumn; colIndex++) {
                const cell = table.rows[rowIndex]?.cells[colIndex];
                const format = cell?.format;
 
                Eif (format) {
                    callback(mutateBlock(cell));
 
                    cell.blocks.forEach(block => {
                        if (block.blockType === 'Paragraph' && block.format.textAlign) {
                            delete mutateBlock(block).format.textAlign;
                        }
                    });
                }
            }
        }
    }
}