All files / roosterjs-content-model-plugins/lib/edit/inputSteps handleEnterOnList.ts

100% Statements 44/44
89.29% Branches 25/28
100% Functions 6/6
100% Lines 43/43

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 1241x 1x 1x                                         1x 76x   38x 38x 38x           38x 38x   38x 22x   22x 7x   15x   15x 15x             22x         1x 22x     1x 20x               1x         15x 15x 15x 15x 15x   15x 15x         15x   15x   15x   15x 2x         15x 15x   15x 11x     15x     1x 15x 15x                      
import { getListAnnounceData } from 'roosterjs-content-model-api';
import { splitParagraph } from '../utils/splitParagraph';
import {
    copyFormat,
    createListItem,
    createListLevel,
    getClosestAncestorBlockGroupIndex,
    ListFormats,
    mutateBlock,
} from 'roosterjs-content-model-dom';
import type {
    ContentModelBlockFormat,
    DeleteSelectionStep,
    ReadonlyContentModelBlock,
    ReadonlyContentModelBlockGroup,
    ReadonlyContentModelListItem,
    ShallowMutableContentModelListItem,
    ValidDeleteSelectionContext,
} from 'roosterjs-content-model-types';
 
/**
 * @internal
 */
export const handleEnterOnList: DeleteSelectionStep = context => {
    const { deleteResult, insertPoint } = context;
 
    Eif (deleteResult == 'notDeleted' || deleteResult == 'nothingToDelete') {
        const { path } = insertPoint;
        const index = getClosestAncestorBlockGroupIndex(
            path,
            ['ListItem'],
            ['TableCell', 'FormatContainer']
        );
 
        const readonlyListItem = path[index];
        const listParent = path[index + 1];
 
        if (readonlyListItem?.blockGroupType === 'ListItem' && listParent) {
            let listItem = mutateBlock(readonlyListItem);
 
            if (isEmptyListItem(listItem)) {
                listItem.levels.pop();
            } else {
                listItem = createNewListItem(context, listItem, listParent);
 
                Eif (context.formatContext) {
                    context.formatContext.announceData = getListAnnounceData([
                        listItem,
                        ...path.slice(index + 1),
                    ]);
                }
            }
 
            context.deleteResult = 'range';
        }
    }
};
 
const isEmptyListItem = (listItem: ReadonlyContentModelListItem) => {
    return listItem.blocks.length === 1 && isEmptyParagraph(listItem.blocks[0]);
};
 
const isEmptyParagraph = (block: ReadonlyContentModelBlock) => {
    return (
        block.blockType === 'Paragraph' &&
        block.segments.length === 2 &&
        block.segments[0].segmentType === 'SelectionMarker' &&
        block.segments[1].segmentType === 'Br'
    );
};
 
const createNewListItem = (
    context: ValidDeleteSelectionContext,
    listItem: ReadonlyContentModelListItem,
    listParent: ReadonlyContentModelBlockGroup
) => {
    const { insertPoint } = context;
    const listIndex = listParent.blocks.indexOf(listItem);
    const currentPara = insertPoint.paragraph;
    const paraIndex = listItem.blocks.indexOf(currentPara);
    const newParagraph = splitParagraph(insertPoint);
 
    const levels = createNewListLevel(listItem);
    const newListItem: ShallowMutableContentModelListItem = createListItem(
        levels,
        listItem.formatHolder.format
    );
 
    newListItem.blocks.push(newParagraph);
 
    copyFormat<ContentModelBlockFormat>(newListItem.format, listItem.format, ListFormats);
 
    const remainingBlockCount = listItem.blocks.length - paraIndex - 1;
 
    if (paraIndex >= 0 && remainingBlockCount > 0) {
        newListItem.blocks.push(
            ...mutateBlock(listItem).blocks.splice(paraIndex + 1, remainingBlockCount)
        );
    }
 
    insertPoint.paragraph = newParagraph;
    mutateBlock(listParent).blocks.splice(listIndex + 1, 0, newListItem);
 
    if (context.lastParagraph == currentPara) {
        context.lastParagraph = newParagraph;
    }
 
    return newListItem;
};
 
const createNewListLevel = (listItem: ReadonlyContentModelListItem) => {
    return listItem.levels.map(level => {
        return createListLevel(
            level.listType,
            {
                ...level.format,
                startNumberOverride: undefined,
                displayForDummyItem: undefined, // When ENTER, we should create a new regular list item, so force its dummy item display to undefined
            },
            level.dataset
        );
    });
};