All files / roosterjs-content-model-dom/lib/modelApi/common ensureParagraph.ts

100% Statements 10/10
100% Branches 6/6
100% Functions 1/1
100% Lines 10/10

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 531x 1x 1x                                                                   1x         5538x   5538x 4351x   1187x 1187x   1187x      
import { addBlock } from './addBlock';
import { createParagraph } from '../creators/createParagraph';
import { mutateBlock } from './mutate';
import type {
    ContentModelBlockFormat,
    ContentModelBlockGroup,
    ContentModelParagraph,
    ContentModelSegmentFormat,
    ShallowMutableContentModelBlockGroup,
    ShallowMutableContentModelParagraph,
} from 'roosterjs-content-model-types';
 
/**
 * @internal
 * Ensure there is a Paragraph that can insert segments in a Content Model Block Group
 * @param group The parent block group of the target paragraph
 * @param blockFormat Format of the paragraph. This is only used if we need to create a new paragraph
 */
export function ensureParagraph(
    group: ContentModelBlockGroup,
    blockFormat?: ContentModelBlockFormat,
    segmentFormat?: ContentModelSegmentFormat
): ContentModelParagraph;
 
/**
 * @internal
 * Ensure there is a Paragraph that can insert segments in a Content Model Block Group (Shallow mutable)
 * @param group The parent block group of the target paragraph
 * @param blockFormat Format of the paragraph. This is only used if we need to create a new paragraph
 */
export function ensureParagraph(
    group: ShallowMutableContentModelBlockGroup,
    blockFormat?: ContentModelBlockFormat,
    segmentFormat?: ContentModelSegmentFormat
): ShallowMutableContentModelParagraph;
 
export function ensureParagraph(
    group: ShallowMutableContentModelBlockGroup,
    blockFormat?: ContentModelBlockFormat,
    segmentFormat?: ContentModelSegmentFormat
): ShallowMutableContentModelParagraph {
    const lastBlock = group.blocks[group.blocks.length - 1];
 
    if (lastBlock?.blockType == 'Paragraph') {
        return mutateBlock(lastBlock);
    } else {
        const paragraph = createParagraph(true, blockFormat, segmentFormat);
        addBlock(group, paragraph);
 
        return paragraph;
    }
}