LCOV - code coverage report
Current view: top level - src/composables/node - useNodeDragAndDrop.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 79.6 % 54 43
Test Date: 2026-11-17 04:17:53

            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              : import { parseAssetInfo } from '@/platform/assets/schemas/mediaAssetSchema'
       5              : import type { ResultItem } from '@comfyorg/api-client/executionTypes'
       6              : import { withHdrImageType } from '@/utils/eventUtils'
       7              : import { getPathDetails } from '@/utils/formatUtil'
       8              : import { parseUrl } from '@/utils/urlSafety'
       9              : 
      10         2356 : const UNSAFE_FILENAME_CHARACTERS = /[\\:*?"<>|]/
      11              : 
      12            0 : function getLowercaseSuffix(filename: string): string | undefined {
      13            0 :   return getPathDetails(filename).suffix?.toLowerCase()
      14              : }
      15              : 
      16            0 : function getFetchedAssetFileName({
      17              :   filename,
      18              :   display_name
      19              : }: ResultItem): string | undefined {
      20            0 :   if (!filename || !display_name) return filename
      21            0 :   const label = getPathDetails(display_name).fullFilename
      22            0 :   const labelSuffix = getLowercaseSuffix(label)
      23            0 :   return labelSuffix &&
      24              :     labelSuffix === getLowercaseSuffix(filename) &&
      25              :     !UNSAFE_FILENAME_CHARACTERS.test(label)
      26              :     ? label
      27              :     : filename
      28              : }
      29              : 
      30              : type DragHandler = (e: DragEvent) => boolean
      31              : type DropHandler<T> = (files: File[]) => Promise<T[]>
      32              : 
      33              : interface DragAndDropOptions<T> {
      34              :   onDragOver?: DragHandler
      35              :   onDrop: DropHandler<T>
      36              :   onResultItemDrop?: (item: ResultItem) => void
      37              :   fileFilter?: (file: File) => boolean
      38              :   onReject?: (files: File[]) => boolean
      39              : }
      40              : 
      41              : /**
      42              :  * Adds drag and drop file handling to a node
      43              :  * Will also resolve 'text/uri-list' to a file before passing
      44              :  */
      45         2356 : export const useNodeDragAndDrop = <T>(
      46              :   node: LGraphNode,
      47              :   options: DragAndDropOptions<T>
      48              : ) => {
      49          282 :   const { onDragOver, onDrop, fileFilter = () => true } = options
      50              : 
      51          282 :   const hasFiles = (items: DataTransferItemList) =>
      52              :     !!Array.from(items).find((f) => f.kind === 'file')
      53              : 
      54          282 :   const filterFiles = (files: FileList | File[]) =>
      55              :     Array.from(files).filter(fileFilter)
      56              : 
      57           40 :   function handleRejectedFiles(files: File[]) {
      58           40 :     return files.length > 0 && (options.onReject?.(files) ?? false)
      59              :   }
      60              : 
      61          282 :   const isDraggingFiles = (e: DragEvent | undefined) => {
      62           47 :     if (!e?.dataTransfer?.items) return false
      63           47 :     return (
      64              :       onDragOver?.(e) ??
      65              :       (hasFiles(e.dataTransfer.items) ||
      66              :         e.dataTransfer.types.includes('text/uri-list'))
      67              :     )
      68              :   }
      69              : 
      70          282 :   const installedDragOver = isDraggingFiles
      71          282 :   node.onDragOver = installedDragOver
      72              : 
      73          282 :   const installedDragDrop = async function (e: DragEvent) {
      74           40 :     const { dataTransfer } = e
      75           40 :     if (!dataTransfer) return false
      76              : 
      77           40 :     const droppedFiles = Array.from(dataTransfer.files, withHdrImageType)
      78           40 :     const [files, rejectedFiles] = partition(droppedFiles, fileFilter)
      79           40 :     const rejectedFilesClaimed = handleRejectedFiles(rejectedFiles)
      80           40 :     if (files.length) {
      81           38 :       await onDrop(files)
      82           38 :       return true
      83              :     }
      84            7 :     if (dataTransfer.files.length) {
      85            0 :       return rejectedFilesClaimed
      86              :     }
      87            2 :     const asset = parseAssetInfo(dataTransfer)
      88           40 :     if (asset?.filename && options.onResultItemDrop) {
      89            1 :       await options.onResultItemDrop(asset)
      90            1 :       return true
      91              :     }
      92              : 
      93            1 :     const baseUri = dataTransfer.getData('text/uri-list')
      94            2 :     if (!baseUri) return false
      95            1 :     const uri = parseUrl(baseUri, location.href)
      96           40 :     if (!uri || uri.origin !== location.origin) return false
      97              : 
      98           40 :     try {
      99            1 :       const resp = await fetch(uri)
     100           40 :       const fileName =
     101              :         (asset && getFetchedAssetFileName(asset)) ??
     102              :         uri.searchParams.get('filename') ??
     103              :         baseUri.split('/').at(-1)
     104           40 :       if (!fileName || !resp.ok) return false
     105              : 
     106            1 :       const blob = await resp.blob()
     107            1 :       const file = new File([blob], fileName, { type: blob.type })
     108            1 :       const uriFiles = filterFiles([file])
     109            2 :       if (!uriFiles.length) {
     110            0 :         return handleRejectedFiles([file])
     111              :       }
     112              : 
     113            1 :       await onDrop(uriFiles)
     114              :     } catch {
     115            0 :       return false
     116              :     }
     117            0 :     return true
     118              :   }
     119          282 :   node.onDragDrop = installedDragDrop
     120              : 
     121          282 :   node.onRemoved = useChainCallback(node.onRemoved, () => {
     122           35 :     if (node.onDragOver === installedDragOver) node.onDragOver = undefined
     123           35 :     if (node.onDragDrop === installedDragDrop) node.onDragDrop = undefined
     124              :   })
     125              : }
        

Generated by: LCOV version 2.0-1