Commit ad888df47b
Unsigned
Layout: unified · split
docs/.vitepress/theme/Layout.vue +122 −3
| @@ -172,6 +172,7 @@ const updateQueryForSelectedTab = async (tabs: HTMLElement) => { | |||
| 172 | if (nextUrl === currentUrl) return | 172 | if (nextUrl === currentUrl) return |
| 173 | 173 | ||
| 174 | window.history.pushState(null, '', nextUrl) | 174 | window.history.pushState(null, '', nextUrl) |
| 175 | rewriteAnchorLinks() | ||
| 175 | } | 176 | } |
| 176 | 177 | ||
| 177 | const queueTabQueryUpdateForSelection = (target: EventTarget | null) => { | 178 | const queueTabQueryUpdateForSelection = (target: EventTarget | null) => { |
| @@ -184,6 +185,51 @@ const queueTabQueryUpdateForSelection = (target: EventTarget | null) => { | |||
| 184 | if (tabs) void updateQueryForSelectedTab(tabs) | 185 | if (tabs) void updateQueryForSelectedTab(tabs) |
| 185 | } | 186 | } |
| 186 | 187 | ||
| 188 | const TAB_LINK_MARKER = 'tab-' | ||
| 189 | |||
| 190 | const rewriteAnchorLinks = () => { | ||
| 191 | const origin = window.location.origin | ||
| 192 | const pathname = window.location.pathname | ||
| 193 | |||
| 194 | document | ||
| 195 | .querySelectorAll<HTMLAnchorElement>('.VPDoc a[href]') | ||
| 196 | .forEach((link) => { | ||
| 197 | // Accept either the original `#…` form or a previously-rewritten absolute URL. | ||
| 198 | const raw = link.getAttribute('href') ?? '' | ||
| 199 | let fragment: string | null = null | ||
| 200 | if (raw.startsWith('#') && raw.length > 1) { | ||
| 201 | fragment = raw.slice(1) | ||
| 202 | } else if (raw.startsWith(origin + pathname + '#')) { | ||
| 203 | fragment = raw.slice((origin + pathname + '#').length) | ||
| 204 | } else if (raw.startsWith(origin + pathname + '?')) { | ||
| 205 | // Already rewritten as a tab link — re-derive the tab name and re-stamp. | ||
| 206 | const qs = raw.slice((origin + pathname + '?').length).split('#')[0] | ||
| 207 | const tabName = new URLSearchParams(qs).get(TAB_QUERY_PARAM) | ||
| 208 | if (tabName) { | ||
| 209 | const href = `${origin}${pathname}?${TAB_QUERY_PARAM}=${encodeURIComponent(tabName)}` | ||
| 210 | if (link.getAttribute('href') !== href) link.setAttribute('href', href) | ||
| 211 | } | ||
| 212 | return | ||
| 213 | } else { | ||
| 214 | return | ||
| 215 | } | ||
| 216 | |||
| 217 | const isTabLink = fragment.startsWith(TAB_LINK_MARKER) | ||
| 218 | const name = (isTabLink | ||
| 219 | ? fragment.slice(TAB_LINK_MARKER.length) | ||
| 220 | : fragment).trim() | ||
| 221 | if (!name) return | ||
| 222 | |||
| 223 | // Use absolute URLs so the browser's Copy Link has nothing to merge with | ||
| 224 | // the current ?tabs= query. | ||
| 225 | const href = isTabLink | ||
| 226 | ? `${origin}${pathname}?${TAB_QUERY_PARAM}=${encodeURIComponent(name)}` | ||
| 227 | : `${origin}${pathname}#${name}` | ||
| 228 | |||
| 229 | if (link.getAttribute('href') !== href) link.setAttribute('href', href) | ||
| 230 | }) | ||
| 231 | } | ||
| 232 | |||
| 187 | const selectTabsByPath = async (tabPath: string[]) => { | 233 | const selectTabsByPath = async (tabPath: string[]) => { |
| 188 | let root: ParentNode = document | 234 | let root: ParentNode = document |
| 189 | let selectedTabs: HTMLElement | undefined | 235 | let selectedTabs: HTMLElement | undefined |
| @@ -358,6 +404,19 @@ const tryOpenAnchoredContent = async () => { | |||
| 358 | return | 404 | return |
| 359 | } | 405 | } |
| 360 | 406 | ||
| 407 | const isTabRelatedTarget = | ||
| 408 | target.classList.contains('tab-search-heading') || | ||
| 409 | Boolean(target.closest('.plugin-tabs')) | ||
| 410 | |||
| 411 | if ( | ||
| 412 | !isTabRelatedTarget && | ||
| 413 | new URLSearchParams(window.location.search).has(TAB_QUERY_PARAM) | ||
| 414 | ) { | ||
| 415 | window.history.replaceState(null, '', buildUrlWithTabs([], hash)) | ||
| 416 | scrollToElement(getScrollTargetForAnchor(target), false) | ||
| 417 | return | ||
| 418 | } | ||
| 419 | |||
| 361 | if ( | 420 | if ( |
| 362 | !requestedTabs.length && target.classList.contains('tab-search-heading') | 421 | !requestedTabs.length && target.classList.contains('tab-search-heading') |
| 363 | ) { | 422 | ) { |
| @@ -379,9 +438,19 @@ const tryOpenAnchoredContent = async () => { | |||
| 379 | } | 438 | } |
| 380 | } | 439 | } |
| 381 | 440 | ||
| 441 | const rewriteAnchorLinksDeferred = () => | ||
| 442 | nextTick(() => rewriteAnchorLinks()) | ||
| 443 | |||
| 382 | onMounted(tryOpenAnchoredContent) | 444 | onMounted(tryOpenAnchoredContent) |
| 383 | watch(() => route.data, tryOpenAnchoredContent, { flush: 'post' }) | 445 | onMounted(rewriteAnchorLinksDeferred) |
| 384 | watch(() => route.query, tryOpenAnchoredContent, { flush: 'post' }) | 446 | watch(() => route.data, () => { |
| 447 | void tryOpenAnchoredContent() | ||
| 448 | rewriteAnchorLinksDeferred() | ||
| 449 | }, { flush: 'post' }) | ||
| 450 | watch(() => route.query, () => { | ||
| 451 | void tryOpenAnchoredContent() | ||
| 452 | rewriteAnchorLinksDeferred() | ||
| 453 | }, { flush: 'post' }) | ||
| 385 | watch(() => route.hash, tryOpenAnchoredContent, { flush: 'post' }) | 454 | watch(() => route.hash, tryOpenAnchoredContent, { flush: 'post' }) |
| 386 | 455 | ||
| 387 | function onSidebarEnter() { | 456 | function onSidebarEnter() { |
| @@ -575,6 +644,55 @@ onMounted(() => { | |||
| 575 | document.addEventListener('mouseout', hideTooltip) | 644 | document.addEventListener('mouseout', hideTooltip) |
| 576 | document.addEventListener('scroll', hideTooltip, true) | 645 | document.addEventListener('scroll', hideTooltip, true) |
| 577 | 646 | ||
| 647 | const onCaptureClick = (e: MouseEvent) => { | ||
| 648 | if (!e.isTrusted) return | ||
| 649 | const link = (e.target as HTMLElement).closest<HTMLAnchorElement>('a[href]') | ||
| 650 | if (!link) return | ||
| 651 | const raw = link.getAttribute('href') ?? '' | ||
| 652 | |||
| 653 | // ── Tab link: #tab-name or already-rewritten /path?tabs=name ────────── | ||
| 654 | const tabFromHash = raw.startsWith('#' + TAB_LINK_MARKER) | ||
| 655 | ? raw.slice(1 + TAB_LINK_MARKER.length) | ||
| 656 | : null | ||
| 657 | const tabFromQuery = | ||
| 658 | !tabFromHash && raw.includes(`?${TAB_QUERY_PARAM}=`) && !raw.includes('#') | ||
| 659 | ? new URLSearchParams(raw.split('?')[1]).get(TAB_QUERY_PARAM) | ||
| 660 | : null | ||
| 661 | const tabName = tabFromHash ?? tabFromQuery | ||
| 662 | |||
| 663 | if (tabName) { | ||
| 664 | e.preventDefault() | ||
| 665 | e.stopImmediatePropagation() | ||
| 666 | window.history.pushState(null, '', buildUrlWithTabs([tabName])) | ||
| 667 | rewriteAnchorLinks() | ||
| 668 | void (async () => { | ||
| 669 | const tabs = await selectTabsByPath([tabName]) | ||
| 670 | if (tabs) scrollToElement(getSelectedTabScrollTarget(tabs), false) | ||
| 671 | })() | ||
| 672 | return | ||
| 673 | } | ||
| 674 | |||
| 675 | // ── Plain heading link: #name or already-rewritten /path#name ───────── | ||
| 676 | // Only intercept when ?tabs= is present — otherwise let the router handle normally. | ||
| 677 | if (!new URLSearchParams(window.location.search).has(TAB_QUERY_PARAM)) return | ||
| 678 | |||
| 679 | let hash: string | null = null | ||
| 680 | if (raw.startsWith('#') && raw.length > 1) { | ||
| 681 | hash = raw.slice(1) | ||
| 682 | } else if (raw.startsWith(window.location.pathname + '#')) { | ||
| 683 | hash = raw.slice(window.location.pathname.length + 1) | ||
| 684 | } | ||
| 685 | if (!hash) return | ||
| 686 | |||
| 687 | e.preventDefault() | ||
| 688 | e.stopImmediatePropagation() | ||
| 689 | window.history.pushState(null, '', `${window.location.pathname}#${hash}`) | ||
| 690 | rewriteAnchorLinks() | ||
| 691 | void tryOpenAnchoredContent() | ||
| 692 | } | ||
| 693 | |||
| 694 | document.addEventListener('click', onCaptureClick, { capture: true }) | ||
| 695 | |||
| 578 | useEventListener(document, 'click', (e: MouseEvent) => { | 696 | useEventListener(document, 'click', (e: MouseEvent) => { |
| 579 | if (!e.isTrusted) return | 697 | if (!e.isTrusted) return |
| 580 | queueTabQueryUpdateForSelection(e.target) | 698 | queueTabQueryUpdateForSelection(e.target) |
| @@ -589,6 +707,7 @@ onMounted(() => { | |||
| 589 | }) | 707 | }) |
| 590 | 708 | ||
| 591 | onUnmounted(() => { | 709 | onUnmounted(() => { |
| 710 | document.removeEventListener('click', onCaptureClick, { capture: true }) | ||
| 592 | document.removeEventListener('mouseover', showTooltip) | 711 | document.removeEventListener('mouseover', showTooltip) |
| 593 | document.removeEventListener('mouseout', hideTooltip) | 712 | document.removeEventListener('mouseout', hideTooltip) |
| 594 | document.removeEventListener('scroll', hideTooltip, true) | 713 | document.removeEventListener('scroll', hideTooltip, true) |
| @@ -799,4 +918,4 @@ onUnmounted(() => { | |||
| 799 | </span> | 918 | </span> |
| 800 | </template> | 919 | </template> |
| 801 | </Layout> | 920 | </Layout> |
| 802 | </template> | 921 | </template>\ No newline at end of file |