(editor: Editor, range: Range, minWidth = 4)
| 300 | * @returns array of DOMRect objects |
| 301 | */ |
| 302 | export const getLineRectsByRange = (editor: Editor, range: Range, minWidth = 4) => { |
| 303 | const anchor = Range.start(range) |
| 304 | const focus = Range.end(range) |
| 305 | // 开始位置的 block节点 |
| 306 | const anchorEntry = Editor.above<Element>(editor, { |
| 307 | at: anchor, |
| 308 | match: n => Editor.isBlock(editor, n), |
| 309 | mode: 'lowest', |
| 310 | }) |
| 311 | // 结束位置的 block 节点 |
| 312 | const focusEntry = Editor.above<Element>(editor, { |
| 313 | at: focus, |
| 314 | match: n => Editor.isBlock(editor, n), |
| 315 | mode: 'lowest', |
| 316 | }) |
| 317 | if (!anchorEntry || !focusEntry) return [] |
| 318 | |
| 319 | const blockRects: DOMRect[] = [] |
| 320 | const rectMap: Map< |
| 321 | DOMRect, |
| 322 | { |
| 323 | dom: DOMElement |
| 324 | element: Element |
| 325 | } |
| 326 | > = new Map() |
| 327 | |
| 328 | let [startBlock, startPath] = anchorEntry |
| 329 | let [_, endPath] = focusEntry |
| 330 | const ranges: DOMRange[] = [] |
| 331 | let isStart = true |
| 332 | let next: NodeEntry<Element> | undefined = anchorEntry |
| 333 | while (next) { |
| 334 | const [nextBlock, nextPath] = next as NodeEntry<Element> |
| 335 | const element = Editable.toDOMNode(editor, nextBlock) |
| 336 | const rect = element.getBoundingClientRect() |
| 337 | rectMap.set(rect, { |
| 338 | dom: element, |
| 339 | element: nextBlock, |
| 340 | }) |
| 341 | blockRects.push(rect) |
| 342 | |
| 343 | if (Path.equals(nextPath, endPath)) break |
| 344 | if (!isStart) { |
| 345 | const range = document.createRange() |
| 346 | range.selectNodeContents(element) |
| 347 | ranges.push(range) |
| 348 | } else { |
| 349 | isStart = false |
| 350 | } |
| 351 | next = Editor.next<Element>(editor, { |
| 352 | at: nextPath, |
| 353 | match: n => Editor.isBlock(editor, n), |
| 354 | }) |
| 355 | } |
| 356 | if (Path.equals(startPath, endPath)) { |
| 357 | ranges.unshift(Editable.toDOMRange(editor, range)) |
| 358 | } else { |
| 359 | ranges.unshift( |
no test coverage detected
searching dependent graphs…