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

feat: Improve the jump-to-fragment fixup for non-heading targets #882

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 50 additions & 5 deletions main/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,57 @@ module.exports = {
}
});

if (location.hash) {
// Re-navigate to the page target once content has loaded.
if (location.hash && location.hash !== '#') {
// Once content has loaded, re-navigate to the page target
// without triggering interfering router/history/scroll logic.
const hash = location.hash;
fixups.set(['main'], _elems => {
const old = location.hash;
location.hash = '';
location.hash = old;
const stopPropagation = evt => {
evt.stopImmediatePropagation();

const props = {};
const proto = Object.getPrototypeOf(evt);
const propSource = proto === Event.prototype ? {} : proto;
for (const name of Object.getOwnPropertyNames(propSource)) {
if (name !== 'constructor') props[name] = evt[name];
}
console.log('suppress', evt.type, { __proto__: evt, ...props });
};
const stopEvents = types => {
const restorers = types.map(type => {
window.addEventListener(type, stopPropagation, true);
return () => window.removeEventListener(type, stopPropagation, true);
});
const passEvents = () => {
// Run and drop references to all restore functions.
while (restorers.length > 0) restorers.pop()();
Copy link
Member

Choose a reason for hiding this comment

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

Any reasons for restorers.pop() instead of restorers.shift()? Maybe add a comment as to why, or why it doesn't matter.

};
return passEvents;
};

// Navigate to the page itself as a blank slate.
const passStateEvents = stopEvents(['hashchange', 'popstate']);
const passScrollEvents = stopEvents(['scroll']);
location.replace('#');

// Restore state-change events, then navigate back to the target.
passStateEvents();
try {
const target = document.getElementById(decodeURIComponent(hash.slice(1)));
if (target && target.innerHTML.trim() === '') {
document.documentElement.classList.add('scrollingToTarget');
target.scrollIntoView({ behavior: 'instant' });
document.documentElement.classList.remove('scrollingToTarget');
}
} catch (err) {
console.warn(err);
}
location.replace(hash);

// Restore scroll events and create a new history entry to be overridden
// if the initial target lacks a TOC entry to highlight.
passScrollEvents();
history.pushState(null, '', hash);
});
}

Expand Down
4 changes: 4 additions & 0 deletions main/.vuepress/styles/index.styl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
}
}

:root.scrollingToTarget {
scroll-padding-top: $navbarHeight;
}

.ag-btn
background-color $accentColor
color $white
Expand Down
Loading