Line data Source code
1 : import { useElementHover, useEventListener } from '@vueuse/core'
2 : import type { Ref } from 'vue'
3 : import { computed, onScopeDispose, ref } from 'vue'
4 :
5 : import type { AssetItem } from '@/platform/assets/schemas/assetSchema'
6 : import {
7 : normalizeMarqueeRect,
8 : selectMarqueeIds
9 : } from '@/platform/assets/utils/marqueeSelectionUtil'
10 : import { clipRectToBounds } from '@/utils/mathUtil'
11 : import type { ReadOnlyRectEdges as RectEdges } from '@comfyorg/shared-frontend-utils/geometry'
12 :
13 2355 : const DRAG_THRESHOLD_PX = 4
14 2355 : const CARD_SELECTOR = '[data-asset-id]'
15 2355 : const INTERACTIVE_SELECTOR =
16 : 'button, input, textarea, select, a[href], [role="slider"], [role="tab"], [contenteditable]'
17 :
18 : interface AssetGridSelectionOptions {
19 : marqueeContainerRef: Ref<HTMLElement | undefined>
20 : hoverTargetRef: Ref<HTMLElement | undefined>
21 : getAssets: () => AssetItem[]
22 : getSelectedIds: () => string[]
23 : setSelectedIds: (ids: string[], allAssets: AssetItem[]) => void
24 : selectAll: (assets: AssetItem[]) => void
25 : isEnabled?: () => boolean
26 : }
27 :
28 3 : function isTextEntryTarget(element: Element | null): boolean {
29 3 : return (
30 : element instanceof HTMLInputElement ||
31 : element instanceof HTMLTextAreaElement ||
32 : (element instanceof HTMLElement && element.isContentEditable)
33 : )
34 : }
35 :
36 84 : export function useAssetGridSelection(options: AssetGridSelectionOptions) {
37 84 : const {
38 : marqueeContainerRef,
39 : hoverTargetRef,
40 : getAssets,
41 : getSelectedIds,
42 : setSelectedIds,
43 : selectAll,
44 : isEnabled = () => true
45 : } = options
46 :
47 84 : const marqueeRect = ref<RectEdges | null>(null)
48 84 : const isHoveringPanel = useElementHover(hoverTargetRef)
49 :
50 84 : let startX = 0
51 84 : let startY = 0
52 84 : let pointerId = 0
53 84 : let baseIds: string[] = []
54 84 : let isSubtractive = false
55 84 : let isTracking = false
56 84 : let isDragging = false
57 84 : let suppressNextClick = false
58 84 : let suppressClickResetTimeout: ReturnType<typeof setTimeout> | null = null
59 84 : let dragCards: { id: string; rect: DOMRect }[] = []
60 84 : let dragBounds: DOMRect | null = null
61 84 : let pointerClientX = 0
62 84 : let pointerClientY = 0
63 84 : let hasPointerSample = false
64 :
65 5 : function collectCards(container: HTMLElement) {
66 5 : return [...container.querySelectorAll<HTMLElement>(CARD_SELECTOR)].flatMap(
67 : (el) => {
68 10 : const id = el.dataset.assetId
69 10 : return id ? [{ id, rect: el.getBoundingClientRect() }] : []
70 : }
71 : )
72 : }
73 :
74 5 : function snapshotDrag() {
75 5 : const container = marqueeContainerRef.value
76 5 : if (!container) return
77 5 : dragCards = collectCards(container)
78 5 : dragBounds = container.getBoundingClientRect()
79 : }
80 :
81 54 : function applyMarquee(clientX: number, clientY: number) {
82 54 : if (!dragBounds) return
83 54 : const rect = clipRectToBounds(
84 : normalizeMarqueeRect(
85 : { x: startX, y: startY },
86 : { x: clientX, y: clientY }
87 : ),
88 : dragBounds
89 : )
90 54 : marqueeRect.value = rect
91 54 : setSelectedIds(
92 : [...selectMarqueeIds(dragCards, rect, baseIds, isSubtractive)],
93 : getAssets()
94 : )
95 : }
96 :
97 200 : function onPointerMove(e: PointerEvent) {
98 200 : pointerClientX = e.clientX
99 200 : pointerClientY = e.clientY
100 200 : hasPointerSample = true
101 200 : if (!isTracking) return
102 200 : if (
103 : !isDragging &&
104 : Math.hypot(e.clientX - startX, e.clientY - startY) < DRAG_THRESHOLD_PX
105 : ) {
106 0 : return
107 : }
108 61 : if (!isDragging) {
109 5 : isDragging = true
110 5 : snapshotDrag()
111 5 : capturePointer()
112 : }
113 54 : applyMarquee(e.clientX, e.clientY)
114 : }
115 :
116 5 : function capturePointer() {
117 5 : try {
118 5 : marqueeContainerRef.value?.setPointerCapture(pointerId)
119 : } catch {
120 : // Stale/invalid pointerId: window listeners still end the drag.
121 : }
122 : }
123 :
124 109 : function endDrag() {
125 109 : if (!isTracking) return
126 10 : isTracking = false
127 10 : marqueeRect.value = null
128 10 : dragCards = []
129 10 : dragBounds = null
130 24 : if (isDragging) scheduleSuppressNextClick()
131 10 : isDragging = false
132 : }
133 :
134 5 : function scheduleSuppressNextClick() {
135 5 : suppressNextClick = true
136 5 : clearSuppressTimer()
137 5 : suppressClickResetTimeout = setTimeout(() => {
138 0 : suppressNextClick = false
139 0 : suppressClickResetTimeout = null
140 : }, 0)
141 : }
142 :
143 69 : function clearSuppressTimer() {
144 69 : if (suppressClickResetTimeout !== null) {
145 5 : clearTimeout(suppressClickResetTimeout)
146 5 : suppressClickResetTimeout = null
147 : }
148 : }
149 :
150 6 : function preventDragStart(e: Event) {
151 6 : if (isTracking) e.preventDefault()
152 : }
153 :
154 18 : function preventTextSelection(e: Event) {
155 18 : if (!isTracking) return
156 1 : const container = marqueeContainerRef.value
157 18 : if (container && e.target instanceof Node && container.contains(e.target)) {
158 1 : e.preventDefault()
159 : }
160 : }
161 :
162 75 : function onPointerDown(e: PointerEvent) {
163 75 : if (e.button !== 0) return
164 55 : if (isTracking) return
165 55 : if (!isEnabled()) return
166 55 : suppressNextClick = false
167 55 : clearSuppressTimer()
168 55 : const container = marqueeContainerRef.value
169 55 : if (!container) return
170 55 : if (!container.querySelector(CARD_SELECTOR)) return
171 53 : const target = e.target
172 54 : if (!(target instanceof HTMLElement)) return
173 54 : if (target.closest(INTERACTIVE_SELECTOR)) return
174 :
175 : const onCard = target.closest(CARD_SELECTOR)
176 75 : if (onCard && !e.ctrlKey && !e.metaKey) return
177 :
178 10 : startX = e.clientX
179 10 : startY = e.clientY
180 10 : pointerId = e.pointerId
181 75 : baseIds = e.shiftKey || e.ctrlKey || e.metaKey ? getSelectedIds() : []
182 75 : isSubtractive = (e.ctrlKey || e.metaKey) && e.shiftKey
183 75 : isDragging = false
184 75 : isTracking = true
185 : }
186 :
187 86 : function onClickCapture(e: MouseEvent) {
188 86 : if (!suppressNextClick) return
189 5 : suppressNextClick = false
190 5 : clearSuppressTimer()
191 5 : e.stopImmediatePropagation()
192 5 : e.preventDefault()
193 : }
194 :
195 : // useElementHover latches stale-false when an overlay under the cursor (the
196 : // selection bar) unmounts on "deselect all"; recheck the live pointer against
197 : // the panel rect so Ctrl/Cmd+A still resolves against the panel it is over.
198 2 : function isPointerInsidePanel(): boolean {
199 2 : const el = hoverTargetRef.value
200 2 : if (!el || !hasPointerSample) return false
201 1 : const { left, top, right, bottom } = el.getBoundingClientRect()
202 2 : return (
203 : pointerClientX >= left &&
204 : pointerClientX <= right &&
205 : pointerClientY >= top &&
206 : pointerClientY <= bottom
207 : )
208 : }
209 :
210 24 : function onKeydown(e: KeyboardEvent) {
211 24 : if (!(e.ctrlKey || e.metaKey) || (e.key !== 'a' && e.key !== 'A')) return
212 24 : if (
213 : !(isHoveringPanel.value || isPointerInsidePanel()) ||
214 : isTextEntryTarget(document.activeElement) ||
215 : document.querySelector('[role="dialog"][aria-modal="true"]')
216 : ) {
217 3 : return
218 : }
219 2 : e.preventDefault()
220 2 : e.stopImmediatePropagation()
221 2 : selectAll(getAssets())
222 : }
223 :
224 84 : useEventListener(marqueeContainerRef, 'pointerdown', onPointerDown)
225 84 : useEventListener(marqueeContainerRef, 'dragstart', preventDragStart, {
226 : capture: true
227 : })
228 84 : useEventListener(window, 'pointermove', onPointerMove)
229 84 : useEventListener(window, ['pointerup', 'pointercancel', 'dragend'], endDrag)
230 84 : useEventListener(window, 'click', onClickCapture, { capture: true })
231 84 : useEventListener(window, 'keydown', onKeydown, { capture: true })
232 84 : useEventListener(window, 'selectstart', preventTextSelection, {
233 : capture: true
234 : })
235 :
236 84 : onScopeDispose(clearSuppressTimer)
237 :
238 : const marqueeStyle = computed(() => {
239 143 : const rect = marqueeRect.value
240 143 : if (!rect) return null
241 54 : return {
242 : left: `${rect.left}px`,
243 : top: `${rect.top}px`,
244 : width: `${rect.right - rect.left}px`,
245 : height: `${rect.bottom - rect.top}px`
246 : }
247 : })
248 :
249 84 : return { marqueeStyle }
250 : }
|