Line data Source code
1 : <script setup lang="ts">
2 : import { computed, provide, watchEffect } from 'vue'
3 : import { useI18n } from 'vue-i18n'
4 :
5 : import WidgetDescription from '@/components/builder/WidgetDescription.vue'
6 : import { useAppModeWidgetResizing } from '@/components/builder/useAppModeWidgetResizing'
7 : import { getLoaderDropIndicator } from '@/components/builder/useLoaderDropIndicator'
8 : import { useResolvedSelectedInputs } from '@/components/builder/useResolvedSelectedInputs'
9 : import Button from '@comfyorg/design-system/button/Button'
10 : import Menu from '@comfyorg/design-system/menu/Menu'
11 : import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
12 : import { LGraphEventMode } from '@comfyorg/graph-model/globalEnums'
13 : import type { IBaseWidget } from '@comfyorg/graph-model/widgets'
14 : import { deriveWidgetRenderState } from '@/lib/litegraph/src/utils/widget'
15 : import type { WidgetId } from '@comfyorg/graph-model/widgetId'
16 : import { useMaskEditor } from '@/renderer/extensions/maskEditor/composables/useMaskEditor'
17 : import DropZone from '@/renderer/extensions/linearMode/DropZone.vue'
18 : import NodeWidgets from '@/renderer/extensions/vueNodes/components/NodeWidgets.vue'
19 : import { app } from '@/scripts/app'
20 : import { draggableItemClass } from '@/scripts/ui/draggableList'
21 : import { useExecutionErrorStore } from '@/stores/executionErrorStore'
22 : import { useLinkStore } from '@/stores/linkStore'
23 : import { graphScopeOf } from '@comfyorg/graph-model/graphScopeId'
24 : import { useWidgetValueStore } from '@/stores/widgetValueStore'
25 : import { useAppModeStore } from '@/stores/appModeStore'
26 : import { useTemplateInputDownloadStore } from '@/platform/workflow/templates/stores/templateInputDownloadStore'
27 : import { cn } from '@comfyorg/tailwind-utils'
28 : import { HideLayoutFieldKey, WidgetHeightKey } from '@/types/widgetTypes'
29 : import { UNASSIGNED_NODE_ID } from '@comfyorg/graph-model/nodeId'
30 : import { promptRenameWidget } from '@/utils/widgetUtil'
31 :
32 : interface WidgetEntry {
33 : key: string
34 : persistedHeight: number | undefined
35 : nodeData: ReturnType<typeof nodeToNodeData>
36 : widgetIds: readonly WidgetId[]
37 : action: { widget: IBaseWidget; node: LGraphNode }
38 : description?: string
39 : }
40 :
41 : const { mobile = false, builderMode = false } = defineProps<{
42 : mobile?: boolean
43 : builderMode?: boolean
44 : }>()
45 :
46 2355 : const { t } = useI18n()
47 85 : const executionErrorStore = useExecutionErrorStore()
48 85 : const appModeStore = useAppModeStore()
49 85 : const templateInputDownloadStore = useTemplateInputDownloadStore()
50 85 : const widgetValueStore = useWidgetValueStore()
51 85 : const linkStore = useLinkStore()
52 85 : const maskEditor = useMaskEditor()
53 :
54 85 : const { onPointerDown } = useAppModeWidgetResizing((widget, config) =>
55 : appModeStore.updateInputConfig(widget, config)
56 : )
57 :
58 85 : provide(HideLayoutFieldKey, true)
59 85 : provide(WidgetHeightKey, mobile ? 'h-10' : 'h-7')
60 :
61 85 : const resolvedInputs = useResolvedSelectedInputs()
62 :
63 128 : function ensureSelectedWidgetState(
64 : widgetId: WidgetId,
65 : widget: IBaseWidget
66 : ): void {
67 128 : if (widgetValueStore.getWidget(widgetId)) return
68 :
69 3 : widgetValueStore.registerWidget(
70 : widgetId,
71 : {
72 : type: widget.type,
73 : value: widget.value,
74 : options: widget.options,
75 : label: widget.label,
76 : serialize: widget.serialize,
77 : disabled: widget.disabled
78 : },
79 : deriveWidgetRenderState(widget)
80 : )
81 : }
82 :
83 131 : function isWidgetInputLinked(node: LGraphNode, widgetName: string): boolean {
84 131 : const graph = node.graph
85 131 : const slot = node.inputs?.findIndex((i) => i.widget?.name === widgetName)
86 131 : if (!graph || slot === undefined || slot < 0) return false
87 131 : return linkStore.isInputSlotConnected(graphScopeOf(graph), node.id, slot)
88 : }
89 :
90 85 : watchEffect(() => {
91 98 : for (const entry of resolvedInputs.value) {
92 131 : if (entry.status !== 'resolved') continue
93 131 : if (entry.node.mode !== LGraphEventMode.ALWAYS) continue
94 128 : ensureSelectedWidgetState(entry.widgetId, entry.widget)
95 : }
96 : })
97 :
98 85 : const mappedSelections = computed((): WidgetEntry[] => {
99 101 : return resolvedInputs.value.flatMap((entry) => {
100 134 : if (entry.status !== 'resolved') return []
101 131 : const { widgetId, node, widget, config } = entry
102 134 : if (node.mode !== LGraphEventMode.ALWAYS) return []
103 :
104 134 : if (isWidgetInputLinked(node, widget.name)) return []
105 131 : const fullNodeData = nodeToNodeData(node, widgetId)
106 :
107 134 : return [
108 : {
109 : key: widgetId,
110 : persistedHeight: config?.height,
111 : description: config?.description,
112 : nodeData: fullNodeData,
113 : widgetIds: [widgetId],
114 : action: { widget, node }
115 : }
116 : ]
117 : })
118 : })
119 :
120 131 : function getDropIndicator(node: LGraphNode, id: WidgetId) {
121 131 : return getLoaderDropIndicator(node, id, {
122 : mobile,
123 : label: t,
124 : onMaskEdit: maskEditor.openMaskEditor,
125 : widgetValueStore,
126 : previewRevision: templateInputDownloadStore.previewRevision
127 : })
128 : }
129 :
130 131 : function nodeToNodeData(node: LGraphNode, id: WidgetId) {
131 131 : const dropIndicator = getDropIndicator(node, id)
132 :
133 131 : return {
134 : ...node._state,
135 : hasErrors: !!executionErrorStore.surfacedNodeErrors?.[node.id],
136 : dropIndicator,
137 : onDragDrop: node.onDragDrop,
138 : onDragOver: node.onDragOver
139 : }
140 : }
141 :
142 3 : async function handleDragDrop() {
143 3 : const onDragDrop = async (e: DragEvent) => {
144 3 : for (const { nodeData } of mappedSelections.value)
145 3 : if (nodeData?.onDragOver?.(e) && (await nodeData.onDragDrop?.(e)))
146 1 : return true
147 1 : return false
148 : }
149 :
150 3 : app.dragOverNode = { id: UNASSIGNED_NODE_ID, onDragDrop }
151 : }
152 :
153 85 : defineExpose({ handleDragDrop })
154 85 : </script>
155 : <template>
156 101 : <div
157 : v-for="{
158 : key,
159 : persistedHeight,
160 : nodeData,
161 : widgetIds,
162 : action,
163 : description
164 : } in mappedSelections"
165 : :key
166 : :class="cn(builderMode && ['pointer-events-auto', draggableItemClass])"
167 : :aria-label="
168 : builderMode
169 : ? `${action.widget.label ?? action.widget.name} — ${action.node.title}`
170 : : undefined
171 : "
172 : :data-testid="builderMode ? 'builder-widget-item' : 'app-mode-widget-item'"
173 : :data-widget-key="key"
174 : >
175 : <div
176 : :class="
177 : cn(
178 : 'mt-1.5 flex min-h-8 items-center gap-1 px-3',
179 : builderMode && 'drag-handle'
180 : )
181 : "
182 : >
183 : <span
184 : :class="cn('truncate text-sm/8', builderMode && 'pointer-events-none')"
185 : data-testid="builder-widget-label"
186 : >
187 : {{ action.widget.label || action.widget.name }}
188 : </span>
189 : <span
190 : v-if="builderMode"
191 : class="pointer-events-none mx-1 min-w-10 flex-1 truncate text-right text-xs text-muted-foreground"
192 : >
193 : {{ action.node.title }}
194 : </span>
195 : <div v-else class="flex-1" />
196 : <Menu
197 : :class="cn('shrink-0', builderMode && 'pointer-events-auto')"
198 : :items="[
199 : {
200 : label: t('g.rename'),
201 : // fallow-ignore-next-line css-token-drift
202 : icon: 'icon-[lucide--pencil]',
203 : command: () => promptRenameWidget(action.widget, action.node, t)
204 : },
205 : {
206 : label: t('g.remove'),
207 : icon: 'icon-[lucide--x]',
208 : command: () => appModeStore.removeSelectedInput(action.widget)
209 : }
210 : ]"
211 : >
212 : <template #trigger>
213 : <Button
214 : variant="textonly"
215 : size="icon"
216 : data-testid="widget-actions-menu"
217 : >
218 : <!-- fallow-ignore-next-line css-token-drift -->
219 : <i class="icon-[lucide--ellipsis]" />
220 : </Button>
221 : </template>
222 : </Menu>
223 : </div>
224 : <div
225 : v-if="description || builderMode"
226 : data-testid="app-mode-widget-description"
227 : class="h-5 px-5 text-muted-foreground"
228 : >
229 : <WidgetDescription
230 : :description
231 : label-class="drag-handle"
232 : label-type="div"
233 : :disabled="!builderMode"
234 : :widget="action.widget"
235 : />
236 : </div>
237 : <div
238 : :style="
239 : persistedHeight
240 : ? { '--persisted-height': `${persistedHeight}px` }
241 : : undefined
242 : "
243 : :class="
244 : cn(
245 : builderMode && 'pointer-events-none',
246 : persistedHeight &&
247 : '**:data-[slot=drop-zone-indicator]:h-(--persisted-height) [&_textarea]:h-(--persisted-height)'
248 : )
249 : "
250 : :inert="builderMode || undefined"
251 : @pointerdown.capture="(e) => onPointerDown(action.widget, e)"
252 : >
253 : <DropZone
254 : :on-drag-over="nodeData.onDragOver"
255 : :on-drag-drop="nodeData.onDragDrop"
256 : :drop-indicator="nodeData.dropIndicator"
257 : class="text-muted-foreground"
258 : >
259 : <NodeWidgets
260 : :node-data
261 : :widget-ids
262 : :class="
263 : cn(
264 : 'gap-y-3 rounded-lg py-1 [&_textarea]:resize-y **:[.col-span-2]:grid-cols-1',
265 : nodeData.hasErrors && 'ring-2 ring-destructive ring-inset'
266 : )
267 : "
268 : />
269 : </DropZone>
270 : </div>
271 : </div>
272 : </template>
|