[Video] Allow drag-and-drop & pasting video (#5252)

* allow DnD/pasting video

* rm await
zio/stable
Samuel Newman 2024-09-10 16:14:28 +01:00 committed by GitHub
parent 6bc5a05f4b
commit 08f5f37b34
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 9 deletions

View File

@ -303,9 +303,13 @@ export const ComposePost = observer(function ComposePost({
const onPhotoPasted = useCallback( const onPhotoPasted = useCallback(
async (uri: string) => { async (uri: string) => {
track('Composer:PastedPhotos') track('Composer:PastedPhotos')
if (uri.startsWith('data:video/')) {
selectVideo({uri, type: 'video', height: 0, width: 0})
} else {
await gallery.paste(uri) await gallery.paste(uri)
}
}, },
[gallery, track], [gallery, track, selectVideo],
) )
const isAltTextRequiredAndMissing = useMemo(() => { const isAltTextRequiredAndMissing = useMemo(() => {

View File

@ -93,9 +93,9 @@ export const TextInput = React.forwardRef(function TextInputImpl(
} }
}, [onPressPublish]) }, [onPressPublish])
React.useEffect(() => { React.useEffect(() => {
textInputWebEmitter.addListener('photo-pasted', onPhotoPasted) textInputWebEmitter.addListener('media-pasted', onPhotoPasted)
return () => { return () => {
textInputWebEmitter.removeListener('photo-pasted', onPhotoPasted) textInputWebEmitter.removeListener('media-pasted', onPhotoPasted)
} }
}, [onPhotoPasted]) }, [onPhotoPasted])
@ -105,8 +105,8 @@ export const TextInput = React.forwardRef(function TextInputImpl(
if (transfer) { if (transfer) {
const items = transfer.items const items = transfer.items
getImageFromUri(items, (uri: string) => { getImageOrVideoFromUri(items, (uri: string) => {
textInputWebEmitter.emit('photo-pasted', uri) textInputWebEmitter.emit('media-pasted', uri)
}) })
} }
@ -160,8 +160,8 @@ export const TextInput = React.forwardRef(function TextInputImpl(
view.pasteText(text) view.pasteText(text)
preventDefault = true preventDefault = true
} }
getImageFromUri(clipboardData.items, (uri: string) => { getImageOrVideoFromUri(clipboardData.items, (uri: string) => {
textInputWebEmitter.emit('photo-pasted', uri) textInputWebEmitter.emit('media-pasted', uri)
}) })
if (preventDefault) { if (preventDefault) {
// Return `true` to prevent ProseMirror's default paste behavior. // Return `true` to prevent ProseMirror's default paste behavior.
@ -346,7 +346,7 @@ const styles = StyleSheet.create({
}, },
}) })
function getImageFromUri( function getImageOrVideoFromUri(
items: DataTransferItemList, items: DataTransferItemList,
callback: (uri: string) => void, callback: (uri: string) => void,
) { ) {
@ -363,11 +363,21 @@ function getImageFromUri(
if (blob.type.startsWith('image/')) { if (blob.type.startsWith('image/')) {
blobToDataUri(blob).then(callback, err => console.error(err)) blobToDataUri(blob).then(callback, err => console.error(err))
} }
if (blob.type.startsWith('video/')) {
blobToDataUri(blob).then(callback, err => console.error(err))
}
} }
}) })
} else if (type.startsWith('image/')) { } else if (type.startsWith('image/')) {
const file = item.getAsFile() const file = item.getAsFile()
if (file) {
blobToDataUri(file).then(callback, err => console.error(err))
}
} else if (type.startsWith('video/')) {
const file = item.getAsFile()
if (file) { if (file) {
blobToDataUri(file).then(callback, err => console.error(err)) blobToDataUri(file).then(callback, err => console.error(err))
} }