tooot/src/components/Parse/Emojis.tsx

107 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-03-27 00:02:32 +01:00
import { useAccessibility } from '@utils/accessibility/AccessibilityManager'
2021-03-10 10:22:53 +01:00
import { getSettingsFontsize } from '@utils/slices/settingsSlice'
2021-03-10 11:49:14 +01:00
import { StyleConstants } from '@utils/styles/constants'
import { adaptiveScale } from '@utils/styles/scaling'
2021-01-19 01:13:45 +01:00
import { useTheme } from '@utils/styles/ThemeManager'
2021-01-22 01:34:20 +01:00
import React, { useMemo } from 'react'
2021-03-27 00:02:32 +01:00
import { StyleSheet, Text } from 'react-native'
import FastImage from 'react-native-fast-image'
2021-03-10 10:22:53 +01:00
import { useSelector } from 'react-redux'
import validUrl from 'valid-url'
2021-01-01 16:48:16 +01:00
const regexEmoji = new RegExp(/(:[A-Za-z0-9_]+:)/)
export interface Props {
content: string
emojis?: Mastodon.Emoji[]
size?: 'S' | 'M' | 'L'
2021-03-10 10:22:53 +01:00
adaptiveSize?: boolean
2021-01-01 16:48:16 +01:00
fontBold?: boolean
}
2021-02-01 02:16:53 +01:00
const ParseEmojis = React.memo(
2021-03-10 10:22:53 +01:00
({
content,
emojis,
size = 'M',
adaptiveSize = false,
fontBold = false
}: Props) => {
2021-03-27 00:02:32 +01:00
const { reduceMotionEnabled } = useAccessibility()
2021-03-10 10:22:53 +01:00
const adaptiveFontsize = useSelector(getSettingsFontsize)
const adaptedFontsize = adaptiveScale(
StyleConstants.Font.Size[size],
adaptiveSize ? adaptiveFontsize : 0
)
const adaptedLineheight = adaptiveScale(
StyleConstants.Font.LineHeight[size],
adaptiveSize ? adaptiveFontsize : 0
)
2021-02-01 02:16:53 +01:00
const { mode, theme } = useTheme()
const styles = useMemo(() => {
return StyleSheet.create({
text: {
color: theme.primaryDefault,
2021-03-10 10:22:53 +01:00
fontSize: adaptedFontsize,
lineHeight: adaptedLineheight,
2021-02-01 02:16:53 +01:00
...(fontBold && { fontWeight: StyleConstants.Font.Weight.Bold })
},
image: {
2021-03-10 10:22:53 +01:00
width: adaptedFontsize,
height: adaptedFontsize,
transform: [{ translateY: -2 }]
2021-02-01 02:16:53 +01:00
}
})
2021-03-10 10:22:53 +01:00
}, [mode, adaptiveFontsize])
2021-01-01 16:48:16 +01:00
2021-02-01 02:16:53 +01:00
return (
<Text style={styles.text}>
{emojis ? (
content
.split(regexEmoji)
.filter(f => f)
.map((str, i) => {
if (str.match(regexEmoji)) {
const emojiShortcode = str.split(regexEmoji)[1]
const emojiIndex = emojis.findIndex(emoji => {
return emojiShortcode === `:${emoji.shortcode}:`
})
if (emojiIndex === -1) {
2021-04-01 18:39:53 +02:00
return <Text key={emojiShortcode}>{emojiShortcode}</Text>
} else {
if (i === 0) {
return <Text key={emojiShortcode}> </Text>
} else {
const uri = reduceMotionEnabled
? emojis[emojiIndex].static_url
: emojis[emojiIndex].url
if (validUrl.isHttpsUri(uri)) {
return (
<FastImage
key={emojiShortcode}
source={{ uri }}
style={styles.image}
/>
)
} else {
return null
}
}
}
2021-02-01 02:16:53 +01:00
} else {
return <Text key={str}>{str}</Text>
2021-02-01 02:16:53 +01:00
}
})
) : (
<Text>{content}</Text>
)}
</Text>
)
},
(prev, next) => prev.content === next.content
)
2021-01-01 16:48:16 +01:00
2021-02-01 02:16:53 +01:00
export default ParseEmojis