Line data Source code
1 : import { LiteGraph } from '@/lib/litegraph/src/litegraph'
2 : import { useNodeImage, useNodeVideo } from '@/composables/node/useNodeImage'
3 : import { useNodeImageUpload } from '@/composables/node/useNodeImageUpload'
4 : import { t } from '@/i18n'
5 : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
6 : import type { IComboWidget } from '@comfyorg/graph-model/widgets'
7 : import type { ResultItem } from '@comfyorg/api-client/executionTypes'
8 : import type { ResultItemType } from '@comfyorg/api-client/resultItemTypeSchema'
9 : import type { InputSpec } from '@comfyorg/object-info-parser'
10 : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
11 : import { useToast } from '@/components/ui/toast/toastStore'
12 : import type { ComfyWidgetConstructor } from '@/types/comfy'
13 : import { useNodeOutputStore } from '@/stores/nodeOutputStore'
14 : import { isImageUploadInput } from '@/types/nodeDefAugmentation'
15 : import { createAnnotatedPath } from '@/utils/createAnnotatedPath'
16 : import { addToComboValues } from '@/utils/litegraphUtil'
17 :
18 : import {
19 : ACCEPTED_IMAGE_TYPES,
20 : ACCEPTED_VIDEO_TYPES,
21 : isExtensionlessVideo,
22 : isUploadableVideo
23 : } from '@/utils/mediaUploadUtil'
24 :
25 49 : function isImageFile(file: File) {
26 49 : return file.type.startsWith('image/')
27 : }
28 :
29 : type ImageUploadComboWidget = Omit<IComboWidget, 'value' | 'callback'> & {
30 : value: string | number | string[]
31 : callback?: (value: string | number | string[]) => void
32 : }
33 :
34 2356 : const findFileComboWidget = (
35 : node: LGraphNode,
36 : inputName: string
37 : ): ImageUploadComboWidget | undefined =>
38 : node.widgets?.find((w): w is ImageUploadComboWidget => w.name === inputName)
39 :
40 2356 : export const useImageUploadWidget = () => {
41 2356 : const widgetConstructor: ComfyWidgetConstructor = (
42 : node: LGraphNode,
43 : inputName: string,
44 : inputData: InputSpec
45 : ) => {
46 244 : if (!isImageUploadInput(inputData)) {
47 0 : throw new Error(
48 : 'Image upload widget requires imageInputName augmentation'
49 : )
50 : }
51 :
52 244 : const inputOptions = inputData[1]
53 244 : const { imageInputName, allow_batch, image_folder = 'input' } = inputOptions
54 244 : const folder: ResultItemType | undefined = image_folder
55 244 : const nodeOutputStore = useNodeOutputStore()
56 :
57 244 : const isAnimated = !!inputOptions.animated_image_upload
58 244 : const isVideo = !!inputOptions.video_upload
59 244 : const accept = isVideo ? ACCEPTED_VIDEO_TYPES : ACCEPTED_IMAGE_TYPES
60 244 : const { showPreview } = isVideo ? useNodeVideo(node) : useNodeImage(node)
61 347 : function showCanvasPreview() {
62 347 : if (isVideo || !LiteGraph.vueNodesMode) showPreview({ block: false })
63 : }
64 :
65 244 : const fileFilter = isVideo ? isUploadableVideo : isImageFile
66 1 : function alertExtensionlessVideo(files: File[]) {
67 1 : if (!files.some(isExtensionlessVideo)) return false
68 1 : useToast().warning(t('g.videoFilenameExtensionRequired'))
69 1 : return true
70 : }
71 244 : const fileComboWidget = findFileComboWidget(node, imageInputName)
72 244 : if (!fileComboWidget) {
73 0 : throw new Error(`Widget "${imageInputName}" not found on node`)
74 : }
75 48 : function formatPath(value: string | ResultItem) {
76 48 : return typeof value === 'string'
77 : ? createAnnotatedPath(value, { rootFolder: image_folder })
78 : : createAnnotatedPath(value)
79 : }
80 :
81 : // Setup file upload handling
82 244 : let rollback: (() => void) | undefined
83 244 : const { openFileSelection } = useNodeImageUpload(node, {
84 : allow_batch,
85 : fileFilter,
86 : accept,
87 : folder,
88 : onReject: isVideo ? alertExtensionlessVideo : undefined,
89 : onUploadStart: (files) => {
90 50 : if (files.length > 0) {
91 50 : const prev = fileComboWidget.value
92 50 : fileComboWidget.value = files[0].name
93 50 : rollback = () => {
94 0 : fileComboWidget.value = prev
95 : }
96 : }
97 : },
98 : onUploadError: () => {
99 0 : rollback?.()
100 0 : rollback = undefined
101 : },
102 : onUploadComplete: (output) => {
103 48 : rollback = undefined
104 48 : const annotated = output.map(formatPath)
105 48 : annotated.forEach((path) => {
106 48 : addToComboValues(fileComboWidget, path)
107 : })
108 :
109 48 : const newValue = allow_batch ? annotated : annotated[0]
110 48 : const oldValue = fileComboWidget.value
111 :
112 48 : fileComboWidget.value = newValue
113 48 : fileComboWidget.callback?.(newValue)
114 48 : node.onWidgetChanged?.(
115 : fileComboWidget.name,
116 : newValue,
117 : oldValue,
118 : fileComboWidget
119 : )
120 48 : useWorkflowStore().activeWorkflow?.changeTracker.captureCanvasState()
121 : }
122 : })
123 :
124 : // Create the button widget for selecting the files
125 244 : const uploadWidget = node.addWidget(
126 : 'button',
127 : inputName,
128 : 'image',
129 : () => openFileSelection(),
130 : {
131 : serialize: false,
132 : surfaces: { canvas: 'shown', vueNode: 'never', panel: 'never' }
133 : }
134 : )
135 244 : uploadWidget.label = t('g.choose_file_to_upload')
136 :
137 : // Add our own callback to the combo widget to render an image when it changes
138 244 : fileComboWidget.callback = function () {
139 103 : node.imgs = undefined
140 103 : nodeOutputStore.setNodeOutputs(node, String(fileComboWidget.value), {
141 : isAnimated
142 : })
143 103 : showCanvasPreview()
144 103 : node.graph?.setDirtyCanvas(true)
145 : }
146 :
147 : // On load if we have a value then render the image
148 : // The value isn't set immediately so we need to wait a moment
149 : // No change callbacks seem to be fired on initial setting of the value
150 244 : requestAnimationFrame(() => {
151 244 : const fileValue = fileComboWidget.value as
152 : | string
153 : | number
154 : | null
155 : | undefined
156 244 : if (fileValue != null) {
157 238 : nodeOutputStore.setNodeOutputs(node, String(fileValue), {
158 : isAnimated
159 : })
160 : }
161 244 : showCanvasPreview()
162 : })
163 :
164 244 : return { widget: uploadWidget }
165 : }
166 :
167 2356 : return widgetConstructor
168 : }
|