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

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

242 lines · 6836 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">
178              {{ cornerLabel ?? 'Sources' }}
179              <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" style="display:inline-block;vertical-align:middle;margin-left:4px;color:inherit">
180                <path fill="currentColor" d="m12 4l-1.41 1.41L16.17 11H4v2h12.17l-5.58 5.59L12 20l8-8z"/>
181              </svg>
182            </th>
183            <th v-for="source in sources" :key="source" class="source-header">
184              <span>{{ source }}</span>
185            </th>
186          </tr>
187        </thead>
188        <tbody>
189          <tr v-for="site in normalizedSites" :key="site.name">
190            <td class="site-name">{{ site.name }}</td>
191            <td
192              v-for="source in sources"
193              :key="source"
194              class="cell"
195              :class="{
196                active: source in site.scrapes,
197                'active-sd': hasMetadata(site.scrapes[source], 'SD'),
198              }"
199              :title="getScrapeTitle(site.scrapes[source])"
200            />
201          </tr>
202        </tbody>
203      </table>
204    </div>
205    <div v-if="canScrollRight" class="scroll-indicator">
206      <span class="i-lucide-chevron-right" />
207    </div>
208  </div>
209</template>
210
211<style>
212.scrape-table-outer {
213  position: relative;
214  margin: 16px 0;
215}
216
217.scrape-table-wrap {
218  overflow-x: auto;
219  max-width: 100%;
220}
221
222.scroll-indicator {
223  position: absolute;
224  right: 0;
225  top: 0;
226  bottom: 0;
227  width: 40px;
228  display: flex;
229  align-items: center;
230  justify-content: center;
231  background: linear-gradient(to right, transparent, var(--vp-c-bg) 70%);
232  pointer-events: none;
233  color: var(--vp-c-text-2);
234  font-size: 20px;
235  animation: pulse-right 1.5s ease-in-out infinite;
236}
237
238@keyframes pulse-right {
239  0%, 100% { opacity: 0.5; transform: translateX(0); }
240  50% { opacity: 1; transform: translateX(4px); }
241}
242</style>