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