Commit 7f3cf96c85
Unsigned
Layout: unified · split
docs/.vitepress/theme/components/ScrapeTable.vue +38 −10
| @@ -85,11 +85,14 @@ const headerHeight = computed(() => { | ||
| 85 | 85 | // Drag to scroll + scroll indicator |
| 86 | 86 | const wrapRef = ref<HTMLElement | null>(null) |
| 87 | 87 | const canScrollRight = ref(false) |
| 88 | let isDragging = false | |
| 88 | const isDragging = ref(false) | |
| 89 | let isPending = false | |
| 89 | 90 | let startX = 0 |
| 90 | 91 | let scrollStart = 0 |
| 92 | let pendingPointerId = -1 | |
| 91 | 93 | |
| 92 | 94 | const SCROLL_END_PX = 2 |
| 95 | const DRAG_THRESHOLD_PX = 5 | |
| 93 | 96 | |
| 94 | 97 | function checkScroll() { |
| 95 | 98 | const el = wrapRef.value |
| @@ -97,26 +100,52 @@ function checkScroll() { | ||
| 97 | 100 | canScrollRight.value = el.scrollLeft + el.clientWidth < el.scrollWidth - SCROLL_END_PX |
| 98 | 101 | } |
| 99 | 102 | |
| 103 | function isTextTarget(e: PointerEvent) { | |
| 104 | const target = e.target as Node | null | |
| 105 | if (!target) return false | |
| 106 | const node = target.nodeType === Node.TEXT_NODE ? target.parentElement : target as Element | |
| 107 | if (!node) return false | |
| 108 | const style = window.getComputedStyle(node as Element) | |
| 109 | return style.userSelect !== 'none' && style.cursor !== 'default' || (node as Element).closest('td.site-name, th') !== null | |
| 110 | } | |
| 111 | ||
| 100 | 112 | function onPointerDown(e: PointerEvent) { |
| 113 | if (e.button !== 0) return | |
| 101 | 114 | const el = wrapRef.value |
| 102 | 115 | if (!el) return |
| 103 | isDragging = true | |
| 116 | // Don't initiate drag when clicking on selectable text elements | |
| 117 | if (isTextTarget(e)) return | |
| 118 | isPending = true | |
| 104 | 119 | startX = e.clientX |
| 105 | 120 | scrollStart = el.scrollLeft |
| 106 | el.setPointerCapture(e.pointerId) | |
| 107 | el.style.cursor = 'grabbing' | |
| 121 | pendingPointerId = e.pointerId | |
| 108 | 122 | } |
| 109 | 123 | |
| 110 | 124 | function onPointerMove(e: PointerEvent) { |
| 111 | if (!isDragging || !wrapRef.value) return | |
| 112 | wrapRef.value.scrollLeft = scrollStart - (e.clientX - startX) | |
| 125 | const el = wrapRef.value | |
| 126 | if (!el) return | |
| 127 | ||
| 128 | // If user is actively selecting text, abort drag | |
| 129 | if (isPending && window.getSelection()?.type === 'Range') { | |
| 130 | isPending = false | |
| 131 | return | |
| 132 | } | |
| 133 | ||
| 134 | if (isPending && Math.abs(e.clientX - startX) > DRAG_THRESHOLD_PX) { | |
| 135 | isPending = false | |
| 136 | isDragging.value = true | |
| 137 | el.setPointerCapture(pendingPointerId) | |
| 138 | } | |
| 139 | ||
| 140 | if (!isDragging.value) return | |
| 141 | el.scrollLeft = scrollStart - (e.clientX - startX) | |
| 113 | 142 | } |
| 114 | 143 | |
| 115 | 144 | function onPointerUp(e: PointerEvent) { |
| 116 | isDragging = false | |
| 145 | isPending = false | |
| 146 | isDragging.value = false | |
| 117 | 147 | if (wrapRef.value) { |
| 118 | 148 | wrapRef.value.releasePointerCapture(e.pointerId) |
| 119 | wrapRef.value.style.cursor = '' | |
| 120 | 149 | } |
| 121 | 150 | } |
| 122 | 151 | |
| @@ -132,6 +161,7 @@ onMounted(() => { | ||
| 132 | 161 | <div |
| 133 | 162 | ref="wrapRef" |
| 134 | 163 | class="scrape-table-wrap" |
| 164 | :style="{ cursor: isDragging ? 'grabbing' : 'auto', userSelect: isDragging ? 'none' : 'auto' }" | |
| 135 | 165 | @pointerdown="onPointerDown" |
| 136 | 166 | @pointermove="onPointerMove" |
| 137 | 167 | @pointerup="onPointerUp" |
| @@ -182,8 +212,6 @@ onMounted(() => { | ||
| 182 | 212 | .scrape-table-wrap { |
| 183 | 213 | overflow-x: auto; |
| 184 | 214 | max-width: 100%; |
| 185 | cursor: grab; | |
| 186 | user-select: none; | |
| 187 | 215 | } |
| 188 | 216 | |
| 189 | 217 | .scroll-indicator { |