* Checks if the cursor is visible within the CodeMirror viewport. * @returns {boolean} - True if the cursor is visible, false otherwise.
()
| 2490 | * @returns {boolean} - True if the cursor is visible, false otherwise. |
| 2491 | */ |
| 2492 | function isCursorVisible() { |
| 2493 | const view = editor; |
| 2494 | const scroller = view?.scrollDOM; |
| 2495 | if (!view || !scroller) return true; |
| 2496 | |
| 2497 | const { head } = view.state.selection.main; |
| 2498 | const caret = safeCoordsAtPos(view, head); |
| 2499 | if (!caret) return true; |
| 2500 | |
| 2501 | const scrollerRect = scroller.getBoundingClientRect(); |
| 2502 | return caret.top >= scrollerRect.top && caret.bottom <= scrollerRect.bottom; |
| 2503 | } |
| 2504 | |
| 2505 | function safeCoordsAtPos(view, pos) { |
| 2506 | try { |
no test coverage detected