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: restore the sidebar position to make the selected entry showed #492

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
39 changes: 23 additions & 16 deletions assets/js/sidebar.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
document.addEventListener("DOMContentLoaded", function () {
scrollToActiveItem();
restoreSidebarPosition();
enableCollapsibles();
});

Expand All @@ -16,21 +16,28 @@ function enableCollapsibles() {
});
}

function scrollToActiveItem() {
function saveSidebarPosition() {
const sidebarScrollbar = document.querySelector("aside.sidebar-container > .hextra-scrollbar");
const activeItems = document.querySelectorAll(".sidebar-active-item");
const visibleActiveItem = Array.from(activeItems).find(function (activeItem) {
return activeItem.getBoundingClientRect().height > 0;
});

if (!visibleActiveItem) {
return;
if (sidebarScrollbar) {
const scrollPosition = sidebarScrollbar.scrollTop;
sessionStorage.setItem('sidebarScrollPosition', scrollPosition);
}

const yOffset = visibleActiveItem.clientHeight;
const yDistance = visibleActiveItem.getBoundingClientRect().top - sidebarScrollbar.getBoundingClientRect().top;
sidebarScrollbar.scrollTo({
behavior: "instant",
top: yDistance - yOffset
});
}

function restoreSidebarPosition() {
const sidebarScrollbar = document.querySelector("aside.sidebar-container > .hextra-scrollbar");
if (sidebarScrollbar) {
const savedPosition = sessionStorage.getItem('sidebarScrollPosition');

if (savedPosition !== null) {
sidebarScrollbar.scrollTo({
top: parseInt(savedPosition),
behavior: 'instant'
});
}

sidebarScrollbar.addEventListener('scroll', function() {
saveSidebarPosition();
Copy link
Owner

Choose a reason for hiding this comment

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

I think you need debouncing the event handler.

Copy link
Author

Choose a reason for hiding this comment

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

ok, I have add the new function.

});
}
}