Line data Source code
1 : import { createSharedComposable, tryOnScopeDispose } from '@vueuse/core'
2 : import { storeToRefs } from 'pinia'
3 : import { toValue } from 'vue'
4 :
5 : import type { Positionable } from '@comfyorg/graph-model/interfaces'
6 : import { onCanvasFrame } from '@/lib/litegraph/src/canvas/canvasFrame'
7 : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
8 : import { AutoPanController } from '@/renderer/core/canvas/useAutoPan'
9 : import { useLayoutMutations } from '@/renderer/core/layout/operations/layoutMutations'
10 : import { layoutStore } from '@/renderer/core/layout/store/layoutStore'
11 : import { LayoutSource } from '@/renderer/core/layout/types'
12 : import type { NodeBoundsUpdate } from '@comfyorg/graph-model/layout'
13 : import type { Point2D as Point } from '@comfyorg/shared-frontend-utils/geometry'
14 : import type { NodeId } from '@comfyorg/graph-model/nodeId'
15 : import { useNodeSnap } from '@/renderer/extensions/vueNodes/composables/useNodeSnap'
16 : import { useShiftKeySync } from '@/renderer/extensions/vueNodes/composables/useShiftKeySync'
17 : import { isLGraphNode } from '@/utils/litegraphUtil'
18 :
19 2355 : export const useNodeDrag = createSharedComposable(() => {
20 1710 : const mutations = useLayoutMutations(LayoutSource.Vue)
21 1710 : const { selectedNodeIds, selectedItems } = storeToRefs(useCanvasStore())
22 :
23 1710 : const { shouldSnap, applySnapToPosition } = useNodeSnap()
24 :
25 1710 : const { trackShiftKey } = useShiftKeySync()
26 :
27 1710 : const canvasStore = useCanvasStore()
28 :
29 1710 : let dragStartPos: Point | null = null
30 1710 : let dragStartMouse: Point | null = null
31 1710 : let otherSelectedNodesStartPositions: Map<NodeId, Point> | null = null
32 1710 : let stopFrame: (() => void) | null = null
33 1710 : let stopShiftSync: (() => void) | null = null
34 :
35 1710 : let nonNodeStartPositions: Map<Positionable, Point> | null = null
36 :
37 1710 : let autoPan: AutoPanController | null = null
38 1710 : let lastPointerX = 0
39 1710 : let lastPointerY = 0
40 :
41 22 : function startDrag(
42 : event: PointerEvent,
43 : nodeId: NodeId,
44 : initialShiftKey: boolean
45 : ) {
46 22 : const { rootGraphId } = canvasStore
47 22 : if (!rootGraphId) return
48 :
49 22 : const layout = layoutStore.getNodeLayout(rootGraphId, nodeId)
50 22 : if (!layout) return
51 22 : const position = layout.position
52 :
53 22 : stopShiftSync = trackShiftKey(initialShiftKey)
54 :
55 22 : dragStartPos = { ...position }
56 22 : dragStartMouse = { x: event.clientX, y: event.clientY }
57 22 : lastPointerX = event.clientX
58 22 : lastPointerY = event.clientY
59 :
60 22 : const selectedNodes = toValue(selectedNodeIds)
61 :
62 22 : const isDraggedNodeInSelection = selectedNodes.has(nodeId)
63 :
64 22 : if (isDraggedNodeInSelection && selectedNodes.size > 1) {
65 2 : otherSelectedNodesStartPositions = new Map()
66 :
67 2 : for (const id of selectedNodes) {
68 5 : if (id === nodeId) continue
69 :
70 3 : const nodeLayout = layoutStore.getNodeLayout(rootGraphId, id)
71 3 : if (nodeLayout) {
72 3 : otherSelectedNodesStartPositions.set(id, { ...nodeLayout.position })
73 : }
74 : }
75 : } else {
76 20 : otherSelectedNodesStartPositions = null
77 : }
78 :
79 22 : nonNodeStartPositions = isDraggedNodeInSelection
80 : ? new Map(
81 : toValue(selectedItems)
82 : .filter((item) => !isLGraphNode(item))
83 : .map((item) => [item, { x: item.pos[0], y: item.pos[1] }])
84 : )
85 : : null
86 : }
87 :
88 290 : function startAutoPan(event: PointerEvent, nodeId: NodeId) {
89 290 : if (autoPan) {
90 268 : autoPan.updatePointer(event.clientX, event.clientY)
91 268 : return
92 : }
93 22 : const lgCanvas = canvasStore.canvas
94 290 : if (!lgCanvas?.ds) return
95 :
96 22 : autoPan = new AutoPanController({
97 : canvas: lgCanvas,
98 : maxPanSpeed: lgCanvas.auto_pan_speed,
99 : onPan: (panX, panY) => {
100 30 : if (dragStartPos) {
101 30 : dragStartPos.x += panX
102 30 : dragStartPos.y += panY
103 : }
104 30 : if (otherSelectedNodesStartPositions) {
105 0 : for (const pos of otherSelectedNodesStartPositions.values()) {
106 0 : pos.x += panX
107 0 : pos.y += panY
108 : }
109 : }
110 30 : if (nonNodeStartPositions) {
111 30 : for (const start of nonNodeStartPositions.values()) {
112 0 : start.x += panX
113 0 : start.y += panY
114 : }
115 : }
116 30 : updateNodePositions(nodeId)
117 : }
118 : })
119 22 : autoPan.updatePointer(event.clientX, event.clientY)
120 22 : autoPan.start()
121 : }
122 :
123 310 : function updateNodePositions(nodeId: NodeId) {
124 310 : stopFrame?.()
125 310 : stopFrame = null
126 310 : if (!dragStartPos || !dragStartMouse) return
127 :
128 310 : const { rootGraphId, canvas } = canvasStore
129 310 : if (!rootGraphId || !canvas || canvasStore.isReadOnly) return
130 :
131 308 : const canvasDelta = {
132 : x: (lastPointerX - dragStartMouse.x) / canvas.ds.scale,
133 : y: (lastPointerY - dragStartMouse.y) / canvas.ds.scale
134 : }
135 :
136 308 : const updates = [
137 : {
138 : nodeId,
139 : position: {
140 : x: dragStartPos.x + canvasDelta.x,
141 : y: dragStartPos.y + canvasDelta.y
142 : }
143 : }
144 : ]
145 :
146 310 : if (
147 : otherSelectedNodesStartPositions &&
148 : otherSelectedNodesStartPositions.size > 0
149 : ) {
150 25 : for (const [otherNodeId, startPos] of otherSelectedNodesStartPositions) {
151 45 : updates.push({
152 : nodeId: otherNodeId,
153 : position: {
154 : x: startPos.x + canvasDelta.x,
155 : y: startPos.y + canvasDelta.y
156 : }
157 : })
158 : }
159 : }
160 :
161 308 : mutations.batchMoveNodes(rootGraphId, updates)
162 :
163 310 : for (const [item, start] of nonNodeStartPositions ?? []) {
164 10 : item.move(
165 : start.x + canvasDelta.x - item.pos[0],
166 : start.y + canvasDelta.y - item.pos[1],
167 : true
168 : )
169 : }
170 : }
171 :
172 300 : function handleDrag(event: PointerEvent, nodeId: NodeId) {
173 300 : if (!dragStartPos || !dragStartMouse) {
174 0 : return
175 : }
176 300 : if (canvasStore.isReadOnly) {
177 10 : autoPan?.stop()
178 10 : const canvas = canvasStore.getCanvas()
179 10 : const delta = [event.clientX - lastPointerX, event.clientY - lastPointerY]
180 :
181 10 : canvas.ds.offset[0] += delta[0] / canvas.ds.scale
182 10 : canvas.ds.offset[1] += delta[1] / canvas.ds.scale
183 10 : canvas.setDirty(true, true)
184 10 : lastPointerX = event.clientX
185 10 : lastPointerY = event.clientY
186 10 : dragStartMouse.x += delta[0]
187 10 : dragStartMouse.y += delta[1]
188 10 : return
189 : }
190 :
191 290 : lastPointerX = event.clientX
192 290 : lastPointerY = event.clientY
193 290 : startAutoPan(event, nodeId)
194 :
195 290 : const { canvas } = canvasStore
196 300 : if (stopFrame || !canvas) return
197 290 : stopFrame = onCanvasFrame(canvas, 'update', () =>
198 : updateNodePositions(nodeId)
199 : )
200 : }
201 :
202 21 : function endDrag(event: PointerEvent, nodeId: NodeId | undefined) {
203 21 : if (nodeId && stopFrame) updateNodePositions(nodeId)
204 21 : const { rootGraphId } = canvasStore
205 21 : if (shouldSnap(event) && nodeId && rootGraphId) {
206 0 : const boundsUpdates: NodeBoundsUpdate[] = []
207 :
208 0 : const currentLayout = layoutStore.getNodeLayout(rootGraphId, nodeId)
209 0 : if (currentLayout) {
210 0 : const currentPos = currentLayout.position
211 0 : const snappedPos = applySnapToPosition({ ...currentPos })
212 :
213 0 : if (snappedPos.x !== currentPos.x || snappedPos.y !== currentPos.y) {
214 0 : boundsUpdates.push({
215 : nodeId,
216 : bounds: {
217 : x: snappedPos.x,
218 : y: snappedPos.y,
219 : width: currentLayout.size.width,
220 : height: currentLayout.size.height
221 : }
222 : })
223 : }
224 : }
225 :
226 0 : if (
227 : otherSelectedNodesStartPositions &&
228 : otherSelectedNodesStartPositions.size > 0
229 : ) {
230 0 : for (const otherNodeId of otherSelectedNodesStartPositions.keys()) {
231 0 : const nodeLayout = layoutStore.getNodeLayout(rootGraphId, otherNodeId)
232 0 : if (nodeLayout) {
233 0 : const currentPos = { ...nodeLayout.position }
234 0 : const snappedPos = applySnapToPosition(currentPos)
235 :
236 0 : if (
237 : snappedPos.x !== currentPos.x ||
238 : snappedPos.y !== currentPos.y
239 : ) {
240 0 : boundsUpdates.push({
241 : nodeId: otherNodeId,
242 : bounds: {
243 : x: snappedPos.x,
244 : y: snappedPos.y,
245 : width: nodeLayout.size.width,
246 : height: nodeLayout.size.height
247 : }
248 : })
249 : }
250 : }
251 : }
252 : }
253 :
254 0 : if (boundsUpdates.length > 0) {
255 0 : layoutStore.batchUpdateNodeBounds(rootGraphId, boundsUpdates, {
256 : source: LayoutSource.Vue
257 : })
258 : }
259 : }
260 :
261 21 : resetDragState()
262 : }
263 :
264 941 : function resetDragState() {
265 941 : dragStartPos = null
266 941 : dragStartMouse = null
267 941 : otherSelectedNodesStartPositions = null
268 941 : nonNodeStartPositions = null
269 :
270 941 : autoPan?.stop()
271 941 : autoPan = null
272 :
273 941 : stopShiftSync?.()
274 941 : stopShiftSync = null
275 :
276 941 : stopFrame?.()
277 941 : stopFrame = null
278 : }
279 :
280 1710 : tryOnScopeDispose(resetDragState)
281 :
282 1710 : return {
283 : startDrag,
284 : handleDrag,
285 : endDrag,
286 : cancelDrag: resetDragState
287 : }
288 : })
|