Line data Source code
1 : import { toRaw } from 'vue'
2 : import type { Component } from 'vue'
3 :
4 : import { useChainCallback } from '@/composables/functional/useChainCallback'
5 : // LegacyWidget is imported from its own module, not the barrel: the barrel
6 : // exports it after SubgraphNode, so a litegraph-internal importer of this file
7 : // would hit its TDZ. LGraphNode/LiteGraph must stay barrel-sourced — a direct
8 : // LGraphNode import re-triggers the LGraph<->Subgraph cycle.
9 : import { LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph'
10 : import { wasWidgetRefused } from '@/lib/litegraph/src/node/widgetsView'
11 : import type { IBaseWidget } from '@comfyorg/graph-model/widgets'
12 : import { LegacyWidget } from '@/lib/litegraph/src/widgets/LegacyWidget'
13 : import type { InputSpec } from '@comfyorg/object-info-parser/schemas/nodeDefSchemaV2'
14 : import { useDomWidgetStore } from '@/stores/domWidgetStore'
15 : import type {
16 : BaseDOMWidget,
17 : ComponentWidget,
18 : ComponentWidgetCustomProps,
19 : DOMWidget,
20 : DOMWidgetOptions
21 : } from '@/types/domWidget'
22 : import { generateUUID } from '@/utils/formatUtil'
23 :
24 36250 : function readDomWidgetValue<V extends object | string>(
25 : options: DOMWidgetOptions<V>
26 : ): V {
27 36250 : const { getValue } = options
28 36250 : return (
29 : (typeof getValue === 'function' ? getValue.call(options) : null) ??
30 : ('' as V)
31 : )
32 : }
33 :
34 6897 : function writeDomWidgetValue<V extends object | string>(
35 : options: DOMWidgetOptions<V>,
36 : value: V
37 : ): void {
38 6897 : const { setValue } = options
39 6897 : if (typeof setValue === 'function') setValue.call(options, value)
40 : }
41 :
42 2356 : export const isDOMWidget = <T extends HTMLElement, V extends object | string>(
43 : widget: IBaseWidget
44 : ): widget is DOMWidget<T, V> => 'element' in widget && !!widget.element
45 :
46 2356 : export const isComponentWidget = <V extends object | string>(
47 : widget: IBaseWidget
48 : ): widget is ComponentWidget<V> => 'component' in widget && !!widget.component
49 :
50 : type BaseDOMWidgetInit<V extends object | string> = Pick<
51 : BaseDOMWidget<V>,
52 : 'node' | 'name' | 'type' | 'options'
53 : >
54 :
55 : type DOMWidgetInit<
56 : T extends HTMLElement,
57 : V extends object | string
58 : > = BaseDOMWidgetInit<V> & Pick<DOMWidget<T, V>, 'element'>
59 :
60 : type ComponentWidgetInit<
61 : V extends object | string,
62 : P extends ComponentWidgetCustomProps
63 : > = Omit<BaseDOMWidgetInit<V>, 'type'> & {
64 : type?: string
65 : } & Pick<ComponentWidget<V, P>, 'component' | 'inputSpec' | 'props'>
66 :
67 2356 : abstract class BaseDOMWidgetImpl<V extends object | string>
68 : extends LegacyWidget<IBaseWidget<V, string, DOMWidgetOptions<V>>>
69 : implements BaseDOMWidget<V>
70 : {
71 : static readonly DEFAULT_MARGIN = 10
72 : declare callback?: (value: V) => void
73 :
74 : readonly id: string
75 :
76 : constructor(obj: BaseDOMWidgetInit<V>) {
77 7359 : const { node, name, type, options } = obj
78 7359 : super({ y: 0, name, type, options }, node)
79 :
80 7359 : this.id = generateUUID()
81 : }
82 :
83 : override get value(): V {
84 1811 : return readDomWidgetValue(this.options)
85 : }
86 :
87 : override set value(v: V) {
88 272 : writeDomWidgetValue(this.options, v)
89 272 : this.callback?.(this.value)
90 : }
91 :
92 : get margin(): number {
93 27830 : return this.options.margin ?? BaseDOMWidgetImpl.DEFAULT_MARGIN
94 : }
95 :
96 : isVisible(): boolean {
97 45659 : return (
98 : !this.hidden && !this.computedDisabled && this.node.isWidgetVisible(this)
99 : )
100 : }
101 :
102 : override draw(
103 : ctx: CanvasRenderingContext2D,
104 : _node: LGraphNode,
105 : widget_width: number,
106 : y: number,
107 : widget_height: number,
108 : lowQuality?: boolean
109 : ): void {
110 21456 : const isVisible = this.isVisible()
111 :
112 21456 : if (this.options.hideOnZoom && lowQuality && isVisible) {
113 : // Draw a placeholder rectangle
114 188 : const originalFillStyle = ctx.fillStyle
115 188 : ctx.beginPath()
116 188 : ctx.fillStyle = LiteGraph.WIDGET_BGCOLOR
117 188 : ctx.rect(
118 : this.margin,
119 : y + this.margin,
120 : widget_width - this.margin * 2,
121 : (this.computedHeight ?? widget_height) - 2 * this.margin
122 : )
123 188 : ctx.fill()
124 188 : ctx.fillStyle = originalFillStyle
125 : } else {
126 21268 : if (!isVisible) {
127 0 : this.options.onDraw?.(this)
128 0 : return
129 : }
130 : }
131 21456 : this.options.onDraw?.(this)
132 : }
133 :
134 : override onRemove(): void {
135 3700 : useDomWidgetStore().unregisterWidget(this.id)
136 : }
137 : }
138 :
139 2356 : export class DOMWidgetImpl<T extends HTMLElement, V extends object | string>
140 : extends BaseDOMWidgetImpl<V>
141 : implements DOMWidget<T, V>
142 : {
143 : override readonly element: T
144 :
145 : constructor(obj: DOMWidgetInit<T, V>) {
146 7252 : super(obj)
147 7252 : this.element = obj.element
148 : }
149 :
150 : /** Extract DOM widget size info */
151 : override computeLayoutSize() {
152 73254 : if (this.type === 'hidden') {
153 0 : return {
154 : minHeight: 0,
155 : maxHeight: 0,
156 : minWidth: 0
157 : }
158 : }
159 :
160 73254 : const styles = getComputedStyle(this.element)
161 73254 : let minHeight =
162 : this.options.getMinHeight?.() ??
163 : parseInt(styles.getPropertyValue('--comfy-widget-min-height'))
164 73254 : const maxHeight =
165 : this.options.getMaxHeight?.() ??
166 : parseInt(styles.getPropertyValue('--comfy-widget-max-height'))
167 :
168 73254 : const prefHeight: string | number =
169 : this.options.getHeight?.() ??
170 : styles.getPropertyValue('--comfy-widget-height')
171 :
172 : const isPercentageHeight =
173 : typeof prefHeight === 'string' && prefHeight.endsWith('%')
174 73254 : if (!isPercentageHeight && isNaN(minHeight)) {
175 73254 : minHeight =
176 : typeof prefHeight === 'number' ? prefHeight : parseInt(prefHeight)
177 : }
178 :
179 73254 : return {
180 : minHeight: isNaN(minHeight) ? 50 : minHeight,
181 : maxHeight: isNaN(maxHeight) ? undefined : maxHeight,
182 : minWidth: 0
183 : }
184 : }
185 : }
186 :
187 2356 : export class ComponentWidgetImpl<
188 : V extends object | string,
189 : P extends ComponentWidgetCustomProps = ComponentWidgetCustomProps
190 : >
191 : extends BaseDOMWidgetImpl<V>
192 : implements ComponentWidget<V, P>
193 : {
194 : readonly component: Component
195 : readonly inputSpec: InputSpec
196 : readonly props?: P
197 :
198 : constructor(obj: ComponentWidgetInit<V, P>) {
199 107 : super({
200 : type: 'custom',
201 : ...obj
202 : })
203 107 : this.component = obj.component
204 107 : this.inputSpec = obj.inputSpec
205 107 : this.props = obj.props
206 : }
207 :
208 : override computeLayoutSize() {
209 : const minHeight = this.options.getMinHeight?.() ?? 50
210 : const maxHeight = this.options.getMaxHeight?.()
211 1191 : return {
212 : minHeight,
213 : maxHeight,
214 : minWidth: 0
215 : }
216 : }
217 :
218 : override serializeValue(): V {
219 1 : return toRaw(this.value)
220 : }
221 : }
222 :
223 2356 : export const addWidget = (node: LGraphNode, widget: BaseDOMWidget) => {
224 6720 : node.addCustomWidget(widget)
225 :
226 6720 : if (wasWidgetRefused(node, widget)) return
227 :
228 6720 : if (node.graph) {
229 83 : useDomWidgetStore().registerWidget(widget)
230 : }
231 :
232 6720 : node.onAdded = useChainCallback(node.onAdded, () => {
233 6618 : if (wasWidgetRefused(node, widget)) return
234 6618 : useDomWidgetStore().registerWidget(widget)
235 : })
236 :
237 6720 : node.onRemoved = useChainCallback(node.onRemoved, () => {
238 3239 : widget.onRemove?.()
239 : })
240 :
241 6720 : node.onResize = useChainCallback(node.onResize, () => {
242 34245 : widget.options.beforeResize?.call(widget, node)
243 34245 : widget.options.afterResize?.call(widget, node)
244 : })
245 : }
246 :
247 2356 : LGraphNode.prototype.addDOMWidget = function <
248 : T extends HTMLElement,
249 : V extends object | string
250 : >(
251 : this: LGraphNode,
252 : name: string,
253 : type: string,
254 : element: T,
255 : options: DOMWidgetOptions<V> = {}
256 : ): DOMWidget<T, V> {
257 6613 : const widget = new DOMWidgetImpl({
258 : node: this,
259 : name,
260 : type,
261 : element,
262 : options: { hideOnZoom: true, ...options }
263 : })
264 : // Note: Before `LGraphNode.configure` is called, `this.id` is always `-1`.
265 6613 : addWidget(this, widget as unknown as BaseDOMWidget)
266 :
267 : // Workaround for https://github.com/Comfy-Org/ComfyUI_frontend/issues/2493
268 : // Some custom nodes are explicitly expecting getter and setter of `value`
269 : // property to be on instance instead of prototype.
270 6613 : Object.defineProperty(widget, 'value', {
271 : get(this: DOMWidgetImpl<T, V>): V {
272 34439 : return readDomWidgetValue(this.options)
273 : },
274 : set(this: DOMWidgetImpl<T, V>, v: V) {
275 6625 : writeDomWidgetValue(this.options, v)
276 6625 : this.callback?.(this.value)
277 : }
278 : })
279 :
280 6613 : return widget
281 : }
|