tooot/src/components/Parse/HTML.tsx

305 lines
8.5 KiB
TypeScript
Raw Normal View History

import Icon from '@components/Icon'
2021-01-01 16:48:16 +01:00
import openLink from '@components/openLink'
2021-01-01 17:52:14 +01:00
import ParseEmojis from '@components/Parse/Emojis'
2021-01-10 02:12:14 +01:00
import { useNavigation, useRoute } from '@react-navigation/native'
2021-01-01 16:48:16 +01:00
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
2020-12-26 12:59:16 +01:00
import { LinearGradient } from 'expo-linear-gradient'
2021-01-07 19:13:09 +01:00
import React, { useCallback, useState } from 'react'
2021-01-10 02:12:14 +01:00
import { Pressable, Text, View } from 'react-native'
2020-12-01 00:44:28 +01:00
import HTMLView from 'react-native-htmlview'
import Animated, {
useAnimatedStyle,
useDerivedValue,
withTiming
} from 'react-native-reanimated'
2020-11-23 00:07:32 +01:00
2020-11-05 21:47:50 +01:00
// Prevent going to the same hashtag multiple times
2020-10-31 21:04:46 +01:00
const renderNode = ({
2021-01-10 02:12:14 +01:00
routeParams,
2020-11-23 00:07:32 +01:00
theme,
2020-10-31 21:04:46 +01:00
node,
index,
2020-12-02 00:16:27 +01:00
size,
2020-10-31 21:04:46 +01:00
navigation,
mentions,
2020-12-28 17:30:20 +01:00
tags,
2021-01-04 18:29:02 +01:00
showFullLink,
disableDetails
2020-10-31 21:04:46 +01:00
}: {
2021-01-10 02:12:14 +01:00
routeParams?: any
2020-11-23 00:07:32 +01:00
theme: any
node: any
2020-10-31 21:04:46 +01:00
index: number
2020-12-12 22:19:18 +01:00
size: 'M' | 'L'
navigation: any
mentions?: Mastodon.Mention[]
2020-12-28 17:30:20 +01:00
tags?: Mastodon.Tag[]
2020-10-31 21:04:46 +01:00
showFullLink: boolean
2021-01-04 18:29:02 +01:00
disableDetails: boolean
2020-10-31 21:04:46 +01:00
}) => {
2021-01-07 19:13:09 +01:00
switch (node.name) {
case 'a':
const classes = node.attribs.class
const href = node.attribs.href
if (classes) {
if (classes.includes('hashtag')) {
2021-01-10 02:12:14 +01:00
const tag = href.split(new RegExp(/\/tag\/(.*)|\/tags\/(.*)/))
const differentTag = routeParams?.hashtag
? routeParams.hashtag !== tag[1] && routeParams.hashtag !== tag[2]
: true
2021-01-07 19:13:09 +01:00
return (
<Text
key={index}
style={{
color: theme.blue,
...StyleConstants.FontStyle[size]
}}
onPress={() => {
!disableDetails &&
2021-01-10 02:12:14 +01:00
differentTag &&
2021-01-07 19:13:09 +01:00
navigation.push('Screen-Shared-Hashtag', {
hashtag: tag[1] || tag[2]
})
}}
>
{node.children[0].data}
{node.children[1]?.children[0].data}
</Text>
)
} else if (classes.includes('mention') && mentions) {
const accountIndex = mentions.findIndex(
mention => mention.url === href
)
2021-01-10 02:12:14 +01:00
const differentAccount = routeParams?.account
? routeParams.account.id !== mentions[accountIndex].id
: true
2021-01-07 19:13:09 +01:00
return (
<Text
key={index}
style={{
color: accountIndex !== -1 ? theme.blue : undefined,
...StyleConstants.FontStyle[size]
}}
onPress={() => {
accountIndex !== -1 &&
!disableDetails &&
2021-01-10 02:12:14 +01:00
differentAccount &&
2021-01-07 19:13:09 +01:00
navigation.push('Screen-Shared-Account', {
account: mentions[accountIndex]
})
}}
>
{node.children[0].data}
{node.children[1]?.children[0].data}
</Text>
)
}
} else {
const domain = href.split(new RegExp(/:\/\/(.[^\/]+)/))
// Need example here
const content =
node.children && node.children[0] && node.children[0].data
const shouldBeTag =
tags && tags.filter(tag => `#${tag.name}` === content).length > 0
2020-10-28 00:02:37 +01:00
return (
<Text
key={index}
2020-12-12 22:19:18 +01:00
style={{
2020-12-26 00:40:27 +01:00
color: theme.blue,
...StyleConstants.FontStyle[size]
2020-12-12 22:19:18 +01:00
}}
2021-01-07 19:13:09 +01:00
onPress={async () =>
!disableDetails && !shouldBeTag
? await openLink(href)
: navigation.push('Screen-Shared-Hashtag', {
hashtag: content.substring(1)
})
}
2020-10-28 00:02:37 +01:00
>
2021-01-07 19:13:09 +01:00
{!shouldBeTag ? (
<Icon
color={theme.blue}
name='ExternalLink'
size={StyleConstants.Font.Size[size]}
/>
) : null}
{content || (showFullLink ? href : domain[1])}
2020-10-28 00:02:37 +01:00
</Text>
)
}
2021-01-07 19:13:09 +01:00
break
case 'p':
if (!node.children.length) {
2020-12-26 12:59:16 +01:00
return <View key={index} /> // bug when the tag is empty
}
2021-01-07 19:13:09 +01:00
break
2020-10-28 00:02:37 +01:00
}
}
2020-10-31 21:04:46 +01:00
export interface Props {
content: string
2020-12-28 00:59:57 +01:00
size?: 'M' | 'L'
emojis?: Mastodon.Emoji[]
mentions?: Mastodon.Mention[]
2020-12-28 17:30:20 +01:00
tags?: Mastodon.Tag[]
2020-10-31 21:04:46 +01:00
showFullLink?: boolean
2020-12-01 00:44:28 +01:00
numberOfLines?: number
2020-12-26 12:59:16 +01:00
expandHint?: string
2021-01-04 18:29:02 +01:00
disableDetails?: boolean
2020-10-31 21:04:46 +01:00
}
2021-01-01 16:48:16 +01:00
const ParseHTML: React.FC<Props> = ({
2020-10-28 00:02:37 +01:00
content,
2020-12-28 00:59:57 +01:00
size = 'M',
2020-10-28 00:02:37 +01:00
emojis,
mentions,
2020-12-28 17:30:20 +01:00
tags,
2020-10-29 14:52:28 +01:00
showFullLink = false,
2020-12-26 12:59:16 +01:00
numberOfLines = 10,
2021-01-04 18:29:02 +01:00
expandHint = '全文',
disableDetails = false
2020-10-31 21:04:46 +01:00
}) => {
2020-10-28 00:02:37 +01:00
const navigation = useNavigation()
2021-01-10 02:12:14 +01:00
const route = useRoute()
2020-11-23 00:07:32 +01:00
const { theme } = useTheme()
2020-10-28 00:02:37 +01:00
2020-11-28 17:07:30 +01:00
const renderNodeCallback = useCallback(
(node, index) =>
2020-12-02 00:16:27 +01:00
renderNode({
2021-01-10 02:12:14 +01:00
routeParams: route.params,
2020-12-02 00:16:27 +01:00
theme,
node,
index,
size,
navigation,
mentions,
2020-12-28 17:30:20 +01:00
tags,
2021-01-04 18:29:02 +01:00
showFullLink,
disableDetails
2020-12-02 00:16:27 +01:00
}),
2020-11-28 17:07:30 +01:00
[]
)
const textComponent = useCallback(({ children }) => {
2020-12-26 12:59:16 +01:00
if (children) {
2021-01-04 14:55:34 +01:00
return (
<ParseEmojis
content={children.toString()}
emojis={emojis}
size={size}
/>
)
2020-12-26 12:59:16 +01:00
} else {
return null
}
}, [])
2020-12-26 23:05:17 +01:00
const rootComponent = useCallback(
({ children }) => {
const lineHeight = StyleConstants.Font.LineHeight[size]
2020-12-26 12:59:16 +01:00
2021-01-04 14:55:34 +01:00
const [lines, setLines] = useState<number | undefined>(undefined)
2020-12-26 23:05:17 +01:00
const [heightOriginal, setHeightOriginal] = useState<number>()
const [heightTruncated, setHeightTruncated] = useState<number>()
2021-01-04 14:55:34 +01:00
const [expandAllow, setExpandAllow] = useState(false)
const [expanded, setExpanded] = useState(false)
2020-12-26 12:59:16 +01:00
const viewHeight = useDerivedValue(() => {
2021-01-04 14:55:34 +01:00
if (expandAllow) {
if (expanded) {
return heightOriginal as number
} else {
return heightTruncated as number
}
} else {
return heightOriginal as number
}
2021-01-04 14:55:34 +01:00
}, [heightOriginal, heightTruncated, expandAllow, expanded])
const ViewHeight = useAnimatedStyle(() => {
return {
2021-01-04 14:55:34 +01:00
height: expandAllow
? expanded
? withTiming(viewHeight.value)
: withTiming(viewHeight.value)
2021-01-04 14:55:34 +01:00
: viewHeight.value
}
})
2021-01-01 16:48:16 +01:00
const onLayout = useCallback(
({ nativeEvent }) => {
2021-01-04 14:55:34 +01:00
if (!heightOriginal) {
setHeightOriginal(nativeEvent.layout.height)
setLines(numberOfLines === 0 ? 1 : numberOfLines)
2021-01-01 16:48:16 +01:00
} else {
2021-01-04 14:55:34 +01:00
if (!heightTruncated) {
setHeightTruncated(nativeEvent.layout.height)
setLines(undefined)
2021-01-01 16:48:16 +01:00
} else {
2021-01-04 14:55:34 +01:00
if (heightOriginal > heightTruncated) {
setExpandAllow(true)
2021-01-01 16:48:16 +01:00
}
}
}
},
[heightOriginal, heightTruncated]
)
2020-12-26 23:05:17 +01:00
return (
<View>
<Animated.View style={[ViewHeight, { overflow: 'hidden' }]}>
2021-01-04 14:55:34 +01:00
<Animated.Text
children={children}
onLayout={onLayout}
numberOfLines={lines}
style={{
...StyleConstants.FontStyle[size],
color: theme.primary,
2021-01-04 14:55:34 +01:00
height: expandAllow ? heightOriginal : undefined
}}
/>
</Animated.View>
2021-01-04 14:55:34 +01:00
{expandAllow ? (
2020-12-26 23:05:17 +01:00
<Pressable
2021-01-04 14:55:34 +01:00
onPress={() => setExpanded(!expanded)}
style={{ marginTop: expanded ? 0 : -lineHeight * 1.25 }}
2020-12-02 00:16:27 +01:00
>
2020-12-26 23:05:17 +01:00
<LinearGradient
colors={[
theme.backgroundGradientStart,
theme.backgroundGradientEnd
]}
locations={[0, lineHeight / (StyleConstants.Font.Size.S * 4)]}
2020-12-02 00:16:27 +01:00
style={{
2020-12-26 23:05:17 +01:00
paddingTop: StyleConstants.Font.Size.S * 2,
paddingBottom: StyleConstants.Font.Size.S
2020-12-02 00:16:27 +01:00
}}
>
2020-12-26 23:05:17 +01:00
<Text
style={{
textAlign: 'center',
...StyleConstants.FontStyle.S,
2020-12-26 23:05:17 +01:00
color: theme.primary
}}
>
2021-01-04 14:55:34 +01:00
{`${expanded ? '折叠' : '展开'}${expandHint}`}
2020-12-26 23:05:17 +01:00
</Text>
</LinearGradient>
</Pressable>
2021-01-01 16:48:16 +01:00
) : null}
2020-12-26 23:05:17 +01:00
</View>
)
},
[theme]
)
2020-11-28 17:07:30 +01:00
2020-10-28 00:02:37 +01:00
return (
<HTMLView
value={content}
2020-11-28 17:07:30 +01:00
TextComponent={textComponent}
RootComponent={rootComponent}
renderNode={renderNodeCallback}
2020-10-28 00:02:37 +01:00
/>
)
}
2021-01-01 16:48:16 +01:00
export default ParseHTML