(container: HTMLElement | null)
| 66 | const SETTLE_MS = 150; |
| 67 | |
| 68 | const read = (container: HTMLElement | null): TextSelection | null => { |
| 69 | const selection = globalThis.getSelection?.(); |
| 70 | |
| 71 | if (!container || !selection || selection.isCollapsed) { |
| 72 | return null; |
| 73 | } |
| 74 | |
| 75 | const text = selection.toString().trim(); |
| 76 | |
| 77 | // Any run the reader marked is a quote, however short. Only a selection |
| 78 | // with no words in it has nothing to put on a card. |
| 79 | if (!text || selection.rangeCount === 0) { |
| 80 | return null; |
| 81 | } |
| 82 | |
| 83 | const range = selection.getRangeAt(0); |
| 84 | |
| 85 | if (!container.contains(range.commonAncestorContainer)) { |
| 86 | return null; |
| 87 | } |
| 88 | |
| 89 | const rect = range.getBoundingClientRect(); |
| 90 | |
| 91 | if (!rect.width && !rect.height) { |
| 92 | return null; |
| 93 | } |
| 94 | |
| 95 | return { |
| 96 | text, |
| 97 | ...readPassage(range, text), |
| 98 | top: rect.top, |
| 99 | bottom: rect.bottom, |
| 100 | left: rect.left, |
| 101 | width: rect.width, |
| 102 | }; |
| 103 | }; |
| 104 | |
| 105 | /** |
| 106 | * The current selection, but only while it lives inside `containerRef` — a |
no test coverage detected