All files / roosterjs-content-model-api/lib/publicApi/utils formatInsertPointWithContentModel.ts

100% Statements 82/82
95.24% Branches 60/63
100% Functions 10/10
100% Lines 73/73

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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 2341x                                                     1x                   101x       101x   5x   5x 4x 4x   4x 4x     5x                                                   101x 51x   51x   51x 51x 39x 39x     51x 51x   51x 77x   77x   77x     51x   51x 39x                         128x         13x 13x     128x 18x 18x     128x 1x 1x     128x 18x 18x     128x 10x       101x         90x 90x 90x   90x         164x 164x   164x 164x 164x   164x     90x         30x     90x 52x   52x     90x 8x     90x 52x   52x     90x 22x     90x                   224x   224x   224x   224x 25x                         141x 141x   141x    
import {
    addSegment,
    addTextSegment,
    buildSelectionMarker,
    getRegularSelectionOffsets,
    mutateBlock,
    processChildNode,
} from 'roosterjs-content-model-dom';
import type {
    ElementProcessor,
    DOMInsertPoint,
    FormatContentModelOptions,
    IEditor,
    InsertPoint,
    DomToModelContext,
    ContentModelBlockGroup,
    FormatContentModelContext,
    ShallowMutableContentModelDocument,
} from 'roosterjs-content-model-types';
 
/**
 * Invoke a callback to format the content in a specific position  using Content Model
 * @param editor The editor object
 * @param insertPoint The insert position.
 * @param callback The callback to insert the format.
 * @param options More options, @see FormatContentModelOptions
 */
export function formatInsertPointWithContentModel(
    editor: IEditor,
    insertPoint: DOMInsertPoint,
    callback: (
        model: ShallowMutableContentModelDocument,
        context: FormatContentModelContext,
        insertPoint?: InsertPoint
    ) => void,
    options?: FormatContentModelOptions
) {
    const bundle: InsertPointBundle = {
        input: insertPoint,
    };
 
    editor.formatContentModel(
        (model, context) => {
            callback(model, context, bundle.result);
 
            if (bundle?.result) {
                const { paragraph, marker } = bundle.result;
                const index = paragraph.segments.indexOf(marker);
 
                Eif (index >= 0) {
                    mutateBlock(paragraph).segments.splice(index, 1);
                }
            }
            return true;
        },
        options,
        {
            processorOverride: {
                child: getShadowChildProcessor(bundle),
                textWithSelection: getShadowTextProcessor(bundle),
            },
            tryGetFromCache: false,
        }
    );
}
 
interface InsertPointBundle {
    input: DOMInsertPoint;
    result?: InsertPoint;
}
 
interface DomToModelContextWithPath extends DomToModelContext {
    /**
     * Block group path of this insert point, from direct parent group to the root group
     */
    path?: ContentModelBlockGroup[];
}
 
function getShadowChildProcessor(bundle: InsertPointBundle): ElementProcessor<ParentNode> {
    return (group, parent, context) => {
        const contextWithPath = context as DomToModelContextWithPath;
 
        contextWithPath.path = contextWithPath.path || [];
 
        let shouldShiftPath = false;
        if (contextWithPath.path[0] != group) {
            contextWithPath.path.unshift(group);
            shouldShiftPath = true;
        }
 
        const offsets = getShadowSelectionOffsets(context, bundle, parent);
        let index = 0;
 
        for (let child = parent.firstChild; child; child = child.nextSibling) {
            handleElementShadowSelection(bundle, index, context, group, offsets, parent);
 
            processChildNode(group, child, context);
 
            index++;
        }
 
        handleElementShadowSelection(bundle, index, context, group, offsets, parent);
 
        if (shouldShiftPath) {
            contextWithPath.path.shift();
        }
    };
}
 
function handleElementShadowSelection(
    bundle: InsertPointBundle,
    index: number,
    context: DomToModelContext,
    group: ContentModelBlockGroup,
    offsets: [number, number, number],
    container?: Node
) {
    if (
        index == offsets[2] &&
        (index <= offsets[0] || offsets[0] < 0) &&
        (index < offsets[1] || offsets[1] < 0)
    ) {
        addSelectionMarker(group, context, container, index, bundle);
        offsets[2] = -1;
    }
 
    if (index == offsets[0]) {
        context.isInSelection = true;
        addSelectionMarker(group, context, container, index);
    }
 
    if (index == offsets[2] && (index < offsets[1] || offsets[1] < 0)) {
        addSelectionMarker(group, context, container, index, bundle);
        offsets[2] = -1;
    }
 
    if (index == offsets[1]) {
        addSelectionMarker(group, context, container, index);
        context.isInSelection = false;
    }
 
    if (index == offsets[2]) {
        addSelectionMarker(group, context, container, index, bundle);
    }
}
 
const getShadowTextProcessor = (bundle: InsertPointBundle): ElementProcessor<Text> => (
    group,
    textNode,
    context
) => {
    let txt = textNode.nodeValue || '';
    const offsets = getShadowSelectionOffsets(context, bundle, textNode);
    const [start, end, shadow] = offsets;
 
    const handleTextSelection = (
        subtract: number,
        originalOffset: number,
        bundle?: InsertPointBundle
    ) => {
        addTextSegment(group, txt.substring(0, subtract), context);
        addSelectionMarker(group, context, textNode, originalOffset, bundle);
 
        offsets[0] -= subtract;
        offsets[1] -= subtract;
        offsets[2] = bundle ? -1 : offsets[2] - subtract;
 
        txt = txt.substring(subtract);
    };
 
    if (
        offsets[2] >= 0 &&
        (offsets[2] <= offsets[0] || offsets[0] < 0) &&
        (offsets[2] < offsets[1] || offsets[1] < 0)
    ) {
        handleTextSelection(offsets[2], shadow, bundle);
    }
 
    if (offsets[0] >= 0) {
        handleTextSelection(offsets[0], start);
 
        context.isInSelection = true;
    }
 
    if (offsets[2] >= 0 && offsets[2] > offsets[0] && (offsets[2] < offsets[1] || offsets[1] < 0)) {
        handleTextSelection(offsets[2], shadow, bundle);
    }
 
    if (offsets[1] >= 0) {
        handleTextSelection(offsets[1], end);
 
        context.isInSelection = false;
    }
 
    if (offsets[2] >= 0 && offsets[2] >= offsets[1]) {
        handleTextSelection(offsets[2], shadow, bundle);
    }
 
    addTextSegment(group, txt, context);
};
 
function addSelectionMarker(
    group: ContentModelBlockGroup,
    context: DomToModelContextWithPath,
    container?: Node,
    offset?: number,
    bundle?: InsertPointBundle
) {
    const marker = buildSelectionMarker(group, context, container, offset);
 
    marker.isSelected = !bundle;
 
    const para = addSegment(group, marker, context.blockFormat, marker.format);
 
    if (bundle && context.path) {
        bundle.result = {
            path: [...context.path],
            paragraph: para,
            marker,
        };
    }
}
 
function getShadowSelectionOffsets(
    context: DomToModelContext,
    bundle: InsertPointBundle,
    currentContainer: Node
): [number, number, number] {
    const [start, end] = getRegularSelectionOffsets(context, currentContainer);
    const shadow = bundle.input.node == currentContainer ? bundle.input.offset : -1;
 
    return [start, end, shadow];
}