Line data Source code
1 : import { useEventListener } from '@vueuse/core'
2 : import { ref } from 'vue'
3 :
4 : import type {
5 : CompassCorners,
6 : Point2D as Point,
7 : Size2D as Size
8 : } from '@comfyorg/shared-frontend-utils/geometry'
9 : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
10 : import { layoutStore } from '@/renderer/core/layout/store/layoutStore'
11 : import { MIN_NODE_WIDTH } from '@/renderer/core/layout/transform/graphRenderTransform'
12 : import { useNodeSnap } from '@/renderer/extensions/vueNodes/composables/useNodeSnap'
13 : import { useShiftKeySync } from '@/renderer/extensions/vueNodes/composables/useShiftKeySync'
14 : import { useTransformState } from '@/renderer/core/layout/transform/useTransformState'
15 : import {
16 : hasNorthEdge,
17 : hasWestEdge
18 : } from '@/renderer/extensions/vueNodes/interactions/resize/resizeHandleConfig'
19 : import { toNodeId } from '@comfyorg/graph-model/nodeId'
20 :
21 : export interface ResizeCallbackPayload {
22 : size: Size
23 : position?: Point
24 : }
25 :
26 : /**
27 : * Composable for node resizing functionality from any corner.
28 : *
29 : * Provides resize handle interaction that integrates with the layout system.
30 : * Handles pointer capture, coordinate calculations, size constraints,
31 : * and position adjustments for non-SE corners.
32 : */
33 8015 : export function useNodeResize(
34 : resizeCallback: (payload: ResizeCallbackPayload, element: HTMLElement) => void
35 : ) {
36 8015 : const transformState = useTransformState()
37 :
38 8015 : const isResizing = ref(false)
39 8015 : const resizeStartPointer = ref<Point | null>(null)
40 8015 : const resizeStartSize = ref<Size | null>(null)
41 8015 : const resizeStartPosition = ref<Point | null>(null)
42 8015 : const resizeCorner = ref<CompassCorners>('SE')
43 :
44 : // Snap-to-grid functionality
45 8015 : const { shouldSnap, applySnapToPosition, applySnapToSize } = useNodeSnap()
46 :
47 : // Shift key sync for LiteGraph canvas preview
48 8015 : const { trackShiftKey } = useShiftKeySync()
49 :
50 8015 : const startResize = (event: PointerEvent, corner: CompassCorners = 'SE') => {
51 25 : event.stopPropagation()
52 :
53 25 : const target = event.currentTarget
54 25 : if (!(target instanceof HTMLElement)) return
55 :
56 25 : const nodeElement = target.closest('[data-node-id]')
57 25 : if (!(nodeElement instanceof HTMLElement)) return
58 :
59 25 : const rawNodeId = nodeElement.dataset.nodeId
60 25 : if (!rawNodeId) return
61 25 : const nodeId = toNodeId(rawNodeId)
62 :
63 25 : const rect = nodeElement.getBoundingClientRect()
64 25 : const scale = transformState.camera.z
65 :
66 25 : const startSize: Size = {
67 : width: rect.width / scale,
68 : height: rect.height / scale
69 : }
70 :
71 25 : const measureMinContentHeight = (candidateWidth: number) => {
72 155 : const savedWidth = nodeElement.style.getPropertyValue('--node-width')
73 155 : const savedHeight = nodeElement.style.getPropertyValue('--node-height')
74 155 : nodeElement.style.setProperty('--node-width', `${candidateWidth}px`)
75 155 : nodeElement.style.setProperty('--node-height', '0px')
76 155 : const measured = nodeElement.getBoundingClientRect().height
77 155 : nodeElement.style.setProperty('--node-height', savedHeight || '')
78 155 : nodeElement.style.setProperty('--node-width', savedWidth || '')
79 : const currentScale = transformState.camera.z || 1
80 : return measured / currentScale
81 : }
82 :
83 25 : const { rootGraphId } = useCanvasStore()
84 25 : const nodeLayout = rootGraphId
85 : ? layoutStore.getNodeLayout(rootGraphId, nodeId)
86 : : null
87 25 : const startPosition: Point = nodeLayout
88 : ? { ...nodeLayout.position }
89 : : { x: 0, y: 0 }
90 :
91 : // Track shift key state and sync to canvas for snap preview
92 25 : const stopShiftSync = trackShiftKey(event.shiftKey)
93 :
94 : // Capture pointer to ensure we get all move/up events
95 25 : target.setPointerCapture(event.pointerId)
96 :
97 : // Mark as resizing to prevent drag from activating
98 25 : layoutStore.isResizingVueNodes.value = true
99 25 : isResizing.value = true
100 25 : resizeStartPointer.value = { x: event.clientX, y: event.clientY }
101 25 : resizeStartSize.value = startSize
102 25 : resizeStartPosition.value = startPosition
103 25 : resizeCorner.value = corner
104 :
105 25 : const handlePointerMove = (moveEvent: PointerEvent) => {
106 155 : if (
107 : !isResizing.value ||
108 : !resizeStartPointer.value ||
109 : !resizeStartSize.value ||
110 : !resizeStartPosition.value
111 : ) {
112 0 : return
113 : }
114 :
115 155 : const scale = transformState.camera.z
116 155 : const deltaX =
117 : (moveEvent.clientX - resizeStartPointer.value.x) / (scale || 1)
118 155 : const deltaY =
119 : (moveEvent.clientY - resizeStartPointer.value.y) / (scale || 1)
120 :
121 155 : const activeCorner = resizeCorner.value
122 155 : let newWidth: number
123 155 : let newHeight: number
124 155 : let newX = resizeStartPosition.value.x
125 155 : let newY = resizeStartPosition.value.y
126 :
127 155 : switch (activeCorner) {
128 : case 'NE':
129 15 : newY = resizeStartPosition.value.y + deltaY
130 15 : newWidth = resizeStartSize.value.width + deltaX
131 15 : newHeight = resizeStartSize.value.height - deltaY
132 15 : break
133 : case 'SW':
134 10 : newX = resizeStartPosition.value.x + deltaX
135 10 : newWidth = resizeStartSize.value.width - deltaX
136 10 : newHeight = resizeStartSize.value.height + deltaY
137 10 : break
138 : case 'NW':
139 5 : newX = resizeStartPosition.value.x + deltaX
140 5 : newY = resizeStartPosition.value.y + deltaY
141 5 : newWidth = resizeStartSize.value.width - deltaX
142 5 : newHeight = resizeStartSize.value.height - deltaY
143 5 : break
144 : default: // SE
145 125 : newWidth = resizeStartSize.value.width + deltaX
146 125 : newHeight = resizeStartSize.value.height + deltaY
147 : break
148 : }
149 :
150 155 : const isWestCorner = hasWestEdge(activeCorner)
151 155 : const isNorthCorner = hasNorthEdge(activeCorner)
152 :
153 : // Apply snap-to-grid
154 155 : if (shouldSnap(moveEvent)) {
155 : // Snap position first for N/W corners, then compensate size
156 0 : if (isNorthCorner || isWestCorner) {
157 0 : const originalX = newX
158 0 : const originalY = newY
159 0 : const snapped = applySnapToPosition({ x: newX, y: newY })
160 0 : newX = snapped.x
161 0 : newY = snapped.y
162 :
163 0 : if (isNorthCorner) {
164 0 : newHeight += originalY - newY
165 : }
166 0 : if (isWestCorner) {
167 0 : newWidth += originalX - newX
168 : }
169 : }
170 :
171 0 : const snappedSize = applySnapToSize({
172 : width: newWidth,
173 : height: newHeight
174 : })
175 0 : newWidth = snappedSize.width
176 0 : newHeight = snappedSize.height
177 : }
178 :
179 : // Enforce minimum size with position compensation (matching litegraph)
180 155 : const minWidth =
181 : parseFloat(nodeElement.style.getPropertyValue('min-width') || '0') ||
182 : MIN_NODE_WIDTH
183 155 : if (newWidth < minWidth) {
184 4 : if (isWestCorner) {
185 4 : newX =
186 : resizeStartPosition.value.x + resizeStartSize.value.width - minWidth
187 : }
188 4 : newWidth = minWidth
189 : }
190 : // Re-measure on each move with the candidate width applied: widget
191 : // content (e.g. painter controls) can re-flow taller as width shrinks,
192 : // raising the true minimum. Probing with newWidth — not the DOM's
193 : // current width — keeps the clamp accurate on the frame that crosses
194 : // a responsive breakpoint.
195 155 : const minContentHeight = measureMinContentHeight(newWidth)
196 155 : if (newHeight < minContentHeight) {
197 19 : if (isNorthCorner) {
198 9 : newY =
199 : resizeStartPosition.value.y +
200 : resizeStartSize.value.height -
201 : minContentHeight
202 : }
203 19 : newHeight = minContentHeight
204 : }
205 :
206 155 : const payload: ResizeCallbackPayload = {
207 : size: { width: newWidth, height: newHeight }
208 : }
209 :
210 : // Only include position for non-SE corners
211 155 : if (activeCorner !== 'SE') {
212 30 : payload.position = { x: newX, y: newY }
213 : }
214 :
215 155 : const targetNodeElement = target.closest('[data-node-id]')
216 155 : if (targetNodeElement instanceof HTMLElement) {
217 155 : resizeCallback(payload, targetNodeElement)
218 : }
219 : }
220 :
221 25 : const cleanup = () => {
222 25 : if (!isResizing.value) return
223 25 : isResizing.value = false
224 25 : layoutStore.isResizingVueNodes.value = false
225 25 : resizeStartPointer.value = null
226 25 : resizeStartSize.value = null
227 25 : resizeStartPosition.value = null
228 :
229 : // Stop tracking shift key state
230 25 : stopShiftSync()
231 :
232 25 : stopMoveListen()
233 25 : stopUpListen()
234 25 : stopCancelListen()
235 : }
236 :
237 25 : const handlePointerUp = (upEvent: PointerEvent) => {
238 25 : if (isResizing.value) {
239 25 : try {
240 25 : target.releasePointerCapture(upEvent.pointerId)
241 : } catch {
242 : // Pointer capture may already be released
243 : }
244 25 : cleanup()
245 : }
246 : }
247 :
248 25 : const stopMoveListen = useEventListener('pointermove', handlePointerMove)
249 25 : const stopUpListen = useEventListener('pointerup', handlePointerUp)
250 25 : const stopCancelListen = useEventListener('pointercancel', cleanup)
251 : }
252 :
253 8015 : return {
254 : startResize,
255 : isResizing
256 : }
257 : }
|