Line data Source code
1 : import { useChainCallback } from '@/composables/functional/useChainCallback'
2 : import { partition } from 'es-toolkit'
3 : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
4 :
5 : type PasteHandler<T> = (files: File[]) => Promise<T>
6 :
7 : interface NodePasteOptions<T> {
8 : onPaste: PasteHandler<T>
9 : fileFilter?: (file: File) => boolean
10 : allow_batch?: boolean
11 : onReject?: (files: File[]) => void
12 : }
13 :
14 : /**
15 : * Adds paste handling to a node
16 : */
17 2356 : export const useNodePaste = <T>(
18 : node: LGraphNode,
19 : options: NodePasteOptions<T>
20 : ) => {
21 260 : const { onPaste, fileFilter = () => true, allow_batch = false } = options
22 :
23 260 : const installedPasteFiles = function (files: File[]) {
24 10 : const [filteredFiles, rejectedFiles] = partition(files, fileFilter)
25 10 : if (rejectedFiles.length) options.onReject?.(rejectedFiles)
26 10 : if (!filteredFiles.length) {
27 1 : return
28 : }
29 :
30 10 : const paste = allow_batch ? filteredFiles : filteredFiles.slice(0, 1)
31 :
32 10 : void onPaste(paste)
33 : }
34 260 : node.pasteFiles = installedPasteFiles
35 :
36 260 : node.onRemoved = useChainCallback(node.onRemoved, () => {
37 35 : if (node.pasteFiles === installedPasteFiles) node.pasteFiles = undefined
38 : })
39 : }
|