docs/.vitepress/theme/components/ScrapeTable.vue

d2d9ddbc463ad3fc50a958e883f7b054ec2e2212
wotaku/docs/.vitepress/theme/components/ScrapeTable.vue history · blame · raw

237 lines · 6502 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)
 88const isDragging = ref(false)
 89let isPending = false
 90let startX = 0
 91let scrollStart = 0
 92let pendingPointerId = -1
 93
 94const SCROLL_END_PX = 2
 95const DRAG_THRESHOLD_PX = 5
 96
 97function checkScroll() {
 98  const el = wrapRef.value
 99  if (!el) return
100  canScrollRight.value = el.scrollLeft + el.clientWidth < el.scrollWidth - SCROLL_END_PX
101}
102
103function 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
112function onPointerDown(e: PointerEvent) {
113  if (e.button !== 0) return
114  const el = wrapRef.value
115  if (!el) return
116  // Don't initiate drag when clicking on selectable text elements
117  if (isTextTarget(e)) return
118  isPending = true
119  startX = e.clientX
120  scrollStart = el.scrollLeft
121  pendingPointerId = e.pointerId
122}
123
124function onPointerMove(e: PointerEvent) {
125  const el = wrapRef.value
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)
142}
143
144function onPointerUp(e: PointerEvent) {
145  isPending = false
146  isDragging.value = false
147  if (wrapRef.value) {
148    wrapRef.value.releasePointerCapture(e.pointerId)
149  }
150}
151
152onMounted(() => {
153  checkScroll()
154  useEventListener(wrapRef, 'scroll', checkScroll, { passive: true })
155  useEventListener(window, 'resize', checkScroll, { passive: true })
156})
157</script>
158
159<template>
160  <div class="scrape-table-outer" :style="{ '--scrape-header-height': `${headerHeight}px` }">
161    <div
162      ref="wrapRef"
163      class="scrape-table-wrap"
164      :style="{ cursor: isDragging ? 'grabbing' : 'auto', userSelect: isDragging ? 'none' : 'auto' }"
165      @pointerdown="onPointerDown"
166      @pointermove="onPointerMove"
167      @pointerup="onPointerUp"
168      @pointercancel="onPointerUp"
169    >
170      <table class="scrape-table">
171        <colgroup>
172          <col />
173          <col v-for="source in sources" :key="source" class="col-cell" />
174        </colgroup>
175        <thead>
176          <tr>
177            <th class="corner">{{ cornerLabel ?? 'Sources' }}</th>
178            <th v-for="source in sources" :key="source" class="source-header">
179              <span>{{ source }}</span>
180            </th>
181          </tr>
182        </thead>
183        <tbody>
184          <tr v-for="site in normalizedSites" :key="site.name">
185            <td class="site-name">{{ site.name }}</td>
186            <td
187              v-for="source in sources"
188              :key="source"
189              class="cell"
190              :class="{
191                active: source in site.scrapes,
192                'active-sd': hasMetadata(site.scrapes[source], 'SD'),
193              }"
194              :title="getScrapeTitle(site.scrapes[source])"
195            />
196          </tr>
197        </tbody>
198      </table>
199    </div>
200    <div v-if="canScrollRight" class="scroll-indicator">
201      <span class="i-lucide-chevron-right" />
202    </div>
203  </div>
204</template>
205
206<style>
207.scrape-table-outer {
208  position: relative;
209  margin: 16px 0;
210}
211
212.scrape-table-wrap {
213  overflow-x: auto;
214  max-width: 100%;
215}
216
217.scroll-indicator {
218  position: absolute;
219  right: 0;
220  top: 0;
221  bottom: 0;
222  width: 40px;
223  display: flex;
224  align-items: center;
225  justify-content: center;
226  background: linear-gradient(to right, transparent, var(--vp-c-bg) 70%);
227  pointer-events: none;
228  color: var(--vp-c-text-2);
229  font-size: 20px;
230  animation: pulse-right 1.5s ease-in-out infinite;
231}
232
233@keyframes pulse-right {
234  0%, 100% { opacity: 0.5; transform: translateX(0); }
235  50% { opacity: 1; transform: translateX(4px); }
236}
237</style>