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

100% Statements 16/16
100% Branches 26/26
100% Functions 1/1
100% Lines 16/16

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 791x                                                                             1x           2001x 2001x   2001x     20x 2x   18x     20x     2001x 365x 267x     1636x         66x     1636x     2001x    
import { ensureParagraph } from './ensureParagraph';
import type {
    ContentModelBlockFormat,
    ContentModelBlockGroup,
    ContentModelParagraph,
    ContentModelSegment,
    ContentModelSegmentFormat,
    ShallowMutableContentModelBlockGroup,
    ShallowMutableContentModelParagraph,
} from 'roosterjs-content-model-types';
 
/**
 * Add a given segment into a paragraph from its parent group. If the last block of the given group is not paragraph, create a new paragraph.
 * @param group The parent block group of the paragraph to add segment into
 * @param newSegment The segment to add
 * @param blockFormat The block format used for creating a new paragraph when need
 * @returns The parent paragraph where the segment is added to
 */
export function addSegment(
    group: ContentModelBlockGroup,
    newSegment: ContentModelSegment,
    blockFormat?: ContentModelBlockFormat,
    segmentFormat?: ContentModelSegmentFormat
): ContentModelParagraph;
 
/**
 * Add a given segment into a paragraph from its parent group. If the last block of the given group is not paragraph, create a new paragraph. (Shallow mutable)
 * @param group The parent block group of the paragraph to add segment into
 * @param newSegment The segment to add
 * @param blockFormat The block format used for creating a new paragraph when need
 * @returns The parent paragraph where the segment is added to
 */
export function addSegment(
    group: ShallowMutableContentModelBlockGroup,
    newSegment: ContentModelSegment,
    blockFormat?: ContentModelBlockFormat,
    segmentFormat?: ContentModelSegmentFormat
): ShallowMutableContentModelParagraph;
 
export function addSegment(
    group: ShallowMutableContentModelBlockGroup,
    newSegment: ContentModelSegment,
    blockFormat?: ContentModelBlockFormat,
    segmentFormat?: ContentModelSegmentFormat
): ShallowMutableContentModelParagraph {
    const paragraph = ensureParagraph(group, blockFormat, segmentFormat);
    const lastSegment = paragraph.segments[paragraph.segments.length - 1];
 
    if (blockFormat?.textIndent) {
        // For a new paragraph, if current text indent is already applied to previous block in the same level,
        // we need to ignore it according to browser rendering behavior
        if (blockFormat.isTextIndentApplied && paragraph.segments.length == 0) {
            delete paragraph.format.textIndent;
        } else {
            blockFormat.isTextIndentApplied = true;
        }
 
        delete paragraph.format.isTextIndentApplied;
    }
 
    if (newSegment.segmentType == 'SelectionMarker') {
        if (!lastSegment || !lastSegment.isSelected || !newSegment.isSelected) {
            paragraph.segments.push(newSegment);
        }
    } else {
        if (
            newSegment.isSelected &&
            lastSegment?.segmentType == 'SelectionMarker' &&
            lastSegment.isSelected
        ) {
            paragraph.segments.pop();
        }
 
        paragraph.segments.push(newSegment);
    }
 
    return paragraph;
}