// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. import {useManagedConfig} from '@mattermost/react-native-emm'; import PasteInput, {type PasteInputRef} from '@mattermost/react-native-paste-input'; import React, {forwardRef, useCallback, useImperativeHandle, useMemo, useRef} from 'react'; import {useIntl} from 'react-intl'; import {type NativeSyntheticEvent, Platform, type TextInputSelectionChangeEventData, useWindowDimensions, View} from 'react-native'; import {useTheme} from '@context/theme'; import {emptyFunction} from '@utils/general'; import {changeOpacity, getKeyboardAppearanceFromTheme, makeStyleSheetFromTheme} from '@utils/theme'; import {typography} from '@utils/typography'; const getStyleSheet = makeStyleSheetFromTheme((theme) => ({ input: { color: theme.centerChannelColor, padding: 15, textAlignVertical: 'top', ...typography('Body', 200), }, inputContainer: { backgroundColor: theme.centerChannelBg, marginTop: 2, }, })); const HEIGHT_DIFF = Platform.select({android: 40, default: 30}); export type EditPostInputRef = { focus: () => void; } type PostInputProps = { message: string; hasError: boolean; onTextSelectionChange: (curPos: number) => void; onChangeText: (text: string) => void; } const EditPostInput = forwardRef(({ message, onChangeText, onTextSelectionChange, hasError, }: PostInputProps, ref) => { const intl = useIntl(); const theme = useTheme(); const styles = getStyleSheet(theme); const {height} = useWindowDimensions(); const managedConfig = useManagedConfig(); const textInputHeight = (height / 2) - HEIGHT_DIFF; const disableCopyAndPaste = managedConfig.copyAndPasteProtection === 'true'; const inputRef = useRef(); const inputStyle = useMemo(() => { return [styles.input, {height: textInputHeight}]; }, [textInputHeight, styles]); const onSelectionChange = useCallback((event: NativeSyntheticEvent) => { const curPos = event.nativeEvent.selection.end; onTextSelectionChange(curPos); }, [onTextSelectionChange]); const containerStyle = useMemo(() => [ styles.inputContainer, hasError && {marginTop: 0}, {height: textInputHeight}, ], [styles, textInputHeight]); useImperativeHandle(ref, () => ({ focus: () => inputRef.current?.focus(), }), [inputRef.current]); return ( ); }); EditPostInput.displayName = 'EditPostInput'; export default EditPostInput;