All files / roosterjs-content-model-api/lib/publicApi/entity insertEntity.ts

97.67% Statements 42/43
96.88% Branches 31/32
66.67% Functions 2/3
97.44% Lines 38/39

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 151 152 153 154 1551x 1x 1x                                     1x 1x                                                                               1x             5x 5x 5x 5x 5x   5x   5x 1x 1x     5x 1x     5x   5x 4x     5x                     5x         5x                   5x   5x 5x   5x     5x 1x   4x     5x 4x   4x 1x 2x   1x                   4x     5x    
import { formatInsertPointWithContentModel } from '../utils/formatInsertPointWithContentModel';
import { insertEntityModel } from '../../modelApi/entity/insertEntityModel';
import {
    ChangeSource,
    createEntity,
    normalizeContentModel,
    parseEntityFormat,
} from 'roosterjs-content-model-dom';
import type {
    ContentModelEntity,
    InsertEntityPosition,
    InsertEntityOptions,
    IEditor,
    EntityState,
    DOMInsertPoint,
    FormatContentModelOptions,
    FormatContentModelContext,
    InsertPoint,
    ShallowMutableContentModelDocument,
} from 'roosterjs-content-model-types';
 
const BlockEntityTag = 'div';
const InlineEntityTag = 'span';
 
/**
 * Insert an entity into editor
 * @param editor The editor object
 * @param type Type of entity
 * @param isBlock True to insert a block entity, false to insert an inline entity
 * @param position Position of the entity to insert. It can be
 * Value of InsertEntityPosition: see InsertEntityPosition
 * selectionRangeEx: Use this range instead of current focus position to insert. After insert, focus will be moved to
 * the beginning of this range (when focusAfterEntity is not set to true) or after the new entity (when focusAfterEntity is set to true)
 * @param options Move options to insert. See InsertEntityOptions
 */
export function insertEntity(
    editor: IEditor,
    type: string,
    isBlock: boolean,
    position: 'focus' | 'begin' | 'end' | DOMInsertPoint,
    options?: InsertEntityOptions
): ContentModelEntity | null;
 
/**
 * Insert a block entity into editor
 * @param editor The editor object
 * @param type Type of entity
 * @param isBlock Must be true for a block entity
 * @param position Position of the entity to insert. It can be
 * Value of InsertEntityPosition: see InsertEntityPosition
 * selectionRangeEx: Use this range instead of current focus position to insert. After insert, focus will be moved to
 * the beginning of this range (when focusAfterEntity is not set to true) or after the new entity (when focusAfterEntity is set to true)
 * @param options Move options to insert. See InsertEntityOptions
 */
export function insertEntity(
    editor: IEditor,
    type: string,
    isBlock: true,
    position: InsertEntityPosition | DOMInsertPoint,
    options?: InsertEntityOptions
): ContentModelEntity | null;
 
export function insertEntity(
    editor: IEditor,
    type: string,
    isBlock: boolean,
    position?: InsertEntityPosition | DOMInsertPoint,
    options?: InsertEntityOptions
): ContentModelEntity | null {
    const { contentNode, focusAfterEntity, wrapperDisplay, skipUndoSnapshot, initialEntityState } =
        options || {};
    const document = editor.getDocument();
    const wrapper = document.createElement(isBlock ? BlockEntityTag : InlineEntityTag);
    const display = wrapperDisplay ?? (isBlock ? undefined : 'inline-block');
 
    wrapper.style.setProperty('display', display || null);
 
    if (display == undefined && isBlock) {
        wrapper.style.setProperty('width', '100%');
        wrapper.style.setProperty('display', 'inline-block');
    }
 
    if (contentNode) {
        wrapper.appendChild(contentNode);
    }
 
    const entityModel = createEntity(wrapper, true /* isReadonly */, undefined /*format*/, type);
 
    if (!skipUndoSnapshot) {
        editor.takeSnapshot();
    }
 
    const formatOptions: FormatContentModelOptions = {
        changeSource: ChangeSource.InsertEntity,
        getChangeData: () => ({
            wrapper,
            type,
            id: '',
            isReadonly: true,
        }),
        apiName: 'insertEntity',
    };
 
    const callback = (
        model: ShallowMutableContentModelDocument,
        context: FormatContentModelContext,
        insertPoint?: InsertPoint
    ) => {
        insertEntityModel(
            model,
            entityModel,
            typeof position == 'string' ? position : 'focus',
            isBlock,
            focusAfterEntity,
            context,
            insertPoint
        );
 
        normalizeContentModel(model);
 
        context.skipUndoSnapshot = true;
        context.newEntities.push(entityModel);
 
        return true;
    };
 
    if (typeof position == 'object') {
        formatInsertPointWithContentModel(editor, position, callback, formatOptions);
    } else {
        editor.formatContentModel(callback, formatOptions);
    }
 
    if (!skipUndoSnapshot) {
        let entityState: EntityState | undefined;
 
        if (initialEntityState) {
            const format = parseEntityFormat(wrapper);
            const { id, entityType } = format;
 
            entityState =
                id && entityType
                    ? {
                          id: id,
                          type: entityType,
                          state: initialEntityState,
                      }
                    : undefined;
        }
 
        editor.takeSnapshot(entityState);
    }
 
    return entityModel;
}