Commit 4051b72b2a
Unsigned
Layout: unified · split
docs/.vitepress/theme/components/Search.vue +24 −8
| @@ -286,14 +286,30 @@ watchDebounced( | |||
| 286 | }) | 286 | }) |
| 287 | 287 | ||
| 288 | const excerpts = el.value?.querySelectorAll('.result .excerpt') ?? [] | 288 | const excerpts = el.value?.querySelectorAll('.result .excerpt') ?? [] |
| 289 | // @ts-expect-error | 289 | const scrollMutations: { element: HTMLElement; scrollTop: number }[] = [] |
| 290 | for (const excerpt of excerpts) { | 290 | for (let i = 0; i < excerpts.length; i++) { |
| 291 | excerpt | 291 | const excerptElement = excerpts[i] as HTMLElement |
| 292 | .querySelector('mark[data-markjs="true"]') | 292 | |
| 293 | ?.scrollIntoView({ block: 'center' }) | 293 | const markNode = excerptElement.querySelector('mark[data-markjs="true"]') as HTMLElement | null |
| 294 | if (markNode) { | ||
| 295 | const markRect = markNode.getBoundingClientRect() | ||
| 296 | const excerptRect = excerptElement.getBoundingClientRect() | ||
| 297 | |||
| 298 | const relativeTop = (markRect.top - excerptRect.top) + excerptElement.scrollTop | ||
| 299 | const targetScrollTop = relativeTop - (excerptElement.clientHeight / 2) + (markRect.height / 2) | ||
| 300 | |||
| 301 | scrollMutations.push({ | ||
| 302 | element: excerptElement, | ||
| 303 | scrollTop: targetScrollTop | ||
| 304 | }) | ||
| 305 | } | ||
| 306 | } | ||
| 307 | for (let i = 0; i < scrollMutations.length; i++) { | ||
| 308 | scrollMutations[i].element.scrollTop = scrollMutations[i].scrollTop | ||
| 309 | } | ||
| 310 | if (resultsEl.value) { | ||
| 311 | resultsEl.value.scrollTop = 0 | ||
| 294 | } | 312 | } |
| 295 | // FIXME: without this whole page scrolls to the bottom | ||
| 296 | resultsEl.value?.firstElementChild?.scrollIntoView({ block: 'start' }) | ||
| 297 | }, | 313 | }, |
| 298 | { debounce: 180, immediate: true } | 314 | { debounce: 180, immediate: true } |
| 299 | ) | 315 | ) |
| @@ -1118,4 +1134,4 @@ svg { | |||
| 1118 | background-color: rgba(var(--vp-c-brand-1), 0.1); | 1134 | background-color: rgba(var(--vp-c-brand-1), 0.1); |
| 1119 | border-radius: 4px; | 1135 | border-radius: 4px; |
| 1120 | } | 1136 | } |
| 1121 | </style> | 1137 | </style>\ No newline at end of file |