docs/.vitepress/theme/components/BookmarksPanel.vue

78ea9fc554970c86ce26acb3978ead8147153dad
wotaku/docs/.vitepress/theme/components/BookmarksPanel.vue

535 lines · 15071 bytes

  1<script setup lang="ts">
  2import { onClickOutside } from '@vueuse/core'
  3import { Bookmark, Hash, X } from 'lucide-vue-next'
  4import { computed, ref } from 'vue'
  5import type { DefaultTheme } from 'vitepress/theme'
  6import type { HomeCard } from '../../configs/constants'
  7import { homeCards, sidebar } from '../../configs/constants'
  8import type { Bookmark as BookmarkItem } from '../composables/useBookmarks'
  9import { useBookmarks } from '../composables/useBookmarks'
 10
 11const ICON_SIZE = 18
 12const ICON_STROKE = 2
 13
 14const { bookmarks, remove } = useBookmarks()
 15const isOpen = ref(false)
 16const wrapperRef = ref<HTMLElement>()
 17const scrollRef = ref<HTMLElement>()
 18const showFade = ref(true)
 19
 20onClickOutside(wrapperRef, () => {
 21  isOpen.value = false
 22})
 23
 24function onScroll() {
 25  if (!scrollRef.value) return
 26  const { scrollTop, scrollHeight, clientHeight } = scrollRef.value
 27  showFade.value = scrollTop + clientHeight < scrollHeight - 4
 28}
 29
 30function flattenSidebarPaths(items: DefaultTheme.SidebarItem[]): string[] {
 31  const paths: string[] = []
 32  for (const item of items) {
 33    if (item.link) paths.push(item.link)
 34    if (item.items) paths.push(...flattenSidebarPaths(item.items))
 35  }
 36  return paths
 37}
 38
 39const sidebarPathOrder = flattenSidebarPaths(Array.isArray(sidebar) ? sidebar : Object.values(sidebar).flat())
 40
 41function sidebarIndex(path: string): number {
 42  const exact = sidebarPathOrder.indexOf(path)
 43  return exact !== -1 ? exact : Infinity
 44}
 45
 46function cardIconToClass(icon: string): string {
 47  const m = icon.match(/^:(.+):$/)
 48  if (!m) return 'i-lucide:file-text'
 49  const name = m[1]
 50  const dash = name.indexOf('-')
 51  if (dash === -1) return 'i-lucide:file-text'
 52  return `i-${name.slice(0, dash)}:${name.slice(dash + 1)}`
 53}
 54
 55function getPageCard(path: string): HomeCard | null {
 56  let best: HomeCard | null = null
 57  let bestLen = 0
 58  for (const card of homeCards) {
 59    if ((path === card.link || path.startsWith(card.link + '/')) && card.link.length > bestLen) {
 60      best = card
 61      bestLen = card.link.length
 62    }
 63  }
 64  return best
 65}
 66
 67type PageGroup = { path: string; card: HomeCard | null; items: BookmarkItem[] }
 68type TopLevelEntry = { type: 'page'; path: string; card: HomeCard | null; items: BookmarkItem[] }
 69type FolderEntry = { type: 'folder'; section: string; parentCard: HomeCard | null; pages: PageGroup[] }
 70type GroupEntry = TopLevelEntry | FolderEntry
 71
 72const grouped = computed((): GroupEntry[] => {
 73  // Collect bookmarks by path
 74  const byPath = new Map<string, BookmarkItem[]>()
 75  for (const bm of bookmarks.value) {
 76    if (!byPath.has(bm.path)) byPath.set(bm.path, [])
 77    byPath.get(bm.path)!.push(bm)
 78  }
 79
 80  // Separate into top-level pages and folder sub-pages (those with a section field)
 81  const topPages: string[] = []
 82  const folderMap = new Map<string, string[]>() // section → paths
 83
 84  for (const path of byPath.keys()) {
 85    const card = getPageCard(path)
 86    if (card?.section) {
 87      if (!folderMap.has(card.section)) folderMap.set(card.section, [])
 88      folderMap.get(card.section)!.push(path)
 89    } else {
 90      topPages.push(path)
 91    }
 92  }
 93
 94  // Sort top-level pages by exact sidebar position
 95  topPages.sort((a, b) => sidebarIndex(a) - sidebarIndex(b))
 96
 97  // Build folder entries, sorted by parent card's sidebar position
 98  const folderEntries: FolderEntry[] = [...folderMap.entries()]
 99    .map(([section, paths]) => {
100      const parentCard = homeCards.find(c => c.title === section && !c.section) ?? null
101      const sortedPages = [...paths]
102        .sort((a, b) => sidebarIndex(a) - sidebarIndex(b))
103        .map(path => ({ path, card: getPageCard(path), items: byPath.get(path)! }))
104      return { type: 'folder' as const, section, parentCard, pages: sortedPages }
105    })
106    .sort((a, b) => sidebarIndex(a.parentCard?.link ?? '') - sidebarIndex(b.parentCard?.link ?? ''))
107
108  // Merge top-level and folder entries in sidebar order
109  const topEntries: TopLevelEntry[] = topPages.map(path => ({
110    type: 'page' as const,
111    path,
112    card: getPageCard(path),
113    items: byPath.get(path)!
114  }))
115
116  return [...topEntries, ...folderEntries].sort((a, b) => {
117    const ai = a.type === 'page' ? sidebarIndex(a.path) : sidebarIndex(a.parentCard?.link ?? '')
118    const bi = b.type === 'page' ? sidebarIndex(b.path) : sidebarIndex(b.parentCard?.link ?? '')
119    return ai - bi
120  })
121})
122</script>
123
124<template>
125  <div ref="wrapperRef" class="bookmarks-wrapper">
126    <button
127      type="button"
128      class="bookmarks-btn"
129      aria-label="Bookmarks"
130      :aria-expanded="isOpen"
131      @click="isOpen = !isOpen"
132    >
133      <Bookmark :size="ICON_SIZE" :stroke-width="ICON_STROKE" />
134    </button>
135
136    <Transition name="bookmarks-panel">
137      <div v-if="isOpen" class="bookmarks-panel" role="dialog" aria-label="Bookmarks">
138        <div class="bookmarks-mobile-header">
139          <span class="bookmarks-mobile-title">Bookmarks</span>
140          <button type="button" class="bookmarks-mobile-close" aria-label="Close" @click="isOpen = false">
141            <X :size="18" :stroke-width="2" />
142          </button>
143        </div>
144        <div
145          ref="scrollRef"
146          class="bookmarks-scroll"
147          @scroll.passive="onScroll"
148        >
149          <div v-if="bookmarks.length === 0" class="bookmarks-empty">
150            <span class="bookmarks-empty-kaomoji">(˶ᵔ ᵕ ᵔ˶)</span>
151            <p class="bookmarks-empty-hint bookmarks-empty-hint--desktop">Tip: Hover over any primary heading under <strong>On this page</strong> and click <Bookmark :size="13" :stroke-width="2" class="bookmarks-empty-inline-icon" /> to bookmark it.</p>
152            <p class="bookmarks-empty-hint bookmarks-empty-hint--mobile">Tip: Tap <strong>On this page</strong> and press <Bookmark :size="13" :stroke-width="2" class="bookmarks-empty-inline-icon" /> next to any primary heading to bookmark it.</p>
153          </div>
154          <div v-else class="bookmarks-groups">
155            <template v-for="group in grouped" :key="group.type === 'page' ? group.path : group.section">
156
157              <!-- Top-level page (no folder) -->
158              <div v-if="group.type === 'page'" class="bookmarks-group">
159                <h3 class="bookmarks-group-title">
160                  <span v-if="group.card" :class="cardIconToClass(group.card.icon)" class="bookmarks-group-icon" />
161                  <span v-else class="i-lucide:file-text bookmarks-group-icon" />
162                  <span class="bookmarks-group-text">{{ group.card?.title ?? group.path }}</span>
163                </h3>
164                <ul class="bookmarks-list">
165                  <li v-for="bm in group.items" :key="bm.anchor" class="bookmarks-item">
166                    <a class="bookmarks-item-link" :href="bm.path + '#' + bm.anchor" @click="isOpen = false">
167                      <Hash :size="12" :stroke-width="2" class="bookmarks-item-icon" />
168                      {{ bm.title }}
169                    </a>
170                    <button type="button" class="bookmarks-item-remove" :aria-label="`Remove bookmark for ${bm.title}`" @click.stop="remove(bm.anchor, bm.path)">
171                      <svg xmlns="http://www.w3.org/2000/svg" width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><path d="M18 6 6 18"/><path d="m6 6 12 12"/></svg>
172                    </button>
173                  </li>
174                </ul>
175              </div>
176
177              <!-- Folder group (e.g. Guides, Glossary) -->
178              <div v-else class="bookmarks-group">
179                <h3 class="bookmarks-group-title">
180                  <span v-if="group.parentCard" :class="cardIconToClass(group.parentCard.icon)" class="bookmarks-group-icon" />
181                  <span v-else class="i-lucide:folder bookmarks-group-icon" />
182                  <span class="bookmarks-group-text">{{ group.section }}</span>
183                </h3>
184                <div v-for="page in group.pages" :key="page.path" class="bookmarks-sub-group">
185                  <h4 class="bookmarks-sub-title">{{ page.card?.title ?? page.path }}</h4>
186                  <ul class="bookmarks-list">
187                    <li v-for="bm in page.items" :key="bm.anchor" class="bookmarks-item">
188                      <a class="bookmarks-item-link" :href="bm.path + '#' + bm.anchor" @click="isOpen = false">
189                        <Hash :size="12" :stroke-width="2" class="bookmarks-item-icon" />
190                        {{ bm.title }}
191                      </a>
192                      <button type="button" class="bookmarks-item-remove" :aria-label="`Remove bookmark for ${bm.title}`" @click.stop="remove(bm.anchor, bm.path)">
193                        <svg xmlns="http://www.w3.org/2000/svg" width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><path d="M18 6 6 18"/><path d="m6 6 12 12"/></svg>
194                      </button>
195                    </li>
196                  </ul>
197                </div>
198              </div>
199
200            </template>
201          </div>
202        </div>
203        <Transition name="bookmarks-fade">
204          <div v-if="showFade && bookmarks.length > 0" class="bookmarks-fade-overlay" />
205        </Transition>
206      </div>
207    </Transition>
208  </div>
209</template>
210
211<style scoped>
212.bookmarks-wrapper {
213  position: relative;
214  width: 100%;
215  height: 100%;
216  display: flex;
217  align-items: center;
218  justify-content: center;
219}
220
221.bookmarks-btn {
222  display: flex;
223  align-items: center;
224  justify-content: center;
225  width: 100%;
226  height: 100%;
227  color: var(--vp-c-text-1);
228  opacity: 0.55;
229  transition:
230    opacity 0.25s,
231    transform 0.2s cubic-bezier(0.16, 1, 0.3, 1);
232}
233
234.bookmarks-btn:hover { opacity: 1; }
235.bookmarks-btn:active { transform: scale(0.94); }
236
237.bookmarks-panel {
238  position: absolute;
239  top: calc(var(--vp-nav-height) / 2 + 20px);
240  right: 0;
241  z-index: 100;
242  width: 282px;
243  background-color: var(--vp-c-bg-elv);
244  border: 1px solid var(--vp-c-divider);
245  border-radius: 12px;
246  box-shadow: var(--vp-shadow-3);
247  transition: background-color 0.5s;
248  transform-origin: top right;
249  overflow: hidden;
250}
251
252.bookmarks-scroll {
253  height: 400px;
254  overflow-y: auto;
255  overflow-x: hidden;
256  overscroll-behavior: contain;
257  padding: 20px;
258  scrollbar-width: none;
259  -ms-overflow-style: none;
260}
261
262.bookmarks-scroll::-webkit-scrollbar {
263  display: none;
264}
265
266.bookmarks-fade-overlay {
267  position: absolute;
268  bottom: 0;
269  left: 0;
270  right: 0;
271  height: 56px;
272  pointer-events: none;
273  background: linear-gradient(
274    to bottom,
275    transparent,
276    var(--vp-c-bg-elv)
277  );
278  border-radius: 0 0 12px 12px;
279}
280
281.bookmarks-empty {
282  display: flex;
283  flex-direction: column;
284  align-items: center;
285  justify-content: center;
286  min-height: 360px;
287  gap: 10px;
288  text-align: center;
289  overflow: hidden;
290}
291
292.bookmarks-empty-kaomoji {
293  font-size: 28px;
294  color: var(--vp-c-text-3);
295  line-height: 1;
296  user-select: none;
297}
298
299.bookmarks-empty-hint {
300  font-size: 13px;
301  color: var(--vp-c-text-2);
302  line-height: 1.6;
303  margin: 0;
304  white-space: normal;
305  overflow-wrap: break-word;
306  word-break: break-word;
307}
308
309.bookmarks-empty-hint--mobile {
310  display: none;
311}
312
313.bookmarks-empty-inline-icon {
314  display: inline;
315  vertical-align: middle;
316  position: relative;
317  top: -1px;
318}
319
320.bookmarks-groups {
321  display: flex;
322  flex-direction: column;
323  gap: 8px;
324}
325
326.bookmarks-sub-group {
327  margin-top: 4px;
328}
329
330.bookmarks-sub-title {
331  font-size: 11px;
332  font-weight: 600;
333  letter-spacing: 0;
334  color: var(--vp-c-text-3);
335  margin: 6px 0 2px;
336  padding: 0 6px 4px;
337  user-select: none;
338  border-bottom: 1px solid #525252;
339}
340
341.bookmarks-group-title {
342  display: flex;
343  align-items: center;
344  gap: 6px;
345  font-size: 16px;
346  font-weight: 500;
347  color: var(--vp-c-text-1);
348  margin: 0 0 2px;
349  padding: 2px 0;
350  user-select: none;
351  min-width: 0;
352}
353
354.bookmarks-group-icon {
355  font-size: 16px;
356  color: var(--vp-c-text-1);
357  flex: none;
358}
359
360.bookmarks-group-text {
361  flex: 1;
362  min-width: 0;
363  overflow: hidden;
364  white-space: nowrap;
365  text-overflow: ellipsis;
366}
367
368.bookmarks-list {
369  list-style: none;
370  padding: 0;
371  margin: 0;
372}
373
374.bookmarks-item {
375  display: flex;
376  align-items: center;
377  border-radius: 6px;
378  transition: background-color 0.15s;
379}
380
381.bookmarks-item:hover {
382  background: var(--vp-c-default-soft);
383}
384
385.bookmarks-item-link {
386  display: flex;
387  align-items: center;
388  gap: 5px;
389  flex: 1;
390  padding: 4px 6px;
391  font-size: 13px;
392  color: var(--vp-c-text-2);
393  text-decoration: none;
394  white-space: nowrap;
395  overflow: hidden;
396  text-overflow: ellipsis;
397  transition: color 0.15s;
398  min-width: 0;
399}
400
401.bookmarks-item-link:hover {
402  color: var(--vp-c-brand-1);
403}
404
405.bookmarks-item-icon {
406  flex: none;
407  color: var(--vp-c-text-3);
408}
409
410.bookmarks-item-remove {
411  flex: none;
412  display: flex;
413  align-items: center;
414  justify-content: center;
415  width: 20px;
416  height: 20px;
417  margin-right: 4px;
418  border: none;
419  background: transparent;
420  color: var(--vp-c-text-3);
421  cursor: pointer;
422  transition: color 0.15s;
423}
424
425.bookmarks-item-remove:hover {
426  color: var(--vp-c-text-1);
427}
428
429.bookmarks-panel-enter-active,
430.bookmarks-panel-leave-active {
431  transition: opacity 0.15s, transform 0.15s cubic-bezier(0.16, 1, 0.3, 1);
432}
433
434.bookmarks-panel-enter-from,
435.bookmarks-panel-leave-to {
436  opacity: 0;
437  transform: scale(0.95) translateY(-4px);
438}
439
440.bookmarks-fade-enter-active,
441.bookmarks-fade-leave-active {
442  transition: opacity 0.2s;
443}
444
445.bookmarks-fade-enter-from,
446.bookmarks-fade-leave-to {
447  opacity: 0;
448}
449
450.bookmarks-mobile-header {
451  display: none;
452}
453
454@media (max-width: 1279px) {
455  .bookmarks-empty-hint--desktop {
456    display: none;
457  }
458
459  .bookmarks-empty-hint--mobile {
460    display: block;
461  }
462}
463
464@media (max-width: 767px) {
465  .bookmarks-panel {
466    position: fixed;
467    top: 0;
468    left: 0;
469    right: 0;
470    bottom: 0;
471    width: 100%;
472    height: 100%;
473    border-radius: 0;
474    border: none;
475    z-index: 9999;
476    transform-origin: center top;
477    display: flex;
478    flex-direction: column;
479  }
480
481  .bookmarks-scroll {
482    flex: 1;
483    height: auto;
484    min-height: 0;
485    display: flex;
486    flex-direction: column;
487  }
488
489  .bookmarks-empty {
490    flex: 1;
491    min-height: 0;
492  }
493
494  .bookmarks-mobile-header {
495    display: flex;
496    align-items: center;
497    justify-content: space-between;
498    height: 56px;
499    flex-shrink: 0;
500    padding: 0 20px;
501    border-bottom: 1px solid var(--vp-c-divider);
502  }
503
504  .bookmarks-mobile-title {
505    font-size: 16px;
506    font-weight: 600;
507    color: var(--vp-c-text-1);
508  }
509
510  .bookmarks-mobile-close {
511    display: flex;
512    align-items: center;
513    justify-content: center;
514    width: 32px;
515    height: 32px;
516    border-radius: 8px;
517    border: none;
518    background: transparent;
519    color: var(--vp-c-text-2);
520    cursor: pointer;
521    transition: background-color 0.15s, color 0.15s;
522  }
523
524  .bookmarks-mobile-close:hover {
525    background: var(--vp-c-default-soft);
526    color: var(--vp-c-text-1);
527  }
528
529  .bookmarks-panel-enter-from,
530  .bookmarks-panel-leave-to {
531    opacity: 0;
532    transform: translateY(8px);
533  }
534}
535</style>