docs/.vitepress/theme/components/ScrapeTable.vue
163 lines · 4171 bytes
1<script setup lang="ts">
2import { useEventListener } from '@vueuse/core'
3import { computed, ref, onMounted } from 'vue'
4
5interface Site {
6 name: string
7 scrapes: Record<string, string>
8}
9
10const props = defineProps<{
11 sites: Site[]
12}>()
13
14// Normalize source names (case-insensitive dedup, keep first-seen casing)
15const normalizedSites = computed(() => {
16 const canonical: Record<string, string> = {}
17 for (const site of props.sites) {
18 for (const source of Object.keys(site.scrapes)) {
19 const key = source.toLowerCase()
20 if (!canonical[key]) canonical[key] = source
21 }
22 }
23 return props.sites.map(site => {
24 const scrapes: Record<string, string> = {}
25 for (const [source, player] of Object.entries(site.scrapes)) {
26 scrapes[canonical[source.toLowerCase()]] = player
27 }
28 return { name: site.name, scrapes }
29 })
30})
31
32const sources = computed(() => {
33 const count: Record<string, number> = {}
34 for (const site of normalizedSites.value) {
35 for (const source of Object.keys(site.scrapes)) {
36 count[source] = (count[source] || 0) + 1
37 }
38 }
39 return Object.keys(count).sort((a, b) => count[b] - count[a])
40})
41
42// Drag to scroll + scroll indicator
43const wrapRef = ref<HTMLElement | null>(null)
44const canScrollRight = ref(false)
45let isDragging = false
46let startX = 0
47let scrollStart = 0
48
49const SCROLL_END_PX = 2
50
51function checkScroll() {
52 const el = wrapRef.value
53 if (!el) return
54 canScrollRight.value = el.scrollLeft + el.clientWidth < el.scrollWidth - SCROLL_END_PX
55}
56
57function onPointerDown(e: PointerEvent) {
58 const el = wrapRef.value
59 if (!el) return
60 isDragging = true
61 startX = e.clientX
62 scrollStart = el.scrollLeft
63 el.setPointerCapture(e.pointerId)
64 el.style.cursor = 'grabbing'
65}
66
67function onPointerMove(e: PointerEvent) {
68 if (!isDragging || !wrapRef.value) return
69 wrapRef.value.scrollLeft = scrollStart - (e.clientX - startX)
70}
71
72function onPointerUp(e: PointerEvent) {
73 isDragging = false
74 if (wrapRef.value) {
75 wrapRef.value.releasePointerCapture(e.pointerId)
76 wrapRef.value.style.cursor = ''
77 }
78}
79
80onMounted(() => {
81 checkScroll()
82 useEventListener(wrapRef, 'scroll', checkScroll, { passive: true })
83 useEventListener(window, 'resize', checkScroll, { passive: true })
84})
85</script>
86
87<template>
88 <div class="scrape-table-outer">
89 <div
90 ref="wrapRef"
91 class="scrape-table-wrap"
92 @pointerdown="onPointerDown"
93 @pointermove="onPointerMove"
94 @pointerup="onPointerUp"
95 @pointercancel="onPointerUp"
96 >
97 <table class="scrape-table">
98 <colgroup>
99 <col />
100 <col v-for="source in sources" :key="source" class="col-cell" />
101 </colgroup>
102 <thead>
103 <tr>
104 <th class="corner">Sources</th>
105 <th v-for="source in sources" :key="source" class="source-header">
106 <span>{{ source }}</span>
107 </th>
108 </tr>
109 </thead>
110 <tbody>
111 <tr v-for="site in normalizedSites" :key="site.name">
112 <td class="site-name">{{ site.name }}</td>
113 <td
114 v-for="source in sources"
115 :key="source"
116 class="cell"
117 :class="{ active: source in site.scrapes }"
118 :title="site.scrapes[source] || undefined"
119 />
120 </tr>
121 </tbody>
122 </table>
123 </div>
124 <div v-if="canScrollRight" class="scroll-indicator">
125 <span class="i-lucide-chevron-right" />
126 </div>
127 </div>
128</template>
129
130<style>
131.scrape-table-outer {
132 position: relative;
133 margin: 16px 0;
134}
135
136.scrape-table-wrap {
137 overflow-x: auto;
138 max-width: 100%;
139 cursor: grab;
140 user-select: none;
141}
142
143.scroll-indicator {
144 position: absolute;
145 right: 0;
146 top: 0;
147 bottom: 0;
148 width: 40px;
149 display: flex;
150 align-items: center;
151 justify-content: center;
152 background: linear-gradient(to right, transparent, var(--vp-c-bg) 70%);
153 pointer-events: none;
154 color: var(--vp-c-text-2);
155 font-size: 20px;
156 animation: pulse-right 1.5s ease-in-out infinite;
157}
158
159@keyframes pulse-right {
160 0%, 100% { opacity: 0.5; transform: translateX(0); }
161 50% { opacity: 1; transform: translateX(4px); }
162}
163</style>