mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-02-03 22:59:14 +00:00
[Refactor] Move node composables to subfolder (#2712)
This commit is contained in:
45
src/composables/node/useNodeFileInput.ts
Normal file
45
src/composables/node/useNodeFileInput.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import type { LGraphNode } from '@comfyorg/litegraph'
|
||||
|
||||
import { useChainCallback } from '@/composables/functional/useChainCallback'
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user