Commit ce307903f9
Unregistered key
Layout: unified · split
docs/.vitepress/theme/Layout.vue +64 −43
| @@ -45,51 +45,88 @@ watch(() => route.path, () => { | |||
| 45 | homeSidebarOpen.value = false | 45 | homeSidebarOpen.value = false |
| 46 | }) | 46 | }) |
| 47 | 47 | ||
| 48 | const openCollapsibles = () => { | 48 | let searchNavigated = false |
| 49 | const hash = window.location.hash.slice(1) | ||
| 50 | if (!hash) return | ||
| 51 | 49 | ||
| 52 | const target = document.getElementById(decodeURIComponent(hash)) || document.getElementById(hash) | 50 | if (!import.meta.env.SSR) { |
| 53 | if (!target) return | 51 | useEventListener(window, 'search-nav', () => { |
| 52 | searchNavigated = true | ||
| 53 | tryOpenCollapsibles() | ||
| 54 | }) | ||
| 55 | } | ||
| 54 | 56 | ||
| 57 | const openCollapsibles = (target: HTMLElement) => { | ||
| 58 | let scrollTarget = target | ||
| 55 | let el: HTMLElement | null = target | 59 | let el: HTMLElement | null = target |
| 60 | |||
| 56 | while ((el = el.closest('details'))) { | 61 | while ((el = el.closest('details'))) { |
| 57 | el.open = true | 62 | el.open = true |
| 63 | scrollTarget = el | ||
| 58 | el = el.parentElement | 64 | el = el.parentElement |
| 59 | } | 65 | } |
| 60 | 66 | ||
| 61 | if (/^H[1-6]$/.test(target.tagName)) { | 67 | const tagName = target.tagName |
| 62 | const level = parseInt(target.tagName[1]) | 68 | if (tagName.length === 2 && tagName[0] === 'H') { |
| 63 | let sibling = target.nextElementSibling | 69 | const level = +tagName[1] |
| 70 | if (level >= 1 && level <= 6) { | ||
| 71 | let sibling = target.nextElementSibling | ||
| 64 | 72 | ||
| 65 | while (sibling) { | 73 | while (sibling) { |
| 66 | if (/^H[1-6]$/.test(sibling.tagName)) { | 74 | const siblingTag = sibling.tagName |
| 67 | if (parseInt(sibling.tagName[1]) <= level) break | 75 | if (siblingTag.length === 2 && siblingTag[0] === 'H' && +siblingTag[1] <= level) break |
| 68 | } | ||
| 69 | 76 | ||
| 70 | if (sibling.tagName === 'DETAILS') { | 77 | if (siblingTag === 'DETAILS') { |
| 71 | (sibling as HTMLDetailsElement).open = true | 78 | (sibling as HTMLDetailsElement).open = true |
| 72 | } | 79 | if (scrollTarget === target) scrollTarget = sibling as HTMLElement |
| 80 | } | ||
| 73 | 81 | ||
| 74 | const nested = sibling.querySelectorAll('details') | 82 | const nested = sibling.getElementsByTagName('details') |
| 75 | for (let i = 0; i < nested.length; i++) { | 83 | for (let i = 0, len = nested.length; i < len; i++) { |
| 76 | nested[i].open = true | 84 | nested[i].open = true |
| 77 | } | 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 | await nextTick() | 109 | await nextTick() |
| 86 | openCollapsibles() | ||
| 87 | }) | ||
| 88 | 110 | ||
| 89 | watch(() => route.hash, async () => { | 111 | requestAnimationFrame(() => { |
| 90 | await nextTick() | 112 | const hash = window.location.hash.slice(1) |
| 91 | openCollapsibles() | 113 | if (!hash) { |
| 92 | }) | 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 | function onSidebarEnter() { | 131 | function onSidebarEnter() { |
| 95 | sidebarRef.value?.focus() | 132 | sidebarRef.value?.focus() |
| @@ -217,22 +254,6 @@ const reloadTakodachi = () => { | |||
| 217 | 254 | ||
| 218 | onMounted(() => { | 255 | onMounted(() => { |
| 219 | if (import.meta.env.SSR) return | 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 | // Arrow key scrolling + Escape close for sidebars and content | 258 | // Arrow key scrolling + Escape close for sidebars and content |
| 238 | let activeScrollTarget: HTMLElement | Window = window | 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 | if (selectedPackage) { | 419 | if (selectedPackage) { |
| 420 | window.dispatchEvent(new CustomEvent('search-nav')) | ||
| 420 | router.go(selectedPackage.id) | 421 | router.go(selectedPackage.id) |
| 421 | showSearch.value = false | 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 | onKeyStroke('Escape', () => { | 431 | onKeyStroke('Escape', () => { |
| 426 | showSearch.value = false | 432 | showSearch.value = false |
| 427 | }) | 433 | }) |
| @@ -723,7 +729,7 @@ function onMouseMove(e: MouseEvent) { | |||
| 723 | @mouseenter="!disableMouseOver && | 729 | @mouseenter="!disableMouseOver && |
| 724 | (selectedIndex = index + 1)" | 730 | (selectedIndex = index + 1)" |
| 725 | @focusin="selectedIndex = index + 1" | 731 | @focusin="selectedIndex = index + 1" |
| 726 | @click="showSearch = false" | 732 | @click="onResultClick" |
| 727 | :data-index="index + 1" | 733 | :data-index="index + 1" |
| 728 | > | 734 | > |
| 729 | <div> | 735 | <div> |