| @@ -0,0 +1,509 @@ |
| 1 | <script setup lang="ts"> |
| 2 | import { onClickOutside } from '@vueuse/core' |
| 3 | import { Bookmark, BookMarked, Hash, X } from 'lucide-vue-next' |
| 4 | import { computed, ref } from 'vue' |
| 5 | import type { DefaultTheme } from 'vitepress/theme' |
| 6 | import type { HomeCard } from '../../configs/constants' |
| 7 | import { homeCards, sidebar } from '../../configs/constants' |
| 8 | import type { Bookmark as BookmarkItem } from '../composables/useBookmarks' |
| 9 | import { useBookmarks } from '../composables/useBookmarks' |
| 10 | |
| 11 | const ICON_SIZE = 18 |
| 12 | const ICON_STROKE = 2 |
| 13 | |
| 14 | const { bookmarks, remove } = useBookmarks() |
| 15 | const isOpen = ref(false) |
| 16 | const wrapperRef = ref<HTMLElement>() |
| 17 | const scrollRef = ref<HTMLElement>() |
| 18 | const showFade = ref(true) |
| 19 | |
| 20 | onClickOutside(wrapperRef, () => { |
| 21 | isOpen.value = false |
| 22 | }) |
| 23 | |
| 24 | function onScroll() { |
| 25 | if (!scrollRef.value) return |
| 26 | const { scrollTop, scrollHeight, clientHeight } = scrollRef.value |
| 27 | showFade.value = scrollTop + clientHeight < scrollHeight - 4 |
| 28 | } |
| 29 | |
| 30 | function 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 | |
| 39 | const sidebarPathOrder = flattenSidebarPaths(Array.isArray(sidebar) ? sidebar : Object.values(sidebar).flat()) |
| 40 | |
| 41 | function sidebarIndex(path: string): number { |
| 42 | const exact = sidebarPathOrder.indexOf(path) |
| 43 | return exact !== -1 ? exact : Infinity |
| 44 | } |
| 45 | |
| 46 | function 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 | |
| 55 | function 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 | |
| 67 | type PageGroup = { path: string; card: HomeCard | null; items: BookmarkItem[] } |
| 68 | type TopLevelEntry = { type: 'page'; path: string; card: HomeCard | null; items: BookmarkItem[] } |
| 69 | type FolderEntry = { type: 'folder'; section: string; parentCard: HomeCard | null; pages: PageGroup[] } |
| 70 | type GroupEntry = TopLevelEntry | FolderEntry |
| 71 | |
| 72 | const 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 | <BookMarked :size="128" :stroke-width="1" class="bookmarks-empty-icon" /> |
| 151 | <p class="bookmarks-empty-hint">Click the bookmark icon next to any heading in the table of contents to save it here.</p> |
| 152 | </div> |
| 153 | <div v-else class="bookmarks-groups"> |
| 154 | <template v-for="group in grouped" :key="group.type === 'page' ? group.path : group.section"> |
| 155 | |
| 156 | <!-- Top-level page (no folder) --> |
| 157 | <div v-if="group.type === 'page'" class="bookmarks-group"> |
| 158 | <h3 class="bookmarks-group-title"> |
| 159 | <span v-if="group.card" :class="cardIconToClass(group.card.icon)" class="bookmarks-group-icon" /> |
| 160 | <span v-else class="i-lucide:file-text bookmarks-group-icon" /> |
| 161 | <span class="bookmarks-group-text">{{ group.card?.title ?? group.path }}</span> |
| 162 | </h3> |
| 163 | <ul class="bookmarks-list"> |
| 164 | <li v-for="bm in group.items" :key="bm.anchor" class="bookmarks-item"> |
| 165 | <a class="bookmarks-item-link" :href="bm.path + '#' + bm.anchor" @click="isOpen = false"> |
| 166 | <Hash :size="12" :stroke-width="2" class="bookmarks-item-icon" /> |
| 167 | {{ bm.title }} |
| 168 | </a> |
| 169 | <button type="button" class="bookmarks-item-remove" :aria-label="`Remove bookmark for ${bm.title}`" @click.stop="remove(bm.anchor, bm.path)"> |
| 170 | <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> |
| 171 | </button> |
| 172 | </li> |
| 173 | </ul> |
| 174 | </div> |
| 175 | |
| 176 | <!-- Folder group (e.g. Guides, Glossary) --> |
| 177 | <div v-else class="bookmarks-group"> |
| 178 | <h3 class="bookmarks-group-title"> |
| 179 | <span v-if="group.parentCard" :class="cardIconToClass(group.parentCard.icon)" class="bookmarks-group-icon" /> |
| 180 | <span v-else class="i-lucide:folder bookmarks-group-icon" /> |
| 181 | <span class="bookmarks-group-text">{{ group.section }}</span> |
| 182 | </h3> |
| 183 | <div v-for="page in group.pages" :key="page.path" class="bookmarks-sub-group"> |
| 184 | <h4 class="bookmarks-sub-title">{{ page.card?.title ?? page.path }}</h4> |
| 185 | <ul class="bookmarks-list"> |
| 186 | <li v-for="bm in page.items" :key="bm.anchor" class="bookmarks-item"> |
| 187 | <a class="bookmarks-item-link" :href="bm.path + '#' + bm.anchor" @click="isOpen = false"> |
| 188 | <Hash :size="12" :stroke-width="2" class="bookmarks-item-icon" /> |
| 189 | {{ bm.title }} |
| 190 | </a> |
| 191 | <button type="button" class="bookmarks-item-remove" :aria-label="`Remove bookmark for ${bm.title}`" @click.stop="remove(bm.anchor, bm.path)"> |
| 192 | <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> |
| 193 | </button> |
| 194 | </li> |
| 195 | </ul> |
| 196 | </div> |
| 197 | </div> |
| 198 | |
| 199 | </template> |
| 200 | </div> |
| 201 | </div> |
| 202 | <Transition name="bookmarks-fade"> |
| 203 | <div v-if="showFade && bookmarks.length > 0" class="bookmarks-fade-overlay" /> |
| 204 | </Transition> |
| 205 | </div> |
| 206 | </Transition> |
| 207 | </div> |
| 208 | </template> |
| 209 | |
| 210 | <style scoped> |
| 211 | .bookmarks-wrapper { |
| 212 | position: relative; |
| 213 | width: 100%; |
| 214 | height: 100%; |
| 215 | display: flex; |
| 216 | align-items: center; |
| 217 | justify-content: center; |
| 218 | } |
| 219 | |
| 220 | .bookmarks-btn { |
| 221 | display: flex; |
| 222 | align-items: center; |
| 223 | justify-content: center; |
| 224 | width: 100%; |
| 225 | height: 100%; |
| 226 | color: var(--vp-c-text-1); |
| 227 | opacity: 0.55; |
| 228 | transition: |
| 229 | opacity 0.25s, |
| 230 | transform 0.2s cubic-bezier(0.16, 1, 0.3, 1); |
| 231 | } |
| 232 | |
| 233 | .bookmarks-btn:hover { opacity: 1; } |
| 234 | .bookmarks-btn:active { transform: scale(0.94); } |
| 235 | |
| 236 | .bookmarks-panel { |
| 237 | position: absolute; |
| 238 | top: calc(var(--vp-nav-height) / 2 + 20px); |
| 239 | right: 0; |
| 240 | z-index: 100; |
| 241 | width: 282px; |
| 242 | background-color: var(--vp-c-bg-elv); |
| 243 | border: 1px solid var(--vp-c-divider); |
| 244 | border-radius: 12px; |
| 245 | box-shadow: var(--vp-shadow-3); |
| 246 | transition: background-color 0.5s; |
| 247 | transform-origin: top right; |
| 248 | overflow: hidden; |
| 249 | } |
| 250 | |
| 251 | .bookmarks-scroll { |
| 252 | height: 400px; |
| 253 | overflow-y: auto; |
| 254 | overflow-x: hidden; |
| 255 | overscroll-behavior: contain; |
| 256 | padding: 20px; |
| 257 | scrollbar-width: none; |
| 258 | -ms-overflow-style: none; |
| 259 | } |
| 260 | |
| 261 | .bookmarks-scroll::-webkit-scrollbar { |
| 262 | display: none; |
| 263 | } |
| 264 | |
| 265 | .bookmarks-fade-overlay { |
| 266 | position: absolute; |
| 267 | bottom: 0; |
| 268 | left: 0; |
| 269 | right: 0; |
| 270 | height: 56px; |
| 271 | pointer-events: none; |
| 272 | background: linear-gradient( |
| 273 | to bottom, |
| 274 | transparent, |
| 275 | var(--vp-c-bg-elv) |
| 276 | ); |
| 277 | border-radius: 0 0 12px 12px; |
| 278 | } |
| 279 | |
| 280 | .bookmarks-empty { |
| 281 | display: flex; |
| 282 | flex-direction: column; |
| 283 | align-items: center; |
| 284 | justify-content: center; |
| 285 | min-height: 360px; |
| 286 | gap: 10px; |
| 287 | text-align: center; |
| 288 | overflow: hidden; |
| 289 | } |
| 290 | |
| 291 | .bookmarks-empty-icon { |
| 292 | color: var(--vp-c-text-3); |
| 293 | flex: none; |
| 294 | } |
| 295 | |
| 296 | .bookmarks-empty-hint { |
| 297 | font-size: 13px; |
| 298 | color: var(--vp-c-text-2); |
| 299 | line-height: 1.6; |
| 300 | margin: 0; |
| 301 | white-space: normal; |
| 302 | overflow-wrap: break-word; |
| 303 | word-break: break-word; |
| 304 | } |
| 305 | |
| 306 | .bookmarks-groups { |
| 307 | display: flex; |
| 308 | flex-direction: column; |
| 309 | gap: 8px; |
| 310 | } |
| 311 | |
| 312 | .bookmarks-sub-group { |
| 313 | margin-top: 4px; |
| 314 | } |
| 315 | |
| 316 | .bookmarks-sub-title { |
| 317 | font-size: 11px; |
| 318 | font-weight: 600; |
| 319 | letter-spacing: 0; |
| 320 | color: var(--vp-c-text-3); |
| 321 | margin: 6px 0 2px; |
| 322 | padding: 0 6px 4px; |
| 323 | user-select: none; |
| 324 | border-bottom: 1px solid #525252; |
| 325 | } |
| 326 | |
| 327 | .bookmarks-group-title { |
| 328 | display: flex; |
| 329 | align-items: center; |
| 330 | gap: 6px; |
| 331 | font-size: 16px; |
| 332 | font-weight: 500; |
| 333 | color: var(--vp-c-text-1); |
| 334 | margin: 0 0 2px; |
| 335 | padding: 2px 0; |
| 336 | user-select: none; |
| 337 | min-width: 0; |
| 338 | } |
| 339 | |
| 340 | .bookmarks-group-icon { |
| 341 | font-size: 16px; |
| 342 | color: var(--vp-c-text-1); |
| 343 | flex: none; |
| 344 | } |
| 345 | |
| 346 | .bookmarks-group-text { |
| 347 | flex: 1; |
| 348 | min-width: 0; |
| 349 | overflow: hidden; |
| 350 | white-space: nowrap; |
| 351 | text-overflow: ellipsis; |
| 352 | } |
| 353 | |
| 354 | .bookmarks-list { |
| 355 | list-style: none; |
| 356 | padding: 0; |
| 357 | margin: 0; |
| 358 | } |
| 359 | |
| 360 | .bookmarks-item { |
| 361 | display: flex; |
| 362 | align-items: center; |
| 363 | border-radius: 6px; |
| 364 | transition: background-color 0.15s; |
| 365 | } |
| 366 | |
| 367 | .bookmarks-item:hover { |
| 368 | background: var(--vp-c-default-soft); |
| 369 | } |
| 370 | |
| 371 | .bookmarks-item-link { |
| 372 | display: flex; |
| 373 | align-items: center; |
| 374 | gap: 5px; |
| 375 | flex: 1; |
| 376 | padding: 4px 6px; |
| 377 | font-size: 13px; |
| 378 | color: var(--vp-c-text-2); |
| 379 | text-decoration: none; |
| 380 | white-space: nowrap; |
| 381 | overflow: hidden; |
| 382 | text-overflow: ellipsis; |
| 383 | transition: color 0.15s; |
| 384 | min-width: 0; |
| 385 | } |
| 386 | |
| 387 | .bookmarks-item-link:hover { |
| 388 | color: var(--vp-c-brand-1); |
| 389 | } |
| 390 | |
| 391 | .bookmarks-item-icon { |
| 392 | flex: none; |
| 393 | color: var(--vp-c-text-3); |
| 394 | } |
| 395 | |
| 396 | .bookmarks-item-remove { |
| 397 | flex: none; |
| 398 | display: flex; |
| 399 | align-items: center; |
| 400 | justify-content: center; |
| 401 | width: 20px; |
| 402 | height: 20px; |
| 403 | margin-right: 4px; |
| 404 | border: none; |
| 405 | background: transparent; |
| 406 | color: var(--vp-c-text-3); |
| 407 | cursor: pointer; |
| 408 | transition: color 0.15s; |
| 409 | } |
| 410 | |
| 411 | .bookmarks-item-remove:hover { |
| 412 | color: var(--vp-c-text-1); |
| 413 | } |
| 414 | |
| 415 | .bookmarks-panel-enter-active, |
| 416 | .bookmarks-panel-leave-active { |
| 417 | transition: opacity 0.15s, transform 0.15s cubic-bezier(0.16, 1, 0.3, 1); |
| 418 | } |
| 419 | |
| 420 | .bookmarks-panel-enter-from, |
| 421 | .bookmarks-panel-leave-to { |
| 422 | opacity: 0; |
| 423 | transform: scale(0.95) translateY(-4px); |
| 424 | } |
| 425 | |
| 426 | .bookmarks-fade-enter-active, |
| 427 | .bookmarks-fade-leave-active { |
| 428 | transition: opacity 0.2s; |
| 429 | } |
| 430 | |
| 431 | .bookmarks-fade-enter-from, |
| 432 | .bookmarks-fade-leave-to { |
| 433 | opacity: 0; |
| 434 | } |
| 435 | |
| 436 | .bookmarks-mobile-header { |
| 437 | display: none; |
| 438 | } |
| 439 | |
| 440 | @media (max-width: 767px) { |
| 441 | .bookmarks-panel { |
| 442 | position: fixed; |
| 443 | top: 0; |
| 444 | left: 0; |
| 445 | right: 0; |
| 446 | bottom: 0; |
| 447 | width: 100%; |
| 448 | height: 100%; |
| 449 | border-radius: 0; |
| 450 | border: none; |
| 451 | z-index: 9999; |
| 452 | transform-origin: center top; |
| 453 | display: flex; |
| 454 | flex-direction: column; |
| 455 | } |
| 456 | |
| 457 | .bookmarks-scroll { |
| 458 | flex: 1; |
| 459 | height: auto; |
| 460 | min-height: 0; |
| 461 | } |
| 462 | |
| 463 | .bookmarks-empty { |
| 464 | min-height: 0; |
| 465 | flex: 1; |
| 466 | } |
| 467 | |
| 468 | .bookmarks-mobile-header { |
| 469 | display: flex; |
| 470 | align-items: center; |
| 471 | justify-content: space-between; |
| 472 | height: 56px; |
| 473 | flex-shrink: 0; |
| 474 | padding: 0 20px; |
| 475 | border-bottom: 1px solid var(--vp-c-divider); |
| 476 | } |
| 477 | |
| 478 | .bookmarks-mobile-title { |
| 479 | font-size: 16px; |
| 480 | font-weight: 600; |
| 481 | color: var(--vp-c-text-1); |
| 482 | } |
| 483 | |
| 484 | .bookmarks-mobile-close { |
| 485 | display: flex; |
| 486 | align-items: center; |
| 487 | justify-content: center; |
| 488 | width: 32px; |
| 489 | height: 32px; |
| 490 | border-radius: 8px; |
| 491 | border: none; |
| 492 | background: transparent; |
| 493 | color: var(--vp-c-text-2); |
| 494 | cursor: pointer; |
| 495 | transition: background-color 0.15s, color 0.15s; |
| 496 | } |
| 497 | |
| 498 | .bookmarks-mobile-close:hover { |
| 499 | background: var(--vp-c-default-soft); |
| 500 | color: var(--vp-c-text-1); |
| 501 | } |
| 502 | |
| 503 | .bookmarks-panel-enter-from, |
| 504 | .bookmarks-panel-leave-to { |
| 505 | opacity: 0; |
| 506 | transform: translateY(8px); |
| 507 | } |
| 508 | } |
| 509 | </style> |