All files / roosterjs-content-model-core/lib/override listMetadataApplier.ts

100% Statements 30/30
97.83% Branches 45/46
100% Functions 4/4
100% Lines 30/30

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 98 99 100 101 102 103 104 105 106 107 1081x                           1x             109x 79x 79x   79x   30x           214x       214x           1x           292x   292x 291x 291x   291x 121x 51x           70x                   1x           208x   208x 207x 207x   207x 93x   58x     58x           58x 56x     35x            
import {
    ListMetadataDefinition,
    OrderedListStyleMap,
    UnorderedListStyleMap,
    getAutoListStyleType,
    getOrderedListNumberStr,
} from 'roosterjs-content-model-dom';
import type {
    ContentModelListItemFormat,
    ContentModelListItemLevelFormat,
    ListMetadataFormat,
    MetadataApplier,
} from 'roosterjs-content-model-types';
 
const OrderedMapPlaceholderRegex = /\$\{(\w+)\}/;
 
function getListStyleValue(
    listType: 'OL' | 'UL',
    listStyleType: number,
    listNumber?: number
): string | undefined {
    if (listType == 'OL') {
        const numberStr = getOrderedListNumberStr(listStyleType, listNumber ?? 1);
        const template = OrderedListStyleMap[listStyleType];
 
        return template ? template.replace(OrderedMapPlaceholderRegex, numberStr) : undefined;
    } else {
        return UnorderedListStyleMap[listStyleType];
    }
}
 
function shouldApplyToItem(listStyleType: number, listType: 'OL' | 'UL') {
    const style =
        listType == 'OL'
            ? OrderedListStyleMap[listStyleType]
            : UnorderedListStyleMap[listStyleType];
 
    return style?.indexOf('"') >= 0;
}
 
/**
 * @internal
 */
export const listItemMetadataApplier: MetadataApplier<
    ListMetadataFormat,
    ContentModelListItemFormat
> = {
    metadataDefinition: ListMetadataDefinition,
    applierFunction: (metadata, format, context) => {
        const depth = context.listFormat.nodeStack.length - 2; // Minus two for the parent element and convert length to index
 
        if (depth >= 0) {
            const listType = context.listFormat.nodeStack[depth + 1].listType ?? 'OL';
            const listStyleType = getAutoListStyleType(listType, metadata ?? {}, depth);
 
            if (listStyleType !== undefined) {
                if (shouldApplyToItem(listStyleType, listType)) {
                    format.listStyleType = getListStyleValue(
                        listType,
                        listStyleType,
                        context.listFormat.threadItemCounts[depth]
                    );
                } else {
                    delete format.listStyleType;
                }
            }
        }
    },
};
 
/**
 * @internal
 */
export const listLevelMetadataApplier: MetadataApplier<
    ListMetadataFormat,
    ContentModelListItemLevelFormat
> = {
    metadataDefinition: ListMetadataDefinition,
    applierFunction: (metadata, format, context) => {
        const depth = context.listFormat.nodeStack.length - 2; // Minus two for the parent element and convert length to index
 
        if (depth >= 0) {
            const listType = context.listFormat.nodeStack[depth + 1].listType ?? 'OL';
            const listStyleType = getAutoListStyleType(listType, metadata ?? {}, depth);
 
            if (listStyleType !== undefined) {
                if (!shouldApplyToItem(listStyleType, listType)) {
                    const listNumber =
                        context.listFormat.threadItemCounts[depth] > 0
                            ? context.listFormat.threadItemCounts[depth]
                            : 1;
                    const listStyleTypeFormat = getListStyleValue(
                        listType,
                        listStyleType,
                        listNumber
                    );
 
                    if (listStyleTypeFormat) {
                        format.listStyleType = listStyleTypeFormat;
                    }
                } else {
                    delete format.listStyleType;
                }
            }
        }
    },
};