( range: Range, text: string, )
| 35 | * first occurrence. |
| 36 | */ |
| 37 | const readPassage = ( |
| 38 | range: Range, |
| 39 | text: string, |
| 40 | ): Pick<TextSelection, 'passage' | 'highlight'> => { |
| 41 | const { commonAncestorContainer: node } = range; |
| 42 | const element = |
| 43 | node instanceof Element ? node : (node.parentElement as Element | null); |
| 44 | const block = element?.closest(PASSAGE_SELECTOR); |
| 45 | const passage = block?.textContent |
| 46 | ? collapseWhitespace(block.textContent) |
| 47 | : undefined; |
| 48 | |
| 49 | if (!block || !passage) { |
| 50 | return { passage: text }; |
| 51 | } |
| 52 | |
| 53 | const lead = document.createRange(); |
| 54 | lead.setStart(block, 0); |
| 55 | lead.setEnd(range.startContainer, range.startOffset); |
| 56 | const highlight = findHighlightRange( |
| 57 | passage, |
| 58 | collapseWhitespace(text), |
| 59 | collapseWhitespace(lead.toString()).length, |
| 60 | ); |
| 61 | |
| 62 | return highlight ? { passage, highlight } : { passage: text }; |
| 63 | }; |
| 64 | |
| 65 | /** How long the selection has to hold still before the toolbar commits to it. */ |
| 66 | const SETTLE_MS = 150; |
no test coverage detected