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

94.44% Statements 68/72
96% Branches 96/100
100% Functions 4/4
94.03% Lines 63/67

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                                                                    1x         1507x                   2418x 2418x 2418x   2418x   2418x 2418x   2418x 2673x   2673x   463x   463x 20x   20x       20x         20x                         443x     3x   460x     149x 149x 253x     149x 19x 1x     130x 205x   205x 442x 442x       442x             442x 5x     437x       433x 433x   433x                 9x             134x     2006x   2006x 2917x 2917x   2917x   23x       23x         23x                         23x 8x   2894x 1622x     2917x 1632x   1285x       2006x 56x   1950x       55x 1x     54x       2343x                     2343x    
import type {
    ContentModelBlockGroup,
    IterateSelectionsCallback,
    IterateSelectionsOption,
    ReadonlyContentModelBlockGroup,
    ReadonlyContentModelSegment,
    ReadonlyIterateSelectionsCallback,
    ReadonlyTableSelectionContext,
} from 'roosterjs-content-model-types';
 
/**
 * 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 {
    internalIterateSelections([group], callback as ReadonlyIterateSelectionsCallback, 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;
}