Line data Source code
1 : import type { ComputedRef, CSSProperties, MaybeRefOrGetter, Ref } from 'vue'
2 : import { computed, ref, toValue } from 'vue'
3 :
4 : import { useNodeDragToCanvas } from '@/composables/node/useNodeDragToCanvas'
5 : import { useSettingStore } from '@/platform/settings/settingStore'
6 : import type { ComfyNodeDefImpl } from '@/core/graph/nodeDef/ComfyNodeDefImpl'
7 :
8 2355 : const PREVIEW_WIDTH = 200
9 2355 : const PREVIEW_MARGIN = 16
10 :
11 : interface UseNodePreviewAndDragReturn {
12 : isHovered: Ref<boolean>
13 : isDragging: Ref<boolean>
14 : showPreview: ComputedRef<boolean>
15 : nodePreviewStyle: Ref<CSSProperties>
16 : sidebarLocation: ComputedRef<'left' | 'right'>
17 : handleMouseEnter: (e: MouseEvent) => void
18 : handleMouseLeave: () => void
19 : handleDragStart: (e: DragEvent) => void
20 : handleDragEnd: (e: DragEvent) => void
21 : }
22 :
23 910 : export function useNodePreviewAndDrag(
24 : nodeDef: Ref<ComfyNodeDefImpl | undefined>,
25 : previewRef: Ref<HTMLElement | null>,
26 : panelRef?: MaybeRefOrGetter<HTMLElement | null>
27 : ): UseNodePreviewAndDragReturn {
28 910 : const { startDrag, handleNativeDrop } = useNodeDragToCanvas()
29 910 : const settingStore = useSettingStore()
30 910 : const sidebarLocation = computed<'left' | 'right'>(() =>
31 : settingStore.get('Comfy.Sidebar.Location')
32 : )
33 :
34 910 : const isHovered = ref(false)
35 910 : const isDragging = ref(false)
36 910 : const showPreview = computed(() => isHovered.value && !isDragging.value)
37 :
38 910 : const nodePreviewStyle = ref<CSSProperties>({
39 : position: 'fixed',
40 : top: '0px',
41 : left: '0px',
42 : pointerEvents: 'none',
43 : zIndex: 'var(--z-index-panel)'
44 : })
45 :
46 9 : function calculatePreviewPosition(rect: DOMRect) {
47 9 : const viewportHeight = window.innerHeight
48 9 : const viewportWidth = window.innerWidth
49 :
50 9 : let left: number
51 9 : if (sidebarLocation.value === 'left') {
52 9 : left = rect.right + PREVIEW_MARGIN
53 9 : if (left + PREVIEW_WIDTH > viewportWidth) {
54 0 : left = rect.left - PREVIEW_MARGIN - PREVIEW_WIDTH
55 : }
56 : } else {
57 0 : left = rect.left - PREVIEW_MARGIN - PREVIEW_WIDTH
58 0 : if (left < 0) {
59 0 : left = rect.right + PREVIEW_MARGIN
60 : }
61 : }
62 :
63 9 : return { left, viewportHeight }
64 : }
65 :
66 9 : function handleMouseEnter(e: MouseEvent) {
67 9 : if (!nodeDef.value) return
68 :
69 : const target = e.currentTarget as HTMLElement
70 9 : const rect = target.getBoundingClientRect()
71 : const horizontalRect = toValue(panelRef)?.getBoundingClientRect() ?? rect
72 9 : const { left, viewportHeight } = calculatePreviewPosition(horizontalRect)
73 :
74 9 : let top = rect.top
75 :
76 9 : nodePreviewStyle.value = {
77 : position: 'fixed',
78 : top: `${top}px`,
79 : left: `${left}px`,
80 : pointerEvents: 'none',
81 : zIndex: 'var(--z-index-panel)',
82 : opacity: 0
83 : }
84 9 : isHovered.value = true
85 :
86 9 : requestAnimationFrame(() => {
87 9 : if (previewRef.value) {
88 : const previewRect = previewRef.value.getBoundingClientRect()
89 9 : const previewHeight = previewRect.height
90 :
91 : const mouseY = rect.top + rect.height / 2
92 9 : top = mouseY - previewHeight * 0.3
93 :
94 9 : const minTop = PREVIEW_MARGIN
95 9 : const maxTop = viewportHeight - previewHeight - PREVIEW_MARGIN
96 9 : top = Math.max(minTop, Math.min(top, maxTop))
97 :
98 9 : nodePreviewStyle.value = {
99 : ...nodePreviewStyle.value,
100 : top: `${top}px`,
101 : opacity: 1
102 : }
103 : }
104 : })
105 : }
106 :
107 6 : function handleMouseLeave() {
108 6 : isHovered.value = false
109 : }
110 :
111 1 : function createEmptyDragImage(): HTMLElement {
112 1 : const el = document.createElement('div')
113 1 : el.style.position = 'absolute'
114 1 : el.style.left = '-9999px'
115 1 : el.style.top = '-9999px'
116 1 : el.style.width = '1px'
117 1 : el.style.height = '1px'
118 1 : return el
119 : }
120 :
121 1 : function handleDragStart(e: DragEvent) {
122 1 : if (!nodeDef.value) return
123 :
124 1 : isDragging.value = true
125 1 : isHovered.value = false
126 :
127 1 : startDrag(nodeDef.value, { mode: 'native' })
128 :
129 1 : if (e.dataTransfer) {
130 1 : e.dataTransfer.effectAllowed = 'copy'
131 1 : e.dataTransfer.setData('application/x-comfy-node', nodeDef.value.name)
132 :
133 1 : const dragImage = createEmptyDragImage()
134 1 : document.body.appendChild(dragImage)
135 1 : e.dataTransfer.setDragImage(dragImage, 0, 0)
136 :
137 1 : requestAnimationFrame(() => {
138 1 : document.body.removeChild(dragImage)
139 : })
140 : }
141 : }
142 :
143 1 : function handleDragEnd(e: DragEvent) {
144 1 : isDragging.value = false
145 1 : handleNativeDrop(e.clientX, e.clientY)
146 : }
147 :
148 910 : return {
149 : isHovered,
150 : isDragging,
151 : showPreview,
152 : nodePreviewStyle,
153 : sidebarLocation,
154 : handleMouseEnter,
155 : handleMouseLeave,
156 : handleDragStart,
157 : handleDragEnd
158 : }
159 : }
|