Line data Source code
1 : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
2 : import { useWidgetValueStore } from '@/stores/widgetValueStore'
3 : import type { WidgetValue } from '@/types/simplifiedWidget'
4 : import type { WidgetId } from '@comfyorg/graph-model/widgetId'
5 : import { widgetId } from '@comfyorg/graph-model/widgetId'
6 :
7 60 : function nodeWidgetId(node: LGraphNode, name: string): WidgetId | null {
8 60 : const graphId = node.graph?.rootGraph.id
9 60 : return graphId ? widgetId(graphId, node.id, name) : null
10 : }
11 :
12 45 : export function getNodeWidgetValue(node: LGraphNode, name: string): unknown {
13 45 : const id = nodeWidgetId(node, name)
14 45 : const state = id ? useWidgetValueStore().getWidget(id) : undefined
15 45 : if (state) return state.value
16 45 : return node.widgets?.find((w) => w.name === name)?.value
17 : }
18 :
19 15 : export function setNodeWidgetValue(
20 : node: LGraphNode,
21 : name: string,
22 : value: WidgetValue
23 : ): boolean {
24 15 : const id = nodeWidgetId(node, name)
25 15 : const widget = node.widgets?.find((w) => w.name === name)
26 15 : const previousValue = getNodeWidgetValue(node, name)
27 15 : if (id && useWidgetValueStore().setValue(id, value)) {
28 15 : if (widget) {
29 15 : widget.value = value
30 15 : widget.callback?.(value)
31 15 : node.onWidgetChanged?.(name, value, previousValue, widget)
32 : }
33 15 : return true
34 : }
35 0 : if (!widget) return false
36 0 : widget.value = value
37 15 : widget.callback?.(value)
38 15 : node.onWidgetChanged?.(name, value, previousValue, widget)
39 15 : return true
40 : }
|