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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | 1x 1x 1x 180x 236x 236x 236x 236x 236x 236x 1x 236x 1x 1x 1x 1x 236x 236x 18x 15x 15x 3x 3x 2x 1x 15x 177x 177x 337x 337x 177x 50x 50x 50x 50x 50x 177x 26x 18x 18x 18x 16x 30x 30x 66x 66x 66x 27x 66x 51x 2x 2x 3x 3x 3x 2x 18x 24x 24x 36x 24x 337x 108x 50x 108x 3x 3x 12x 12x 7x 12x 214x 214x 229x 72x 72x 72x 305x 99x 65x 34x 99x 588x 588x 588x | import { isGeneralSegment } from '../typeCheck/isGeneralSegment'; import { mutateBlock, mutateSegment } from '../common/mutate'; import type { MutableType, ReadonlyContentModelBlock, ReadonlyContentModelBlockGroup, ReadonlyContentModelParagraph, ReadonlyContentModelSegment, ReadonlyContentModelTable, ReadonlySelectable, ShallowMutableSelectable, TableCellCoordinate, } from 'roosterjs-content-model-types'; /** * Set selection into Content Model. If the Content Model already has selection, existing selection will be overwritten by the new one. * @param group The root level group of Content Model * @param start The start selected element. If not passed, existing selection of content model will be cleared * @param end The end selected element. If not passed, only the start element will be selected. If passed, all elements between start and end elements will be selected */ export function setSelection( group: ReadonlyContentModelBlockGroup, start?: ReadonlySelectable, end?: ReadonlySelectable ) { setSelectionToBlockGroup(group, false /*isInSelection*/, start || null, end || null); } function setSelectionToBlockGroup( group: ReadonlyContentModelBlockGroup, isInSelection: boolean, start: ReadonlySelectable | null, end: ReadonlySelectable | null ): boolean { return handleSelection(isInSelection, group, start, end, isInSelection => { Iif (isGeneralSegment(group) && needToSetSelection(group, isInSelection)) { setIsSelected(mutateBlock(group), isInSelection); } const blocksToDelete: number[] = []; group.blocks.forEach((block, i) => { isInSelection = setSelectionToBlock(block, isInSelection, start, end); if (block.blockType == 'Paragraph' && block.segments.length == 0 && block.isImplicit) { blocksToDelete.push(i); } }); let index: number | undefined; if (blocksToDelete.length > 0) { const mutableGroup = mutateBlock(group); while ((index = blocksToDelete.pop()) !== undefined) { Eif (index >= 0) { mutableGroup.blocks.splice(index, 1); } } } return isInSelection; }); } function setSelectionToBlock( block: ReadonlyContentModelBlock, isInSelection: boolean, start: ReadonlySelectable | null, end: ReadonlySelectable | null ) { switch (block.blockType) { case 'BlockGroup': return setSelectionToBlockGroup(block, isInSelection, start, end); case 'Table': return setSelectionToTable(block, isInSelection, start, end); case 'Divider': case 'Entity': return handleSelection(isInSelection, block, start, end, isInSelection => { if (needToSetSelection(block, isInSelection)) { const mutableBlock = mutateBlock(block); if (isInSelection) { mutableBlock.isSelected = true; } else { delete mutableBlock.isSelected; } } return isInSelection; }); case 'Paragraph': const segmentsToDelete: number[] = []; block.segments.forEach((segment, i) => { isInSelection = handleSelection( isInSelection, segment, start, end, isInSelection => { return setSelectionToSegment( block, segment, isInSelection, segmentsToDelete, start, end, i ); } ); }); if (segmentsToDelete.length > 0) { const mutablePara = mutateBlock(block); let index: number | undefined; while ((index = segmentsToDelete.pop()) !== undefined) { Eif (index >= 0) { mutablePara.segments.splice(index, 1); } } } return isInSelection; default: return isInSelection; } } function setSelectionToTable( table: ReadonlyContentModelTable, isInSelection: boolean, start: ReadonlySelectable | null, end: ReadonlySelectable | null ): boolean { const first = findCell(table, start); const last = end ? findCell(table, end) : first; if (!isInSelection) { for (let row = 0; row < table.rows.length; row++) { const currentRow = table.rows[row]; for (let col = 0; col < currentRow.cells.length; col++) { const currentCell = table.rows[row].cells[col]; const isSelected = row >= first.row && row <= last.row && col >= first.col && col <= last.col; if (needToSetSelection(currentCell, isSelected)) { setIsSelected(mutateBlock(currentCell), isSelected); } if (!isSelected) { setSelectionToBlockGroup(currentCell, false /*isInSelection*/, start, end); } } } } else { table.rows.forEach(row => row.cells.forEach(cell => { const wasInSelection = isInSelection; isInSelection = setSelectionToBlockGroup(cell, isInSelection, start, end); if (wasInSelection && isInSelection) { mutateBlock(cell).isSelected = true; } }) ); } return isInSelection; } function findCell( table: ReadonlyContentModelTable, cell: ReadonlySelectable | null ): TableCellCoordinate { let col = -1; const row = cell ? table.rows.findIndex( row => (col = (row.cells as ReadonlyArray<ReadonlySelectable>).indexOf(cell)) >= 0 ) : -1; return { row, col }; } function setSelectionToSegment( paragraph: ReadonlyContentModelParagraph, segment: ReadonlyContentModelSegment, isInSelection: boolean, segmentsToDelete: number[], start: ReadonlySelectable | null, end: ReadonlySelectable | null, i: number ) { switch (segment.segmentType) { case 'SelectionMarker': if (!isInSelection || (segment != start && segment != end)) { // Delete the selection marker when // 1. It is not in selection any more. Or // 2. It is in middle of selection, so no need to have it segmentsToDelete.push(i); } return isInSelection; case 'General': internalSetSelectionToSegment(paragraph, segment, isInSelection); return segment != start && segment != end ? setSelectionToBlockGroup(segment, isInSelection, start, end) : isInSelection; case 'Image': const isSelectedAsImageSelection = start == segment && (!end || end == segment); internalSetSelectionToSegment( paragraph, segment, isInSelection, !segment.isSelectedAsImageSelection != !isSelectedAsImageSelection ? image => (image.isSelectedAsImageSelection = isSelectedAsImageSelection) : undefined ); return isInSelection; default: internalSetSelectionToSegment(paragraph, segment, isInSelection); return isInSelection; } } function internalSetSelectionToSegment<T extends ReadonlyContentModelSegment>( paragraph: ReadonlyContentModelParagraph, segment: T, isInSelection: boolean, additionAction?: (segment: MutableType<T>) => void ) { if (additionAction || needToSetSelection(segment, isInSelection)) { mutateSegment(paragraph, segment, mutableSegment => { setIsSelected(mutableSegment, isInSelection); additionAction?.(mutableSegment); }); } } function needToSetSelection(selectable: ReadonlySelectable, isSelected: boolean) { return !selectable.isSelected != !isSelected; } function setIsSelected(selectable: ShallowMutableSelectable, value: boolean) { if (value) { selectable.isSelected = true; } else { delete selectable.isSelected; } return value; } function handleSelection( isInSelection: boolean, model: ReadonlyContentModelBlockGroup | ReadonlyContentModelBlock | ReadonlyContentModelSegment, start: ReadonlySelectable | null, end: ReadonlySelectable | null, callback: (isInSelection: boolean) => boolean ) { isInSelection = isInSelection || model == start; isInSelection = callback(isInSelection); return isInSelection && !!end && model != end; } |