-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7637f12
commit 7d54c01
Showing
13 changed files
with
136 additions
and
84 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
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 |
---|---|---|
@@ -1,48 +1,27 @@ | ||
import { some } from 'lodash-es' | ||
import { useContext } from 'react' | ||
import { Platform, Text, TextInput } from 'react-native' | ||
import { Platform, Text } from 'react-native' | ||
import { | ||
CustomTextualRenderer, | ||
TNode, | ||
getNativePropsForTNode, | ||
} from 'react-native-render-html' | ||
|
||
import { HtmlContext } from './HtmlContext' | ||
|
||
const resetTextInputStyle = { | ||
paddingTop: 0, | ||
marginTop: -3, | ||
paddingBottom: 3, | ||
} | ||
|
||
const TextRenderer: CustomTextualRenderer = props => { | ||
const renderProps = getNativePropsForTNode(props) | ||
|
||
const { disabledPartialSelectable } = useContext(HtmlContext) | ||
|
||
if ( | ||
disabledPartialSelectable || | ||
!renderProps.selectable || | ||
Platform.OS === 'android' || | ||
hasLink(props.tnode) | ||
) | ||
return <Text {...renderProps} /> | ||
|
||
return ( | ||
<TextInput | ||
editable={false} | ||
multiline | ||
scrollEnabled={false} | ||
style={resetTextInputStyle} | ||
textAlignVertical="top" | ||
> | ||
<Text {...renderProps} /> | ||
</TextInput> | ||
) | ||
} | ||
|
||
function hasLink(tnode: TNode): boolean { | ||
return tnode.domNode?.name === 'a' || some(tnode.children, hasLink) | ||
const { onSelectText } = useContext(HtmlContext) | ||
let renderProps = getNativePropsForTNode(props) | ||
|
||
if (Platform.OS === 'ios' && renderProps.selectable) { | ||
renderProps = { | ||
...renderProps, | ||
selectable: false, | ||
onLongPress: () => { | ||
onSelectText() | ||
}, | ||
} | ||
} | ||
|
||
return <Text {...renderProps} /> | ||
} | ||
|
||
export default TextRenderer |
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,38 @@ | ||
import { RouteProp, useRoute } from '@react-navigation/native' | ||
import { ScrollView, TextInput, View } from 'react-native' | ||
import { | ||
CustomTextualRenderer, | ||
getNativePropsForTNode, | ||
} from 'react-native-render-html' | ||
|
||
import Html from '@/components/Html' | ||
import NavBar from '@/components/NavBar' | ||
import { RootStackParamList } from '@/types' | ||
import tw from '@/utils/tw' | ||
|
||
const TextRenderer: CustomTextualRenderer = props => { | ||
return ( | ||
<TextInput {...getNativePropsForTNode(props)} multiline editable={false} /> | ||
) | ||
} | ||
|
||
export default function SelectableTextScreen() { | ||
const { | ||
params: { html }, | ||
} = useRoute<RouteProp<RootStackParamList, 'SelectableText'>>() | ||
|
||
return ( | ||
<View style={tw`bg-body-1 flex-1`}> | ||
<NavBar title="选择文本" hideSafeTop /> | ||
|
||
<ScrollView style={tw`px-4`}> | ||
<Html | ||
source={{ html }} | ||
renderers={{ | ||
_TEXT_: TextRenderer, | ||
}} | ||
/> | ||
</ScrollView> | ||
</View> | ||
) | ||
} |
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