MCPcopy Create free account
hub / github.com/pacexy/flow / TextSelectionMenu

Function TextSelectionMenu

apps/reader/src/components/TextSelectionMenu.tsx:35–102  ·  view source on GitHub ↗
({
  tab,
})

Source from the content-addressed store, hash-verified

33 tab: BookTab
34}
35export const TextSelectionMenu: React.FC<TextSelectionMenuProps> = ({
36 tab,
37}) => {
38 const { rendition, annotationRange } = useSnapshot(tab)
39 const [settings] = useSettings()
40
41 // `manager` is not reactive, so we need to use getter
42 const view = useCallback(() => {
43 return rendition?.manager?.views._views[0]
44 }, [rendition])
45
46 const win = view()?.window
47 const [selection, setSelection] = useTextSelection(win)
48
49 // If text selection menu is disabled, don't render it
50 if (settings.enableTextSelectionMenu === false) {
51 return null
52 }
53
54 const el = view()?.element as HTMLElement
55 if (!el) return null
56
57 // it is possible that both `selection` and `tab.annotationRange`
58 // are set when select end within an annotation
59 const range = selection?.getRangeAt(0) ?? annotationRange
60 if (!range) return null
61
62 // prefer to display above the selection to avoid text selection helpers
63 // https://stackoverflow.com/questions/68081757/hide-the-two-text-selection-helpers-in-mobile-browsers
64 const forward = isTouchScreen
65 ? false
66 : selection
67 ? isForwardSelection(selection)
68 : true
69
70 const rects = [...range.getClientRects()].filter((r) => Math.round(r.width))
71 const anchorRect = rects && (forward ? last(rects) : rects[0])
72 if (!anchorRect) return null
73
74 const contents = range.cloneContents()
75 const text = contents.textContent?.trim()
76 if (!text) return null
77
78 return (
79 // to reset inner state
80 <TextSelectionMenuRenderer
81 tab={tab}
82 range={range as Range}
83 anchorRect={anchorRect}
84 containerRect={el.parentElement!.getBoundingClientRect()}
85 viewRect={el.getBoundingClientRect()}
86 text={text}
87 forward={forward}
88 hide={() => {
89 if (selection) {
90 selection.removeAllRanges()
91 setSelection(undefined)
92 }

Callers

nothing calls this directly

Calls 6

useSettingsFunction · 0.90
useTextSelectionFunction · 0.90
isForwardSelectionFunction · 0.90
lastFunction · 0.90
filterMethod · 0.80
trimMethod · 0.80

Tested by

no test coverage detected