1
0
mirror of https://github.com/tooot-app/app synced 2025-02-14 10:50:48 +01:00

Rewrite formatText logic

Previously cannot has two same tags due to `split`
This commit is contained in:
Zhiyuan Zheng 2020-12-07 23:34:47 +01:00
parent 8d715f4324
commit 2272ea3841
No known key found for this signature in database
GPG Key ID: 078A93AB607D85E0

View File

@ -17,11 +17,7 @@ export interface Params {
const TagText = ({ text }: { text: string }) => { const TagText = ({ text }: { text: string }) => {
const { theme } = useTheme() const { theme } = useTheme()
return ( return <Text style={{ color: theme.link }}>{text}</Text>
<Text style={{ color: theme.link }} key={Math.random()}>
{text}
</Text>
)
} }
const debouncedSuggestions = debounce( const debouncedSuggestions = debounce(
@ -65,7 +61,8 @@ const formatText = ({
tags.push({ tags.push({
type: newType, type: newType,
text: props.getMatchedText(), text: props.getMatchedText(),
offset: props.getOffset() offset: props.getOffset(),
length: props.getMatchedText().length
}) })
return return
} }
@ -82,27 +79,30 @@ const formatText = ({
} }
prevTags = tags prevTags = tags
let _content = content let _content = content
let pointer = 0
let contentLength: number = 0 let contentLength: number = 0
const children = [] const children = []
tags.forEach(tag => { tags.forEach((tag, index) => {
const parts = _content.split(tag!.text) const prev = _content.substr(0, tag!.offset - pointer)
const prevPart = parts.shift() const main = _content.substr(tag!.offset - pointer, tag!.length)
children.push(prevPart) const next = _content.substr(tag!.offset - pointer + tag!.length)
contentLength = contentLength + (prevPart ? prevPart.length : 0) children.push(prev)
children.push(<TagText text={tag!.text} />) contentLength = contentLength + prev.length
children.push(<TagText key={index} text={main} />)
switch (tag!.type) { switch (tag!.type) {
case 'url': case 'url':
contentLength = contentLength + 23 contentLength = contentLength + 23
break break
case 'accounts': case 'accounts':
contentLength = contentLength =
contentLength + tag!.text.split(new RegExp('(@.*)@?'))[1].length contentLength + main.split(new RegExp('(@.*)@?'))[1].length
break break
case 'hashtags': case 'hashtags':
contentLength = contentLength + tag!.text.length contentLength = contentLength + main.length
break break
} }
_content = parts.join() _content = next
pointer = pointer + prev.length + tag!.length
}) })
children.push(_content) children.push(_content)
contentLength = contentLength + _content.length contentLength = contentLength + _content.length