This repository was archived by the owner on Jun 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathSelectableText.js
148 lines (133 loc) · 4.23 KB
/
SelectableText.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import React from 'react'
import { Text, requireNativeComponent, Platform } from 'react-native'
import { v4 } from 'uuid'
import memoize from 'fast-memoize'
const RNSelectableText = requireNativeComponent('RNSelectableText')
/**
* numbers: array({start: int, end: int, id: string})
*/
const combineHighlights = memoize(numbers => {
return numbers
.sort((a, b) => a.start - b.start || a.end - b.end)
.reduce(function(combined, next) {
if (!combined.length || combined[combined.length - 1].end < next.start) combined.push(next)
else {
var prev = combined.pop()
combined.push({
start: prev.start,
end: Math.max(prev.end, next.end),
id: next.id,
})
}
return combined
}, [])
})
/**
* value: string
* highlights: array({start: int, end: int, id: any})
*/
const mapHighlightsRanges = (value, highlights) => {
const combinedHighlights = combineHighlights(highlights)
if (combinedHighlights.length === 0) return [{ isHighlight: false, text: value }]
const data = [{ isHighlight: false, text: value.slice(0, combinedHighlights[0].start) }]
combinedHighlights.forEach(({ start, end }, idx) => {
data.push({
isHighlight: true,
text: value.slice(start, end),
})
if (combinedHighlights[idx + 1]) {
data.push({
isHighlight: false,
text: value.slice(end, combinedHighlights[idx + 1].start),
})
}
})
data.push({
isHighlight: false,
text: value.slice(combinedHighlights[combinedHighlights.length - 1].end, value.length),
})
return data.filter(x => x.text)
}
/**
* Props
* ...TextProps
* onSelection: ({ content: string, eventType: string, selectionStart: int, selectionEnd: int }) => void
* children: ReactNode
* highlights: array({ id, start, end })
* highlightColor: string
* onHighlightPress: string => void
* textValueProp: string
* TextComponent: ReactNode
* textComponentProps: object
*/
export const SelectableText = ({
onSelection, onHighlightPress, textValueProp, value, TextComponent,
textComponentProps, ...props
}) => {
const usesTextComponent = !TextComponent;
TextComponent = TextComponent || Text;
textValueProp = textValueProp || 'children'; // default to `children` which will render `value` as a child of `TextComponent`
const onSelectionNative = ({
nativeEvent: { content, eventType, selectionStart, selectionEnd },
}) => {
onSelection && onSelection({ content, eventType, selectionStart, selectionEnd })
}
const onHighlightPressNative = onHighlightPress
? Platform.OS === 'ios'
? ({ nativeEvent: { clickedRangeStart, clickedRangeEnd } }) => {
if (!props.highlights || props.highlights.length === 0) return
const mergedHighlights = combineHighlights(props.highlights)
const hightlightInRange = mergedHighlights.find(
({ start, end }) => clickedRangeStart >= start - 1 && clickedRangeEnd <= end + 1,
)
if (hightlightInRange) {
onHighlightPress(hightlightInRange.id)
}
}
: onHighlightPress
: () => {}
// highlights feature is only supported if `TextComponent == Text`
let textValue = value;
if (usesTextComponent) {
textValue = (
props.highlights && props.highlights.length > 0
? mapHighlightsRanges(value, props.highlights).map(({ id, isHighlight, text }) => (
<Text
key={v4()}
selectable
style={
isHighlight
? {
backgroundColor: props.highlightColor,
}
: {}
}
onPress={() => {
if (isHighlight) {
onHighlightPress && onHighlightPress(id)
}
}}
>
{text}
</Text>
))
: [value]
);
if (props.appendToChildren) {
textValue.push(props.appendToChildren);
}
}
return (
<RNSelectableText
{...props}
onHighlightPress={onHighlightPressNative}
selectable
onSelection={onSelectionNative}
>
<TextComponent
key={v4()}
{...{[textValueProp]: textValue, ...textComponentProps}}
/>
</RNSelectableText>
)
}