Commit cbbdbc2b3f
Unregistered key
Layout: unified · split
docs/.vitepress/theme/Layout.vue +63 −1
| @@ -17,7 +17,7 @@ import type { DefaultTheme as Theme } from 'vitepress' | ||
| 17 | 17 | import DefaultTheme from 'vitepress/theme' |
| 18 | 18 | import VPSidebarGroup from 'vitepress/dist/client/theme-default/components/VPSidebarGroup.vue' |
| 19 | 19 | import { getSidebarGroups } from 'vitepress/dist/client/theme-default/support/sidebar' |
| 20 | import { computed, onMounted, onUnmounted, ref, watch } from 'vue' | |
| 20 | import { computed, nextTick, onMounted, onUnmounted, ref, watch } from 'vue' | |
| 21 | 21 | import { sidebar } from '../configs/constants' |
| 22 | 22 | import AnnouncementPill from './components/AnnouncementPill.vue' |
| 23 | 23 | import NotFoundComponent from './components/NotFound.vue' |
| @@ -45,6 +45,52 @@ 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 | |
| 51 | ||
| 52 | const target = document.getElementById(decodeURIComponent(hash)) || document.getElementById(hash) | |
| 53 | if (!target) return | |
| 54 | ||
| 55 | let el: HTMLElement | null = target | |
| 56 | while ((el = el.closest('details'))) { | |
| 57 | el.open = true | |
| 58 | el = el.parentElement | |
| 59 | } | |
| 60 | ||
| 61 | if (/^H[1-6]$/.test(target.tagName)) { | |
| 62 | const level = parseInt(target.tagName[1]) | |
| 63 | let sibling = target.nextElementSibling | |
| 64 | ||
| 65 | while (sibling) { | |
| 66 | if (/^H[1-6]$/.test(sibling.tagName)) { | |
| 67 | if (parseInt(sibling.tagName[1]) <= level) break | |
| 68 | } | |
| 69 | ||
| 70 | if (sibling.tagName === 'DETAILS') { | |
| 71 | (sibling as HTMLDetailsElement).open = true | |
| 72 | } | |
| 73 | ||
| 74 | const nested = sibling.querySelectorAll('details') | |
| 75 | for (let i = 0; i < nested.length; i++) { | |
| 76 | nested[i].open = true | |
| 77 | } | |
| 78 | ||
| 79 | sibling = sibling.nextElementSibling | |
| 80 | } | |
| 81 | } | |
| 82 | } | |
| 83 | ||
| 84 | watch(() => route.data, async () => { | |
| 85 | await nextTick() | |
| 86 | openCollapsibles() | |
| 87 | }) | |
| 88 | ||
| 89 | watch(() => route.hash, async () => { | |
| 90 | await nextTick() | |
| 91 | openCollapsibles() | |
| 92 | }) | |
| 93 | ||
| 48 | 94 | function onSidebarEnter() { |
| 49 | 95 | sidebarRef.value?.focus() |
| 50 | 96 | } |
| @@ -171,6 +217,22 @@ const reloadTakodachi = () => { | ||
| 171 | 217 | |
| 172 | 218 | onMounted(() => { |
| 173 | 219 | 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 | }) | |
| 174 | 236 | |
| 175 | 237 | // Arrow key scrolling + Escape close for sidebars and content |
| 176 | 238 | let activeScrollTarget: HTMLElement | Window = window |