From 2272ea3841ed7120cb34c81ed086ca561507c6f9 Mon Sep 17 00:00:00 2001 From: Zhiyuan Zheng Date: Mon, 7 Dec 2020 23:34:47 +0100 Subject: [PATCH] Rewrite formatText logic Previously cannot has two same tags due to `split` --- src/screens/Shared/Compose/formatText.tsx | 30 +++++++++++------------ 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/screens/Shared/Compose/formatText.tsx b/src/screens/Shared/Compose/formatText.tsx index 910741ae..5adaef2b 100644 --- a/src/screens/Shared/Compose/formatText.tsx +++ b/src/screens/Shared/Compose/formatText.tsx @@ -17,11 +17,7 @@ export interface Params { const TagText = ({ text }: { text: string }) => { const { theme } = useTheme() - return ( - - {text} - - ) + return {text} } const debouncedSuggestions = debounce( @@ -65,7 +61,8 @@ const formatText = ({ tags.push({ type: newType, text: props.getMatchedText(), - offset: props.getOffset() + offset: props.getOffset(), + length: props.getMatchedText().length }) return } @@ -82,27 +79,30 @@ const formatText = ({ } prevTags = tags let _content = content + let pointer = 0 let contentLength: number = 0 const children = [] - tags.forEach(tag => { - const parts = _content.split(tag!.text) - const prevPart = parts.shift() - children.push(prevPart) - contentLength = contentLength + (prevPart ? prevPart.length : 0) - children.push() + tags.forEach((tag, index) => { + const prev = _content.substr(0, tag!.offset - pointer) + const main = _content.substr(tag!.offset - pointer, tag!.length) + const next = _content.substr(tag!.offset - pointer + tag!.length) + children.push(prev) + contentLength = contentLength + prev.length + children.push() switch (tag!.type) { case 'url': contentLength = contentLength + 23 break case 'accounts': contentLength = - contentLength + tag!.text.split(new RegExp('(@.*)@?'))[1].length + contentLength + main.split(new RegExp('(@.*)@?'))[1].length break case 'hashtags': - contentLength = contentLength + tag!.text.length + contentLength = contentLength + main.length break } - _content = parts.join() + _content = next + pointer = pointer + prev.length + tag!.length }) children.push(_content) contentLength = contentLength + _content.length