(editor: Editor, node: Node, minWidth = 4)
| 261 | * @returns An array of DOMRect objects representing the line rectangles. |
| 262 | */ |
| 263 | export const getLineRectsByNode = (editor: Editor, node: Node, minWidth = 4) => { |
| 264 | const path = Editable.findPath(editor, node) |
| 265 | const block: NodeEntry | undefined = |
| 266 | Editor.isBlock(editor, node) && path.length === 1 |
| 267 | ? [node, path] |
| 268 | : Editor.above<Element>(editor, { |
| 269 | at: path, |
| 270 | match: n => Editor.isBlock(editor, n), |
| 271 | mode: 'highest', |
| 272 | }) |
| 273 | if (!block) return [] |
| 274 | const domEl = Editable.toDOMNode(editor, block[0]) |
| 275 | const domRect = domEl.getBoundingClientRect() |
| 276 | const range = document.createRange() |
| 277 | range.selectNodeContents(Editable.toDOMNode(editor, node)) |
| 278 | const lines = splitRectsIntoLines(range.getClientRects()) |
| 279 | const lineRects: DOMRect[] = [] |
| 280 | for (const [line, rects] of lines) { |
| 281 | let width = line.right - line.left |
| 282 | const lineRect = matchHighest(editor, domEl, line.top, line.bottom) |
| 283 | line.top = lineRect.top |
| 284 | line.height = lineRect.height |
| 285 | line.bottom = lineRect.bottom |
| 286 | // 空节点的宽度给个最小值 |
| 287 | if (width < 1 && domRect.left === rects[0].left) { |
| 288 | width = minWidth |
| 289 | } |
| 290 | lineRects.push(new DOMRect(rects[0].left, line.top, width, line.height)) |
| 291 | } |
| 292 | return lineRects |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Get DOMRect objects split by line within range |
no test coverage detected
searching dependent graphs…