All files / roosterjs-content-model-plugins/lib/autoFormat/list getNumberingListStyle.ts

100% Statements 62/62
81.08% Branches 60/74
100% Functions 12/12
100% Lines 62/62

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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 1821x 1x   1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x     1x           1x           1x           1x 1x 1x   1x 52x 17x 35x 17x           8x 9x       9x   18x 18x           8x 10x       10x         1x 17x 9x 10x 8x 8x     1x             1x             1x             1x             1x             1x         54x       54x 52x 52x 52x   2x           1x         63x 63x     63x 63x 63x   63x           9x     54x             54x                 54x       5x    
import { getIndex } from './getIndex';
import { NumberingListType } from 'roosterjs-content-model-dom';
 
const enum NumberingTypes {
    Decimal = 1,
    LowerAlpha = 2,
    UpperAlpha = 3,
    LowerRoman = 4,
    UpperRoman = 5,
}
 
const enum Character {
    Dot = 1,
    Dash = 2,
    Parenthesis = 3,
    DoubleParenthesis = 4,
}
 
const characters: Record<string, number> = {
    '.': Character.Dot,
    '-': Character.Dash,
    ')': Character.Parenthesis,
};
 
const lowerRomanTypes = [
    NumberingListType.LowerRoman,
    NumberingListType.LowerRomanDash,
    NumberingListType.LowerRomanDoubleParenthesis,
    NumberingListType.LowerRomanParenthesis,
];
const upperRomanTypes = [
    NumberingListType.UpperRoman,
    NumberingListType.UpperRomanDash,
    NumberingListType.UpperRomanDoubleParenthesis,
    NumberingListType.UpperRomanParenthesis,
];
const numberingTriggers = ['1', 'a', 'A', 'I', 'i'];
const lowerRomanNumbers = ['i', 'v', 'x', 'l', 'c', 'd', 'm'];
const upperRomanNumbers = ['I', 'V', 'X', 'L', 'C', 'D', 'M'];
 
const identifyNumberingType = (text: string, previousListStyle?: number) => {
    if (!isNaN(parseInt(text))) {
        return NumberingTypes.Decimal;
    } else if (/[a-z]+/g.test(text)) {
        if (
            (previousListStyle === NumberingTypes.LowerRoman &&
                lowerRomanTypes.indexOf(previousListStyle) > -1 &&
                lowerRomanNumbers.indexOf(text[0]) > -1) ||
            (!previousListStyle && text === 'i')
        ) {
            return NumberingTypes.LowerRoman;
        } else Eif (
            previousListStyle === NumberingTypes.LowerAlpha ||
            (!previousListStyle && text === 'a')
        ) {
            return NumberingTypes.LowerAlpha;
        }
    } else Eif (/[A-Z]+/g.test(text)) {
        if (
            (previousListStyle == NumberingTypes.UpperRoman &&
                upperRomanTypes.indexOf(previousListStyle) > -1 &&
                upperRomanNumbers.indexOf(text[0]) > -1) ||
            (!previousListStyle && text === 'I')
        ) {
            return NumberingTypes.UpperRoman;
        } else Eif (
            previousListStyle == NumberingTypes.UpperAlpha ||
            (!previousListStyle && text === 'A')
        ) {
            return NumberingTypes.UpperAlpha;
        }
    }
};
 
const numberingListTypes: Record<number, (char: number) => number | undefined> = {
    [NumberingTypes.Decimal]: char => DecimalsTypes[char] || undefined,
    [NumberingTypes.LowerAlpha]: char => LowerAlphaTypes[char] || undefined,
    [NumberingTypes.UpperAlpha]: char => UpperAlphaTypes[char] || undefined,
    [NumberingTypes.LowerRoman]: char => LowerRomanTypes[char] || undefined,
    [NumberingTypes.UpperRoman]: char => UpperRomanTypes[char] || undefined,
};
 
const UpperRomanTypes: Record<number, number> = {
    [Character.Dot]: NumberingListType.UpperRoman,
    [Character.Dash]: NumberingListType.UpperRomanDash,
    [Character.Parenthesis]: NumberingListType.UpperRomanParenthesis,
    [Character.DoubleParenthesis]: NumberingListType.UpperRomanDoubleParenthesis,
};
 
const LowerRomanTypes: Record<number, number> = {
    [Character.Dot]: NumberingListType.LowerRoman,
    [Character.Dash]: NumberingListType.LowerRomanDash,
    [Character.Parenthesis]: NumberingListType.LowerRomanParenthesis,
    [Character.DoubleParenthesis]: NumberingListType.LowerRomanDoubleParenthesis,
};
 
const UpperAlphaTypes: Record<number, number> = {
    [Character.Dot]: NumberingListType.UpperAlpha,
    [Character.Dash]: NumberingListType.UpperAlphaDash,
    [Character.Parenthesis]: NumberingListType.UpperAlphaParenthesis,
    [Character.DoubleParenthesis]: NumberingListType.UpperAlphaDoubleParenthesis,
};
 
const LowerAlphaTypes: Record<number, number> = {
    [Character.Dot]: NumberingListType.LowerAlpha,
    [Character.Dash]: NumberingListType.LowerAlphaDash,
    [Character.Parenthesis]: NumberingListType.LowerAlphaParenthesis,
    [Character.DoubleParenthesis]: NumberingListType.LowerAlphaDoubleParenthesis,
};
 
const DecimalsTypes: Record<number, number> = {
    [Character.Dot]: NumberingListType.Decimal,
    [Character.Dash]: NumberingListType.DecimalDash,
    [Character.Parenthesis]: NumberingListType.DecimalParenthesis,
    [Character.DoubleParenthesis]: NumberingListType.DecimalDoubleParenthesis,
};
 
const identifyNumberingListType = (
    numbering: string,
    isDoubleParenthesis: boolean,
    previousListStyle?: number
): number | undefined => {
    const separatorCharacter = isDoubleParenthesis
        ? Character.DoubleParenthesis
        : characters[numbering[numbering.length - 1]];
    // if separator is not valid, no need to check if the number is valid.
    if (separatorCharacter) {
        const number = isDoubleParenthesis ? numbering.slice(1, -1) : numbering.slice(0, -1);
        const numberingType = identifyNumberingType(number, previousListStyle);
        return numberingType ? numberingListTypes[numberingType](separatorCharacter) : undefined;
    }
    return undefined;
};
 
/**
 * @internal
 */
export function getNumberingListStyle(
    textBeforeCursor: string,
    previousListIndex?: number,
    previousListStyle?: number
): number | undefined {
    const trigger = textBeforeCursor.trim();
    const isDoubleParenthesis = trigger[0] === '(' && trigger[trigger.length - 1] === ')';
    //Only the staring items ['1', 'a', 'A', 'I', 'i'] must trigger a new list. All the other triggers is used to keep the list chain.
    //The index is always the characters before the last character
    const listIndex = isDoubleParenthesis ? trigger.slice(1, -1) : trigger.slice(0, -1);
    const index = getIndex(listIndex);
    const isContinuosList = numberingTriggers.indexOf(listIndex) < 0;
 
    if (
        !index ||
        index < 1 ||
        (!previousListIndex && isContinuosList) ||
        (previousListIndex && isContinuosList && !canAppendList(index, previousListIndex))
    ) {
        return undefined;
    }
 
    const numberingType = isValidNumbering(listIndex)
        ? identifyNumberingListType(
              trigger,
              isDoubleParenthesis,
              isContinuosList ? previousListStyle : undefined
          )
        : undefined;
    return numberingType;
}
 
/**
 * Check if index has only numbers or only letters to avoid sequence of character such 1:1. trigger a list.
 * @param index
 * @returns
 */
function isValidNumbering(index: string) {
    return Number(index) || /^[A-Za-z\s]*$/.test(index);
}
 
function canAppendList(index?: number, previousListIndex?: number) {
    return previousListIndex && index && previousListIndex + 1 === index;
}