Commit 90e81c5c31
Unsigned
Layout: unified · split
docs/.vitepress/theme/Layout.vue +28 −26
| @@ -65,7 +65,7 @@ if (!import.meta.env.SSR) { | |||
| 65 | }) | 65 | }) |
| 66 | } | 66 | } |
| 67 | 67 | ||
| 68 | const openCollapsibles = (target: HTMLElement) => { | 68 | const openCollapsibles = (target: HTMLElement, ancestorOnly = false) => { |
| 69 | let scrollTarget = target | 69 | let scrollTarget = target |
| 70 | let el: HTMLElement | null = target | 70 | let el: HTMLElement | null = target |
| 71 | 71 | ||
| @@ -75,31 +75,33 @@ const openCollapsibles = (target: HTMLElement) => { | |||
| 75 | el = el.parentElement | 75 | el = el.parentElement |
| 76 | } | 76 | } |
| 77 | 77 | ||
| 78 | const tagName = target.tagName | 78 | if (!ancestorOnly) { |
| 79 | if (tagName.length === 2 && tagName[0] === 'H') { | 79 | const tagName = target.tagName |
| 80 | const level = +tagName[1] | 80 | if (tagName.length === 2 && tagName[0] === 'H') { |
| 81 | if (level >= 1 && level <= 6) { | 81 | const level = +tagName[1] |
| 82 | let sibling = target.nextElementSibling | 82 | if (level >= 1 && level <= 6) { |
| 83 | 83 | let sibling = target.nextElementSibling | |
| 84 | while (sibling) { | 84 | |
| 85 | const siblingTag = sibling.tagName | 85 | while (sibling) { |
| 86 | if ( | 86 | const siblingTag = sibling.tagName |
| 87 | siblingTag.length === 2 && siblingTag[0] === 'H' && | 87 | if ( |
| 88 | +siblingTag[1] <= level | 88 | siblingTag.length === 2 && siblingTag[0] === 'H' && |
| 89 | ) break | 89 | +siblingTag[1] <= level |
| 90 | 90 | ) break | |
| 91 | if (siblingTag === 'DETAILS') { | 91 | |
| 92 | ;(sibling as HTMLDetailsElement).open = true | 92 | if (siblingTag === 'DETAILS') { |
| 93 | if (scrollTarget === target) scrollTarget = sibling as HTMLElement | 93 | ;(sibling as HTMLDetailsElement).open = true |
| 94 | if (scrollTarget === target) scrollTarget = sibling as HTMLElement | ||
| 95 | } | ||
| 96 | |||
| 97 | const nested = sibling.getElementsByTagName('details') | ||
| 98 | for (let i = 0, len = nested.length; i < len; i++) { | ||
| 99 | nested[i].open = true | ||
| 100 | if (scrollTarget === target) scrollTarget = nested[i] | ||
| 101 | } | ||
| 102 | |||
| 103 | sibling = sibling.nextElementSibling | ||
| 94 | } | 104 | } |
| 95 | |||
| 96 | const nested = sibling.getElementsByTagName('details') | ||
| 97 | for (let i = 0, len = nested.length; i < len; i++) { | ||
| 98 | nested[i].open = true | ||
| 99 | if (scrollTarget === target) scrollTarget = nested[i] | ||
| 100 | } | ||
| 101 | |||
| 102 | sibling = sibling.nextElementSibling | ||
| 103 | } | 105 | } |
| 104 | } | 106 | } |
| 105 | } | 107 | } |
| @@ -136,7 +138,7 @@ const tryOpenCollapsibles = async () => { | |||
| 136 | const target = document.getElementById(id) || document.getElementById(hash) | 138 | const target = document.getElementById(id) || document.getElementById(hash) |
| 137 | 139 | ||
| 138 | if (target) { | 140 | if (target) { |
| 139 | openCollapsibles(target) | 141 | openCollapsibles(target, true) |
| 140 | searchNavigated = false | 142 | searchNavigated = false |
| 141 | } | 143 | } |
| 142 | }) | 144 | }) |
docs/.vitepress/theme/components/Collapsible.vue +24 −1
| @@ -1,11 +1,21 @@ | |||
| 1 | <script setup lang="ts"> | 1 | <script setup lang="ts"> |
| 2 | import { ref } from 'vue' | 2 | import { computed, ref } from 'vue' |
| 3 | 3 | ||
| 4 | const props = defineProps<{ | 4 | const props = defineProps<{ |
| 5 | title?: string | 5 | title?: string |
| 6 | }>() | 6 | }>() |
| 7 | 7 | ||
| 8 | const open = ref(false) | 8 | const open = ref(false) |
| 9 | |||
| 10 | const headingId = computed(() => | ||
| 11 | props.title | ||
| 12 | ? props.title | ||
| 13 | .toLowerCase() | ||
| 14 | .trim() | ||
| 15 | .replace(/\s+/g, '-') | ||
| 16 | .replace(/[^\w-]/g, '') | ||
| 17 | : undefined | ||
| 18 | ) | ||
| 9 | </script> | 19 | </script> |
| 10 | 20 | ||
| 11 | <template> | 21 | <template> |
| @@ -15,6 +25,7 @@ const open = ref(false) | |||
| 15 | <span v-else class="i-iconoir-nav-arrow-right collapsible-icon" /> | 25 | <span v-else class="i-iconoir-nav-arrow-right collapsible-icon" /> |
| 16 | {{ title || 'Details' }} | 26 | {{ title || 'Details' }} |
| 17 | </summary> | 27 | </summary> |
| 28 | <h3 v-if="headingId" :id="headingId" class="collapsible-anchor" aria-hidden="true">{{ title }}</h3> | ||
| 18 | <slot /> | 29 | <slot /> |
| 19 | </details> | 30 | </details> |
| 20 | </template> | 31 | </template> |
| @@ -37,4 +48,16 @@ summary::-webkit-details-marker { | |||
| 37 | height: 1em; | 48 | height: 1em; |
| 38 | flex-shrink: 0; | 49 | flex-shrink: 0; |
| 39 | } | 50 | } |
| 51 | |||
| 52 | .collapsible-anchor { | ||
| 53 | position: absolute; | ||
| 54 | width: 1px; | ||
| 55 | height: 1px; | ||
| 56 | overflow: hidden; | ||
| 57 | clip: rect(0, 0, 0, 0); | ||
| 58 | white-space: nowrap; | ||
| 59 | border: 0; | ||
| 60 | margin: 0; | ||
| 61 | padding: 0; | ||
| 62 | } | ||
| 40 | </style>\ No newline at end of file | 63 | </style>\ No newline at end of file |