Line data Source code
1 : import type { INodeInputSlot } from '@comfyorg/graph-model/slots'
2 : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
3 : import type { IBaseWidget } from '@comfyorg/graph-model/widgets'
4 : import { useWidgetValueStore } from '@/stores/widgetValueStore'
5 : import type { NodeId } from '@comfyorg/graph-model/nodeId'
6 :
7 : import { resolveSubgraphInputTarget } from './resolveSubgraphInputTarget'
8 :
9 : /**
10 : * Where a promoted subgraph input is sourced from inside the subgraph. The
11 : * interior node id + widget name that the host input slot forwards to. Resolved
12 : * by walking the live link, so it is authoritative derived data — never stored
13 : * on the projected widget.
14 : */
15 : export interface PromotedSource {
16 : nodeId: NodeId
17 : widgetName: string
18 : }
19 :
20 : /**
21 : * The interior source of a host input slot, or undefined when the slot is not a
22 : * promoted widget input.
23 : */
24 2144 : export function promotedInputSource(
25 : node: LGraphNode,
26 : input: INodeInputSlot
27 : ): PromotedSource | undefined {
28 2144 : if (!input.widgetId) return undefined
29 2144 : return resolveSubgraphInputTarget(node, input.name)
30 : }
31 :
32 : /** The host input slot backing a projected widget, matched by widgetId. */
33 47286 : export function inputForWidget(
34 : node: LGraphNode,
35 : widget: IBaseWidget
36 : ): INodeInputSlot | undefined {
37 47286 : return node.getSlotFromWidget(widget)
38 : }
39 :
40 : /**
41 : * Projects a promoted subgraph input into an ordinary widget descriptor. The
42 : * descriptor is store-backed: type/value/options read live from
43 : * {@link useWidgetValueStore} by widgetId (mirroring BaseWidget), so the row
44 : * list does not reactively rebuild — and re-key — on every value edit.
45 : *
46 : * `name` is the input slot name (unique + fixed; widgetId derives from it), and
47 : * `label` is the mutable display label. Returns null when the input is not a
48 : * promoted widget input.
49 : */
50 1294 : export function promotedInputWidget(input: INodeInputSlot): IBaseWidget | null {
51 1294 : const id = input.widgetId
52 1294 : if (!id) return null
53 585 : const store = useWidgetValueStore()
54 585 : return {
55 : get name() {
56 932 : return store.getWidget(id)?.name ?? input.name
57 : },
58 : get label() {
59 131 : return store.getWidget(id)?.label ?? input.label ?? input.name
60 : },
61 : set label(next) {
62 4 : const state = store.getWidget(id)
63 4 : if (state) state.label = next
64 : },
65 : get y() {
66 0 : return store.getWidget(id)?.y ?? 0
67 : },
68 : set y(next) {
69 0 : const state = store.getWidget(id)
70 0 : if (state) state.y = next
71 : },
72 : widgetId: id,
73 : get type() {
74 64 : return store.getWidget(id)?.type ?? 'text'
75 : },
76 : get options() {
77 3 : return store.getWidget(id)?.options ?? {}
78 : },
79 : set options(next) {
80 0 : store.setOptions(id, next)
81 : },
82 : get value() {
83 92 : return store.getWidget(id)?.value
84 : },
85 : set value(next) {
86 1 : store.setValue(id, next)
87 : },
88 : // Canvas edits operate on a transient concrete widget (toConcreteWidget),
89 : // so the value setter above is never invoked; BaseWidget.setValue writes its
90 : // own local state and then calls this callback, which is the only bridge
91 : // back to the store.
92 : callback(next) {
93 3 : store.setValue(id, next)
94 : }
95 : }
96 : }
97 :
98 500 : export function promotedInputWidgets(node: LGraphNode): IBaseWidget[] {
99 500 : return node.inputs.flatMap((input) => {
100 1220 : const widget = promotedInputWidget(input)
101 1220 : return widget ? [widget] : []
102 : })
103 : }
|