Line data Source code
1 : import type { PointTuple as Point } from '@comfyorg/shared-frontend-utils/geometry'
2 : import type { LLink } from '@/lib/litegraph/src/litegraph'
3 : import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
4 : import type { CanvasPointerEvent } from '@comfyorg/graph-model/events'
5 : import { app } from '@/scripts/app'
6 : import { useLinkStore } from '@/stores/linkStore'
7 : import { graphScopeOf } from '@comfyorg/graph-model/graphScopeId'
8 : import type { NodeId } from '@comfyorg/graph-model/nodeId'
9 : import type { WidgetValue } from '@/types/simplifiedWidget'
10 :
11 : type SourceNode = Pick<LGraphNode, 'id' | 'graph' | 'widgets'>
12 :
13 : interface TargetEndpoint {
14 : targetNodeId: NodeId
15 : targetSlot: number
16 : }
17 :
18 13 : export function applyFirstWidgetValueToGraph(
19 : node: SourceNode,
20 : extraLinks: LLink[] = [],
21 : transformValue?: (value: WidgetValue) => WidgetValue
22 : ) {
23 13 : const { graph } = node
24 13 : if (!graph) return
25 :
26 13 : const linked = [
27 : ...useLinkStore().getOutputSlotLinks(graphScopeOf(graph), node.id, 0)
28 : ]
29 13 : if (!linked.length) return
30 :
31 13 : const sourceWidget = node.widgets?.[0]
32 13 : if (!sourceWidget) return
33 :
34 13 : let value = sourceWidget.value
35 13 : if (transformValue) {
36 13 : value = transformValue(value)
37 : }
38 :
39 13 : const graphMouse: Point = app.canvas.graph_mouse
40 :
41 13 : const endpoints: TargetEndpoint[] = [
42 : ...linked.map(({ targetNodeId, targetSlot }) => ({
43 : targetNodeId,
44 : targetSlot
45 : })),
46 : ...extraLinks.map((link) => ({
47 : targetNodeId: link.target_id,
48 : targetSlot: link.target_slot
49 : }))
50 : ]
51 :
52 13 : for (const endpoint of endpoints) {
53 19 : const targetNode = graph.getNodeById(endpoint.targetNodeId)
54 19 : const input = targetNode?.inputs[endpoint.targetSlot]
55 19 : if (!targetNode || !input) {
56 0 : console.warn('Unable to resolve node or input for link', endpoint)
57 0 : continue
58 : }
59 :
60 19 : const widgetName = input.widget?.name
61 19 : if (!widgetName) {
62 0 : console.warn('Invalid widget or widget name', input.widget)
63 0 : continue
64 : }
65 :
66 19 : const targetWidget = targetNode.widgets?.find(
67 : (widget) => widget.name === widgetName
68 : )
69 19 : if (!targetWidget) {
70 4 : console.warn(
71 : `Unable to find widget "${widgetName}" on node [${targetNode.id}]`
72 : )
73 4 : continue
74 : }
75 :
76 15 : targetWidget.value = value
77 19 : targetWidget.callback?.(
78 : targetWidget.value,
79 : app.canvas,
80 : targetNode,
81 : graphMouse,
82 : {} as CanvasPointerEvent
83 : )
84 : }
85 : }
|