|
| 1 | +import { onBeforeUnmount, onMounted, ref, type Ref } from 'vue' |
| 2 | + |
| 3 | +export interface TouchReorderCallbacks { |
| 4 | + onDragStart: (_id: number) => void |
| 5 | + onReorderOver: (_hoveredId: number, _clientX: number, _clientY: number) => void |
| 6 | + onDrop: () => void |
| 7 | + onCancel: () => void |
| 8 | +} |
| 9 | + |
| 10 | +const LONG_PRESS_MS = 300 |
| 11 | +const MOVE_THRESHOLD = 8 |
| 12 | +const SCROLL_EDGE = 40 |
| 13 | +const SCROLL_SPEED = 8 |
| 14 | + |
| 15 | +/** |
| 16 | + * Adds touch-based drag-and-drop reorder to a container. |
| 17 | + * |
| 18 | + * Children must have `[data-drag-id]` attributes to be considered draggable. |
| 19 | + * The composable creates a floating ghost clone during drag and calls the same |
| 20 | + * callbacks the existing HTML5 DnD system uses. |
| 21 | + */ |
| 22 | +export function useTouchReorder( |
| 23 | + containerRef: Ref<HTMLElement | null>, |
| 24 | + callbacks: TouchReorderCallbacks, |
| 25 | +) { |
| 26 | + const isTouchDragging = ref(false) |
| 27 | + |
| 28 | + let longPressTimer: ReturnType<typeof setTimeout> | null = null |
| 29 | + let scrollTimer: ReturnType<typeof setInterval> | null = null |
| 30 | + let ghost: HTMLElement | null = null |
| 31 | + let dragId: number | null = null |
| 32 | + let startX = 0 |
| 33 | + let startY = 0 |
| 34 | + |
| 35 | + function findDraggable(el: HTMLElement): HTMLElement | null { |
| 36 | + return el.closest<HTMLElement>('[data-drag-id]') |
| 37 | + } |
| 38 | + |
| 39 | + function getDragId(el: HTMLElement): number | null { |
| 40 | + const val = el.dataset.dragId |
| 41 | + return val != null ? Number(val) : null |
| 42 | + } |
| 43 | + |
| 44 | + function createGhost(source: HTMLElement, x: number, y: number) { |
| 45 | + const rect = source.getBoundingClientRect() |
| 46 | + ghost = source.cloneNode(true) as HTMLElement |
| 47 | + ghost.style.position = 'fixed' |
| 48 | + ghost.style.width = rect.width + 'px' |
| 49 | + ghost.style.height = rect.height + 'px' |
| 50 | + ghost.style.left = x - rect.width / 2 + 'px' |
| 51 | + ghost.style.top = y - rect.height / 2 + 'px' |
| 52 | + ghost.style.zIndex = '999999' |
| 53 | + ghost.style.opacity = '0.85' |
| 54 | + ghost.style.pointerEvents = 'none' |
| 55 | + ghost.style.transition = 'none' |
| 56 | + ghost.style.transform = 'scale(1.05)' |
| 57 | + ghost.style.boxShadow = '0 8px 24px rgba(0,0,0,0.3)' |
| 58 | + document.body.appendChild(ghost) |
| 59 | + } |
| 60 | + |
| 61 | + function moveGhost(x: number, y: number) { |
| 62 | + if (!ghost) return |
| 63 | + const w = ghost.offsetWidth |
| 64 | + const h = ghost.offsetHeight |
| 65 | + ghost.style.left = x - w / 2 + 'px' |
| 66 | + ghost.style.top = y - h / 2 + 'px' |
| 67 | + } |
| 68 | + |
| 69 | + function removeGhost() { |
| 70 | + if (ghost) { |
| 71 | + ghost.remove() |
| 72 | + ghost = null |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + function autoScroll(clientY: number) { |
| 77 | + if (scrollTimer) { |
| 78 | + clearInterval(scrollTimer) |
| 79 | + scrollTimer = null |
| 80 | + } |
| 81 | + |
| 82 | + const vh = window.innerHeight |
| 83 | + if (clientY < SCROLL_EDGE) { |
| 84 | + scrollTimer = setInterval(() => window.scrollBy(0, -SCROLL_SPEED), 16) |
| 85 | + } else if (clientY > vh - SCROLL_EDGE) { |
| 86 | + scrollTimer = setInterval(() => window.scrollBy(0, SCROLL_SPEED), 16) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + function stopAutoScroll() { |
| 91 | + if (scrollTimer) { |
| 92 | + clearInterval(scrollTimer) |
| 93 | + scrollTimer = null |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + function hitTest(x: number, y: number): HTMLElement | null { |
| 98 | + if (ghost) ghost.style.display = 'none' |
| 99 | + const el = document.elementFromPoint(x, y) as HTMLElement | null |
| 100 | + if (ghost) ghost.style.display = '' |
| 101 | + return el ? findDraggable(el) : null |
| 102 | + } |
| 103 | + |
| 104 | + function clearLongPress() { |
| 105 | + if (longPressTimer) { |
| 106 | + clearTimeout(longPressTimer) |
| 107 | + longPressTimer = null |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + function cancelDrag() { |
| 112 | + clearLongPress() |
| 113 | + stopAutoScroll() |
| 114 | + removeGhost() |
| 115 | + if (isTouchDragging.value) { |
| 116 | + isTouchDragging.value = false |
| 117 | + callbacks.onCancel() |
| 118 | + } |
| 119 | + dragId = null |
| 120 | + } |
| 121 | + |
| 122 | + function onTouchStart(e: TouchEvent) { |
| 123 | + const touch = e.touches[0] |
| 124 | + if (!touch) return |
| 125 | + |
| 126 | + const target = e.target as HTMLElement |
| 127 | + if (target.closest('button, a, input, textarea, select, .nc-actions')) return |
| 128 | + |
| 129 | + const draggable = findDraggable(target) |
| 130 | + if (!draggable) return |
| 131 | + |
| 132 | + const id = getDragId(draggable) |
| 133 | + if (id === null) return |
| 134 | + |
| 135 | + startX = touch.clientX |
| 136 | + startY = touch.clientY |
| 137 | + dragId = id |
| 138 | + |
| 139 | + longPressTimer = setTimeout(() => { |
| 140 | + longPressTimer = null |
| 141 | + isTouchDragging.value = true |
| 142 | + callbacks.onDragStart(id) |
| 143 | + createGhost(draggable, startX, startY) |
| 144 | + }, LONG_PRESS_MS) |
| 145 | + } |
| 146 | + |
| 147 | + function onTouchMove(e: TouchEvent) { |
| 148 | + const touch = e.touches[0] |
| 149 | + if (!touch) return |
| 150 | + |
| 151 | + const dx = touch.clientX - startX |
| 152 | + const dy = touch.clientY - startY |
| 153 | + |
| 154 | + // If moved before long press triggers, cancel — user is scrolling |
| 155 | + if (!isTouchDragging.value) { |
| 156 | + if (Math.abs(dx) > MOVE_THRESHOLD || Math.abs(dy) > MOVE_THRESHOLD) { |
| 157 | + clearLongPress() |
| 158 | + } |
| 159 | + return |
| 160 | + } |
| 161 | + |
| 162 | + // We're dragging — prevent scroll |
| 163 | + e.preventDefault() |
| 164 | + |
| 165 | + moveGhost(touch.clientX, touch.clientY) |
| 166 | + autoScroll(touch.clientY) |
| 167 | + |
| 168 | + const hovered = hitTest(touch.clientX, touch.clientY) |
| 169 | + if (hovered) { |
| 170 | + const hoveredId = getDragId(hovered) |
| 171 | + if (hoveredId !== null && hoveredId !== dragId) { |
| 172 | + callbacks.onReorderOver(hoveredId, touch.clientX, touch.clientY) |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + function onTouchEnd() { |
| 178 | + clearLongPress() |
| 179 | + stopAutoScroll() |
| 180 | + removeGhost() |
| 181 | + |
| 182 | + if (isTouchDragging.value) { |
| 183 | + isTouchDragging.value = false |
| 184 | + callbacks.onDrop() |
| 185 | + } |
| 186 | + dragId = null |
| 187 | + } |
| 188 | + |
| 189 | + onMounted(() => { |
| 190 | + const el = containerRef.value |
| 191 | + if (!el) return |
| 192 | + el.addEventListener('touchstart', onTouchStart, { passive: false }) |
| 193 | + el.addEventListener('touchmove', onTouchMove, { passive: false }) |
| 194 | + el.addEventListener('touchend', onTouchEnd) |
| 195 | + el.addEventListener('touchcancel', cancelDrag) |
| 196 | + }) |
| 197 | + |
| 198 | + onBeforeUnmount(() => { |
| 199 | + cancelDrag() |
| 200 | + const el = containerRef.value |
| 201 | + if (!el) return |
| 202 | + el.removeEventListener('touchstart', onTouchStart) |
| 203 | + el.removeEventListener('touchmove', onTouchMove) |
| 204 | + el.removeEventListener('touchend', onTouchEnd) |
| 205 | + el.removeEventListener('touchcancel', cancelDrag) |
| 206 | + }) |
| 207 | + |
| 208 | + return { isTouchDragging } |
| 209 | +} |
0 commit comments