Skip to content

Commit 4020b41

Browse files
committed
Revert "fix(theme): misaligned outline indicator\"
This reverts commit 910b212.
1 parent d16e580 commit 4020b41

File tree

3 files changed

+34
-40
lines changed

3 files changed

+34
-40
lines changed

src/client/app/router.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import { reactive, inject, markRaw, nextTick, readonly } from 'vue'
12
import type { Component, InjectionKey } from 'vue'
2-
import { inject, markRaw, nextTick, reactive, readonly } from 'vue'
3-
import type { Awaitable, PageData, PageDataPayload } from '../shared'
43
import { notFoundPageData, treatAsHtml } from '../shared'
4+
import type { PageData, PageDataPayload, Awaitable } from '../shared'
5+
import { inBrowser, withBase } from './utils'
56
import { siteDataRef } from './data'
6-
import { getScrollOffset, inBrowser, withBase } from './utils'
77

88
export interface Route {
99
path: string
@@ -261,14 +261,34 @@ export function scrollTo(el: Element, hash: string, smooth = false) {
261261
}
262262

263263
if (target) {
264+
let scrollOffset = siteDataRef.value.scrollOffset
265+
let offset = 0
266+
let padding = 24
267+
if (typeof scrollOffset === 'object' && 'padding' in scrollOffset) {
268+
padding = scrollOffset.padding
269+
scrollOffset = scrollOffset.selector
270+
}
271+
if (typeof scrollOffset === 'number') {
272+
offset = scrollOffset
273+
} else if (typeof scrollOffset === 'string') {
274+
offset = tryOffsetSelector(scrollOffset, padding)
275+
} else if (Array.isArray(scrollOffset)) {
276+
for (const selector of scrollOffset) {
277+
const res = tryOffsetSelector(selector, padding)
278+
if (res) {
279+
offset = res
280+
break
281+
}
282+
}
283+
}
264284
const targetPadding = parseInt(
265285
window.getComputedStyle(target).paddingTop,
266286
10
267287
)
268288
const targetTop =
269289
window.scrollY +
270290
target.getBoundingClientRect().top -
271-
getScrollOffset() +
291+
offset +
272292
targetPadding
273293
function scrollToTarget() {
274294
// only smooth scroll if distance is smaller than screen height.
@@ -280,6 +300,14 @@ export function scrollTo(el: Element, hash: string, smooth = false) {
280300
}
281301
}
282302

303+
function tryOffsetSelector(selector: string, padding: number): number {
304+
const el = document.querySelector(selector)
305+
if (!el) return 0
306+
const bot = el.getBoundingClientRect().bottom
307+
if (bot < 0) return 0
308+
return bot + padding
309+
}
310+
283311
function handleHMR(route: Route): void {
284312
// update route.data on HMR updates of active page
285313
if (import.meta.hot) {

src/client/app/utils.ts

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -107,36 +107,3 @@ export function defineClientComponent(
107107
}
108108
}
109109
}
110-
111-
export function getScrollOffset() {
112-
let scrollOffset = siteDataRef.value.scrollOffset
113-
let offset = 0
114-
let padding = 24
115-
if (typeof scrollOffset === 'object' && 'padding' in scrollOffset) {
116-
padding = scrollOffset.padding
117-
scrollOffset = scrollOffset.selector
118-
}
119-
if (typeof scrollOffset === 'number') {
120-
offset = scrollOffset
121-
} else if (typeof scrollOffset === 'string') {
122-
offset = tryOffsetSelector(scrollOffset, padding)
123-
} else if (Array.isArray(scrollOffset)) {
124-
for (const selector of scrollOffset) {
125-
const res = tryOffsetSelector(selector, padding)
126-
if (res) {
127-
offset = res
128-
break
129-
}
130-
}
131-
}
132-
133-
return offset
134-
}
135-
136-
function tryOffsetSelector(selector: string, padding: number): number {
137-
const el = document.querySelector(selector)
138-
if (!el) return 0
139-
const bot = el.getBoundingClientRect().bottom
140-
if (bot < 0) return 0
141-
return bot + padding
142-
}

src/client/theme-default/composables/outline.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import type { DefaultTheme } from 'vitepress/theme'
22
import { onMounted, onUnmounted, onUpdated, type Ref } from 'vue'
3-
import { getScrollOffset } from '../../app/utils'
43
import type { Header } from '../../shared'
5-
import { throttleAndDebounce } from '../support/utils'
64
import { useAside } from './aside'
5+
import { throttleAndDebounce } from '../support/utils'
76

87
// cached list of anchor elements from resolveHeaders
98
const resolvedHeaders: { element: HTMLHeadElement; link: string }[] = []
@@ -180,7 +179,7 @@ export function useActiveAnchor(
180179
// find the last header above the top of viewport
181180
let activeLink: string | null = null
182181
for (const { link, top } of headers) {
183-
if (top > scrollY + offsetDocTop + getScrollOffset()) {
182+
if (top > scrollY + offsetDocTop) {
184183
break
185184
}
186185
activeLink = link

0 commit comments

Comments
 (0)