Line data Source code
1 : import { SUBGRAPH_INPUT_ID } from '@/lib/litegraph/src/constants'
2 : import type { LGraphNode, SubgraphNode } from '@/lib/litegraph/src/litegraph'
3 : import { app } from '@/scripts/app'
4 : import { useSubgraphNavigationStore } from '@/stores/subgraphNavigationStore'
5 : import type { NodeId } from '@comfyorg/graph-model/nodeId'
6 : import { resolveInputSourceNode } from '@/utils/graphTraversalUtil'
7 :
8 158 : function viewedSubgraphHost(
9 : node: LGraphNode,
10 : renderedNodeId: NodeId
11 : ): SubgraphNode | undefined {
12 158 : const graph = node.graph
13 158 : if (!graph || graph === graph.rootGraph) return undefined
14 :
15 158 : const rendered = app.canvas.graph?.getNodeById(renderedNodeId)
16 158 : if (rendered?.isSubgraphNode() && rendered.subgraph === graph) return rendered
17 :
18 0 : const { navigationStack } = useSubgraphNavigationStore()
19 0 : if (navigationStack.at(-1) !== graph) return undefined
20 : const parent = navigationStack.at(-2) ?? graph.rootGraph
21 158 : const instances = parent.nodes.filter(
22 : (candidate): candidate is SubgraphNode =>
23 : candidate.isSubgraphNode() && candidate.subgraph === graph
24 : )
25 158 : return instances.length === 1 ? instances[0] : undefined
26 : }
27 :
28 158 : export function resolveWidgetInputSource(
29 : node: LGraphNode,
30 : slot: number,
31 : renderedNodeId: NodeId
32 : ): LGraphNode | undefined {
33 158 : return resolveInputSourceNode(
34 : node,
35 : slot,
36 : viewedSubgraphHost(node, renderedNodeId)
37 : )
38 : }
39 :
40 31 : export function isWidgetInputConnected(
41 : node: LGraphNode,
42 : slot: number,
43 : renderedNodeId: NodeId
44 : ): boolean {
45 31 : const link = node.getInputLink(slot)
46 31 : if (!link) return false
47 3 : if (link.origin_id !== SUBGRAPH_INPUT_ID) return true
48 0 : const host = viewedSubgraphHost(node, renderedNodeId)
49 31 : return host ? host.isInputConnected(link.origin_slot) : true
50 : }
|