Skip to content

Commit b24e1bd

Browse files
committed
fix: Prevent initial unnecessary IntersectionObserver callback execution
1 parent c18df4c commit b24e1bd

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

src/core/event/index.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export function Events(Base) {
4545
// =========================================================================
4646
/**
4747
* Initialize cover observer
48-
* Toggles sticky behavior when when cover is not in view
48+
* Toggles sticky behavior when cover is not in view
4949
* @void
5050
*/
5151
#initCover() {
@@ -74,11 +74,17 @@ export function Events(Base) {
7474
#initHeadings() {
7575
const headingElms = dom.findAll('#main :where(h1, h2, h3, h4, h5)');
7676
const headingsInView = new Set();
77+
let isInitialLoad = true;
7778

7879
// Mark sidebar active item on heading intersection
7980
this.#intersectionObserver?.disconnect();
8081
this.#intersectionObserver = new IntersectionObserver(
8182
entries => {
83+
if (isInitialLoad) {
84+
isInitialLoad = false;
85+
return;
86+
}
87+
8288
if (this.#isScrolling) {
8389
return;
8490
}
@@ -89,18 +95,16 @@ export function Events(Base) {
8995
headingsInView[op](entry.target);
9096
}
9197

92-
const activeHeading =
93-
headingsInView.size > 1
94-
? // Sort headings by proximity to viewport top and select first
95-
Array.from(headingsInView).sort((a, b) =>
96-
a.compareDocumentPosition(b) &
97-
Node.DOCUMENT_POSITION_FOLLOWING
98-
? -1
99-
: 1,
100-
)[0]
101-
: // Get first and only item in set.
102-
// May be undefined if no headings are in view.
103-
headingsInView.values().next().value;
98+
let activeHeading = undefined;
99+
if (headingsInView.size === 1) {
100+
activeHeading = headingsInView.values().next().value;
101+
} else if (headingsInView.size > 1) {
102+
activeHeading = Array.from(headingsInView).reduce((closest, current) => {
103+
return !closest || (closest.compareDocumentPosition(current) & Node.DOCUMENT_POSITION_FOLLOWING)
104+
? current
105+
: closest;
106+
}, null);
107+
}
104108

105109
if (activeHeading) {
106110
const id = activeHeading.getAttribute('id');

src/core/render/compiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ export class Compiler {
181181
}
182182

183183
/**
184-
* Compile sidebar, it uses _sidebar.md ( or specific file) or the content's headings toc to render sidebar.
184+
* Compile sidebar, it uses _sidebar.md (or specific file) or the content's headings toc to render sidebar.
185185
* @param {String} text Text content from the sidebar file, maybe empty
186186
* @param {Number} level Type of heading (h<level> tag)
187187
* @returns {String} Sidebar element

0 commit comments

Comments
 (0)