mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-01-26 19:09:52 +00:00
- Updated all imports from '@comfyorg/litegraph' to '@/lib/litegraph/src/' - Replaced deep dist imports with direct source paths - Updated CSS import in main.ts - All imports now use the @ alias consistently
48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
|
|
|
|
type DragHandler = (e: DragEvent) => boolean
|
|
type DropHandler<T> = (files: File[]) => Promise<T[]>
|
|
|
|
interface DragAndDropOptions<T> {
|
|
onDragOver?: DragHandler
|
|
onDrop: DropHandler<T>
|
|
fileFilter?: (file: File) => boolean
|
|
}
|
|
|
|
/**
|
|
* Adds drag and drop file handling to a node
|
|
*/
|
|
export const useNodeDragAndDrop = <T>(
|
|
node: LGraphNode,
|
|
options: DragAndDropOptions<T>
|
|
) => {
|
|
const { onDragOver, onDrop, fileFilter = () => true } = options
|
|
|
|
const hasFiles = (items: DataTransferItemList) =>
|
|
!!Array.from(items).find((f) => f.kind === 'file')
|
|
|
|
const filterFiles = (files: FileList) => Array.from(files).filter(fileFilter)
|
|
|
|
const hasValidFiles = (files: FileList) => filterFiles(files).length > 0
|
|
|
|
const isDraggingFiles = (e: DragEvent | undefined) => {
|
|
if (!e?.dataTransfer?.items) return false
|
|
return onDragOver?.(e) ?? hasFiles(e.dataTransfer.items)
|
|
}
|
|
|
|
const isDraggingValidFiles = (e: DragEvent | undefined) => {
|
|
if (!e?.dataTransfer?.files) return false
|
|
return hasValidFiles(e.dataTransfer.files)
|
|
}
|
|
|
|
node.onDragOver = isDraggingFiles
|
|
|
|
node.onDragDrop = function (e: DragEvent) {
|
|
if (!isDraggingValidFiles(e)) return false
|
|
|
|
const files = filterFiles(e.dataTransfer!.files)
|
|
void onDrop(files)
|
|
return true
|
|
}
|
|
}
|