All files / roosterjs-content-model-api/lib/modelApi/entity insertEntityModel.ts

100% Statements 52/52
92.5% Branches 74/80
100% Functions 2/2
100% Lines 52/52

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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 1511x                                                 1x                   115x     115x 56x 56x   56x 28x   59x 153x   51x 26x   26x   26x 26x       25x     25x   25x   25x         25x 25x       115x 81x 81x   81x 53x   53x   53x 18x 35x 33x 33x 33x     28x           28x 28x     81x   81x 40x 40x   40x 40x                   59x 12x 3x 3x                 3x                 56x 56x   56x 8x     56x      
import {
    createBr,
    createParagraph,
    createSelectionMarker,
    normalizeContentModel,
    deleteSelection,
    getClosestAncestorBlockGroupIndex,
    setSelection,
    mutateBlock,
} from 'roosterjs-content-model-dom';
import type {
    ContentModelEntity,
    FormatContentModelContext,
    InsertEntityPosition,
    InsertPoint,
    ReadonlyContentModelBlock,
    ReadonlyContentModelBlockGroup,
    ReadonlyContentModelDocument,
    ShallowMutableContentModelBlock,
    ShallowMutableContentModelParagraph,
} from 'roosterjs-content-model-types';
 
/**
 * @internal
 */
export function insertEntityModel(
    model: ReadonlyContentModelDocument,
    entityModel: ContentModelEntity,
    position: InsertEntityPosition,
    isBlock: boolean,
    focusAfterEntity?: boolean,
    context?: FormatContentModelContext,
    insertPointOverride?: InsertPoint
) {
    let blockParent: ReadonlyContentModelBlockGroup | undefined;
    let blockIndex = -1;
    let insertPoint: InsertPoint | null;
 
    if (position == 'begin' || position == 'end') {
        blockParent = model;
        blockIndex = position == 'begin' ? 0 : model.blocks.length;
 
        if (!isBlock) {
            Object.assign(entityModel.format, model.format);
        }
    } else if ((insertPoint = getInsertPoint(model, insertPointOverride, context))) {
        const { marker, paragraph, path } = insertPoint;
 
        if (!isBlock) {
            const index = paragraph.segments.indexOf(marker);
 
            Object.assign(entityModel.format, marker.format);
 
            Eif (index >= 0) {
                paragraph.segments.splice(focusAfterEntity ? index : index + 1, 0, entityModel);
            }
        } else {
            const pathIndex =
                position == 'root'
                    ? getClosestAncestorBlockGroupIndex(path, ['TableCell', 'Document'])
                    : 0;
            blockParent = mutateBlock(path[pathIndex]);
 
            const child = path[pathIndex - 1];
            const directChild: ReadonlyContentModelBlock =
                child?.blockGroupType == 'FormatContainer' ||
                child?.blockGroupType == 'General' ||
                child?.blockGroupType == 'ListItem'
                    ? child
                    : paragraph;
            const childIndex = blockParent.blocks.indexOf(directChild);
            blockIndex = childIndex >= 0 ? childIndex + 1 : -1;
        }
    }
 
    if (blockIndex >= 0 && blockParent) {
        const blocksToInsert: ShallowMutableContentModelBlock[] = [];
        let nextParagraph: ShallowMutableContentModelParagraph | undefined;
 
        if (isBlock) {
            const nextBlock = blockParent.blocks[blockIndex];
 
            blocksToInsert.push(entityModel);
 
            if (nextBlock?.blockType == 'Paragraph') {
                nextParagraph = mutateBlock(nextBlock);
            } else if (!nextBlock || nextBlock.blockType == 'Entity' || focusAfterEntity) {
                nextParagraph = createParagraph(false /*isImplicit*/, {}, model.format);
                nextParagraph.segments.push(createBr(model.format));
                blocksToInsert.push(nextParagraph);
            }
        } else {
            nextParagraph = createParagraph(
                false /*isImplicit*/,
                undefined /*format*/,
                model.format
            );
 
            nextParagraph.segments.push(entityModel);
            blocksToInsert.push(nextParagraph);
        }
 
        mutateBlock(blockParent).blocks.splice(blockIndex, 0, ...blocksToInsert);
 
        if (focusAfterEntity && nextParagraph) {
            const marker = createSelectionMarker(nextParagraph.segments[0]?.format || model.format);
            const segments = nextParagraph.segments;
 
            isBlock ? segments.unshift(marker) : segments.push(marker);
            setSelection(model, marker, marker);
        }
    }
}
 
function getInsertPoint(
    model: ReadonlyContentModelDocument,
    insertPointOverride?: InsertPoint,
    context?: FormatContentModelContext
): InsertPoint | null {
    if (insertPointOverride) {
        const { paragraph, marker, tableContext, path } = insertPointOverride;
        const index = paragraph.segments.indexOf(marker);
        const previousSegment = index > 0 ? paragraph.segments[index - 1] : null;
 
        // It is possible that the real selection is right before the override selection marker.
        // This happens when:
        // [Override marker][Entity node to wrap][Real marker]
        // Then we will move the entity node into entity wrapper, causes the override marker and real marker are at the same place
        // And recreating content model causes real marker to appear before override marker.
        // Once that happens, we need to use the real marker instead so that after insert entity, real marker can be placed
        // after new entity (if insertPointOverride==true)
        return previousSegment?.segmentType == 'SelectionMarker' && previousSegment.isSelected
            ? {
                  marker: previousSegment,
                  paragraph,
                  tableContext,
                  path,
              }
            : insertPointOverride;
    } else {
        const deleteResult = deleteSelection(model, [], context);
        const insertPoint = deleteResult.insertPoint;
 
        if (deleteResult.deleteResult == 'range') {
            normalizeContentModel(model);
        }
 
        return insertPoint;
    }
}