2022-05-07 00:52:32 +02:00
|
|
|
import CustomText from '@components/Text'
|
2021-03-27 00:02:32 +01:00
|
|
|
import { useAccessibility } from '@utils/accessibility/AccessibilityManager'
|
2023-01-31 14:26:43 +01:00
|
|
|
import { connectMedia } from '@utils/api/helpers/connect'
|
2022-12-28 23:41:36 +01:00
|
|
|
import { useGlobalStorage } from '@utils/storage/actions'
|
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'
|
2023-02-08 01:10:59 +01:00
|
|
|
import { Image } from 'expo-image'
|
2022-12-19 23:06:39 +01:00
|
|
|
import React from 'react'
|
2023-01-29 19:27:15 +01:00
|
|
|
import { ColorValue, Platform, TextStyle } from 'react-native'
|
2021-01-01 16:48:16 +01:00
|
|
|
|
|
|
|
const regexEmoji = new RegExp(/(:[A-Za-z0-9_]+:)/)
|
|
|
|
|
|
|
|
export interface Props {
|
2022-12-22 18:38:04 +01:00
|
|
|
content?: string
|
2021-01-01 16:48:16 +01:00
|
|
|
emojis?: Mastodon.Emoji[]
|
|
|
|
size?: 'S' | 'M' | 'L'
|
2023-01-29 19:27:15 +01:00
|
|
|
color?: ColorValue
|
2021-03-10 10:22:53 +01:00
|
|
|
adaptiveSize?: boolean
|
2021-01-01 16:48:16 +01:00
|
|
|
fontBold?: boolean
|
2022-12-11 16:52:34 +01:00
|
|
|
style?: TextStyle
|
2021-01-01 16:48:16 +01:00
|
|
|
}
|
|
|
|
|
2022-12-29 00:36:35 +01:00
|
|
|
const ParseEmojis: React.FC<Props> = ({
|
|
|
|
content,
|
|
|
|
emojis,
|
|
|
|
size = 'M',
|
2023-01-29 19:27:15 +01:00
|
|
|
color,
|
2022-12-29 00:36:35 +01:00
|
|
|
adaptiveSize = false,
|
|
|
|
fontBold = false,
|
|
|
|
style
|
|
|
|
}) => {
|
|
|
|
if (!content) return null
|
2022-12-22 18:38:04 +01:00
|
|
|
|
2022-12-29 00:36:35 +01:00
|
|
|
const { reduceMotionEnabled } = useAccessibility()
|
2021-03-27 00:02:32 +01:00
|
|
|
|
2022-12-29 00:36:35 +01:00
|
|
|
const [adaptiveFontsize] = useGlobalStorage.number('app.font_size')
|
|
|
|
const adaptedFontsize = adaptiveScale(
|
|
|
|
StyleConstants.Font.Size[size],
|
|
|
|
adaptiveSize ? adaptiveFontsize : 0
|
|
|
|
)
|
|
|
|
const adaptedLineheight = adaptiveScale(
|
|
|
|
StyleConstants.Font.LineHeight[size],
|
|
|
|
adaptiveSize ? adaptiveFontsize : 0
|
|
|
|
)
|
2021-03-10 10:22:53 +01:00
|
|
|
|
2023-01-29 19:27:15 +01:00
|
|
|
const { colors } = useTheme()
|
2021-01-01 16:48:16 +01:00
|
|
|
|
2022-12-29 00:36:35 +01:00
|
|
|
return (
|
|
|
|
<CustomText
|
|
|
|
style={[
|
|
|
|
{
|
2023-01-29 19:27:15 +01:00
|
|
|
color: color || colors.primaryDefault,
|
2022-12-29 00:36:35 +01:00
|
|
|
fontSize: adaptedFontsize,
|
|
|
|
lineHeight: adaptedLineheight
|
|
|
|
},
|
|
|
|
style
|
|
|
|
]}
|
|
|
|
fontWeight={fontBold ? 'Bold' : undefined}
|
|
|
|
>
|
|
|
|
{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) {
|
|
|
|
return <CustomText key={emojiShortcode + i}>{emojiShortcode}</CustomText>
|
|
|
|
} else {
|
|
|
|
const uri = reduceMotionEnabled
|
|
|
|
? emojis[emojiIndex].static_url
|
|
|
|
: emojis[emojiIndex].url
|
2023-01-03 23:57:23 +01:00
|
|
|
return (
|
|
|
|
<CustomText key={emojiShortcode + i}>
|
|
|
|
{i === 0 ? ' ' : undefined}
|
2023-02-08 01:10:59 +01:00
|
|
|
<Image
|
|
|
|
source={connectMedia({ uri })}
|
2023-01-03 23:57:23 +01:00
|
|
|
style={{
|
|
|
|
width: adaptedFontsize,
|
|
|
|
height: adaptedFontsize,
|
|
|
|
transform: [{ translateY: Platform.OS === 'ios' ? -1 : 2 }]
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</CustomText>
|
|
|
|
)
|
2021-02-01 02:16:53 +01:00
|
|
|
}
|
2022-12-29 00:36:35 +01:00
|
|
|
} else {
|
|
|
|
return <CustomText key={i}>{str}</CustomText>
|
|
|
|
}
|
|
|
|
})
|
|
|
|
) : (
|
|
|
|
<CustomText>{content}</CustomText>
|
|
|
|
)}
|
|
|
|
</CustomText>
|
|
|
|
)
|
|
|
|
}
|
2021-01-01 16:48:16 +01:00
|
|
|
|
2021-02-01 02:16:53 +01:00
|
|
|
export default ParseEmojis
|