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 | let searchNavigated = false | 59 | let searchNavigated = false |
| 60 | let searchQuery = '' | ||
| 60 | 61 | ||
| 61 | if (!import.meta.env.SSR) { | 62 | if (!import.meta.env.SSR) { |
| 62 | useEventListener(window, 'search-nav', () => { | 63 | useEventListener(window, 'search-nav', (event) => { |
| 63 | searchNavigated = true | 64 | searchNavigated = true |
| 65 | searchQuery = event instanceof CustomEvent && | ||
| 66 | typeof event.detail?.query === 'string' | ||
| 67 | ? event.detail.query | ||
| 68 | : '' | ||
| 64 | tryOpenCollapsibles() | 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 | let scrollTarget = target | 147 | let scrollTarget = target |
| 70 | let el: HTMLElement | null = target | 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 | requestAnimationFrame(() => { | 187 | requestAnimationFrame(() => { |
| 108 | requestAnimationFrame(() => { | 188 | requestAnimationFrame(() => { |
| 109 | const navBar = document.querySelector<HTMLElement>('.VPNavBar') | 189 | const navBar = document.querySelector<HTMLElement>('.VPNavBar') |
| @@ -138,6 +218,7 @@ const tryOpenCollapsibles = async () => { | |||
| 138 | if (target) { | 218 | if (target) { |
| 139 | openCollapsibles(target) | 219 | openCollapsibles(target) |
| 140 | searchNavigated = false | 220 | searchNavigated = false |
| 221 | searchQuery = '' | ||
| 141 | } | 222 | } |
| 142 | }) | 223 | }) |
| 143 | } | 224 | } |
| @@ -289,7 +370,9 @@ onMounted(() => { | |||
| 289 | document.body.appendChild(tooltipEl) | 370 | document.body.appendChild(tooltipEl) |
| 290 | 371 | ||
| 291 | const showTooltip = (e: MouseEvent) => { | 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 | if (!target) return | 376 | if (!target) return |
| 294 | const tip = target.dataset.tip | 377 | const tip = target.dataset.tip |
| 295 | if (!tip) return | 378 | if (!tip) return |
| @@ -527,4 +610,4 @@ onUnmounted(() => { | |||
| 527 | </span> | 610 | </span> |
| 528 | </template> | 611 | </template> |
| 529 | </Layout> | 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 | TextAlignStart | 43 | TextAlignStart |
| 44 | } from 'lucide-vue-next' | 44 | } from 'lucide-vue-next' |
| 45 | import Mark from 'mark.js/dist/mark.es6.js' | 45 | import Mark from 'mark.js/dist/mark.es6.js' |
| 46 | import { enhanceAppWithTabs } from 'vitepress-plugin-tabs/client' | ||
| 46 | import { LRUCache } from '../composables/search/lru-cache' | 47 | import { LRUCache } from '../composables/search/lru-cache' |
| 47 | import { createSearchTranslate } from '../composables/search/translation' | 48 | import { createSearchTranslate } from '../composables/search/translation' |
| 48 | import { useData } from '../composables/search/use-data' | 49 | import { useData } from '../composables/search/use-data' |
| @@ -214,20 +215,22 @@ watchDebounced( | |||
| 214 | 215 | ||
| 215 | // Highlighting | 216 | // Highlighting |
| 216 | const mods = showDetailedListValue | 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 | if (canceled) return | 220 | if (canceled) return |
| 220 | for (const { id, mod } of mods) { | 221 | for (const { id, mod } of mods) { |
| 221 | const mapId = id.slice(0, id.indexOf('#')) | 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 | if (map) continue | 225 | if (map) continue |
| 224 | map = new Map() | 226 | map = new Map() |
| 225 | cache.set(mapId, map) | 227 | cache.set(cacheId, map) |
| 226 | const comp = mod.default ?? mod | 228 | const comp = mod.default ?? mod |
| 227 | if (comp?.render || comp?.setup) { | 229 | if (comp?.render || comp?.setup) { |
| 228 | const app = createApp(comp) | 230 | const app = createApp(comp) |
| 229 | // Silence warnings about missing components | 231 | // Silence warnings about missing components |
| 230 | app.config.warnHandler = () => {} | 232 | app.config.warnHandler = () => {} |
| 233 | enhanceAppWithTabs(app) | ||
| 231 | app.provide(dataSymbol, vitePressData) | 234 | app.provide(dataSymbol, vitePressData) |
| 232 | Object.defineProperties(app.config.globalProperties, { | 235 | Object.defineProperties(app.config.globalProperties, { |
| 233 | $frontmatter: { | 236 | $frontmatter: { |
| @@ -243,6 +246,7 @@ watchDebounced( | |||
| 243 | }) | 246 | }) |
| 244 | const div = document.createElement('div') | 247 | const div = document.createElement('div') |
| 245 | app.mount(div) | 248 | app.mount(div) |
| 249 | await selectExcerptTabs(div, filterTextValue) | ||
| 246 | const headings = div.querySelectorAll('h1, h2, h3, h4, h5, h6') | 250 | const headings = div.querySelectorAll('h1, h2, h3, h4, h5, h6') |
| 247 | headings.forEach((el) => { | 251 | headings.forEach((el) => { |
| 248 | const href = el.querySelector('a')?.getAttribute('href') | 252 | const href = el.querySelector('a')?.getAttribute('href') |
| @@ -266,7 +270,7 @@ watchDebounced( | |||
| 266 | 270 | ||
| 267 | results.value = _result.map((r) => { | 271 | results.value = _result.map((r) => { |
| 268 | const [id, anchor] = r.id.split('#') | 272 | const [id, anchor] = r.id.split('#') |
| 269 | const map = cache.get(id) | 273 | const map = cache.get(`${id}\n${filterTextValue}`) |
| 270 | const text = map?.get(anchor) ?? '' | 274 | const text = map?.get(anchor) ?? '' |
| 271 | for (const term in r.match) { | 275 | for (const term in r.match) { |
| 272 | terms.add(term) | 276 | terms.add(term) |
| @@ -290,13 +294,17 @@ watchDebounced( | |||
| 290 | for (let i = 0; i < excerpts.length; i++) { | 294 | for (let i = 0; i < excerpts.length; i++) { |
| 291 | const excerptElement = excerpts[i] as HTMLElement | 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 | if (markNode) { | 300 | if (markNode) { |
| 295 | const markRect = markNode.getBoundingClientRect() | 301 | const markRect = markNode.getBoundingClientRect() |
| 296 | const excerptRect = excerptElement.getBoundingClientRect() | 302 | const excerptRect = excerptElement.getBoundingClientRect() |
| 297 | 303 | ||
| 298 | const relativeTop = (markRect.top - excerptRect.top) + excerptElement.scrollTop | 304 | const relativeTop = (markRect.top - excerptRect.top) + |
| 299 | const targetScrollTop = relativeTop - (excerptElement.clientHeight / 2) + (markRect.height / 2) | 305 | excerptElement.scrollTop |
| 306 | const targetScrollTop = relativeTop - | ||
| 307 | (excerptElement.clientHeight / 2) + (markRect.height / 2) | ||
| 300 | 308 | ||
| 301 | scrollMutations.push({ | 309 | scrollMutations.push({ |
| 302 | element: excerptElement, | 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 | /* Search input focus */ | 397 | /* Search input focus */ |
| 338 | 398 | ||
| 339 | const searchInput = ref<HTMLInputElement>() | 399 | const searchInput = ref<HTMLInputElement>() |
| @@ -417,14 +477,22 @@ onKeyStroke('Enter', (e) => { | |||
| 417 | } | 477 | } |
| 418 | 478 | ||
| 419 | if (selectedPackage) { | 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 | router.go(selectedPackage.id) | 485 | router.go(selectedPackage.id) |
| 422 | showSearch.value = false | 486 | showSearch.value = false |
| 423 | } | 487 | } |
| 424 | }) | 488 | }) |
| 425 | 489 | ||
| 426 | function onResultClick() { | 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 | showSearch.value = false | 496 | showSearch.value = false |
| 429 | } | 497 | } |
| 430 | 498 | ||
| @@ -1140,4 +1208,4 @@ svg { | |||
| 1140 | background-color: rgba(var(--vp-c-brand-1), 0.1); | 1208 | background-color: rgba(var(--vp-c-brand-1), 0.1); |
| 1141 | border-radius: 4px; | 1209 | border-radius: 4px; |
| 1142 | } | 1210 | } |
| 1143 | </style>\ No newline at end of file | 1211 | </style> |