All files / roosterjs-content-model-plugins/lib/edit/utils splitParagraph.ts

100% Statements 13/13
100% Branches 2/2
100% Functions 1/1
100% Lines 13/13

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 471x                                   1x 66x 33x           33x 33x         33x 9x     33x   33x   33x   33x   33x    
import {
    createBr,
    createParagraph,
    normalizeParagraph,
    setParagraphNotImplicit,
} from 'roosterjs-content-model-dom';
import type {
    InsertPoint,
    ShallowMutableContentModelParagraph,
} from 'roosterjs-content-model-types';
 
/**
 * @internal
 * Split the given paragraph from insert point into two paragraphs,
 * and move the selection marker to the beginning of the second paragraph
 * @param insertPoint The input insert point which includes the paragraph and selection marker
 * @returns The new paragraph it created
 */
export function splitParagraph(insertPoint: InsertPoint) {
    const { paragraph, marker } = insertPoint;
    const newParagraph: ShallowMutableContentModelParagraph = createParagraph(
        false /*isImplicit*/,
        paragraph.format,
        paragraph.segmentFormat
    );
 
    const markerIndex = paragraph.segments.indexOf(marker);
    const segments = paragraph.segments.splice(
        markerIndex,
        paragraph.segments.length - markerIndex
    );
 
    if (paragraph.segments.length == 0) {
        paragraph.segments.push(createBr(marker.format));
    }
 
    newParagraph.segments.push(...segments);
 
    setParagraphNotImplicit(paragraph);
 
    insertPoint.paragraph = newParagraph;
 
    normalizeParagraph(paragraph);
 
    return newParagraph;
}