Files
ComfyUI_frontend/src/composables/node/useNodeFileInput.ts
Benjamin Lu fef02e5f56 [refactor] Migrate litegraph imports from npm package to local subtree
- 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
2025-08-03 22:06:29 -04:00

45 lines
1.1 KiB
TypeScript

import { useChainCallback } from '@/composables/functional/useChainCallback'
import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
interface FileInputOptions {
accept?: string
allow_batch?: boolean
fileFilter?: (file: File) => boolean
onSelect: (files: File[]) => void
}
/**
* Creates a file input for a node.
*/
export function useNodeFileInput(node: LGraphNode, options: FileInputOptions) {
const {
accept,
allow_batch = false,
fileFilter = () => true,
onSelect
} = options
let fileInput: HTMLInputElement | null = document.createElement('input')
fileInput.type = 'file'
fileInput.accept = accept ?? '*'
fileInput.multiple = allow_batch
fileInput.onchange = () => {
if (fileInput?.files?.length) {
const files = Array.from(fileInput.files).filter(fileFilter)
if (files.length) onSelect(files)
}
}
node.onRemoved = useChainCallback(node.onRemoved, () => {
if (fileInput) {
fileInput.onchange = null
fileInput = null
}
})
return {
openFileSelection: () => fileInput?.click()
}
}