This repository has been archived by the owner on Jun 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from Astrocoders/patched-highlights
feature/highlights
- Loading branch information
Showing
8 changed files
with
193 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,129 @@ | ||
import React from "react"; | ||
import { Text, requireNativeComponent, Platform } from "react-native"; | ||
import React from 'react' | ||
import { Text, requireNativeComponent, Platform } from 'react-native' | ||
import { v4 } from 'uuid' | ||
import memoize from 'fast-memoize' | ||
|
||
const RNSelectableText = requireNativeComponent("RNSelectableText"); | ||
const RNSelectableText = requireNativeComponent('RNSelectableText') | ||
|
||
export const SelectableText = ({ onSelection, value, children, ...props }) => { | ||
/** | ||
* 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 | ||
*/ | ||
export const SelectableText = ({ onSelection, onHighlightPress, value, children, ...props }) => { | ||
const onSelectionNative = ({ | ||
nativeEvent: { content, eventType, selectionStart, selectionEnd } | ||
nativeEvent: { content, eventType, selectionStart, selectionEnd }, | ||
}) => { | ||
onSelection && | ||
onSelection({ 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 | ||
: () => {} | ||
|
||
return Platform.OS === "ios" ? ( | ||
return ( | ||
<RNSelectableText | ||
{...props} | ||
value={children ? children : value} | ||
onHighlightPress={onHighlightPressNative} | ||
selectable | ||
onSelection={onSelectionNative} | ||
/> | ||
) : ( | ||
<RNSelectableText {...props} selectable onSelection={onSelectionNative}> | ||
{children ? children : <Text selectable>{value}</Text>} | ||
> | ||
<Text selectable key={v4()}> | ||
{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} | ||
{props.appendToChildren ? props.appendToChildren : null} | ||
</Text> | ||
</RNSelectableText> | ||
); | ||
}; | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. | ||
# yarn lockfile v1 | ||
|
||
|
||
fast-memoize@^2.5.1: | ||
version "2.5.1" | ||
resolved "https://registry.yarnpkg.com/fast-memoize/-/fast-memoize-2.5.1.tgz#c3519241e80552ce395e1a32dcdde8d1fd680f5d" | ||
integrity sha512-xdmw296PCL01tMOXx9mdJSmWY29jQgxyuZdq0rEHMu+Tpe1eOEtCycoG6chzlcrWsNgpZP7oL8RiQr7+G6Bl6g== | ||
|
||
uuid@^3.3.2: | ||
version "3.3.2" | ||
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.2.tgz#1b4af4955eb3077c501c23872fc6513811587131" | ||
integrity sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA== |