Commit 0182ddb741
Unsigned
Layout: unified · split
docs/.vitepress/theme/Layout.vue +87 −4
| @@ -57,15 +57,93 @@ watch(() => route.path, () => { | ||
| 57 | 57 | }) |
| 58 | 58 | |
| 59 | 59 | let searchNavigated = false |
| 60 | let searchQuery = '' | |
| 60 | 61 | |
| 61 | 62 | if (!import.meta.env.SSR) { |
| 62 | useEventListener(window, 'search-nav', () => { | |
| 63 | useEventListener(window, 'search-nav', (event) => { | |
| 63 | 64 | searchNavigated = true |
| 65 | searchQuery = event instanceof CustomEvent && | |
| 66 | typeof event.detail?.query === 'string' | |
| 67 | ? event.detail.query | |
| 68 | : '' | |
| 64 | 69 | tryOpenCollapsibles() |
| 65 | 70 | }) |
| 66 | 71 | } |
| 67 | 72 | |
| 68 | const openCollapsibles = (target: HTMLElement) => { | |
| 73 | const nextFrame = () => | |
| 74 | new Promise<void>((resolve) => requestAnimationFrame(() => resolve())) | |
| 75 | ||
| 76 | const getSearchTerms = (query: string) => | |
| 77 | query | |
| 78 | .toLowerCase() | |
| 79 | .split(/\s+/) | |
| 80 | .map((term) => term.trim()) | |
| 81 | .filter(Boolean) | |
| 82 | ||
| 83 | const getTabsInSearchScope = (target: HTMLElement) => { | |
| 84 | const tabs = new Set<HTMLElement>() | |
| 85 | const closestTabs = target.closest<HTMLElement>('.plugin-tabs') | |
| 86 | if (closestTabs) tabs.add(closestTabs) | |
| 87 | ||
| 88 | const tagName = target.tagName | |
| 89 | if (tagName.length === 2 && tagName[0] === 'H') { | |
| 90 | const level = +tagName[1] | |
| 91 | if (level >= 1 && level <= 6) { | |
| 92 | let sibling = target.nextElementSibling | |
| 93 | ||
| 94 | while (sibling) { | |
| 95 | const siblingTag = sibling.tagName | |
| 96 | if ( | |
| 97 | siblingTag.length === 2 && siblingTag[0] === 'H' && | |
| 98 | +siblingTag[1] <= level | |
| 99 | ) break | |
| 100 | ||
| 101 | if (sibling.matches('.plugin-tabs')) tabs.add(sibling as HTMLElement) | |
| 102 | sibling.querySelectorAll<HTMLElement>('.plugin-tabs').forEach((tab) => { | |
| 103 | tabs.add(tab) | |
| 104 | }) | |
| 105 | ||
| 106 | sibling = sibling.nextElementSibling | |
| 107 | } | |
| 108 | } | |
| 109 | } | |
| 110 | ||
| 111 | return [...tabs] | |
| 112 | } | |
| 113 | ||
| 114 | const selectTabContainingQuery = async (target: HTMLElement, query: string) => { | |
| 115 | const terms = getSearchTerms(query) | |
| 116 | if (!terms.length) return | |
| 117 | ||
| 118 | for (const tabs of getTabsInSearchScope(target)) { | |
| 119 | const selectedTab = tabs.querySelector<HTMLButtonElement>( | |
| 120 | '.plugin-tabs--tab[aria-selected="true"]' | |
| 121 | ) | |
| 122 | const buttons = tabs.querySelectorAll<HTMLButtonElement>( | |
| 123 | '.plugin-tabs--tab[role="tab"]' | |
| 124 | ) | |
| 125 | ||
| 126 | for (let i = 0, len = buttons.length; i < len; i++) { | |
| 127 | const button = buttons[i] | |
| 128 | button.click() | |
| 129 | await nextTick() | |
| 130 | await nextFrame() | |
| 131 | ||
| 132 | const panelId = button.getAttribute('aria-controls') | |
| 133 | const panel = panelId | |
| 134 | ? document.getElementById(panelId) | |
| 135 | : tabs.querySelector<HTMLElement>('.plugin-tabs--content') | |
| 136 | const text = panel?.textContent?.toLowerCase() ?? '' | |
| 137 | ||
| 138 | if (terms.every((term) => text.includes(term))) return | |
| 139 | } | |
| 140 | ||
| 141 | selectedTab?.click() | |
| 142 | await nextTick() | |
| 143 | } | |
| 144 | } | |
| 145 | ||
| 146 | const openCollapsibles = async (target: HTMLElement) => { | |
| 69 | 147 | let scrollTarget = target |
| 70 | 148 | let el: HTMLElement | null = target |
| 71 | 149 | |
| @@ -104,6 +182,8 @@ const openCollapsibles = (target: HTMLElement) => { | ||
| 104 | 182 | } |
| 105 | 183 | } |
| 106 | 184 | |
| 185 | await selectTabContainingQuery(target, searchQuery) | |
| 186 | ||
| 107 | 187 | requestAnimationFrame(() => { |
| 108 | 188 | requestAnimationFrame(() => { |
| 109 | 189 | const navBar = document.querySelector<HTMLElement>('.VPNavBar') |
| @@ -138,6 +218,7 @@ const tryOpenCollapsibles = async () => { | ||
| 138 | 218 | if (target) { |
| 139 | 219 | openCollapsibles(target) |
| 140 | 220 | searchNavigated = false |
| 221 | searchQuery = '' | |
| 141 | 222 | } |
| 142 | 223 | }) |
| 143 | 224 | } |
| @@ -289,7 +370,9 @@ onMounted(() => { | ||
| 289 | 370 | document.body.appendChild(tooltipEl) |
| 290 | 371 | |
| 291 | 372 | const showTooltip = (e: MouseEvent) => { |
| 292 | const target = (e.target as HTMLElement).closest('.icon-tip') as HTMLElement | null | |
| 373 | const target = (e.target as HTMLElement).closest('.icon-tip') as | |
| 374 | | HTMLElement | |
| 375 | | null | |
| 293 | 376 | if (!target) return |
| 294 | 377 | const tip = target.dataset.tip |
| 295 | 378 | if (!tip) return |
| @@ -527,4 +610,4 @@ onUnmounted(() => { | ||
| 527 | 610 | </span> |
| 528 | 611 | </template> |
| 529 | 612 | </Layout> |
| 530 | </template> | |
| \ No newline at end of file | ||
| 613 | </template> | |
docs/.vitepress/theme/components/Search.vue +78 −10
| @@ -43,6 +43,7 @@ import { | ||
| 43 | 43 | TextAlignStart |
| 44 | 44 | } from 'lucide-vue-next' |
| 45 | 45 | import Mark from 'mark.js/dist/mark.es6.js' |
| 46 | import { enhanceAppWithTabs } from 'vitepress-plugin-tabs/client' | |
| 46 | 47 | import { LRUCache } from '../composables/search/lru-cache' |
| 47 | 48 | import { createSearchTranslate } from '../composables/search/translation' |
| 48 | 49 | import { useData } from '../composables/search/use-data' |
| @@ -214,20 +215,22 @@ watchDebounced( | ||
| 214 | 215 | |
| 215 | 216 | // Highlighting |
| 216 | 217 | const mods = showDetailedListValue |
| 217 | ? await Promise.all(results.value.map((r) => fetchExcerpt(r.id))) | |
| 218 | ? await Promise.all(_result.map((r) => fetchExcerpt(r.id))) | |
| 218 | 219 | : [] |
| 219 | 220 | if (canceled) return |
| 220 | 221 | for (const { id, mod } of mods) { |
| 221 | 222 | const mapId = id.slice(0, id.indexOf('#')) |
| 222 | let map = cache.get(mapId) | |
| 223 | const cacheId = `${mapId}\n${filterTextValue}` | |
| 224 | let map = cache.get(cacheId) | |
| 223 | 225 | if (map) continue |
| 224 | 226 | map = new Map() |
| 225 | cache.set(mapId, map) | |
| 227 | cache.set(cacheId, map) | |
| 226 | 228 | const comp = mod.default ?? mod |
| 227 | 229 | if (comp?.render || comp?.setup) { |
| 228 | 230 | const app = createApp(comp) |
| 229 | 231 | // Silence warnings about missing components |
| 230 | 232 | app.config.warnHandler = () => {} |
| 233 | enhanceAppWithTabs(app) | |
| 231 | 234 | app.provide(dataSymbol, vitePressData) |
| 232 | 235 | Object.defineProperties(app.config.globalProperties, { |
| 233 | 236 | $frontmatter: { |
| @@ -243,6 +246,7 @@ watchDebounced( | ||
| 243 | 246 | }) |
| 244 | 247 | const div = document.createElement('div') |
| 245 | 248 | app.mount(div) |
| 249 | await selectExcerptTabs(div, filterTextValue) | |
| 246 | 250 | const headings = div.querySelectorAll('h1, h2, h3, h4, h5, h6') |
| 247 | 251 | headings.forEach((el) => { |
| 248 | 252 | const href = el.querySelector('a')?.getAttribute('href') |
| @@ -266,7 +270,7 @@ watchDebounced( | ||
| 266 | 270 | |
| 267 | 271 | results.value = _result.map((r) => { |
| 268 | 272 | const [id, anchor] = r.id.split('#') |
| 269 | const map = cache.get(id) | |
| 273 | const map = cache.get(`${id}\n${filterTextValue}`) | |
| 270 | 274 | const text = map?.get(anchor) ?? '' |
| 271 | 275 | for (const term in r.match) { |
| 272 | 276 | terms.add(term) |
| @@ -290,13 +294,17 @@ watchDebounced( | ||
| 290 | 294 | for (let i = 0; i < excerpts.length; i++) { |
| 291 | 295 | const excerptElement = excerpts[i] as HTMLElement |
| 292 | 296 | |
| 293 | const markNode = excerptElement.querySelector('mark[data-markjs="true"]') as HTMLElement | null | |
| 297 | const markNode = excerptElement.querySelector( | |
| 298 | 'mark[data-markjs="true"]' | |
| 299 | ) as HTMLElement | null | |
| 294 | 300 | if (markNode) { |
| 295 | 301 | const markRect = markNode.getBoundingClientRect() |
| 296 | 302 | const excerptRect = excerptElement.getBoundingClientRect() |
| 297 | 303 | |
| 298 | const relativeTop = (markRect.top - excerptRect.top) + excerptElement.scrollTop | |
| 299 | const targetScrollTop = relativeTop - (excerptElement.clientHeight / 2) + (markRect.height / 2) | |
| 304 | const relativeTop = (markRect.top - excerptRect.top) + | |
| 305 | excerptElement.scrollTop | |
| 306 | const targetScrollTop = relativeTop - | |
| 307 | (excerptElement.clientHeight / 2) + (markRect.height / 2) | |
| 300 | 308 | |
| 301 | 309 | scrollMutations.push({ |
| 302 | 310 | element: excerptElement, |
| @@ -334,6 +342,58 @@ async function fetchExcerpt(id: string) { | ||
| 334 | 342 | } |
| 335 | 343 | } |
| 336 | 344 | |
| 345 | const nextFrame = () => | |
| 346 | new Promise<void>((resolve) => requestAnimationFrame(() => resolve())) | |
| 347 | ||
| 348 | const getSearchTerms = (query: string) => | |
| 349 | query | |
| 350 | .toLowerCase() | |
| 351 | .split(/\s+/) | |
| 352 | .map((term) => term.trim()) | |
| 353 | .filter(Boolean) | |
| 354 | ||
| 355 | async function selectExcerptTabs(root: HTMLElement, query: string) { | |
| 356 | const terms = getSearchTerms(query) | |
| 357 | if (!terms.length) return | |
| 358 | ||
| 359 | const tabs = root.querySelectorAll<HTMLElement>('.plugin-tabs') | |
| 360 | ||
| 361 | for (let i = 0; i < tabs.length; i++) { | |
| 362 | const tab = tabs[i] | |
| 363 | const selectedTab = tab.querySelector<HTMLButtonElement>( | |
| 364 | '.plugin-tabs--tab[aria-selected="true"]' | |
| 365 | ) | |
| 366 | const buttons = tab.querySelectorAll<HTMLButtonElement>( | |
| 367 | '.plugin-tabs--tab[role="tab"]' | |
| 368 | ) | |
| 369 | ||
| 370 | let found = false | |
| 371 | ||
| 372 | for (let j = 0; j < buttons.length; j++) { | |
| 373 | const button = buttons[j] | |
| 374 | button.click() | |
| 375 | await nextTick() | |
| 376 | await nextFrame() | |
| 377 | ||
| 378 | const panelId = button.getAttribute('aria-controls') | |
| 379 | const panel = panelId | |
| 380 | ? root.querySelector<HTMLElement>(`#${CSS.escape(panelId)}`) | |
| 381 | : tab.querySelector<HTMLElement>('.plugin-tabs--content') | |
| 382 | const text = panel?.textContent?.toLowerCase() ?? '' | |
| 383 | ||
| 384 | if (terms.every((term) => text.includes(term))) { | |
| 385 | found = true | |
| 386 | break | |
| 387 | } | |
| 388 | } | |
| 389 | ||
| 390 | if (!found) { | |
| 391 | selectedTab?.click() | |
| 392 | await nextTick() | |
| 393 | } | |
| 394 | } | |
| 395 | } | |
| 396 | ||
| 337 | 397 | /* Search input focus */ |
| 338 | 398 | |
| 339 | 399 | const searchInput = ref<HTMLInputElement>() |
| @@ -417,14 +477,22 @@ onKeyStroke('Enter', (e) => { | ||
| 417 | 477 | } |
| 418 | 478 | |
| 419 | 479 | if (selectedPackage) { |
| 420 | window.dispatchEvent(new CustomEvent('search-nav')) | |
| 480 | window.dispatchEvent( | |
| 481 | new CustomEvent('search-nav', { | |
| 482 | detail: { query: filterText.value } | |
| 483 | }) | |
| 484 | ) | |
| 421 | 485 | router.go(selectedPackage.id) |
| 422 | 486 | showSearch.value = false |
| 423 | 487 | } |
| 424 | 488 | }) |
| 425 | 489 | |
| 426 | 490 | function onResultClick() { |
| 427 | window.dispatchEvent(new CustomEvent('search-nav')) | |
| 491 | window.dispatchEvent( | |
| 492 | new CustomEvent('search-nav', { | |
| 493 | detail: { query: filterText.value } | |
| 494 | }) | |
| 495 | ) | |
| 428 | 496 | showSearch.value = false |
| 429 | 497 | } |
| 430 | 498 | |
| @@ -1140,4 +1208,4 @@ svg { | ||
| 1140 | 1208 | background-color: rgba(var(--vp-c-brand-1), 0.1); |
| 1141 | 1209 | border-radius: 4px; |
| 1142 | 1210 | } |
| 1143 | </style> | |
| \ No newline at end of file | ||
| 1211 | </style> | |