All files / roosterjs-content-model-api/lib/modelApi/block splitSelectedParagraphByBr.ts

100% Statements 36/36
88.89% Branches 16/18
100% Functions 3/3
100% Lines 26/26

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 691x                               1x 167x           226x 270x 36x 36x 36x 36x 36x   36x 65x 65x 40x 26x     40x 40x     40x 40x   25x   25x 24x         34x 10x     34x                 76x    
import {
    createParagraph,
    getSelectedSegmentsAndParagraphs,
    mutateBlock,
} from 'roosterjs-content-model-dom';
import type {
    ReadonlyContentModelDocument,
    ReadonlyContentModelParagraph,
    ShallowMutableContentModelParagraph,
} from 'roosterjs-content-model-types';
 
/**
 * @internal
 * For all selected paragraphs, if it has BR in middle of other segments, split the paragraph into multiple paragraphs
 * @param model The model to process
 */
export function splitSelectedParagraphByBr(model: ReadonlyContentModelDocument) {
    const selections = getSelectedSegmentsAndParagraphs(
        model,
        false /*includingFormatHolder*/,
        false /*includingEntity*/
    );
 
    for (const [_, para, path] of selections) {
        if (para?.segments.some(s => s.segmentType == 'Br')) {
            let currentParagraph = shallowColonParagraph(para);
            let hasVisibleSegment = false;
            const newParagraphs: ShallowMutableContentModelParagraph[] = [];
            const parent = mutateBlock(path[0]);
            const index = parent.blocks.indexOf(para);
 
            if (index >= 0) {
                for (const segment of mutateBlock(para).segments) {
                    if (segment.segmentType == 'Br') {
                        if (!hasVisibleSegment) {
                            currentParagraph.segments.push(segment);
                        }
 
                        Eif (currentParagraph.segments.length > 0) {
                            newParagraphs.push(currentParagraph);
                        }
 
                        currentParagraph = shallowColonParagraph(para);
                        hasVisibleSegment = false;
                    } else {
                        currentParagraph.segments.push(segment);
 
                        if (segment.segmentType != 'SelectionMarker') {
                            hasVisibleSegment = true;
                        }
                    }
                }
 
                if (currentParagraph.segments.length > 0) {
                    newParagraphs.push(currentParagraph);
                }
 
                parent.blocks.splice(index, 1, ...newParagraphs);
            }
        }
    }
}
 
function shallowColonParagraph(
    para: ReadonlyContentModelParagraph
): ShallowMutableContentModelParagraph {
    return createParagraph(false /*isImplicit*/, para.format, para.segmentFormat, para.decorator);
}