Line data Source code
1 : <script setup lang="ts">
2 : import { computed, customRef, ref } from 'vue'
3 : import { useI18n } from 'vue-i18n'
4 :
5 : import EditableText from '@comfyorg/design-system/common/EditableText'
6 : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
7 : import type { SubgraphNode } from '@/lib/litegraph/src/subgraph/SubgraphNode'
8 : import type { IBaseWidget } from '@comfyorg/graph-model/widgets'
9 : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
10 : import { draggableItemClass } from '@/scripts/ui/draggableList'
11 : import WidgetLegacy from '@/renderer/extensions/vueNodes/widgets/components/WidgetLegacy.vue'
12 : import {
13 : getComponent,
14 : shouldExpand
15 : } from '@/renderer/extensions/vueNodes/widgets/registry/widgetRegistry'
16 : import { useLinkStore } from '@/stores/linkStore'
17 : import { graphScopeOf } from '@comfyorg/graph-model/graphScopeId'
18 : import { useNodeDefStore } from '@/stores/nodeDefStore'
19 : import {
20 : stripGraphPrefix,
21 : useWidgetValueStore
22 : } from '@/stores/widgetValueStore'
23 : import { useFavoritedWidgetsStore } from '@/renderer/extensions/propertiesPanel/favoritedWidgetsStore'
24 : import { getControlWidget } from '@/types/simplifiedWidget'
25 : import type {
26 : SimplifiedWidget,
27 : WidgetValue as SimplifiedWidgetValue
28 : } from '@/types/simplifiedWidget'
29 : import { widgetId } from '@comfyorg/graph-model/widgetId'
30 : import { resolveNodeDisplayName } from '@/utils/nodeTitleUtil'
31 : import { cn } from '@comfyorg/tailwind-utils'
32 : import { renameWidget } from '@/utils/widgetUtil'
33 : import type { WidgetValue } from '@/utils/widgetUtil'
34 :
35 : import WidgetActions from './WidgetActions.vue'
36 :
37 : const {
38 : widget,
39 : node,
40 : isDraggable = false,
41 : hiddenFavoriteIndicator = false,
42 : hiddenWidgetActions = false,
43 : showNodeName = false,
44 : parents = [],
45 : isShownOnParents = false
46 : } = defineProps<{
47 : widget: IBaseWidget
48 : node: LGraphNode
49 : isDraggable?: boolean
50 : hiddenFavoriteIndicator?: boolean
51 : hiddenWidgetActions?: boolean
52 : showNodeName?: boolean
53 : parents?: SubgraphNode[]
54 : isShownOnParents?: boolean
55 : }>()
56 :
57 2355 : const emit = defineEmits<{
58 : 'update:widgetValue': [value: WidgetValue]
59 : resetToDefault: [value: WidgetValue]
60 : }>()
61 :
62 387 : const { t } = useI18n()
63 :
64 387 : const canvasStore = useCanvasStore()
65 387 : const nodeDefStore = useNodeDefStore()
66 387 : const widgetValueStore = useWidgetValueStore()
67 387 : const favoritedWidgetsStore = useFavoritedWidgetsStore()
68 387 : const linkStore = useLinkStore()
69 387 : const isEditing = ref(false)
70 :
71 387 : const widgetComponent = computed(() => {
72 : const component = getComponent(widget.type)
73 387 : return component || WidgetLegacy
74 : })
75 :
76 387 : const isLinked = computed(() => {
77 387 : const graph = node.graph
78 387 : const slot = node.inputs?.findIndex((i) => i.widget?.name === widget.name)
79 387 : if (!graph || slot === undefined || slot < 0) return false
80 371 : return linkStore.isInputSlotConnected(graphScopeOf(graph), node.id, slot)
81 : })
82 :
83 387 : const simplifiedWidget = computed((): SimplifiedWidget => {
84 396 : const graphId = node.graph?.rootGraph.id
85 396 : const bareNodeId = stripGraphPrefix(node.id)
86 396 : const widgetState = widget.widgetId
87 : ? useWidgetValueStore().getWidget(widget.widgetId)
88 : : graphId && bareNodeId
89 : ? widgetValueStore.getWidget(widgetId(graphId, bareNodeId, widget.name))
90 : : undefined
91 396 : const widgetName = widgetState?.name ?? widget.name
92 396 : const widgetType = widgetState?.type ?? widget.type
93 :
94 396 : const baseOptions = widgetState?.options ?? widget.options
95 396 : const disabled = isLinked.value || !!widget.disabled || undefined
96 396 : return {
97 : name: widgetName,
98 : type: widgetType,
99 : value: (widgetState
100 : ? widgetState.value
101 : : widget.value) as SimplifiedWidgetValue,
102 : label: widgetState?.label ?? widget.label,
103 : options: { ...baseOptions, disabled },
104 : callback:
105 : typeof widgetType === 'string' && widgetType.toLowerCase() === 'button'
106 : ? (value) =>
107 : widget.callback?.(value, canvasStore.canvas ?? undefined, node)
108 : : undefined,
109 : spec: nodeDefStore.getInputSpecForWidget(node, widgetName),
110 : controlWidget: getControlWidget(widget)
111 : }
112 : })
113 :
114 387 : const displayNodeName = computed((): string | null => {
115 399 : if (!node) return null
116 399 : const fallbackNodeTitle = t('rightSidePanel.fallbackNodeTitle')
117 399 : return resolveNodeDisplayName(node, {
118 : emptyLabel: fallbackNodeTitle,
119 : untitledLabel: fallbackNodeTitle
120 : })
121 : })
122 :
123 387 : const hasParents = computed(() => parents.length > 0)
124 387 : const favoriteNode = computed(() =>
125 : isShownOnParents && hasParents.value ? parents[0] : node
126 : )
127 :
128 387 : const widgetValue = computed({
129 : get: () => widget.value,
130 : set: (newValue: WidgetValue) => {
131 1 : emit('update:widgetValue', newValue)
132 : }
133 : })
134 :
135 387 : const displayLabel = customRef((track, trigger) => {
136 387 : return {
137 : get() {
138 844 : track()
139 844 : return widget.label || widget.name
140 : },
141 : set(newValue: string) {
142 0 : isEditing.value = false
143 :
144 0 : const trimmedLabel = newValue.trim()
145 :
146 : const success = renameWidget(widget, node, trimmedLabel)
147 :
148 0 : if (success) {
149 0 : canvasStore.canvas?.setDirty(true)
150 0 : trigger()
151 : }
152 : }
153 : }
154 : })
155 387 : </script>
156 :
157 : <template>
158 422 : <div
159 : :class="
160 : cn(
161 : 'widget-item group col-span-full grid grid-cols-subgrid rounded-lg',
162 : isDraggable && [
163 : draggableItemClass,
164 : 'bg-background outline-background will-change-auto [&.is-draggable]:opacity-70 [&.is-draggable]:outline-4'
165 : ]
166 : )
167 : "
168 : >
169 : <div
170 : :class="
171 : cn(
172 : 'mb-1.5 flex min-h-8 min-w-0 items-center justify-between gap-1',
173 : isDraggable && 'pointer-events-none'
174 : )
175 : "
176 : >
177 : <EditableText
178 : v-if="widget.name"
179 : :model-value="displayLabel"
180 : :is-editing
181 : :input-attrs="{ placeholder: widget.name }"
182 : class="pointer-events-auto m-0 cursor-text truncate p-0 text-sm/8"
183 : @edit="displayLabel = $event"
184 : @cancel="isEditing = false"
185 : @click="isEditing = true"
186 : />
187 :
188 : <span
189 : v-if="(showNodeName || hasParents) && displayNodeName"
190 : class="mx-1 my-0 min-w-10 flex-1 truncate text-right text-xs text-muted-foreground"
191 : >
192 : {{ displayNodeName }}
193 : </span>
194 : <div
195 : v-if="!hiddenWidgetActions"
196 : class="pointer-events-auto flex shrink-0 items-center gap-1"
197 : >
198 : <WidgetActions
199 : v-model:label="displayLabel"
200 : :widget
201 : :node
202 : :parents
203 : :is-shown-on-parents
204 : @reset-to-default="emit('resetToDefault', $event)"
205 : />
206 : </div>
207 : </div>
208 : <div
209 : v-if="
210 : !hiddenFavoriteIndicator &&
211 : favoritedWidgetsStore.isFavorited(favoriteNode, widget.name)
212 : "
213 : class="pointer-events-none relative z-2"
214 : >
215 : <i
216 : class="pi pi-star-fill pointer-events-none absolute -top-1 -right-1 text-xs text-muted-foreground"
217 : />
218 : </div>
219 : <component
220 : :is="widgetComponent"
221 : v-model="widgetValue"
222 : :widget="simplifiedWidget"
223 : :node-id="node.id"
224 : :node-type="node.type"
225 : :node-title="displayNodeName ?? undefined"
226 : :class="
227 : cn(
228 : 'col-span-1 text-xs text-node-foreground',
229 : shouldExpand(widget.type) && 'min-h-36'
230 : )
231 : "
232 : />
233 : <div
234 : :class="
235 : cn(
236 : 'pointer-events-none mx-auto mt-1.5 h-1 w-1/2 max-w-40 rounded-lg bg-transparent transition-colors duration-150',
237 : 'group-hover:bg-border group-[.is-draggable]:bg-muted-foreground',
238 : !isDraggable && 'opacity-0'
239 : )
240 : "
241 : />
242 : </div>
243 : </template>
|