Skip to content

Commit 4c6ded2

Browse files
committed
fix: article outline scrolling issue on chrome
#26
1 parent 13ce189 commit 4c6ded2

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

package-lock.json

+7-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
"dependencies": {
1414
"@astrojs/mdx": "^2.0.0",
1515
"@fortawesome/fontawesome-free": "^6.4.0",
16+
"@types/lodash": "^4.14.202",
1617
"astro": "^4.0.3",
1718
"autoprefixer": "^10.4.14",
1819
"axios": "^1.6.0",
1920
"dayjs": "^1.11.9",
2021
"fuse.js": "^6.6.2",
2122
"highlight.js": "^11.8.0",
2223
"jsdom": "^22.1.0",
24+
"lodash": "^4.17.21",
2325
"mermaid": "^10.2.4",
2426
"playwright": "^1.36.1",
2527
"rehype-mdx-code-props": "^1.0.0",

src/layouts/WikiArticle.astro

+12-10
Original file line numberDiff line numberDiff line change
@@ -130,39 +130,41 @@ export interface Props {
130130
</script>
131131

132132
<script>
133+
import lodash from "lodash";
134+
133135
const headings = document.querySelectorAll(
134136
"article .heading,.article-metadata h1"
135137
);
136138
const outline = document.querySelector("#outline .content");
137139

140+
let topLink: HTMLAnchorElement;
138141
const intersectionObserver = new IntersectionObserver((entries) => {
139-
let topLink;
140-
141142
for (const entry of entries) {
142143
const outlineLink = outline?.querySelector(
143144
`a[href="#${entry.target.id}"]`
144145
);
145146

146147
if (entry.intersectionRatio > 0) {
147148
outlineLink?.classList.add("in-view");
148-
topLink = outlineLink as HTMLDivElement;
149+
topLink = outlineLink as HTMLAnchorElement;
149150
} else {
150151
outlineLink?.classList.remove("in-view");
151152
}
152153
}
154+
});
153155

154-
const outlineHasScroll =
155-
(outline?.scrollHeight ?? 0) > (outline?.clientHeight ?? 0);
156+
headings.forEach((heading) => intersectionObserver.observe(heading));
156157

157-
if (outlineHasScroll && topLink) {
158+
document.addEventListener(
159+
"scroll",
160+
lodash.debounce(() => {
161+
console.warn("INVOKE");
158162
outline?.scrollTo({
159163
top: topLink.offsetTop - outline.clientHeight / 2,
160164
behavior: "smooth",
161165
});
162-
}
163-
});
164-
165-
headings.forEach((heading) => intersectionObserver.observe(heading));
166+
}, 10)
167+
);
166168
</script>
167169

168170
<style lang="scss">

0 commit comments

Comments
 (0)