docs/.vitepress/theme/components/ScrapeTable.vue
174 lines · 4619 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] || a.localeCompare(b))
40})
41
42const HEADER_MIN_PX = 48
43const HEADER_MAX_PX = 140
44const HEADER_PER_CHAR_PX = 7.2
45const HEADER_PADDING_PX = 20
46
47const headerHeight = computed(() => {
48 const longest = sources.value.reduce((n, s) => Math.max(n, s.length), 0)
49 const raw = Math.round(longest * HEADER_PER_CHAR_PX) + HEADER_PADDING_PX
50 return Math.min(HEADER_MAX_PX, Math.max(HEADER_MIN_PX, raw))
51})
52
53// Drag to scroll + scroll indicator
54const wrapRef = ref<HTMLElement | null>(null)
55const canScrollRight = ref(false)
56let isDragging = false
57let startX = 0
58let scrollStart = 0
59
60const SCROLL_END_PX = 2
61
62function checkScroll() {
63 const el = wrapRef.value
64 if (!el) return
65 canScrollRight.value = el.scrollLeft + el.clientWidth < el.scrollWidth - SCROLL_END_PX
66}
67
68function onPointerDown(e: PointerEvent) {
69 const el = wrapRef.value
70 if (!el) return
71 isDragging = true
72 startX = e.clientX
73 scrollStart = el.scrollLeft
74 el.setPointerCapture(e.pointerId)
75 el.style.cursor = 'grabbing'
76}
77
78function onPointerMove(e: PointerEvent) {
79 if (!isDragging || !wrapRef.value) return
80 wrapRef.value.scrollLeft = scrollStart - (e.clientX - startX)
81}
82
83function onPointerUp(e: PointerEvent) {
84 isDragging = false
85 if (wrapRef.value) {
86 wrapRef.value.releasePointerCapture(e.pointerId)
87 wrapRef.value.style.cursor = ''
88 }
89}
90
91onMounted(() => {
92 checkScroll()
93 useEventListener(wrapRef, 'scroll', checkScroll, { passive: true })
94 useEventListener(window, 'resize', checkScroll, { passive: true })
95})
96</script>
97
98<template>
99 <div class="scrape-table-outer" :style="{ '--scrape-header-height': `${headerHeight}px` }">
100 <div
101 ref="wrapRef"
102 class="scrape-table-wrap"
103 @pointerdown="onPointerDown"
104 @pointermove="onPointerMove"
105 @pointerup="onPointerUp"
106 @pointercancel="onPointerUp"
107 >
108 <table class="scrape-table">
109 <colgroup>
110 <col />
111 <col v-for="source in sources" :key="source" class="col-cell" />
112 </colgroup>
113 <thead>
114 <tr>
115 <th class="corner">Sources</th>
116 <th v-for="source in sources" :key="source" class="source-header">
117 <span>{{ source }}</span>
118 </th>
119 </tr>
120 </thead>
121 <tbody>
122 <tr v-for="site in normalizedSites" :key="site.name">
123 <td class="site-name">{{ site.name }}</td>
124 <td
125 v-for="source in sources"
126 :key="source"
127 class="cell"
128 :class="{ active: source in site.scrapes }"
129 :title="site.scrapes[source] || undefined"
130 />
131 </tr>
132 </tbody>
133 </table>
134 </div>
135 <div v-if="canScrollRight" class="scroll-indicator">
136 <span class="i-lucide-chevron-right" />
137 </div>
138 </div>
139</template>
140
141<style>
142.scrape-table-outer {
143 position: relative;
144 margin: 16px 0;
145}
146
147.scrape-table-wrap {
148 overflow-x: auto;
149 max-width: 100%;
150 cursor: grab;
151 user-select: none;
152}
153
154.scroll-indicator {
155 position: absolute;
156 right: 0;
157 top: 0;
158 bottom: 0;
159 width: 40px;
160 display: flex;
161 align-items: center;
162 justify-content: center;
163 background: linear-gradient(to right, transparent, var(--vp-c-bg) 70%);
164 pointer-events: none;
165 color: var(--vp-c-text-2);
166 font-size: 20px;
167 animation: pulse-right 1.5s ease-in-out infinite;
168}
169
170@keyframes pulse-right {
171 0%, 100% { opacity: 0.5; transform: translateX(0); }
172 50% { opacity: 1; transform: translateX(4px); }
173}
174</style>