mirror of
https://github.com/langgenius/dify.git
synced 2024-11-16 19:59:50 +08:00
fbf31b5d52
Co-authored-by: crazywoola <427733928@qq.com>
44 lines
991 B
TypeScript
44 lines
991 B
TypeScript
import { useCallback, useState } from 'react'
|
|
|
|
export const useDraggableUploader = <T extends HTMLElement>(setImageFn: (file: File) => void) => {
|
|
const [isDragActive, setIsDragActive] = useState(false)
|
|
|
|
const handleDragEnter = useCallback((e: React.DragEvent<T>) => {
|
|
e.preventDefault()
|
|
e.stopPropagation()
|
|
setIsDragActive(true)
|
|
}, [])
|
|
|
|
const handleDragOver = useCallback((e: React.DragEvent<T>) => {
|
|
e.preventDefault()
|
|
e.stopPropagation()
|
|
}, [])
|
|
|
|
const handleDragLeave = useCallback((e: React.DragEvent<T>) => {
|
|
e.preventDefault()
|
|
e.stopPropagation()
|
|
setIsDragActive(false)
|
|
}, [])
|
|
|
|
const handleDrop = useCallback((e: React.DragEvent<T>) => {
|
|
e.preventDefault()
|
|
e.stopPropagation()
|
|
setIsDragActive(false)
|
|
|
|
const file = e.dataTransfer.files[0]
|
|
|
|
if (!file)
|
|
return
|
|
|
|
setImageFn(file)
|
|
}, [setImageFn])
|
|
|
|
return {
|
|
handleDragEnter,
|
|
handleDragOver,
|
|
handleDragLeave,
|
|
handleDrop,
|
|
isDragActive,
|
|
}
|
|
}
|