Line data Source code
1 : import type { LGraph } from '@/lib/litegraph/src/LGraph'
2 : import type { LinkConnectorAdapter } from '@/renderer/core/canvas/links/linkConnectorAdapter'
3 : import { useSlotLinkDragUIState } from '@/renderer/core/canvas/links/slotLinkDragUIState'
4 : import type { SlotDropCandidate } from '@/renderer/core/canvas/links/slotLinkDragUIState'
5 : import { getGraphSlotLayout } from '@/renderer/core/canvas/litegraph/slotCalculations'
6 : import { getSlotKey } from '@/renderer/core/layout/slots/slotIdentifier'
7 : import { layoutStore } from '@/renderer/core/layout/store/layoutStore'
8 : import type { SlotLayout } from '@comfyorg/graph-model/layout'
9 : import type { SlotLinkDragContext } from '@/renderer/extensions/vueNodes/composables/slotLinkDragContext'
10 : import { toNodeId } from '@comfyorg/graph-model/nodeId'
11 : import type { NodeId } from '@comfyorg/graph-model/nodeId'
12 : import { parseSlotId } from '@comfyorg/graph-model/slotId'
13 : import type { SlotId } from '@comfyorg/graph-model/slotId'
14 :
15 : interface DropResolutionContext {
16 : adapter: LinkConnectorAdapter | null
17 : graph: LGraph | null
18 : session: SlotLinkDragContext
19 : }
20 :
21 116 : function getCandidateSlotLayout(
22 : graph: LGraph,
23 : rawKey: string
24 : ): { key: SlotId; layout: SlotLayout } | null {
25 116 : const identifier = parseSlotId(rawKey)
26 116 : if (!identifier) return null
27 116 : const layout = getGraphSlotLayout(
28 : graph,
29 : identifier.nodeId,
30 : identifier.index,
31 : identifier.direction === 'input'
32 : )
33 :
34 116 : return layout ? { key: identifier.key, layout } : null
35 : }
36 :
37 2355 : export const resolveSlotTargetCandidate = (
38 : target: EventTarget | null,
39 : { adapter, graph }: DropResolutionContext
40 : ): SlotDropCandidate | null => {
41 167 : const { state: dragState, setCompatibleForKey } = useSlotLinkDragUIState()
42 167 : if (!(target instanceof HTMLElement)) return null
43 :
44 : const elWithKey = target
45 : .closest('.lg-slot, .lg-node-widget')
46 : ?.querySelector<HTMLElement>('[data-slot-key]')
47 167 : const rawKey = elWithKey?.dataset['slotKey']
48 167 : if (!rawKey) return null
49 :
50 161 : if (!graph) return null
51 116 : const candidateLayout = getCandidateSlotLayout(graph, rawKey)
52 161 : if (!candidateLayout) return null
53 116 : const { key, layout } = candidateLayout
54 :
55 116 : const candidate: SlotDropCandidate = { layout, compatible: false }
56 :
57 116 : if (adapter) {
58 116 : const cached = dragState.compatible.get(key)
59 116 : if (cached != null) {
60 99 : candidate.compatible = cached
61 : } else {
62 17 : const nodeId: NodeId = layout.nodeId
63 17 : const compatible =
64 : layout.type === 'input'
65 : ? adapter.isInputValidDrop(nodeId, layout.index)
66 : : adapter.isOutputValidDrop(nodeId, layout.index)
67 :
68 17 : setCompatibleForKey(key, compatible)
69 17 : candidate.compatible = compatible
70 : }
71 : }
72 :
73 116 : return candidate
74 : }
75 :
76 2355 : export const resolveNodeSurfaceSlotCandidate = (
77 : target: EventTarget | null,
78 : { adapter, graph, session }: DropResolutionContext
79 : ): SlotDropCandidate | null => {
80 167 : const { setCompatibleForKey } = useSlotLinkDragUIState()
81 167 : if (!(target instanceof HTMLElement)) return null
82 :
83 : const elWithNode = target.closest<HTMLElement>('[data-node-id]')
84 167 : const nodeIdAttr = elWithNode?.dataset['nodeId']
85 167 : if (!nodeIdAttr) return null
86 :
87 167 : if (!adapter || !graph) return null
88 :
89 127 : const nodeId: NodeId = toNodeId(nodeIdAttr)
90 :
91 127 : const cachedPreferredSlotForNode = session.preferredSlotForNode.get(nodeId)
92 161 : if (cachedPreferredSlotForNode !== undefined) {
93 60 : return cachedPreferredSlotForNode
94 : ? { layout: cachedPreferredSlotForNode.layout, compatible: true }
95 : : null
96 : }
97 :
98 67 : const node = graph.getNodeById(nodeId)
99 161 : if (!node) return null
100 :
101 67 : const firstLink = adapter.renderLinks.at(0)
102 161 : if (!firstLink) return null
103 :
104 67 : const connectingTo = adapter.linkConnector.state.connectingTo
105 167 : if (connectingTo !== 'input' && connectingTo !== 'output') return null
106 :
107 67 : const isInput = connectingTo === 'input'
108 67 : const slotType = firstLink.fromSlot.type
109 :
110 : const result = isInput
111 : ? node.findInputByType(slotType)
112 : : node.findOutputByType(slotType)
113 :
114 167 : const index = result?.index
115 167 : if (index == null) {
116 9 : session.preferredSlotForNode.set(nodeId, null)
117 9 : return null
118 : }
119 :
120 58 : const key = getSlotKey(nodeId, index, isInput)
121 : const offset = layoutStore.getSlotOffset(
122 : graph.rootGraph.id,
123 : nodeId,
124 : index,
125 : isInput ? 'input' : 'output',
126 : node.flags.collapsed ? 'collapsed' : 'expanded'
127 : )
128 167 : if (!offset) {
129 0 : session.preferredSlotForNode.set(nodeId, null)
130 0 : return null
131 : }
132 :
133 58 : const layout = getGraphSlotLayout(graph, nodeId, index, isInput)
134 154 : if (!layout) {
135 0 : session.preferredSlotForNode.set(nodeId, null)
136 0 : return null
137 : }
138 :
139 167 : const compatible = isInput
140 : ? adapter.isInputValidDrop(nodeId, index)
141 : : adapter.isOutputValidDrop(nodeId, index)
142 :
143 167 : setCompatibleForKey(key, compatible)
144 :
145 167 : if (!compatible) {
146 7 : session.preferredSlotForNode.set(nodeId, null)
147 7 : return null
148 : }
149 :
150 51 : const preferred = { index, key, layout }
151 51 : session.preferredSlotForNode.set(nodeId, preferred)
152 :
153 51 : return { layout, compatible: true }
154 : }
|