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

100% Statements 26/26
82.35% Branches 14/17
100% Functions 3/3
100% Lines 25/25

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 771x 1x 1x                               1x       15x 15x     15x 15x 12x 36x 12x 12x 12x   12x   3x     1x             12x 12x 12x 2x   12x                           12x           12x 24x 12x      
import { getListTypeStyle } from './getListTypeStyle';
import { getOperationalBlocks, isBlockGroupOfType } from 'roosterjs-content-model-dom';
import {
    getListAnnounceData,
    setListType,
    setModelListStartNumber,
    setModelListStyle,
} from 'roosterjs-content-model-api';
import type {
    ContentModelListItem,
    FormatContentModelContext,
    ReadonlyContentModelDocument,
    ShallowMutableContentModelParagraph,
} from 'roosterjs-content-model-types';
 
/**
 * @internal
 */
export function keyboardListTrigger(
    model: ReadonlyContentModelDocument,
    paragraph: ShallowMutableContentModelParagraph,
    context: FormatContentModelContext,
    IshouldSearchForBullet: boolean = true,
    IshouldSearchForNumbering: boolean = true,
    removeListMargins?: boolean
) {
    const listStyleType = getListTypeStyle(model, shouldSearchForBullet, shouldSearchForNumbering);
    if (listStyleType) {
        paragraph.segments.splice(0, 1);
        const { listType, styleType, index } = listStyleType;
        triggerList(model, listType, styleType, index, removeListMargins);
        context.canUndoByBackspace = true;
        setAnnounceData(model, context);
 
        return true;
    }
    return false;
}
 
const triggerList = (
    model: ReadonlyContentModelDocument,
    listType: 'OL' | 'UL',
    styleType: number,
    index?: number,
    removeListMargins?: boolean
) => {
    setListType(model, listType, removeListMargins);
    const isOrderedList = listType == 'OL';
    if (index && index > 0 && isOrderedList) {
        setModelListStartNumber(model, index);
    }
    setModelListStyle(
        model,
        isOrderedList
            ? {
                  orderedStyleType: styleType,
                  applyListStyleFromLevel: false,
              }
            : {
                  unorderedStyleType: styleType,
                  applyListStyleFromLevel: false,
              }
    );
};
function setAnnounceData(model: ReadonlyContentModelDocument, context: FormatContentModelContext) {
    const [paragraphOrListItems] = getOperationalBlocks<ContentModelListItem>(
        model,
        ['ListItem'],
        [] // Set stop types to be empty so we can find list items even cross the boundary of table, then we can always operation on the list item if any
    );
 
    Eif (paragraphOrListItems && isBlockGroupOfType(paragraphOrListItems.block, 'ListItem')) {
        const { path, block } = paragraphOrListItems;
        context.announceData = getListAnnounceData([block, ...path]);
    }
}