Commit ce307903f9
Unregistered key
Layout: unified · split
docs/.vitepress/theme/Layout.vue +64 −43
| @@ -45,51 +45,88 @@ watch(() => route.path, () => { | ||
| 45 | 45 | homeSidebarOpen.value = false |
| 46 | 46 | }) |
| 47 | 47 | |
| 48 | const openCollapsibles = () => { | |
| 49 | const hash = window.location.hash.slice(1) | |
| 50 | if (!hash) return | |
| 48 | let searchNavigated = false | |
| 51 | 49 | |
| 52 | const target = document.getElementById(decodeURIComponent(hash)) || document.getElementById(hash) | |
| 53 | if (!target) return | |
| 50 | if (!import.meta.env.SSR) { | |
| 51 | useEventListener(window, 'search-nav', () => { | |
| 52 | searchNavigated = true | |
| 53 | tryOpenCollapsibles() | |
| 54 | }) | |
| 55 | } | |
| 54 | 56 | |
| 57 | const openCollapsibles = (target: HTMLElement) => { | |
| 58 | let scrollTarget = target | |
| 55 | 59 | let el: HTMLElement | null = target |
| 60 | ||
| 56 | 61 | while ((el = el.closest('details'))) { |
| 57 | 62 | el.open = true |
| 63 | scrollTarget = el | |
| 58 | 64 | el = el.parentElement |
| 59 | 65 | } |
| 60 | 66 | |
| 61 | if (/^H[1-6]$/.test(target.tagName)) { | |
| 62 | const level = parseInt(target.tagName[1]) | |
| 63 | let sibling = target.nextElementSibling | |
| 67 | const tagName = target.tagName | |
| 68 | if (tagName.length === 2 && tagName[0] === 'H') { | |
| 69 | const level = +tagName[1] | |
| 70 | if (level >= 1 && level <= 6) { | |
| 71 | let sibling = target.nextElementSibling | |
| 64 | 72 | |
| 65 | while (sibling) { | |
| 66 | if (/^H[1-6]$/.test(sibling.tagName)) { | |
| 67 | if (parseInt(sibling.tagName[1]) <= level) break | |
| 68 | } | |
| 73 | while (sibling) { | |
| 74 | const siblingTag = sibling.tagName | |
| 75 | if (siblingTag.length === 2 && siblingTag[0] === 'H' && +siblingTag[1] <= level) break | |
| 69 | 76 | |
| 70 | if (sibling.tagName === 'DETAILS') { | |
| 71 | (sibling as HTMLDetailsElement).open = true | |
| 72 | } | |
| 77 | if (siblingTag === 'DETAILS') { | |
| 78 | (sibling as HTMLDetailsElement).open = true | |
| 79 | if (scrollTarget === target) scrollTarget = sibling as HTMLElement | |
| 80 | } | |
| 73 | 81 | |
| 74 | const nested = sibling.querySelectorAll('details') | |
| 75 | for (let i = 0; i < nested.length; i++) { | |
| 76 | nested[i].open = true | |
| 77 | } | |
| 82 | const nested = sibling.getElementsByTagName('details') | |
| 83 | for (let i = 0, len = nested.length; i < len; i++) { | |
| 84 | nested[i].open = true | |
| 85 | if (scrollTarget === target) scrollTarget = nested[i] | |
| 86 | } | |
| 78 | 87 | |
| 79 | sibling = sibling.nextElementSibling | |
| 88 | sibling = sibling.nextElementSibling | |
| 89 | } | |
| 80 | 90 | } |
| 81 | 91 | } |
| 92 | ||
| 93 | requestAnimationFrame(() => { | |
| 94 | requestAnimationFrame(() => { | |
| 95 | const navBar = document.querySelector<HTMLElement>('.VPNavBar') | |
| 96 | const navOffset = navBar ? navBar.offsetHeight : 0 | |
| 97 | window.scrollTo({ | |
| 98 | top: scrollTarget.getBoundingClientRect().top + window.scrollY - navOffset, | |
| 99 | behavior: window.matchMedia('(prefers-reduced-motion: reduce)').matches ? 'auto' : 'smooth' | |
| 100 | }) | |
| 101 | scrollTarget.setAttribute('tabindex', '-1') | |
| 102 | scrollTarget.focus({ preventScroll: true }) | |
| 103 | }) | |
| 104 | }) | |
| 82 | 105 | } |
| 83 | 106 | |
| 84 | watch(() => route.data, async () => { | |
| 107 | const tryOpenCollapsibles = async () => { | |
| 108 | if (!searchNavigated) return | |
| 85 | 109 | await nextTick() |
| 86 | openCollapsibles() | |
| 87 | }) | |
| 88 | 110 | |
| 89 | watch(() => route.hash, async () => { | |
| 90 | await nextTick() | |
| 91 | openCollapsibles() | |
| 92 | }) | |
| 111 | requestAnimationFrame(() => { | |
| 112 | const hash = window.location.hash.slice(1) | |
| 113 | if (!hash) { | |
| 114 | searchNavigated = false | |
| 115 | return | |
| 116 | } | |
| 117 | ||
| 118 | const id = decodeURIComponent(hash) | |
| 119 | const target = document.getElementById(id) || document.getElementById(hash) | |
| 120 | ||
| 121 | if (target) { | |
| 122 | openCollapsibles(target) | |
| 123 | searchNavigated = false | |
| 124 | } | |
| 125 | }) | |
| 126 | } | |
| 127 | ||
| 128 | watch(() => route.data, tryOpenCollapsibles, { flush: 'post' }) | |
| 129 | watch(() => route.hash, tryOpenCollapsibles, { flush: 'post' }) | |
| 93 | 130 | |
| 94 | 131 | function onSidebarEnter() { |
| 95 | 132 | sidebarRef.value?.focus() |
| @@ -217,22 +254,6 @@ const reloadTakodachi = () => { | ||
| 217 | 254 | |
| 218 | 255 | onMounted(() => { |
| 219 | 256 | if (import.meta.env.SSR) return |
| 220 | useEventListener(document, 'click', async (e: MouseEvent) => { | |
| 221 | if ((e.target as HTMLElement).closest('a.result')) { | |
| 222 | await nextTick() | |
| 223 | requestAnimationFrame(() => requestAnimationFrame(openCollapsibles)) | |
| 224 | } | |
| 225 | }) | |
| 226 | ||
| 227 | useEventListener(document, 'keydown', async (e: KeyboardEvent) => { | |
| 228 | if (e.key === 'Enter') { | |
| 229 | const searchBox = document.querySelector('.VPLocalSearchBox') || document.querySelector('.VPNavBarSearch') | |
| 230 | if (searchBox && searchBox.contains(e.target as Node)) { | |
| 231 | await nextTick() | |
| 232 | requestAnimationFrame(() => requestAnimationFrame(openCollapsibles)) | |
| 233 | } | |
| 234 | } | |
| 235 | }) | |
| 236 | 257 | |
| 237 | 258 | // Arrow key scrolling + Escape close for sidebars and content |
| 238 | 259 | let activeScrollTarget: HTMLElement | Window = window |
docs/.vitepress/theme/components/Search.vue +7 −1
| @@ -417,11 +417,17 @@ onKeyStroke('Enter', (e) => { | ||
| 417 | 417 | } |
| 418 | 418 | |
| 419 | 419 | if (selectedPackage) { |
| 420 | window.dispatchEvent(new CustomEvent('search-nav')) | |
| 420 | 421 | router.go(selectedPackage.id) |
| 421 | 422 | showSearch.value = false |
| 422 | 423 | } |
| 423 | 424 | }) |
| 424 | 425 | |
| 426 | function onResultClick() { | |
| 427 | window.dispatchEvent(new CustomEvent('search-nav')) | |
| 428 | showSearch.value = false | |
| 429 | } | |
| 430 | ||
| 425 | 431 | onKeyStroke('Escape', () => { |
| 426 | 432 | showSearch.value = false |
| 427 | 433 | }) |
| @@ -723,7 +729,7 @@ function onMouseMove(e: MouseEvent) { | ||
| 723 | 729 | @mouseenter="!disableMouseOver && |
| 724 | 730 | (selectedIndex = index + 1)" |
| 725 | 731 | @focusin="selectedIndex = index + 1" |
| 726 | @click="showSearch = false" | |
| 732 | @click="onResultClick" | |
| 727 | 733 | :data-index="index + 1" |
| 728 | 734 | > |
| 729 | 735 | <div> |