Add alt text limit to image dialog (#5611)

* Add alt text limit to image dialog

* GIF alt text too

* Fix

* tweaks, save alt on dialog dismiss

* simplify close behavior

* use state in gif alt

* state

---------

Co-authored-by: Hailey <me@haileyok.com>
This commit is contained in:
Eric Bailey
2024-10-05 05:58:04 -05:00
committed by GitHub
parent 6dfd57e621
commit 76d63f9967
5 changed files with 231 additions and 118 deletions
+1 -1
View File
@@ -50,7 +50,7 @@ export const MAX_DM_GRAPHEME_LENGTH = 1000
// Recommended is 100 per: https://www.w3.org/WAI/GL/WCAG20/tests/test3.html // Recommended is 100 per: https://www.w3.org/WAI/GL/WCAG20/tests/test3.html
// but increasing limit per user feedback // but increasing limit per user feedback
export const MAX_ALT_TEXT = 1000 export const MAX_ALT_TEXT = 2000
export function IS_TEST_USER(handle?: string) { export function IS_TEST_USER(handle?: string) {
return handle && handle?.endsWith('.test') return handle && handle?.endsWith('.test')
@@ -0,0 +1,33 @@
import React from 'react'
import {View} from 'react-native'
import {MAX_ALT_TEXT} from '#/lib/constants'
import {CharProgress} from '#/view/com/composer/char-progress/CharProgress'
import {atoms as a, useTheme} from '#/alf'
export function AltTextCounterWrapper({
altText,
children,
}: {
altText?: string
children: React.ReactNode
}) {
const t = useTheme()
return (
<View style={[a.flex_row]}>
<CharProgress
style={[
a.flex_col_reverse,
a.align_center,
a.mr_xs,
{minWidth: 50, gap: 1},
]}
textStyle={[{marginRight: 0}, a.text_sm, t.atoms.text_contrast_medium]}
size={26}
count={altText?.length || 0}
max={MAX_ALT_TEXT}
/>
{children}
</View>
)
}
+54 -31
View File
@@ -1,4 +1,4 @@
import React, {useCallback, useState} from 'react' import React, {useState} from 'react'
import {TouchableOpacity, View} from 'react-native' import {TouchableOpacity, View} from 'react-native'
import {AppBskyEmbedExternal} from '@atproto/api' import {AppBskyEmbedExternal} from '@atproto/api'
import {msg, Trans} from '@lingui/macro' import {msg, Trans} from '@lingui/macro'
@@ -11,14 +11,16 @@ import {
EmbedPlayerParams, EmbedPlayerParams,
parseEmbedPlayerFromUrl, parseEmbedPlayerFromUrl,
} from '#/lib/strings/embed-player' } from '#/lib/strings/embed-player'
import {enforceLen} from '#/lib/strings/helpers'
import {isAndroid} from '#/platform/detection' import {isAndroid} from '#/platform/detection'
import {Gif} from '#/state/queries/tenor' import {Gif} from '#/state/queries/tenor'
import {AltTextCounterWrapper} from '#/view/com/composer/AltTextCounterWrapper'
import {atoms as a, native, useTheme} from '#/alf' import {atoms as a, native, useTheme} from '#/alf'
import {Button, ButtonText} from '#/components/Button' import {Button, ButtonText} from '#/components/Button'
import * as Dialog from '#/components/Dialog' import * as Dialog from '#/components/Dialog'
import {DialogControlProps} from '#/components/Dialog'
import * as TextField from '#/components/forms/TextField' import * as TextField from '#/components/forms/TextField'
import {Check_Stroke2_Corner0_Rounded as Check} from '#/components/icons/Check' import {Check_Stroke2_Corner0_Rounded as Check} from '#/components/icons/Check'
import {CircleInfo_Stroke2_Corner0_Rounded as CircleInfo} from '#/components/icons/CircleInfo'
import {PlusSmall_Stroke2_Corner0_Rounded as Plus} from '#/components/icons/Plus' import {PlusSmall_Stroke2_Corner0_Rounded as Plus} from '#/components/icons/Plus'
import {PortalComponent} from '#/components/Portal' import {PortalComponent} from '#/components/Portal'
import {Text} from '#/components/Typography' import {Text} from '#/components/Typography'
@@ -52,18 +54,11 @@ export function GifAltText({
} }
}, [linkProp]) }, [linkProp])
const onPressSubmit = useCallback( const parsedAlt = parseAltFromGIFDescription(link.description)
(alt: string) => { const [altText, setAltText] = useState(parsedAlt.alt)
control.close(() => {
onSubmit(alt)
})
},
[onSubmit, control],
)
if (!gif || !params) return null if (!gif || !params) return null
const parsedAlt = parseAltFromGIFDescription(link.description)
return ( return (
<> <>
<TouchableOpacity <TouchableOpacity
@@ -99,13 +94,19 @@ export function GifAltText({
<AltTextReminder /> <AltTextReminder />
<Dialog.Outer control={control} Portal={Portal}> <Dialog.Outer
control={control}
onClose={() => {
onSubmit(altText)
}}
Portal={Portal}>
<Dialog.Handle /> <Dialog.Handle />
<AltTextInner <AltTextInner
onSubmit={onPressSubmit} altText={altText}
setAltText={setAltText}
control={control}
link={link} link={link}
params={params} params={params}
initialValue={parsedAlt.isPreferred ? parsedAlt.alt : ''}
key={link.uri} key={link.uri}
/> />
</Dialog.Outer> </Dialog.Outer>
@@ -114,29 +115,27 @@ export function GifAltText({
} }
function AltTextInner({ function AltTextInner({
onSubmit, altText,
setAltText,
control,
link, link,
params, params,
initialValue: initalValue,
}: { }: {
onSubmit: (text: string) => void altText: string
setAltText: (text: string) => void
control: DialogControlProps
link: AppBskyEmbedExternal.ViewExternal link: AppBskyEmbedExternal.ViewExternal
params: EmbedPlayerParams params: EmbedPlayerParams
initialValue: string
}) { }) {
const {_} = useLingui() const t = useTheme()
const [altText, setAltText] = useState(initalValue) const {_, i18n} = useLingui()
const control = Dialog.useDialogContext()
const onPressSubmit = useCallback(() => {
onSubmit(altText)
}, [onSubmit, altText])
return ( return (
<Dialog.ScrollableInner label={_(msg`Add alt text`)}> <Dialog.ScrollableInner label={_(msg`Add alt text`)}>
<View style={a.flex_col_reverse}> <View style={a.flex_col_reverse}>
<View style={[a.mt_md, a.gap_md]}> <View style={[a.mt_md, a.gap_md]}>
<View> <View style={[a.gap_sm]}>
<View style={[a.relative]}>
<TextField.LabelText> <TextField.LabelText>
<Trans>Descriptive alt text</Trans> <Trans>Descriptive alt text</Trans>
</TextField.LabelText> </TextField.LabelText>
@@ -144,10 +143,10 @@ function AltTextInner({
<Dialog.Input <Dialog.Input
label={_(msg`Alt text`)} label={_(msg`Alt text`)}
placeholder={link.title} placeholder={link.title}
onChangeText={text => onChangeText={text => {
setAltText(enforceLen(text, MAX_ALT_TEXT)) setAltText(text)
} }}
value={altText} defaultValue={altText}
multiline multiline
numberOfLines={3} numberOfLines={3}
autoFocus autoFocus
@@ -159,16 +158,40 @@ function AltTextInner({
/> />
</TextField.Root> </TextField.Root>
</View> </View>
{altText.length > MAX_ALT_TEXT && (
<View style={[a.pb_sm, a.flex_row, a.gap_xs]}>
<CircleInfo fill={t.palette.negative_500} />
<Text
style={[
a.italic,
a.leading_snug,
t.atoms.text_contrast_medium,
]}>
<Trans>
Alt text will be truncated. Limit:{' '}
{i18n.number(MAX_ALT_TEXT)} characters.
</Trans>
</Text>
</View>
)}
</View>
<AltTextCounterWrapper altText={altText}>
<Button <Button
label={_(msg`Save`)} label={_(msg`Save`)}
size="large" size="large"
color="primary" color="primary"
variant="solid" variant="solid"
onPress={onPressSubmit}> onPress={() => {
control.close()
}}
style={[a.flex_grow]}>
<ButtonText> <ButtonText>
<Trans>Save</Trans> <Trans>Save</Trans>
</ButtonText> </ButtonText>
</Button> </Button>
</AltTextCounterWrapper>
</View> </View>
{/* below the text input to force tab order */} {/* below the text input to force tab order */}
<View> <View>
@@ -1,48 +1,56 @@
import React from 'react' import React from 'react'
import {View} from 'react-native' import {StyleProp, TextStyle, View, ViewStyle} from 'react-native'
// @ts-ignore no type definition -prf // @ts-ignore no type definition -prf
import ProgressCircle from 'react-native-progress/Circle' import ProgressCircle from 'react-native-progress/Circle'
// @ts-ignore no type definition -prf // @ts-ignore no type definition -prf
import ProgressPie from 'react-native-progress/Pie' import ProgressPie from 'react-native-progress/Pie'
import {MAX_GRAPHEME_LENGTH} from 'lib/constants' import {MAX_GRAPHEME_LENGTH} from '#/lib/constants'
import {usePalette} from 'lib/hooks/usePalette' import {usePalette} from '#/lib/hooks/usePalette'
import {s} from 'lib/styles' import {s} from '#/lib/styles'
import {Text} from '../../util/text/Text' import {Text} from '../../util/text/Text'
const DANGER_LENGTH = MAX_GRAPHEME_LENGTH export function CharProgress({
count,
export function CharProgress({count}: {count: number}) { max,
style,
textStyle,
size,
}: {
count: number
max?: number
style?: StyleProp<ViewStyle>
textStyle?: StyleProp<TextStyle>
size?: number
}) {
const maxLength = max || MAX_GRAPHEME_LENGTH
const pal = usePalette('default') const pal = usePalette('default')
const textColor = count > DANGER_LENGTH ? '#e60000' : pal.colors.text const textColor = count > maxLength ? '#e60000' : pal.colors.text
const circleColor = count > DANGER_LENGTH ? '#e60000' : pal.colors.link const circleColor = count > maxLength ? '#e60000' : pal.colors.link
return ( return (
<> <View style={style}>
<Text style={[s.mr10, s.tabularNum, {color: textColor}]}> <Text style={[s.mr10, s.tabularNum, {color: textColor}, textStyle]}>
{MAX_GRAPHEME_LENGTH - count} {maxLength - count}
</Text> </Text>
<View> <View>
{count > DANGER_LENGTH ? ( {count > maxLength ? (
<ProgressPie <ProgressPie
size={30} size={size ?? 30}
borderWidth={4} borderWidth={4}
borderColor={circleColor} borderColor={circleColor}
color={circleColor} color={circleColor}
progress={Math.min( progress={Math.min((count - maxLength) / maxLength, 1)}
(count - MAX_GRAPHEME_LENGTH) / MAX_GRAPHEME_LENGTH,
1,
)}
/> />
) : ( ) : (
<ProgressCircle <ProgressCircle
size={30} size={size ?? 30}
borderWidth={1} borderWidth={1}
borderColor={pal.colors.border} borderColor={pal.colors.border}
color={circleColor} color={circleColor}
progress={count / MAX_GRAPHEME_LENGTH} progress={count / maxLength}
/> />
)} )}
</View> </View>
</> </View>
) )
} }
@@ -5,12 +5,16 @@ import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react' import {useLingui} from '@lingui/react'
import {MAX_ALT_TEXT} from '#/lib/constants' import {MAX_ALT_TEXT} from '#/lib/constants'
import {enforceLen} from '#/lib/strings/helpers'
import {isAndroid, isWeb} from '#/platform/detection' import {isAndroid, isWeb} from '#/platform/detection'
import {ComposerImage} from '#/state/gallery' import {ComposerImage} from '#/state/gallery'
import {AltTextCounterWrapper} from '#/view/com/composer/AltTextCounterWrapper'
import {atoms as a, useTheme} from '#/alf' import {atoms as a, useTheme} from '#/alf'
import {Button, ButtonText} from '#/components/Button' import {Button, ButtonText} from '#/components/Button'
import * as Dialog from '#/components/Dialog' import * as Dialog from '#/components/Dialog'
import {DialogControlProps} from '#/components/Dialog'
import * as TextField from '#/components/forms/TextField' import * as TextField from '#/components/forms/TextField'
import {CircleInfo_Stroke2_Corner0_Rounded as CircleInfo} from '#/components/icons/CircleInfo'
import {PortalComponent} from '#/components/Portal' import {PortalComponent} from '#/components/Portal'
import {Text} from '#/components/Typography' import {Text} from '#/components/Typography'
@@ -21,32 +25,50 @@ type Props = {
Portal: PortalComponent Portal: PortalComponent
} }
export const ImageAltTextDialog = (props: Props): React.ReactNode => { export const ImageAltTextDialog = ({
control,
image,
onChange,
Portal,
}: Props): React.ReactNode => {
const [altText, setAltText] = React.useState(image.alt)
return ( return (
<Dialog.Outer control={props.control} Portal={props.Portal}> <Dialog.Outer
control={control}
onClose={() => {
onChange({
...image,
alt: enforceLen(altText, MAX_ALT_TEXT, true),
})
}}
Portal={Portal}>
<Dialog.Handle /> <Dialog.Handle />
<ImageAltTextInner {...props} /> <ImageAltTextInner
control={control}
image={image}
altText={altText}
setAltText={setAltText}
/>
</Dialog.Outer> </Dialog.Outer>
) )
} }
const ImageAltTextInner = ({ const ImageAltTextInner = ({
altText,
setAltText,
control, control,
image, image,
onChange, }: {
}: Props): React.ReactNode => { altText: string
const {_} = useLingui() setAltText: (text: string) => void
control: DialogControlProps
image: Props['image']
}): React.ReactNode => {
const {_, i18n} = useLingui()
const t = useTheme() const t = useTheme()
const windim = useWindowDimensions() const windim = useWindowDimensions()
const [altText, setAltText] = React.useState(image.alt)
const onPressSubmit = React.useCallback(() => {
control.close()
onChange({...image, alt: altText.trim()})
}, [control, image, altText, onChange])
const imageStyle = React.useMemo<ImageStyle>(() => { const imageStyle = React.useMemo<ImageStyle>(() => {
const maxWidth = isWeb ? 450 : windim.width const maxWidth = isWeb ? 450 : windim.width
const source = image.transformed ?? image.source const source = image.transformed ?? image.source
@@ -90,32 +112,59 @@ const ImageAltTextInner = ({
</View> </View>
<View style={[a.mt_md, a.gap_md]}> <View style={[a.mt_md, a.gap_md]}>
<View> <View style={[a.gap_sm]}>
<View style={[a.relative, {width: '100%'}]}>
<TextField.LabelText> <TextField.LabelText>
<Trans>Descriptive alt text</Trans> <Trans>Descriptive alt text</Trans>
</TextField.LabelText> </TextField.LabelText>
<TextField.Root> <TextField.Root>
<Dialog.Input <Dialog.Input
label={_(msg`Alt text`)} label={_(msg`Alt text`)}
onChangeText={text => setAltText(text)} onChangeText={text => {
value={altText} setAltText(text)
}}
defaultValue={altText}
multiline multiline
numberOfLines={3} numberOfLines={3}
autoFocus autoFocus
/> />
</TextField.Root> </TextField.Root>
</View> </View>
{altText.length > MAX_ALT_TEXT && (
<View style={[a.pb_sm, a.flex_row, a.gap_xs]}>
<CircleInfo fill={t.palette.negative_500} />
<Text
style={[
a.italic,
a.leading_snug,
t.atoms.text_contrast_medium,
]}>
<Trans>
Alt text will be truncated. Limit: {i18n.number(MAX_ALT_TEXT)}{' '}
characters.
</Trans>
</Text>
</View>
)}
</View>
<AltTextCounterWrapper altText={altText}>
<Button <Button
label={_(msg`Save`)} label={_(msg`Save`)}
disabled={altText.length > MAX_ALT_TEXT || altText === image.alt} disabled={altText === image.alt}
size="large" size="large"
color="primary" color="primary"
variant="solid" variant="solid"
onPress={onPressSubmit}> onPress={() => {
control.close()
}}
style={[a.flex_grow]}>
<ButtonText> <ButtonText>
<Trans>Save</Trans> <Trans>Save</Trans>
</ButtonText> </ButtonText>
</Button> </Button>
</AltTextCounterWrapper>
</View> </View>
{/* Maybe fix this later -h */} {/* Maybe fix this later -h */}
{isAndroid ? <View style={{height: 300}} /> : null} {isAndroid ? <View style={{height: 300}} /> : null}