All files / roosterjs-content-model-dom/lib/formatHandlers/list listLevelThreadFormatHandler.ts

100% Statements 21/21
100% Branches 15/15
100% Functions 2/2
100% Lines 20/20

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 491x             1x   375x 147x 294x 147x   147x         139x     147x         197x 197x       197x   197x 109x   109x 75x 34x 25x     109x 109x        
import { isElementOfType } from '../../domUtils/isElementOfType';
import type { FormatHandler } from '../FormatHandler';
import type { ListThreadFormat } from 'roosterjs-content-model-types';
 
/**
 * @internal
 */
export const listLevelThreadFormatHandler: FormatHandler<ListThreadFormat> = {
    parse: (format, element, context) => {
        if (isElementOfType(element, 'ol')) {
            const { listFormat } = context;
            const { threadItemCounts, levels } = listFormat;
            const depth = levels.length;
 
            if (
                element.start == 1 ||
                (typeof threadItemCounts[depth] === 'number' &&
                    element.start != threadItemCounts[depth] + 1)
            ) {
                format.startNumberOverride = element.start;
            }
 
            threadItemCounts[depth] = element.start - 1;
        }
    },
    apply: (format, element, context) => {
        const {
            listFormat: { threadItemCounts, nodeStack },
        } = context;
 
        // The first one is always the parent of list, and minus another one to convert length to index
        // This format applier needs to be executed after new list level is pushed into node stack
        const depth = nodeStack.length - 2;
 
        if (depth >= 0 && isElementOfType(element, 'ol')) {
            const startNumber = format.startNumberOverride;
 
            if (typeof startNumber === 'number') {
                threadItemCounts[depth] = startNumber - 1;
            } else if (typeof threadItemCounts[depth] != 'number') {
                threadItemCounts[depth] = 0;
            }
 
            threadItemCounts.splice(depth + 1);
            element.start = threadItemCounts[depth] + 1;
        }
    },
};