[GIFs] Restore default alt text (#3893)

* restore default alt text

* factor out gif alt logic + enable require alt text setting

* rm console.log

* don't prefill input + esc handling

* typo

* Nits

* shorten user alt prefix

* Remove unnecessary condition, rename for clarity

* Add comment

---------

Co-authored-by: Dan Abramov <dan.abramov@gmail.com>
This commit is contained in:
Samuel Newman 2024-05-07 20:05:40 +01:00 committed by GitHub
parent 77e6c75a2c
commit 7d72dfb1cb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 84 additions and 20 deletions

36
src/lib/gif-alt-text.ts Normal file
View file

@ -0,0 +1,36 @@
// Kind of a hack. We needed some way to distinguish these.
const USER_ALT_PREFIX = 'Alt: '
const DEFAULT_ALT_PREFIX = 'ALT: '
export function createGIFDescription(
tenorDescription: string,
preferredAlt: string = '',
) {
preferredAlt = preferredAlt.trim()
if (preferredAlt !== '') {
return USER_ALT_PREFIX + preferredAlt
} else {
return DEFAULT_ALT_PREFIX + tenorDescription
}
}
export function parseAltFromGIFDescription(description: string): {
isPreferred: boolean
alt: string
} {
if (description.startsWith(USER_ALT_PREFIX)) {
return {
isPreferred: true,
alt: description.replace(USER_ALT_PREFIX, ''),
}
} else if (description.startsWith(DEFAULT_ALT_PREFIX)) {
return {
isPreferred: false,
alt: description.replace(DEFAULT_ALT_PREFIX, ''),
}
}
return {
isPreferred: false,
alt: description,
}
}