Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix auto-scroll undershooting correction location on initial page load #2238

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/core/event/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,20 @@ export function Events(Base) {
#enableScrollEvent = true;
#coverHeight = 0;

#scrollTo(el, offset = 0) {
#delayScrollInterval;
async #scrollTo(el, path, offset = 0) {
if (document.readyState !== 'complete') {
clearInterval(this.#delayScrollInterval);
await new Promise(resolve => {
this.#delayScrollInterval = setInterval(() => {
if (document.readyState === 'complete') {
clearInterval(this.#delayScrollInterval);
resolve();
}
}, 100);
});
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this reliable? Does the page go into non-complete ready state on each markdown fetch/render? If not, perhaps we should instead track images (etc) ourself, and wait for load event on all of them. Need to check this out.


if (this.#scroller) {
this.#scroller.stop();
}
Expand All @@ -71,11 +84,13 @@ export function Events(Base) {
.on('done', () => {
this.#enableScrollEvent = true;
this.#scroller = null;
el.scrollIntoView({ behavior: 'smooth' });
this.#highlight(path, true);
})
.begin();
}

#highlight(path) {
#highlight(path, scroll = false) {
if (!this.#enableScrollEvent) {
return;
}
Expand Down Expand Up @@ -107,7 +122,7 @@ export function Events(Base) {

const li = this.#nav[this.#getNavKey(path, last.getAttribute('data-id'))];

if (!li || li === active) {
if (!li || (li === active && !scroll)) {
return;
}

Expand Down Expand Up @@ -193,7 +208,7 @@ export function Events(Base) {
// Use [id='1234'] instead of #id to handle special cases such as reserved characters and pure number id
// https://stackoverflow.com/questions/37270787/uncaught-syntaxerror-failed-to-execute-queryselector-on-document
const section = dom.find("[id='" + id + "']");
section && this.#scrollTo(section, topMargin);
section && this.#scrollTo(section, path, topMargin);

const li = this.#nav[this.#getNavKey(path, id)];
const sidebar = dom.getNode('.sidebar');
Expand Down