Line data Source code
1 : import type { Point2D as Position } from '@comfyorg/shared-frontend-utils/geometry'
2 : import { ref, shallowRef } from 'vue'
3 :
4 : import { t } from '@/i18n'
5 : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
6 : import { withNodeAddSource } from '@/platform/telemetry/nodeAdded/nodeAddSource'
7 : import type { NodeAddSource } from '@comfyorg/telemetry/types'
8 : import { useToast } from '@/components/ui/toast/toastStore'
9 : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
10 : import { useLitegraphService } from '@/services/litegraphService'
11 : import type { ComfyNodeDefImpl } from '@/core/graph/nodeDef/ComfyNodeDefImpl'
12 : import { matchesWidgetName } from '@/utils/widgetBinding'
13 : import type { WidgetValueBinding } from '@/utils/widgetBinding'
14 :
15 : type DragMode = 'click' | 'native'
16 :
17 : interface StartDragOptions {
18 : mode?: DragMode
19 : widgetValues?: readonly WidgetValueBinding[]
20 : source?: NodeAddSource
21 : }
22 :
23 2355 : const isDragging = ref(false)
24 2355 : const draggedNode = shallowRef<ComfyNodeDefImpl | null>(null)
25 2355 : const dragMode = ref<DragMode>('click')
26 2355 : const lastNativeDragPosition = shallowRef<Position>()
27 2355 : const pendingWidgetValues = shallowRef<readonly WidgetValueBinding[]>()
28 2355 : const pendingSource = ref<NodeAddSource>('sidebar_drag')
29 2355 : let listenersSetup = false
30 :
31 : // Firefox dragend can report stale clientX/Y and `drag` can fire with
32 : // (0, 0). dragover on the target reliably reports real client coords.
33 : // https://bugzilla.mozilla.org/show_bug.cgi?id=1773886
34 1 : function trackNativeDragPosition(e: DragEvent) {
35 1 : if (dragMode.value !== 'native') return
36 1 : if (e.clientX === 0 && e.clientY === 0) return
37 1 : lastNativeDragPosition.value = { x: e.clientX, y: e.clientY }
38 : }
39 :
40 2 : function applyWidgetValues(
41 : node: LGraphNode,
42 : values: readonly WidgetValueBinding[]
43 : ) {
44 2 : for (const { selector, value } of values) {
45 2 : const widget = node.widgets?.find((w) =>
46 : matchesWidgetName(w.name, selector)
47 : )
48 2 : if (!widget) {
49 0 : console.error(`Widget ${selector} not found on node ${node.type}`)
50 0 : useToast().warning(t('g.warning'), {
51 : description: t('assetBrowser.failedToSetModelValue')
52 : })
53 0 : continue
54 : }
55 2 : widget.value = value
56 : }
57 : }
58 :
59 7 : function isOverCanvas(clientX: number, clientY: number): boolean {
60 7 : const canvasElement = useCanvasStore().canvas?.canvas
61 7 : if (!canvasElement) return false
62 7 : const rect = canvasElement.getBoundingClientRect()
63 7 : return (
64 : clientX >= rect.left &&
65 : clientX <= rect.right &&
66 : clientY >= rect.top &&
67 : clientY <= rect.bottom
68 : )
69 : }
70 :
71 4 : function addNodeAtPosition(clientX: number, clientY: number): boolean {
72 4 : const nodeDef = draggedNode.value
73 4 : if (!nodeDef) return false
74 4 : const canvas = useCanvasStore().canvas
75 4 : if (!canvas) return false
76 4 : if (!isOverCanvas(clientX, clientY)) return false
77 :
78 4 : const pos = canvas.convertEventToCanvasOffset({
79 : clientX,
80 : clientY
81 : } as PointerEvent)
82 4 : const node = withNodeAddSource(pendingSource.value, () =>
83 : useLitegraphService().addNodeOnGraph(nodeDef, { pos })
84 : )
85 4 : if (!node) {
86 0 : console.error(`Failed to add node to graph: ${nodeDef.name}`)
87 0 : useToast().error(t('g.error'), {
88 : description: t('assetBrowser.failedToCreateNode')
89 : })
90 0 : return true
91 : }
92 :
93 4 : if (pendingWidgetValues.value)
94 2 : applyWidgetValues(node, pendingWidgetValues.value)
95 4 : canvas.selectItems([node])
96 4 : return true
97 : }
98 :
99 3 : function endDrag(e: PointerEvent) {
100 3 : if (!isDragging.value || !draggedNode.value) return
101 3 : if (dragMode.value !== 'click') return
102 :
103 3 : try {
104 3 : addNodeAtPosition(e.clientX, e.clientY)
105 : } finally {
106 3 : cancelDrag()
107 : }
108 : }
109 :
110 0 : function handleKeydown(e: KeyboardEvent) {
111 0 : if (e.key === 'Escape') cancelDrag()
112 : }
113 :
114 : // Prevent LiteGraph's empty-canvas hit-test from deselecting the placed node on pointerup.
115 3 : function blockCommitPointerDown(e: PointerEvent) {
116 3 : if (!isDragging.value || dragMode.value !== 'click') return
117 3 : if (!isOverCanvas(e.clientX, e.clientY)) return
118 3 : e.stopImmediatePropagation()
119 : }
120 :
121 5 : function setupGlobalListeners() {
122 5 : if (listenersSetup) return
123 5 : listenersSetup = true
124 :
125 5 : document.addEventListener('pointerdown', blockCommitPointerDown, true)
126 5 : document.addEventListener('pointerup', endDrag, true)
127 5 : document.addEventListener('keydown', handleKeydown)
128 5 : document.addEventListener('dragover', trackNativeDragPosition)
129 : }
130 :
131 4 : function cleanupGlobalListeners() {
132 4 : if (!listenersSetup) return
133 4 : listenersSetup = false
134 :
135 4 : document.removeEventListener('pointerdown', blockCommitPointerDown, true)
136 4 : document.removeEventListener('pointerup', endDrag, true)
137 4 : document.removeEventListener('keydown', handleKeydown)
138 4 : document.removeEventListener('dragover', trackNativeDragPosition)
139 : }
140 :
141 4 : function cancelDrag() {
142 4 : isDragging.value = false
143 4 : draggedNode.value = null
144 4 : dragMode.value = 'click'
145 4 : lastNativeDragPosition.value = undefined
146 4 : pendingWidgetValues.value = undefined
147 4 : pendingSource.value = 'sidebar_drag'
148 4 : cleanupGlobalListeners()
149 : }
150 :
151 3290 : export function useNodeDragToCanvas() {
152 5 : function startDrag(
153 : nodeDef: ComfyNodeDefImpl,
154 : {
155 : mode = 'click',
156 : widgetValues,
157 : source = 'sidebar_drag'
158 : }: StartDragOptions = {}
159 : ) {
160 5 : isDragging.value = true
161 5 : draggedNode.value = nodeDef
162 5 : dragMode.value = mode
163 5 : pendingWidgetValues.value = widgetValues
164 5 : pendingSource.value = source
165 5 : setupGlobalListeners()
166 : }
167 :
168 1 : function handleNativeDrop(clientX: number, clientY: number) {
169 1 : if (dragMode.value !== 'native') return
170 1 : const tracked = lastNativeDragPosition.value
171 1 : try {
172 1 : addNodeAtPosition(tracked?.x ?? clientX, tracked?.y ?? clientY)
173 : } finally {
174 1 : cancelDrag()
175 : }
176 : }
177 :
178 3290 : return {
179 : isDragging,
180 : draggedNode,
181 : pendingWidgetValues,
182 : startDrag,
183 : cancelDrag,
184 : handleNativeDrop
185 : }
186 : }
|