All files / roosterjs-content-model-api/lib/modelApi/block setModelAlignment.ts

100% Statements 22/22
92.86% Branches 13/14
100% Functions 3/3
100% Lines 21/21

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 1x                   1x                                           1x                                   1x       31x   31x           39x 39x 39x   39x 9x       30x 30x 12x 27x 27x     30x 30x       31x    
import { alignTable } from '../table/alignTable';
import { getOperationalBlocks, mutateBlock } from 'roosterjs-content-model-dom';
import { splitSelectedParagraphByBr } from './splitSelectedParagraphByBr';
import type {
    ContentModelListItem,
    ReadonlyContentModelDocument,
    TableAlignOperation,
} from 'roosterjs-content-model-types';
 
const ResultMap: Record<
    'left' | 'center' | 'right' | 'justify',
    Record<'ltr' | 'rtl', 'start' | 'center' | 'end' | 'justify'>
> = {
    left: {
        ltr: 'start',
        rtl: 'end',
    },
    center: {
        ltr: 'center',
        rtl: 'center',
    },
    right: {
        ltr: 'end',
        rtl: 'start',
    },
    justify: {
        ltr: 'justify',
        rtl: 'justify',
    },
};
 
const TableAlignMap: Record<
    'left' | 'center' | 'right',
    Record<'ltr' | 'rtl', TableAlignOperation>
> = {
    left: {
        ltr: 'alignLeft',
        rtl: 'alignRight',
    },
    center: {
        ltr: 'alignCenter',
        rtl: 'alignCenter',
    },
    right: {
        ltr: 'alignRight',
        rtl: 'alignLeft',
    },
};
 
/**
 * @internal
 */
export function setModelAlignment(
    model: ReadonlyContentModelDocument,
    alignment: 'left' | 'center' | 'right' | 'justify'
) {
    splitSelectedParagraphByBr(model);
 
    const paragraphOrListItemOrTable = getOperationalBlocks<ContentModelListItem>(
        model,
        ['ListItem'],
        ['TableCell']
    );
 
    paragraphOrListItemOrTable.forEach(({ block: readonlyBlock }) => {
        const block = mutateBlock(readonlyBlock);
        const newAlignment = ResultMap[alignment][block.format.direction == 'rtl' ? 'rtl' : 'ltr'];
 
        if (block.blockType === 'Table' && alignment !== 'justify') {
            alignTable(
                block,
                TableAlignMap[alignment][block.format.direction == 'rtl' ? 'rtl' : 'ltr']
            );
        } else Eif (block) {
            if (block.blockType === 'BlockGroup' && block.blockGroupType === 'ListItem') {
                block.blocks.forEach(b => {
                    const { format } = mutateBlock(b);
                    format.textAlign = newAlignment;
                });
            }
            const { format } = block;
            format.textAlign = newAlignment;
        }
    });
 
    return paragraphOrListItemOrTable.length > 0;
}