@@ -22,8 +22,8 @@ type NavState = { [key: string]: boolean };
2222const PAGE_NAV_STATE_KEY = 'pagesNavState' ;
2323const sessionState = JSON . parse ( sessionStorage . getItem ( PAGE_NAV_STATE_KEY ) ) as NavState
2424
25- function keepNavState ( element : HTMLElement ) {
26- const inputs = $$ ( 'input[type="checkbox"]' , element ) ;
25+ function keepNavState ( nav : HTMLElement ) {
26+ const inputs = $$ ( 'input[type="checkbox"]' , nav ) ;
2727 if ( sessionState ) {
2828 inputs . forEach ( input => {
2929 const key = input . id ;
@@ -35,7 +35,7 @@ function keepNavState(element: HTMLElement) {
3535 } ) ;
3636 }
3737 window . addEventListener ( 'beforeunload' , ( ) => {
38- const inputs = $$ ( 'input[type="checkbox"]' , element ) ;
38+ const inputs = $$ ( 'input[type="checkbox"]' , nav ) ;
3939 const state : NavState = inputs . reduce ( ( state : NavState , input ) => {
4040 const key = input . id ;
4141 const value = input . checked ;
@@ -48,15 +48,35 @@ function keepNavState(element: HTMLElement) {
4848const PAGE_NAV_SCROLL_POSITION_KEY = 'pagesNavScrollPosition' ;
4949const scrollPosition = sessionStorage . getItem ( PAGE_NAV_SCROLL_POSITION_KEY ) ;
5050
51- function keepNavPosition ( element : HTMLElement ) {
51+ function keepNavPosition ( nav : HTMLElement ) {
5252 if ( scrollPosition ) {
53- element . scrollTop = parseInt ( scrollPosition ) ;
53+ nav . scrollTop = parseInt ( scrollPosition ) ;
5454 }
5555 window . addEventListener ( 'beforeunload' , ( ) => {
56- sessionStorage . setItem ( PAGE_NAV_SCROLL_POSITION_KEY , element . scrollTop . toString ( ) ) ;
56+ sessionStorage . setItem ( PAGE_NAV_SCROLL_POSITION_KEY , nav . scrollTop . toString ( ) ) ;
5757 } ) ;
5858}
5959
60+ function scrollCurrentNaviItemIntoView ( nav : HTMLElement , delay : number ) {
61+ setTimeout ( ( ) => {
62+ const currentNavItem = $ ( '.current' ) ;
63+ if ( currentNavItem && ! isElementInViewport ( currentNavItem ) ) {
64+ currentNavItem . scrollIntoView ( { behavior : 'smooth' , block : 'center' } ) ;
65+ }
66+ } , delay ) ;
67+ }
68+ function isElementInViewport ( el : HTMLElement ) : boolean {
69+ const rect = el . getBoundingClientRect ( ) ;
70+ return (
71+ rect . top >= 0 &&
72+ rect . left >= 0 &&
73+ rect . bottom <= ( window . innerHeight || document . documentElement . clientHeight ) &&
74+ rect . right <= ( window . innerWidth || document . documentElement . clientWidth )
75+ ) ;
76+ }
77+
78+
6079const pagesNav = $ ( '#pages-nav' ) ;
6180keepNavPosition ( pagesNav ) ;
6281keepNavState ( pagesNav ) ;
82+ scrollCurrentNaviItemIntoView ( pagesNav , 100 ) ;
0 commit comments