MCPcopy Create free account
hub / github.com/dailydotdev/apps / readPassage

Function readPassage

packages/shared/src/features/snapshot/useTextSelection.ts:37–63  ·  view source on GitHub ↗
(
  range: Range,
  text: string,
)

Source from the content-addressed store, hash-verified

35 * first occurrence.
36 */
37const 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. */
66const SETTLE_MS = 150;

Callers 1

readFunction · 0.85

Calls 2

collapseWhitespaceFunction · 0.90
findHighlightRangeFunction · 0.90

Tested by

no test coverage detected