Commit d2a6817e0c
Unsigned
Layout: unified · split
docs/.vitepress/theme/Layout.vue +59
| @@ -103,6 +103,52 @@ const getScrollTargetForAnchor = (target: HTMLElement) => { | ||
| 103 | 103 | return target |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | const getHeadingAnchor = (heading: HTMLElement) => { | |
| 107 | const href = heading.querySelector<HTMLAnchorElement>('a[href^="#"]') | |
| 108 | ?.getAttribute('href') | |
| 109 | ||
| 110 | if (href?.startsWith('#') && href.length > 1) return href.slice(1) | |
| 111 | ||
| 112 | return heading.id || undefined | |
| 113 | } | |
| 114 | ||
| 115 | const getSelectedTabHeadingAnchor = (tabs: HTMLElement) => { | |
| 116 | const selectedTab = tabs.querySelector<HTMLButtonElement>( | |
| 117 | '.plugin-tabs--tab[aria-selected="true"]' | |
| 118 | ) | |
| 119 | const panelId = selectedTab?.getAttribute('aria-controls') | |
| 120 | const panel = panelId | |
| 121 | ? document.getElementById(panelId) | |
| 122 | : tabs.querySelector<HTMLElement>('.plugin-tabs--content') | |
| 123 | const heading = panel?.querySelector<HTMLElement>('.tab-search-heading') | |
| 124 | ||
| 125 | return heading ? getHeadingAnchor(heading) : undefined | |
| 126 | } | |
| 127 | ||
| 128 | const updateHashForSelectedTab = async (tabs: HTMLElement) => { | |
| 129 | await nextTick() | |
| 130 | await nextFrame() | |
| 131 | ||
| 132 | const anchor = getSelectedTabHeadingAnchor(tabs) | |
| 133 | if (!anchor || window.location.hash.slice(1) === anchor) return | |
| 134 | ||
| 135 | window.history.pushState( | |
| 136 | null, | |
| 137 | '', | |
| 138 | `${window.location.pathname}${window.location.search}#${anchor}` | |
| 139 | ) | |
| 140 | } | |
| 141 | ||
| 142 | const queueHashUpdateForTabSelection = (target: EventTarget | null) => { | |
| 143 | if (!(target instanceof HTMLElement)) return | |
| 144 | ||
| 145 | const button = target.closest<HTMLButtonElement>( | |
| 146 | '.plugin-tabs--tab[role="tab"]' | |
| 147 | ) | |
| 148 | const tabs = button?.closest<HTMLElement>('.plugin-tabs') | |
| 149 | if (tabs) void updateHashForSelectedTab(tabs) | |
| 150 | } | |
| 151 | ||
| 106 | 152 | const scrollToElement = (target: HTMLElement, smooth = true) => { |
| 107 | 153 | requestAnimationFrame(() => { |
| 108 | 154 | requestAnimationFrame(() => { |
| @@ -502,6 +548,19 @@ onMounted(() => { | ||
| 502 | 548 | document.addEventListener('mouseout', hideTooltip) |
| 503 | 549 | document.addEventListener('scroll', hideTooltip, true) |
| 504 | 550 | |
| 551 | useEventListener(document, 'click', (e: MouseEvent) => { | |
| 552 | if (!e.isTrusted) return | |
| 553 | queueHashUpdateForTabSelection(e.target) | |
| 554 | }) | |
| 555 | ||
| 556 | useEventListener(document, 'keydown', (e: KeyboardEvent) => { | |
| 557 | if (!e.isTrusted || (e.key !== 'ArrowLeft' && e.key !== 'ArrowRight')) { | |
| 558 | return | |
| 559 | } | |
| 560 | ||
| 561 | queueHashUpdateForTabSelection(e.target) | |
| 562 | }) | |
| 563 | ||
| 505 | 564 | onUnmounted(() => { |
| 506 | 565 | document.removeEventListener('mouseover', showTooltip) |
| 507 | 566 | document.removeEventListener('mouseout', hideTooltip) |