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

100% Statements 32/32
87.5% Branches 21/24
100% Functions 6/6
100% Lines 27/27

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 88 89 90 91 92 93 94 95 96 97 981x                             1x                                   1x                 1x       13x   27x 27x 27x 8x 1x 7x 6x                 1x       4x 12x   12x 12x 12x 12x                 17x   17x 15x 23x 39x 39x   39x 39x            
import {
    getSelectedCells,
    mutateBlock,
    updateTableCellMetadata,
} from 'roosterjs-content-model-dom';
import type {
    ShallowMutableContentModelTable,
    ShallowMutableContentModelTableCell,
    TableCellHorizontalAlignOperation,
    TableCellVerticalAlignOperation,
} from 'roosterjs-content-model-types';
 
const TextAlignValueMap: Record<
    TableCellHorizontalAlignOperation,
    Partial<Record<'ltr' | 'rtl', 'start' | 'center' | 'end'>>
> = {
    alignCellLeft: {
        ltr: 'start',
        rtl: 'end',
    },
    alignCellCenter: {
        ltr: 'center',
        rtl: 'center',
    },
    alignCellRight: {
        ltr: 'end',
        rtl: 'start',
    },
};
 
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 => {
        const alignment =
            TextAlignValueMap[operation][cell.format.direction == 'rtl' ? 'rtl' : 'ltr'];
        cell.format.textAlign = alignment;
        for (const block of cell.blocks) {
            if (block.blockType === 'Paragraph' && block.format.textAlign) {
                delete mutateBlock(block).format.textAlign;
            } else if (block.blockType == 'BlockGroup' && block.blockGroupType == 'ListItem') {
                mutateBlock(block).format.textAlign = alignment;
            }
        }
    });
}
 
/**
 * @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));
                }
            }
        }
    }
}