tooot/src/screens/Compose/Root/Header/TextInput.tsx

108 lines
3.1 KiB
TypeScript
Raw Normal View History

import CustomText from '@components/Text'
2022-06-05 21:17:39 +02:00
import PasteInput, { PastedFile } from '@mattermost/react-native-paste-input'
import { getInstanceConfigurationStatusMaxAttachments } from '@utils/slices/instancesSlice'
2020-12-13 14:04:25 +01:00
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
2021-01-19 01:13:45 +01:00
import React, { useContext } from 'react'
import { useTranslation } from 'react-i18next'
2022-06-05 21:17:39 +02:00
import { Alert } from 'react-native'
import { useSelector } from 'react-redux'
2021-01-19 01:13:45 +01:00
import formatText from '../../formatText'
import ComposeContext from '../../utils/createContext'
import { uploadAttachment } from '../Footer/addAttachment'
2020-12-04 01:17:10 +01:00
const ComposeTextInput: React.FC = () => {
const { composeState, composeDispatch } = useContext(ComposeContext)
2021-03-28 23:31:10 +02:00
const { t } = useTranslation('screenCompose')
2022-02-12 14:51:01 +01:00
const { colors, mode } = useTheme()
2020-12-04 01:17:10 +01:00
const maxAttachments = useSelector(
getInstanceConfigurationStatusMaxAttachments,
() => true
)
2020-12-04 01:17:10 +01:00
return (
2022-06-05 21:17:39 +02:00
<PasteInput
keyboardAppearance={mode}
style={{
...StyleConstants.FontStyle.M,
marginTop: StyleConstants.Spacing.S,
paddingBottom: StyleConstants.Spacing.M,
marginLeft: StyleConstants.Spacing.Global.PagePadding,
marginRight: StyleConstants.Spacing.Global.PagePadding,
color: colors.primaryDefault,
borderBottomColor: colors.border
}}
2020-12-04 01:17:10 +01:00
autoFocus
enablesReturnKeyAutomatically
multiline
2021-01-19 01:13:45 +01:00
placeholder={t('content.root.header.textInput.placeholder')}
2022-02-12 14:51:01 +01:00
placeholderTextColor={colors.secondary}
2020-12-04 01:17:10 +01:00
onChangeText={content =>
formatText({
textInput: 'text',
2020-12-07 12:31:40 +01:00
composeDispatch,
2020-12-04 01:17:10 +01:00
content
})
}
onFocus={() =>
composeDispatch({
type: 'textInputFocus',
payload: { current: 'text' }
})
}
2020-12-04 01:17:10 +01:00
onSelectionChange={({
nativeEvent: {
selection: { start, end }
}
}) => {
2020-12-10 19:19:56 +01:00
composeDispatch({
type: 'text',
payload: { selection: { start, end } }
})
2020-12-04 01:17:10 +01:00
}}
ref={composeState.textInputFocus.refs.text}
2021-03-18 23:32:31 +01:00
scrollEnabled={false}
2022-06-05 21:17:39 +02:00
disableCopyPaste={false}
onPaste={(error: string | null | undefined, files: PastedFile[]) => {
if (
composeState.attachments.uploads.length + files.length >
maxAttachments
) {
Alert.alert(
t(
'content.root.header.textInput.keyboardImage.exceedMaximum.title'
),
undefined,
[
{
text: t(
'content.root.header.textInput.keyboardImage.exceedMaximum.OK'
),
style: 'default'
}
]
)
return
}
2022-06-05 21:17:39 +02:00
for (const file of files) {
uploadAttachment({
composeDispatch,
2022-06-05 17:58:18 +02:00
media: {
2022-06-05 21:17:39 +02:00
path: file.uri,
mime: file.type,
width: 100,
height: 100
}
})
}
}}
2020-12-04 01:17:10 +01:00
>
<CustomText>{composeState.text.formatted}</CustomText>
2022-06-05 21:17:39 +02:00
</PasteInput>
2020-12-04 01:17:10 +01:00
)
}
export default ComposeTextInput