Line data Source code
1 : <template>
2 19 : <div
3 : :data-node-id="nodeData.id"
4 : :class="
5 : cn(
6 : 'lg-node flex w-[350px] touch-none flex-col rounded-2xl border border-solid border-node-border bg-node pb-1 text-xs text-node-foreground outline-2 outline-transparent contain-layout contain-style',
7 : position
8 : )
9 : "
10 : >
11 : <div
12 : class="pointer-events-none relative flex flex-col items-center justify-center"
13 : >
14 : <NodeHeader :node-data="nodeData" />
15 : </div>
16 : <div
17 : class="pointer-events-none flex flex-1 flex-col gap-1 pb-2"
18 : data-testid="node-preview-body"
19 : >
20 : <NodeSlots :node-data="nodeData" :sync-layout="false" />
21 :
22 : <WidgetGrid
23 : v-if="previewWidgets.length"
24 : :processed-widgets="previewWidgets"
25 : :node-type="nodeData.type"
26 : :node-id="nodeData.id"
27 : :sync-layout="false"
28 : class="pointer-events-none"
29 : />
30 : </div>
31 : </div>
32 : </template>
33 :
34 : <script setup lang="ts">
35 : import { computed } from 'vue'
36 :
37 : import type {
38 : INodeInputSlot,
39 : INodeOutputSlot
40 : } from '@comfyorg/graph-model/slots'
41 : import { LGraphEventMode, RenderShape } from '@/lib/litegraph/src/litegraph'
42 : import NodeHeader from '@/renderer/extensions/vueNodes/components/NodeHeader.vue'
43 : import NodeSlots from '@/renderer/extensions/vueNodes/components/NodeSlots.vue'
44 : import WidgetGrid from '@/renderer/extensions/vueNodes/components/WidgetGrid.vue'
45 : import type { WidgetGridItem } from '@/renderer/extensions/vueNodes/types/widgetGrid'
46 : import WidgetLegacy from '@/renderer/extensions/vueNodes/widgets/components/WidgetLegacy.vue'
47 : import { getComponent } from '@/renderer/extensions/vueNodes/widgets/registry/widgetRegistry'
48 : import { dynamicComboOptionKeys } from '@/core/graph/nodeDef/inputSpecInterpreter'
49 : import type { ComfyNodeDef as ComfyNodeDefV2 } from '@comfyorg/object-info-parser/schemas/nodeDefSchemaV2'
50 : import { useWidgetStore } from '@/stores/widgetStore'
51 : import { toNodeId } from '@comfyorg/graph-model/nodeId'
52 : import type { NodeState } from '@comfyorg/graph-model/nodeState'
53 : import type { WidgetValue } from '@/types/simplifiedWidget'
54 : import { zeroUuid } from '@comfyorg/graph-model/uuid'
55 : import { matchesWidgetName } from '@/utils/widgetBinding'
56 : import type { WidgetValueBinding } from '@/utils/widgetBinding'
57 : import { cn } from '@comfyorg/tailwind-utils'
58 :
59 : const {
60 : nodeDef,
61 : position = 'absolute',
62 : widgetValues
63 : } = defineProps<{
64 : nodeDef: ComfyNodeDefV2
65 : position?: 'absolute' | 'relative'
66 : widgetValues?: readonly WidgetValueBinding[]
67 : }>()
68 :
69 19 : const widgetStore = useWidgetStore()
70 :
71 19 : const previewInputs = computed<INodeInputSlot[]>(() =>
72 : Object.entries(nodeDef.inputs || {})
73 : .filter(([, input]) => !widgetStore.inputIsWidget(input))
74 : .map(([name, input]) => ({
75 : name,
76 : type: input.type,
77 : shape: input.isOptional ? RenderShape.HollowCircle : undefined,
78 : boundingRect: [0, 0, 0, 0],
79 : link: null
80 : }))
81 : )
82 :
83 19 : const previewOutputs = computed<INodeOutputSlot[]>(() =>
84 : (nodeDef.outputs || []).map((output) => {
85 27 : if (typeof output === 'string') {
86 0 : return {
87 : name: output,
88 : type: output,
89 : boundingRect: [0, 0, 0, 0],
90 : links: []
91 : }
92 : }
93 27 : return {
94 : ...output,
95 : boundingRect: [0, 0, 0, 0],
96 : links: []
97 : }
98 : })
99 : )
100 :
101 19 : const nodeData = computed<NodeState>(() => ({
102 : id: toNodeId(`preview-${nodeDef.name}`),
103 : graphId: zeroUuid,
104 : title: nodeDef.display_name || nodeDef.name,
105 : type: nodeDef.name,
106 : mode: LGraphEventMode.ALWAYS,
107 : flags: {
108 : collapsed: false
109 : },
110 : inputs: previewInputs.value,
111 : outputs: previewOutputs.value,
112 : properties: {}
113 : }))
114 :
115 19 : const widgetInputs = computed(() =>
116 : Object.entries(nodeDef.inputs || {}).filter(([, input]) =>
117 : widgetStore.inputIsWidget(input)
118 : )
119 : )
120 :
121 19 : const resolvedWidgetValues = computed(() =>
122 : Object.fromEntries(
123 : (widgetValues ?? []).flatMap(({ selector, value }) => {
124 3 : const input = widgetInputs.value.find(([name]) =>
125 : matchesWidgetName(name, selector)
126 : )
127 3 : return input ? [[input[0], value]] : []
128 : })
129 : )
130 : )
131 :
132 19 : const previewWidgets = computed<WidgetGridItem[]>(() =>
133 : widgetInputs.value
134 : .filter(([, input]) => !input.hidden && !input.advanced)
135 : .map(([name, input]) => {
136 51 : const isDynamicCombo = input.type === 'COMFY_DYNAMICCOMBO_V3'
137 51 : const comboValues = isDynamicCombo
138 : ? dynamicComboOptionKeys(input)
139 : : input.type === 'COMBO' && Array.isArray(input.options)
140 : ? input.options
141 : : undefined
142 51 : const leadValue = resolvedWidgetValues.value[name]
143 51 : const value = (leadValue ??
144 : input.default ??
145 : comboValues?.[0] ??
146 : '') as WidgetValue
147 51 : const type = isDynamicCombo ? 'COMBO' : input.widgetType || input.type
148 51 : const values =
149 : leadValue !== undefined && comboValues
150 : ? [leadValue, ...comboValues.filter((option) => option !== leadValue)]
151 : : comboValues
152 51 : return {
153 : visible: true,
154 : renderKey: `preview:${nodeDef.name}:${name}`,
155 : vueComponent: getComponent(type) ?? WidgetLegacy,
156 : simplified: { name, type, value, options: { values }, spec: input }
157 : }
158 : })
159 : )
160 : </script>
|