From ca32bf5e0c34eecd98b85f3c164647c1c6d0d46e Mon Sep 17 00:00:00 2001 From: Samuel Newman Date: Wed, 10 Apr 2024 17:35:57 +0100 Subject: [PATCH] highlight text in composer if over the character limit --- .../com/composer/text-input/TextInput.tsx | 36 ++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/view/com/composer/text-input/TextInput.tsx b/src/view/com/composer/text-input/TextInput.tsx index e9c5d86b90..0d63ff37ef 100644 --- a/src/view/com/composer/text-input/TextInput.tsx +++ b/src/view/com/composer/text-input/TextInput.tsx @@ -19,12 +19,11 @@ import PasteInput, { type PasteInputRef, // @ts-expect-error no types when installing from github } from '@mattermost/react-native-paste-input' -import {POST_IMG_MAX} from '#/lib/constants' +import {MAX_GRAPHEME_LENGTH, POST_IMG_MAX} from '#/lib/constants' import {downloadAndResize} from '#/lib/media/manip' import {isUriImage} from '#/lib/media/util' import {cleanError} from '#/lib/strings/errors' import {getMentionAt, insertMentionAt} from '#/lib/strings/mention-manip' -import {useTheme} from '#/lib/ThemeContext' import {isAndroid, isNative} from '#/platform/detection' import { type LinkFacetMatch, @@ -74,7 +73,6 @@ export const TextInput = forwardRef(function TextInputImpl( const {theme: t, fonts} = useAlf() const textInput = useRef(null) const textInputSelection = useRef({start: 0, end: 0}) - const theme = useTheme() const [autocompletePrefix, setAutocompletePrefix] = useState('') const prevLength = React.useRef(richtext.length) @@ -214,9 +212,31 @@ export const TextInput = forwardRef(function TextInputImpl( }, [t, fonts]) const textDecorated = useMemo(() => { + let excess + let truncatedRichtext = richtext + if (richtext.graphemeLength > MAX_GRAPHEME_LENGTH) { + truncatedRichtext = richtext.clone() + const excessText = + truncatedRichtext.unicodeText.slice(MAX_GRAPHEME_LENGTH) + excess = ( + + {excessText} + + ) + truncatedRichtext.delete( + MAX_GRAPHEME_LENGTH, + truncatedRichtext.graphemeLength, + ) + } let i = 0 - return Array.from(richtext.segments()).map(segment => { + const segments = Array.from(truncatedRichtext.segments()).map(segment => { return ( ) }) + + if (excess) { + segments.push(excess) + } + + return segments }, [t, richtext, inputTextStyle]) return ( @@ -243,7 +269,7 @@ export const TextInput = forwardRef(function TextInputImpl( onSelectionChange={onSelectionChange} placeholder={placeholder} placeholderTextColor={t.atoms.text_contrast_medium.color} - keyboardAppearance={theme.colorScheme} + keyboardAppearance={t.scheme} autoFocus={true} allowFontScaling multiline