Total toot length count working

This commit is contained in:
Zhiyuan Zheng 2020-11-09 23:06:07 +01:00
parent d8d8a6c1fa
commit 6804e3bde6
No known key found for this signature in database
GPG Key ID: 078A93AB607D85E0
1 changed files with 38 additions and 14 deletions

View File

@ -21,6 +21,11 @@ import { FlatList } from 'react-native-gesture-handler'
const Stack = createNativeStackNavigator() const Stack = createNativeStackNavigator()
export interface Tag {
type: 'url' | 'accounts' | 'hashtags'
text: string
}
const Suggestion = React.memo(({ item, index }) => { const Suggestion = React.memo(({ item, index }) => {
return ( return (
<View key={index}> <View key={index}>
@ -104,23 +109,32 @@ const PostTootMain = () => {
debounce(tag => setSuggestionsShown({ display: true, tag }), 300), debounce(tag => setSuggestionsShown({ display: true, tag }), 300),
[] []
) )
let prevTags: { type: 'url' | 'mention' | 'hashtag'; text: string }[] = [] let prevTags: Tag[] = []
const onChangeText = useCallback(content => { const onChangeText = useCallback(content => {
const tags: { type: 'url' | 'mention' | 'hashtag'; text: string }[] = [] const tags: Tag[] = []
Autolinker.link(content, { Autolinker.link(content, {
email: false, email: false,
phone: false, phone: false,
mention: 'mastodon', mention: 'mastodon',
hashtag: 'twitter', hashtag: 'twitter',
replaceFn: props => { replaceFn: props => {
let type = props.getType()
switch (type) {
case 'mention':
type = 'accounts'
break
case 'hashtag':
type = 'hashtags'
break
}
// @ts-ignore // @ts-ignore
tags.push({ type: props.getType(), text: props.getMatchedText() }) tags.push({ type: type, text: props.getMatchedText() })
return return
} }
}) })
const changedTag = differenceWith(prevTags, tags, isEqual) const changedTag = differenceWith(prevTags, tags, isEqual)
if (changedTag.length) { if (changedTag.length && tags.length !== 0) {
if (changedTag[0].type !== 'url') { if (changedTag[0].type !== 'url') {
debouncedSuggestions(changedTag[0]) debouncedSuggestions(changedTag[0])
} }
@ -128,23 +142,38 @@ const PostTootMain = () => {
setSuggestionsShown({ display: false, tag: undefined }) setSuggestionsShown({ display: false, tag: undefined })
} }
prevTags = tags prevTags = tags
let _content = content let _content = content
let contentLength: number = 0
const children = [] const children = []
tags.forEach(tag => { tags.forEach(tag => {
const parts = _content.split(tag.text) const parts = _content.split(tag.text)
children.push(parts.shift()) const prevPart = parts.shift()
children.push(prevPart)
contentLength = contentLength + prevPart.length
children.push( children.push(
<Text style={{ color: 'red' }} key={Math.random()}> <Text style={{ color: 'red' }} key={Math.random()}>
{tag.text} {tag.text}
</Text> </Text>
) )
_content = parts.join(tag.text) switch (tag.type) {
case 'url':
contentLength = contentLength + 23
break
case 'accounts':
contentLength =
contentLength + tag.text.split(new RegExp('(@.*)@?'))[1].length
break
case 'hashtags':
contentLength = contentLength + tag.text.length
break
}
_content = parts.join()
}) })
children.push(_content) children.push(_content)
contentLength = contentLength + _content.length
setFormattedText(React.createElement(Text, null, children)) setFormattedText(React.createElement(Text, null, children))
setCharCount(content.length) setCharCount(500 - contentLength)
}, []) }, [])
return ( return (
@ -176,12 +205,7 @@ const PostTootMain = () => {
<Text>{formattedText}</Text> <Text>{formattedText}</Text>
</TextInput> </TextInput>
{suggestionsShown.display ? ( {suggestionsShown.display ? (
<View <View style={[styles.suggestions]}>
style={[
styles.suggestions
// { height: viewHeight - contentHeight - keyboardHeight - 44 }
]}
>
<Suggestions {...suggestionsShown.tag} /> <Suggestions {...suggestionsShown.tag} />
</View> </View>
) : ( ) : (