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