tooot/src/screens/Compose/EditAttachment/Root.tsx

116 lines
3.6 KiB
TypeScript
Raw Normal View History

2021-02-08 23:47:20 +01:00
import AttachmentVideo from '@components/Timeline/Shared/Attachment/Video'
2020-12-30 00:56:25 +01:00
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
2021-03-18 23:32:31 +01:00
import React, { useContext, useMemo, useRef } from 'react'
2021-01-19 01:13:45 +01:00
import { useTranslation } from 'react-i18next'
2020-12-30 00:56:25 +01:00
import { ScrollView, StyleSheet, Text, TextInput, View } from 'react-native'
2021-01-01 17:52:14 +01:00
import ComposeContext from '../utils/createContext'
2020-12-30 00:56:25 +01:00
import ComposeEditAttachmentImage from './Image'
export interface Props {
index: number
}
2021-03-09 14:43:31 +01:00
const ComposeEditAttachmentRoot: React.FC<Props> = ({ index }) => {
2021-01-19 01:13:45 +01:00
const { t } = useTranslation('sharedCompose')
const { mode, theme } = useTheme()
2021-03-09 14:43:31 +01:00
const { composeState, composeDispatch } = useContext(ComposeContext)
const theAttachment = composeState.attachments.uploads[index].remote!
2020-12-30 00:56:25 +01:00
const mediaDisplay = useMemo(() => {
2021-01-22 01:34:20 +01:00
if (theAttachment) {
switch (theAttachment.type) {
case 'image':
2021-03-09 14:43:31 +01:00
return <ComposeEditAttachmentImage index={index} />
2021-01-22 01:34:20 +01:00
case 'video':
case 'gifv':
const video = composeState.attachments.uploads[index]
return (
<AttachmentVideo
total={1}
index={0}
sensitiveShown={false}
video={
video.local
? ({
url: video.local.uri,
preview_url: video.local.local_thumbnail,
blurhash: video.remote?.blurhash
} as Mastodon.AttachmentVideo)
: (video.remote as Mastodon.AttachmentVideo)
}
/>
)
}
2020-12-30 00:56:25 +01:00
}
return null
}, [])
2021-03-09 14:43:31 +01:00
const onChangeText = (e: any) =>
composeDispatch({
type: 'attachment/edit',
payload: {
...theAttachment,
description: e
}
})
2021-03-18 23:32:31 +01:00
const scrollViewRef = useRef<ScrollView>(null)
2020-12-30 00:56:25 +01:00
return (
2021-03-18 23:32:31 +01:00
<ScrollView ref={scrollViewRef}>
2020-12-30 00:56:25 +01:00
{mediaDisplay}
<View style={styles.altTextContainer}>
<Text style={[styles.altTextInputHeading, { color: theme.primaryDefault }]}>
2021-01-19 01:13:45 +01:00
{t('content.editAttachment.content.altText.heading')}
2020-12-30 00:56:25 +01:00
</Text>
<TextInput
2021-01-13 01:03:46 +01:00
style={[
styles.altTextInput,
{ borderColor: theme.border, color: theme.primaryDefault }
2021-01-13 01:03:46 +01:00
]}
2021-03-18 23:32:31 +01:00
onFocus={() => scrollViewRef.current?.scrollToEnd()}
2020-12-30 00:56:25 +01:00
autoCapitalize='none'
autoCorrect={false}
maxLength={1500}
multiline
2021-03-09 14:43:31 +01:00
onChangeText={onChangeText}
2021-01-19 01:13:45 +01:00
placeholder={t('content.editAttachment.content.altText.placeholder')}
2020-12-30 00:56:25 +01:00
placeholderTextColor={theme.secondary}
scrollEnabled
2021-03-09 14:43:31 +01:00
value={theAttachment.description}
keyboardAppearance={mode}
2020-12-30 00:56:25 +01:00
/>
<Text style={[styles.altTextLength, { color: theme.secondary }]}>
2021-03-09 14:43:31 +01:00
{theAttachment.description?.length || 0} / 1500
2020-12-30 00:56:25 +01:00
</Text>
</View>
</ScrollView>
)
}
const styles = StyleSheet.create({
altTextContainer: { padding: StyleConstants.Spacing.Global.PagePadding },
altTextInputHeading: {
...StyleConstants.FontStyle.M,
fontWeight: StyleConstants.Font.Weight.Bold
},
altTextInput: {
height: 200,
...StyleConstants.FontStyle.M,
marginTop: StyleConstants.Spacing.M,
marginBottom: StyleConstants.Spacing.S,
padding: StyleConstants.Spacing.Global.PagePadding,
paddingTop: StyleConstants.Spacing.S * 1.5,
borderWidth: StyleSheet.hairlineWidth
},
altTextLength: {
textAlign: 'right',
marginRight: StyleConstants.Spacing.S,
...StyleConstants.FontStyle.S,
marginBottom: StyleConstants.Spacing.M
}
})
export default ComposeEditAttachmentRoot