docs/.vitepress/theme/components/BookmarksPanel.vue
725 lines · 18893 bytes
1<script setup lang="ts">
2import { onClickOutside, useEventListener } from '@vueuse/core'
3import { ArrowLeft, Bookmark, Hash } from 'lucide-vue-next'
4import { inBrowser } from 'vitepress'
5import type { DefaultTheme } from 'vitepress/theme'
6import { computed, nextTick, ref, watch } from 'vue'
7import type { HomeCard } from '../../configs/constants'
8import { homeCards, sidebar } from '../../configs/constants'
9import type { Bookmark as BookmarkItem } from '../composables/useBookmarks'
10import { useBookmarks } from '../composables/useBookmarks'
11
12const ICON_SIZE = 18
13const ICON_STROKE = 2
14
15const { bookmarks, remove } = useBookmarks()
16const isOpen = ref(false)
17const wrapperRef = ref<HTMLElement>()
18const buttonRef = ref<HTMLElement>()
19const panelRef = ref<HTMLElement>()
20const scrollRef = ref<HTMLElement>()
21const showFade = ref(true)
22const panelStyle = ref<Record<string, string>>({})
23
24onClickOutside(wrapperRef, () => {
25 isOpen.value = false
26})
27
28function onScroll() {
29 if (!scrollRef.value) return
30 const { scrollTop, scrollHeight, clientHeight } = scrollRef.value
31 showFade.value = scrollTop + clientHeight < scrollHeight - 4
32}
33
34function updatePanelPosition() {
35 const wrapper = wrapperRef.value
36 const button = buttonRef.value
37 const panel = panelRef.value
38 if (!wrapper || !button || !panel) return
39
40 if (!inBrowser || window.innerWidth <= 767) {
41 panelStyle.value = {}
42 return
43 }
44
45 const viewportMargin = 12
46 const wrapperRect = wrapper.getBoundingClientRect()
47 const buttonRect = button.getBoundingClientRect()
48 const panelWidth = panel.offsetWidth
49 const centeredLeft = buttonRect.left + buttonRect.width / 2 - panelWidth / 2
50 const maxLeft = Math.max(
51 viewportMargin,
52 window.innerWidth - viewportMargin - panelWidth
53 )
54 const viewportLeft = Math.min(Math.max(centeredLeft, viewportMargin), maxLeft)
55
56 panelStyle.value = {
57 '--bookmarks-panel-left': `${viewportLeft - wrapperRect.left}px`,
58 '--bookmarks-panel-shift': '0px'
59 }
60}
61
62watch(isOpen, async (open) => {
63 if (!open) return
64 await nextTick()
65 updatePanelPosition()
66 onScroll()
67})
68
69useEventListener(inBrowser ? window : undefined, 'resize', () => {
70 if (isOpen.value) updatePanelPosition()
71})
72
73function flattenSidebarPaths(items: DefaultTheme.SidebarItem[]): string[] {
74 const paths: string[] = []
75 for (const item of items) {
76 if (item.link) paths.push(item.link)
77 if (item.items) paths.push(...flattenSidebarPaths(item.items))
78 }
79 return paths
80}
81
82const sidebarPathOrder = flattenSidebarPaths(
83 Array.isArray(sidebar) ? sidebar : Object.values(sidebar).flat()
84)
85
86function sidebarIndex(path: string): number {
87 const exact = sidebarPathOrder.indexOf(path)
88 return exact !== -1 ? exact : Infinity
89}
90
91function cardIconToClass(icon: string): string {
92 const m = icon.match(/^:(.+):$/)
93 if (!m) return 'i-lucide:file-text'
94 const name = m[1]
95 const dash = name.indexOf('-')
96 if (dash === -1) return 'i-lucide:file-text'
97 return `i-${name.slice(0, dash)}:${name.slice(dash + 1)}`
98}
99
100function getPageCard(path: string): HomeCard | null {
101 let best: HomeCard | null = null
102 let bestLen = 0
103 for (const card of homeCards) {
104 if (
105 (path === card.link || path.startsWith(card.link + '/')) &&
106 card.link.length > bestLen
107 ) {
108 best = card
109 bestLen = card.link.length
110 }
111 }
112 return best
113}
114
115type PageGroup = { path: string; card: HomeCard | null; items: BookmarkItem[] }
116type TopLevelEntry = {
117 type: 'page'
118 path: string
119 card: HomeCard | null
120 items: BookmarkItem[]
121}
122type FolderEntry = {
123 type: 'folder'
124 section: string
125 parentCard: HomeCard | null
126 pages: PageGroup[]
127}
128type GroupEntry = TopLevelEntry | FolderEntry
129
130const grouped = computed((): GroupEntry[] => {
131 // Collect bookmarks by path
132 const byPath = new Map<string, BookmarkItem[]>()
133 for (const bm of bookmarks.value) {
134 if (!byPath.has(bm.path)) byPath.set(bm.path, [])
135 byPath.get(bm.path)!.push(bm)
136 }
137
138 // Separate into top-level pages and folder sub-pages (those with a section field)
139 const topPages: string[] = []
140 const folderMap = new Map<string, string[]>() // section → paths
141
142 for (const path of byPath.keys()) {
143 const card = getPageCard(path)
144 if (card?.section) {
145 if (!folderMap.has(card.section)) folderMap.set(card.section, [])
146 folderMap.get(card.section)!.push(path)
147 } else {
148 topPages.push(path)
149 }
150 }
151
152 // Sort top-level pages by exact sidebar position
153 topPages.sort((a, b) => sidebarIndex(a) - sidebarIndex(b))
154
155 // Build folder entries, sorted by parent card's sidebar position
156 const folderEntries: FolderEntry[] = [...folderMap.entries()]
157 .map(([section, paths]) => {
158 const parentCard = homeCards.find(c =>
159 c.title === section && !c.section
160 ) ?? null
161 const sortedPages = [...paths]
162 .sort((a, b) => sidebarIndex(a) - sidebarIndex(b))
163 .map(path => ({
164 path,
165 card: getPageCard(path),
166 items: byPath.get(path)!
167 }))
168 return {
169 type: 'folder' as const,
170 section,
171 parentCard,
172 pages: sortedPages
173 }
174 })
175 .sort((a, b) =>
176 sidebarIndex(a.parentCard?.link ?? '') -
177 sidebarIndex(b.parentCard?.link ?? '')
178 )
179
180 // Merge top-level and folder entries in sidebar order
181 const topEntries: TopLevelEntry[] = topPages.map(path => ({
182 type: 'page' as const,
183 path,
184 card: getPageCard(path),
185 items: byPath.get(path)!
186 }))
187
188 return [...topEntries, ...folderEntries].sort((a, b) => {
189 const ai = a.type === 'page'
190 ? sidebarIndex(a.path)
191 : sidebarIndex(a.parentCard?.link ?? '')
192 const bi = b.type === 'page'
193 ? sidebarIndex(b.path)
194 : sidebarIndex(b.parentCard?.link ?? '')
195 return ai - bi
196 })
197})
198</script>
199
200<template>
201 <div ref="wrapperRef" class="bookmarks-wrapper">
202 <button
203 ref="buttonRef"
204 type="button"
205 class="bookmarks-btn"
206 aria-label="Bookmarks"
207 :aria-expanded="isOpen"
208 @click="isOpen = !isOpen"
209 >
210 <Bookmark :size="ICON_SIZE" :stroke-width="ICON_STROKE" />
211 </button>
212
213 <Transition name="bookmarks-panel">
214 <div
215 v-if="isOpen"
216 ref="panelRef"
217 class="bookmarks-panel"
218 :style="panelStyle"
219 role="dialog"
220 aria-label="Bookmarks"
221 >
222 <div class="bookmarks-mobile-header">
223 <button
224 type="button"
225 class="bookmarks-mobile-back"
226 aria-label="Back"
227 @click="isOpen = false"
228 >
229 <ArrowLeft :size="18" :stroke-width="2" />
230 </button>
231 <span class="bookmarks-mobile-title">Bookmarks</span>
232 </div>
233 <div
234 ref="scrollRef"
235 class="bookmarks-scroll"
236 @scroll.passive="onScroll"
237 >
238 <div v-if="bookmarks.length === 0" class="bookmarks-empty">
239 <span class="bookmarks-empty-kaomoji">(˶ᵔ ᵕ ᵔ˶)</span>
240 <p class="bookmarks-empty-hint bookmarks-empty-hint--desktop">
241 Tip: Hover over any primary heading under <strong>On this
242 page</strong> and click <Bookmark
243 :size="13"
244 :stroke-width="2"
245 class="bookmarks-empty-inline-icon"
246 /> to bookmark it.
247 </p>
248 <p class="bookmarks-empty-hint bookmarks-empty-hint--mobile">
249 Tip: Tap <strong>On this page</strong> and press <Bookmark
250 :size="13"
251 :stroke-width="2"
252 class="bookmarks-empty-inline-icon"
253 /> next to any primary heading to bookmark it.
254 </p>
255 </div>
256 <div v-else class="bookmarks-groups">
257 <template
258 v-for="group in grouped"
259 :key="group.type === 'page' ? group.path : group.section"
260 >
261 <!-- Top-level page (no folder) -->
262 <div v-if="group.type === 'page'" class="bookmarks-group">
263 <h3 class="bookmarks-group-title">
264 <span
265 v-if="group.card"
266 :class="cardIconToClass(group.card.icon)"
267 class="bookmarks-group-icon"
268 />
269 <span
270 v-else
271 class="i-lucide:file-text bookmarks-group-icon"
272 />
273 <span class="bookmarks-group-text">{{
274 group.card?.title ?? group.path
275 }}</span>
276 </h3>
277 <ul class="bookmarks-list">
278 <li
279 v-for="bm in group.items"
280 :key="bm.anchor"
281 class="bookmarks-item"
282 >
283 <a
284 class="bookmarks-item-link"
285 :href="bm.path + '#' + bm.anchor"
286 @click="isOpen = false"
287 >
288 <Hash
289 :size="12"
290 :stroke-width="2"
291 class="bookmarks-item-icon"
292 />
293 {{ bm.title }}
294 </a>
295 <button
296 type="button"
297 class="bookmarks-item-remove"
298 :aria-label="`Remove bookmark for ${bm.title}`"
299 @click.stop="remove(bm.anchor, bm.path)"
300 >
301 <svg
302 xmlns="http://www.w3.org/2000/svg"
303 width="11"
304 height="11"
305 viewBox="0 0 24 24"
306 fill="none"
307 stroke="currentColor"
308 stroke-width="2.5"
309 stroke-linecap="round"
310 stroke-linejoin="round"
311 >
312 <path d="M18 6 6 18" />
313 <path d="m6 6 12 12" />
314 </svg>
315 </button>
316 </li>
317 </ul>
318 </div>
319
320 <!-- Folder group (e.g. Guides, Glossary) -->
321 <div v-else class="bookmarks-group">
322 <h3 class="bookmarks-group-title">
323 <span
324 v-if="group.parentCard"
325 :class="cardIconToClass(group.parentCard.icon)"
326 class="bookmarks-group-icon"
327 />
328 <span v-else class="i-lucide:folder bookmarks-group-icon" />
329 <span class="bookmarks-group-text">{{ group.section }}</span>
330 </h3>
331 <div
332 v-for="page in group.pages"
333 :key="page.path"
334 class="bookmarks-sub-group"
335 >
336 <h4 class="bookmarks-sub-title">
337 {{ page.card?.title ?? page.path }}
338 </h4>
339 <ul class="bookmarks-list">
340 <li
341 v-for="bm in page.items"
342 :key="bm.anchor"
343 class="bookmarks-item"
344 >
345 <a
346 class="bookmarks-item-link"
347 :href="bm.path + '#' + bm.anchor"
348 @click="isOpen = false"
349 >
350 <Hash
351 :size="12"
352 :stroke-width="2"
353 class="bookmarks-item-icon"
354 />
355 {{ bm.title }}
356 </a>
357 <button
358 type="button"
359 class="bookmarks-item-remove"
360 :aria-label="`Remove bookmark for ${bm.title}`"
361 @click.stop="remove(bm.anchor, bm.path)"
362 >
363 <svg
364 xmlns="http://www.w3.org/2000/svg"
365 width="11"
366 height="11"
367 viewBox="0 0 24 24"
368 fill="none"
369 stroke="currentColor"
370 stroke-width="2.5"
371 stroke-linecap="round"
372 stroke-linejoin="round"
373 >
374 <path d="M18 6 6 18" />
375 <path d="m6 6 12 12" />
376 </svg>
377 </button>
378 </li>
379 </ul>
380 </div>
381 </div>
382 </template>
383 </div>
384 </div>
385 <Transition name="bookmarks-fade">
386 <div
387 v-if="showFade && bookmarks.length > 0"
388 class="bookmarks-fade-overlay"
389 />
390 </Transition>
391 </div>
392 </Transition>
393 </div>
394</template>
395
396<style scoped>
397.bookmarks-wrapper {
398 position: relative;
399 width: 100%;
400 height: 100%;
401 display: flex;
402 align-items: center;
403 justify-content: center;
404}
405
406.bookmarks-btn {
407 display: flex;
408 align-items: center;
409 justify-content: center;
410 width: 100%;
411 height: 100%;
412 color: var(--vp-c-text-1);
413 opacity: 0.55;
414 transition: opacity 0.25s, transform 0.2s cubic-bezier(0.16, 1, 0.3, 1);
415}
416
417.bookmarks-btn:hover {
418 opacity: 1;
419}
420.bookmarks-btn:active {
421 transform: scale(0.94);
422}
423
424.bookmarks-panel {
425 position: absolute;
426 top: calc(var(--vp-nav-height) / 2 + 20px);
427 left: var(--bookmarks-panel-left, 50%);
428 z-index: 100;
429 width: 282px;
430 max-width: calc(100vw - 24px);
431 --bm-panel-bg: var(--vp-c-bg-elv);
432 background-color: var(--bm-panel-bg);
433 border: 1px solid var(--vp-c-divider);
434 border-radius: 12px;
435 box-shadow: var(--vp-shadow-3);
436 transition: background-color 0.5s;
437 transform: translateX(var(--bookmarks-panel-shift, -50%));
438 transform-origin: top center;
439 overflow: hidden;
440}
441
442.bookmarks-scroll {
443 height: 400px;
444 overflow-y: auto;
445 overflow-x: hidden;
446 overscroll-behavior: contain;
447 padding: 20px;
448 scrollbar-width: none;
449 -ms-overflow-style: none;
450}
451
452.bookmarks-scroll::-webkit-scrollbar {
453 display: none;
454}
455
456.bookmarks-fade-overlay {
457 position: absolute;
458 bottom: 0;
459 left: 0;
460 right: 0;
461 height: 56px;
462 pointer-events: none;
463 background: linear-gradient(
464 to bottom,
465 transparent,
466 var(--bm-panel-bg)
467 );
468 border-radius: 0 0 12px 12px;
469}
470
471.bookmarks-empty {
472 display: flex;
473 flex-direction: column;
474 align-items: center;
475 justify-content: center;
476 min-height: 360px;
477 gap: 10px;
478 text-align: center;
479 overflow: hidden;
480}
481
482.bookmarks-empty-kaomoji {
483 font-size: 28px;
484 color: var(--vp-c-text-3);
485 line-height: 1;
486 user-select: none;
487}
488
489.bookmarks-empty-hint {
490 font-size: 13px;
491 color: var(--vp-c-text-2);
492 line-height: 1.6;
493 margin: 0;
494 white-space: normal;
495 overflow-wrap: break-word;
496 word-break: break-word;
497}
498
499.bookmarks-empty-hint--mobile {
500 display: none;
501}
502
503.bookmarks-empty-inline-icon {
504 display: inline;
505 vertical-align: middle;
506 position: relative;
507 top: -1px;
508}
509
510.bookmarks-groups {
511 display: flex;
512 flex-direction: column;
513 gap: 8px;
514}
515
516.bookmarks-sub-group {
517 margin-top: 4px;
518}
519
520.bookmarks-sub-title {
521 font-size: 11px;
522 font-weight: 600;
523 letter-spacing: 0;
524 color: var(--vp-c-text-3);
525 margin: 6px 0 2px;
526 padding: 0 6px 4px;
527 user-select: none;
528 border-bottom: 1px solid #525252;
529}
530
531.bookmarks-group-title {
532 display: flex;
533 align-items: center;
534 gap: 6px;
535 font-size: 16px;
536 font-weight: 500;
537 color: var(--vp-c-text-1);
538 margin: 0 0 2px;
539 padding: 2px 0;
540 user-select: none;
541 min-width: 0;
542}
543
544.bookmarks-group-icon {
545 font-size: 16px;
546 color: var(--vp-c-text-1);
547 flex: none;
548}
549
550.bookmarks-group-text {
551 flex: 1;
552 min-width: 0;
553 overflow: hidden;
554 white-space: nowrap;
555 text-overflow: ellipsis;
556}
557
558.bookmarks-list {
559 list-style: none;
560 padding: 0;
561 margin: 0;
562}
563
564.bookmarks-item {
565 display: flex;
566 align-items: center;
567 border-radius: 6px;
568 transition: background-color 0.15s;
569}
570
571.bookmarks-item:hover {
572 background: var(--vp-c-default-soft);
573}
574
575.bookmarks-item-link {
576 display: flex;
577 align-items: center;
578 gap: 5px;
579 flex: 1;
580 padding: 4px 6px;
581 font-size: 13px;
582 color: var(--vp-c-text-2);
583 text-decoration: none;
584 white-space: nowrap;
585 overflow: hidden;
586 text-overflow: ellipsis;
587 transition: color 0.15s;
588 min-width: 0;
589}
590
591.bookmarks-item-link:hover {
592 color: var(--vp-c-brand-1);
593}
594
595.bookmarks-item-icon {
596 flex: none;
597 color: var(--vp-c-text-3);
598}
599
600.bookmarks-item-remove {
601 flex: none;
602 display: flex;
603 align-items: center;
604 justify-content: center;
605 width: 20px;
606 height: 20px;
607 margin-right: 4px;
608 border: none;
609 background: transparent;
610 color: var(--vp-c-text-3);
611 cursor: pointer;
612 transition: color 0.15s;
613}
614
615.bookmarks-item-remove:hover {
616 color: var(--vp-c-text-1);
617}
618
619.bookmarks-panel-enter-active,
620.bookmarks-panel-leave-active {
621 transition: opacity 0.15s, transform 0.15s cubic-bezier(0.16, 1, 0.3, 1);
622}
623
624.bookmarks-panel-enter-from,
625.bookmarks-panel-leave-to {
626 opacity: 0;
627 transform: translateX(var(--bookmarks-panel-shift, -50%)) scale(0.95)
628 translateY(-4px);
629}
630
631.bookmarks-fade-enter-active,
632.bookmarks-fade-leave-active {
633 transition: opacity 0.2s;
634}
635
636.bookmarks-fade-enter-from,
637.bookmarks-fade-leave-to {
638 opacity: 0;
639}
640
641.bookmarks-mobile-header {
642 display: none;
643}
644
645@media (max-width: 1279px) {
646 .bookmarks-empty-hint--desktop {
647 display: none;
648 }
649
650 .bookmarks-empty-hint--mobile {
651 display: block;
652 }
653}
654
655@media (max-width: 767px) {
656 .bookmarks-panel {
657 position: fixed;
658 top: 0;
659 left: 0;
660 right: 0;
661 bottom: 0;
662 width: 100%;
663 max-width: none;
664 height: 100%;
665 border-radius: 0;
666 border: none;
667 z-index: 9999;
668 transform: none;
669 transform-origin: center top;
670 display: flex;
671 flex-direction: column;
672 --bm-panel-bg: var(--vp-c-bg);
673 }
674
675 .bookmarks-scroll {
676 flex: 1;
677 height: auto;
678 min-height: 0;
679 display: flex;
680 flex-direction: column;
681 }
682
683 .bookmarks-empty {
684 flex: 1;
685 min-height: 0;
686 }
687
688 .bookmarks-mobile-header {
689 display: flex;
690 align-items: center;
691 height: 49px;
692 flex-shrink: 0;
693 padding: 6px 8px;
694 border-bottom: 1px solid var(--wk-fs-header-divider);
695 }
696
697 .bookmarks-mobile-title {
698 font-size: 16px;
699 font-weight: 600;
700 color: var(--vp-c-text-1);
701 }
702
703 .bookmarks-mobile-back {
704 display: flex;
705 align-items: center;
706 justify-content: center;
707 padding: 8px;
708 border: none;
709 background: transparent;
710 color: var(--vp-c-text-1);
711 cursor: pointer;
712 transition: color 0.15s;
713 }
714
715 .bookmarks-mobile-back:hover {
716 color: var(--vp-c-brand-1);
717 }
718
719 .bookmarks-panel-enter-from,
720 .bookmarks-panel-leave-to {
721 opacity: 0;
722 transform: translateY(8px);
723 }
724}
725</style>