mirror of
https://github.com/ikun0014/lx-music-mobile.git
synced 2025-07-04 05:12:09 +08:00
优化评论过长判断折起算法
This commit is contained in:
parent
5cf9863f9f
commit
333d8437e9
@ -1,22 +1,40 @@
|
|||||||
import { useState } from 'react'
|
import { memo, useMemo, useState } from 'react'
|
||||||
import { TouchableOpacity, View } from 'react-native'
|
import { TouchableOpacity, View } from 'react-native'
|
||||||
import { createStyle } from '@/utils/tools'
|
import { createStyle } from '@/utils/tools'
|
||||||
import Text from '@/components/common/Text'
|
import Text from '@/components/common/Text'
|
||||||
import { useTheme } from '@/store/theme/hook'
|
import { useTheme } from '@/store/theme/hook'
|
||||||
|
|
||||||
const TEXT_LIMIT = 130
|
const TEXT_LIMIT = 160
|
||||||
|
// const CHAR_RXP = /\n/g
|
||||||
|
|
||||||
export default ({ text }: { text: string }) => {
|
export default memo(({ text }: { text: string }) => {
|
||||||
const [show, setShow] = useState(false)
|
const [show, setShow] = useState(false)
|
||||||
const theme = useTheme()
|
const theme = useTheme()
|
||||||
|
|
||||||
|
const length = useMemo(() => {
|
||||||
|
// text.length + (text.match(CHAR_RXP)?.length ?? 0) * 40
|
||||||
|
let count = 0
|
||||||
|
let bCount = 0
|
||||||
|
for (let i = 0; i < text.length; i++) {
|
||||||
|
let char = text.charAt(i)
|
||||||
|
if (char == '\n') {
|
||||||
|
count += bCount * 24 + 20
|
||||||
|
bCount++
|
||||||
|
} else {
|
||||||
|
count++
|
||||||
|
if (char.trim() != '') bCount &&= 0
|
||||||
|
}
|
||||||
|
if (count >= TEXT_LIMIT) return i
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}, [text])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
text.length > TEXT_LIMIT ? (
|
length ? (
|
||||||
<View>
|
<View>
|
||||||
{
|
{
|
||||||
show ? <Text selectable style={styles.text}>{text}</Text>
|
show ? <Text selectable style={styles.text}>{text}</Text>
|
||||||
: <Text selectable style={styles.text}>{text.substring(0, TEXT_LIMIT)} <Text color={theme['c-font-label']}>……</Text></Text>
|
: <Text selectable style={styles.text}>{text.substring(0, length)} <Text color={theme['c-font-label']}>……</Text></Text>
|
||||||
}
|
}
|
||||||
<TouchableOpacity style={styles.toggle} onPress={() => { setShow(!show) }}>
|
<TouchableOpacity style={styles.toggle} onPress={() => { setShow(!show) }}>
|
||||||
<Text color={theme['c-primary-font']}>{show ? global.i18n.t('comment_hide_text') : global.i18n.t('comment_show_text')}</Text>
|
<Text color={theme['c-primary-font']}>{show ? global.i18n.t('comment_hide_text') : global.i18n.t('comment_show_text')}</Text>
|
||||||
@ -25,7 +43,7 @@ export default ({ text }: { text: string }) => {
|
|||||||
</View>
|
</View>
|
||||||
) : <Text selectable style={styles.text}>{text}</Text>
|
) : <Text selectable style={styles.text}>{text}</Text>
|
||||||
)
|
)
|
||||||
}
|
})
|
||||||
|
|
||||||
const styles = createStyle({
|
const styles = createStyle({
|
||||||
text: {
|
text: {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user