All files / roosterjs-content-model-plugins/lib/edit/tabUtils handleTabOnList.ts

100% Statements 13/13
100% Branches 11/11
100% Functions 5/5
100% Lines 12/12

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 521x 1x                       1x           13x 13x         5x   8x           8x 8x         13x             13x   18x      
import { handleTabOnParagraph } from './handleTabOnParagraph';
import { setModelIndentation } from 'roosterjs-content-model-api';
import type {
    FormatContentModelContext,
    ReadonlyContentModelDocument,
    ReadonlyContentModelListItem,
} from 'roosterjs-content-model-types';
 
/**
 * 1. When the selection is collapsed and the cursor is at start of a list item, call setModelIndentation.
 * 2. Otherwise call handleTabOnParagraph.
 * @internal
 */
export function handleTabOnList(
    model: ReadonlyContentModelDocument,
    listItem: ReadonlyContentModelListItem,
    rawEvent: KeyboardEvent,
    context?: FormatContentModelContext
) {
    const selectedParagraph = findSelectedParagraph(listItem);
    if (
        !isMarkerAtStartOfBlock(listItem) &&
        selectedParagraph.length == 1 &&
        selectedParagraph[0].blockType === 'Paragraph'
    ) {
        return handleTabOnParagraph(model, selectedParagraph[0], rawEvent, context);
    } else {
        setModelIndentation(
            model,
            rawEvent.shiftKey ? 'outdent' : 'indent',
            undefined /*length*/,
            context
        );
        rawEvent.preventDefault();
        return true;
    }
}
 
function isMarkerAtStartOfBlock(listItem: ReadonlyContentModelListItem) {
    return (
        listItem.blocks[0].blockType == 'Paragraph' &&
        listItem.blocks[0].segments[0].segmentType == 'SelectionMarker'
    );
}
 
function findSelectedParagraph(listItem: ReadonlyContentModelListItem) {
    return listItem.blocks.filter(
        block =>
            block.blockType == 'Paragraph' && block.segments.some(segment => segment.isSelected)
    );
}