All files / roosterjs-content-model-dom/lib/modelApi/selection iterateSelections.ts

94.81% Statements 73/77
95.45% Branches 105/110
100% Functions 5/5
94.44% Lines 68/72

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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259                                                                                              1x           1375x     1375x     1489x 59x     1489x               1375x                   2212x 2212x 2212x   2212x   2212x 2212x   2212x 2482x   2482x   450x   450x 20x   20x       20x         20x                         430x     3x   447x     120x 120x 214x     120x 19x 1x     101x 167x   167x 377x 377x       377x             377x 1x     376x       372x 372x   372x                 4x             114x     1857x   1857x 2703x 2703x   2703x   23x       23x         23x                         23x 8x   2680x 1488x     2703x 1498x   1205x       1857x 49x   1808x       55x 1x     54x       2153x                     2153x    
import type {
    ContentModelBlockGroup,
    ContentModelBlockWithCache,
    ContentModelDocument,
    IterateSelectionsCallback,
    IterateSelectionsOption,
    ReadonlyContentModelBlockGroup,
    ReadonlyContentModelSegment,
    ReadonlyIterateSelectionsCallback,
    ReadonlyTableSelectionContext,
} from 'roosterjs-content-model-types';
 
/**
 * @internal
 * This is a temporary type to pass the information of whether element cache should be persisted when possible
 */
export interface ContentModelDocumentWithPersistedCache extends ContentModelDocument {
    /**
     * When set to
     */
    persistCache?: boolean;
}
 
/**
 * Iterate all selected elements in a given model
 * @param group The given Content Model to iterate selection from
 * @param callback The callback function to access the selected element
 * @param option Option to determine how to iterate
 */
export function iterateSelections(
    group: ContentModelBlockGroup,
    callback: IterateSelectionsCallback,
    option?: IterateSelectionsOption
): void;
 
/**
 * Iterate all selected elements in a given model (Readonly)
 * @param group The given Content Model to iterate selection from
 * @param callback The callback function to access the selected element
 * @param option Option to determine how to iterate
 */
export function iterateSelections(
    group: ReadonlyContentModelBlockGroup,
    callback: ReadonlyIterateSelectionsCallback,
    option?: IterateSelectionsOption
): void;
 
export function iterateSelections(
    group: ReadonlyContentModelBlockGroup,
    callback: ReadonlyIterateSelectionsCallback | IterateSelectionsCallback,
    option?: IterateSelectionsOption
): void {
    const persistCache =
        group.blockGroupType == 'Document'
            ? (group as ContentModelDocumentWithPersistedCache).persistCache
            : false;
    const internalCallback: ReadonlyIterateSelectionsCallback = persistCache
        ? (callback as ReadonlyIterateSelectionsCallback)
        : (path, tableContext, block, segments) => {
              if (!!(block as ContentModelBlockWithCache)?.cachedElement) {
                  delete (block as ContentModelBlockWithCache).cachedElement;
              }
 
              return (callback as ReadonlyIterateSelectionsCallback)(
                  path,
                  tableContext,
                  block,
                  segments
              );
          };
 
    internalIterateSelections([group], internalCallback, option);
}
 
function internalIterateSelections(
    path: ReadonlyContentModelBlockGroup[],
    callback: ReadonlyIterateSelectionsCallback,
    option?: IterateSelectionsOption,
    table?: ReadonlyTableSelectionContext,
    treatAllAsSelect?: boolean
): boolean {
    const parent = path[0];
    const includeListFormatHolder = option?.includeListFormatHolder || 'allSegments';
    const contentUnderSelectedTableCell = option?.contentUnderSelectedTableCell || 'include';
    const contentUnderSelectedGeneralElement =
        option?.contentUnderSelectedGeneralElement || 'contentOnly';
 
    let hasSelectedSegment = false;
    let hasUnselectedSegment = false;
 
    for (let i = 0; i < parent.blocks.length; i++) {
        const block = parent.blocks[i];
 
        switch (block.blockType) {
            case 'BlockGroup':
                const newPath = [block, ...path];
 
                if (block.blockGroupType == 'General') {
                    const isSelected = treatAllAsSelect || block.isSelected;
                    const handleGeneralContent =
                        !isSelected ||
                        contentUnderSelectedGeneralElement == 'both' ||
                        contentUnderSelectedGeneralElement == 'contentOnly';
                    const handleGeneralElement =
                        isSelected &&
                        (contentUnderSelectedGeneralElement == 'both' ||
                            contentUnderSelectedGeneralElement == 'generalElementOnly' ||
                            block.blocks.length == 0);
 
                    Iif (
                        (handleGeneralContent &&
                            internalIterateSelections(
                                newPath,
                                callback,
                                option,
                                table,
                                isSelected
                            )) ||
                        (handleGeneralElement && callback(path, table, block))
                    ) {
                        return true;
                    }
                } else if (
                    internalIterateSelections(newPath, callback, option, table, treatAllAsSelect)
                ) {
                    return true;
                }
                break;
 
            case 'Table':
                const rows = block.rows;
                const isWholeTableSelected = rows.every(row =>
                    row.cells.every(cell => cell.isSelected)
                );
 
                if (contentUnderSelectedTableCell != 'include' && isWholeTableSelected) {
                    if (callback(path, table, block)) {
                        return true;
                    }
                } else {
                    for (let rowIndex = 0; rowIndex < rows.length; rowIndex++) {
                        const row = rows[rowIndex];
 
                        for (let colIndex = 0; colIndex < row.cells.length; colIndex++) {
                            const cell = row.cells[colIndex];
                            Iif (!cell) {
                                continue;
                            }
 
                            const newTable: ReadonlyTableSelectionContext = {
                                table: block,
                                rowIndex,
                                colIndex,
                                isWholeTableSelected,
                            };
 
                            if (cell.isSelected && callback(path, newTable)) {
                                return true;
                            }
 
                            if (
                                !cell.isSelected ||
                                contentUnderSelectedTableCell != 'ignoreForTableOrCell'
                            ) {
                                const newPath = [cell, ...path];
                                const isSelected = treatAllAsSelect || cell.isSelected;
 
                                if (
                                    internalIterateSelections(
                                        newPath,
                                        callback,
                                        option,
                                        newTable,
                                        isSelected
                                    )
                                ) {
                                    return true;
                                }
                            }
                        }
                    }
                }
 
                break;
 
            case 'Paragraph':
                const segments: ReadonlyContentModelSegment[] = [];
 
                for (let i = 0; i < block.segments.length; i++) {
                    const segment = block.segments[i];
                    const isSelected = treatAllAsSelect || segment.isSelected;
 
                    if (segment.segmentType == 'General') {
                        const handleGeneralContent =
                            !isSelected ||
                            contentUnderSelectedGeneralElement == 'both' ||
                            contentUnderSelectedGeneralElement == 'contentOnly';
                        const handleGeneralElement =
                            isSelected &&
                            (contentUnderSelectedGeneralElement == 'both' ||
                                contentUnderSelectedGeneralElement == 'generalElementOnly' ||
                                segment.blocks.length == 0);
 
                        Iif (
                            handleGeneralContent &&
                            internalIterateSelections(
                                [segment, ...path],
                                callback,
                                option,
                                table,
                                isSelected
                            )
                        ) {
                            return true;
                        }
 
                        if (handleGeneralElement) {
                            segments.push(segment);
                        }
                    } else if (isSelected) {
                        segments.push(segment);
                    }
 
                    if (isSelected) {
                        hasSelectedSegment = true;
                    } else {
                        hasUnselectedSegment = true;
                    }
                }
 
                if (segments.length > 0 && callback(path, table, block, segments)) {
                    return true;
                }
                break;
 
            case 'Divider':
            case 'Entity':
                if ((treatAllAsSelect || block.isSelected) && callback(path, table, block)) {
                    return true;
                }
 
                break;
        }
    }
 
    Iif (
        includeListFormatHolder != 'never' &&
        parent.blockGroupType == 'ListItem' &&
        hasSelectedSegment &&
        (!hasUnselectedSegment || includeListFormatHolder == 'anySegment') &&
        // When whole list item is selected, also add its format holder as selected segment
        callback(path, table, undefined /*block*/, [parent.formatHolder])
    ) {
        return true;
    }
 
    return false;
}