Commit 7f3cf96c85
Unsigned
Layout: unified · split
docs/.vitepress/theme/components/ScrapeTable.vue +38 −10
| @@ -85,11 +85,14 @@ const headerHeight = computed(() => { | |||
| 85 | // Drag to scroll + scroll indicator | 85 | // Drag to scroll + scroll indicator |
| 86 | const wrapRef = ref<HTMLElement | null>(null) | 86 | const wrapRef = ref<HTMLElement | null>(null) |
| 87 | const canScrollRight = ref(false) | 87 | const canScrollRight = ref(false) |
| 88 | let isDragging = false | 88 | const isDragging = ref(false) |
| 89 | let isPending = false | ||
| 89 | let startX = 0 | 90 | let startX = 0 |
| 90 | let scrollStart = 0 | 91 | let scrollStart = 0 |
| 92 | let pendingPointerId = -1 | ||
| 91 | 93 | ||
| 92 | const SCROLL_END_PX = 2 | 94 | const SCROLL_END_PX = 2 |
| 95 | const DRAG_THRESHOLD_PX = 5 | ||
| 93 | 96 | ||
| 94 | function checkScroll() { | 97 | function checkScroll() { |
| 95 | const el = wrapRef.value | 98 | const el = wrapRef.value |
| @@ -97,26 +100,52 @@ function checkScroll() { | |||
| 97 | canScrollRight.value = el.scrollLeft + el.clientWidth < el.scrollWidth - SCROLL_END_PX | 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 | function onPointerDown(e: PointerEvent) { | 112 | function onPointerDown(e: PointerEvent) { |
| 113 | if (e.button !== 0) return | ||
| 101 | const el = wrapRef.value | 114 | const el = wrapRef.value |
| 102 | if (!el) return | 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 | startX = e.clientX | 119 | startX = e.clientX |
| 105 | scrollStart = el.scrollLeft | 120 | scrollStart = el.scrollLeft |
| 106 | el.setPointerCapture(e.pointerId) | 121 | pendingPointerId = e.pointerId |
| 107 | el.style.cursor = 'grabbing' | ||
| 108 | } | 122 | } |
| 109 | 123 | ||
| 110 | function onPointerMove(e: PointerEvent) { | 124 | function onPointerMove(e: PointerEvent) { |
| 111 | if (!isDragging || !wrapRef.value) return | 125 | const el = wrapRef.value |
| 112 | wrapRef.value.scrollLeft = scrollStart - (e.clientX - startX) | 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 | function onPointerUp(e: PointerEvent) { | 144 | function onPointerUp(e: PointerEvent) { |
| 116 | isDragging = false | 145 | isPending = false |
| 146 | isDragging.value = false | ||
| 117 | if (wrapRef.value) { | 147 | if (wrapRef.value) { |
| 118 | wrapRef.value.releasePointerCapture(e.pointerId) | 148 | wrapRef.value.releasePointerCapture(e.pointerId) |
| 119 | wrapRef.value.style.cursor = '' | ||
| 120 | } | 149 | } |
| 121 | } | 150 | } |
| 122 | 151 | ||
| @@ -132,6 +161,7 @@ onMounted(() => { | |||
| 132 | <div | 161 | <div |
| 133 | ref="wrapRef" | 162 | ref="wrapRef" |
| 134 | class="scrape-table-wrap" | 163 | class="scrape-table-wrap" |
| 164 | :style="{ cursor: isDragging ? 'grabbing' : 'auto', userSelect: isDragging ? 'none' : 'auto' }" | ||
| 135 | @pointerdown="onPointerDown" | 165 | @pointerdown="onPointerDown" |
| 136 | @pointermove="onPointerMove" | 166 | @pointermove="onPointerMove" |
| 137 | @pointerup="onPointerUp" | 167 | @pointerup="onPointerUp" |
| @@ -182,8 +212,6 @@ onMounted(() => { | |||
| 182 | .scrape-table-wrap { | 212 | .scrape-table-wrap { |
| 183 | overflow-x: auto; | 213 | overflow-x: auto; |
| 184 | max-width: 100%; | 214 | max-width: 100%; |
| 185 | cursor: grab; | ||
| 186 | user-select: none; | ||
| 187 | } | 215 | } |
| 188 | 216 | ||
| 189 | .scroll-indicator { | 217 | .scroll-indicator { |