11import { $ , $$ } from "select-dom" ;
22
3- type NavExpandState = {
4- current :string ,
5- selected : Record < string , boolean >
6- } ;
7- const PAGE_NAV_EXPAND_STATE_KEY = 'pagesNavState' ;
8-
9- // Initialize the nav state from the session storage
10- // Return a function to keep the nav state in the session storage that should be called before the page is unloaded
11- function keepNavState ( nav : HTMLElement ) : ( ) => void {
12-
13- const currentNavigation = nav . dataset . currentNavigation ;
14- const currentPageId = nav . dataset . currentPageId ;
15-
16- let navState = JSON . parse ( sessionStorage . getItem ( PAGE_NAV_EXPAND_STATE_KEY ) ?? "{}" ) as NavExpandState
17- if ( navState . current !== currentNavigation )
18- {
19- sessionStorage . removeItem ( PAGE_NAV_EXPAND_STATE_KEY ) ;
20- navState = { current : currentNavigation } as NavExpandState ;
21- }
22- if ( currentPageId )
23- {
24- const currentPageLink = $ ( 'a[id="page-' + currentPageId + '"]' , nav ) ;
25- currentPageLink . classList . add ( 'current' ) ;
26- currentPageLink . classList . add ( 'pointer-events-none' ) ;
27- currentPageLink . classList . add ( 'text-blue-elastic!' ) ;
28- currentPageLink . classList . add ( 'font-semibold' ) ;
29-
30- const parentIds = nav . dataset . currentPageParentIds ?. split ( ',' ) ?? [ ] ;
31- for ( const parentId of parentIds )
32- {
33- const input = $ ( 'input[type="checkbox"][id=\"' + parentId + '\"]' , nav ) as HTMLInputElement ;
34- if ( input ) {
35- input . checked = true ;
36- const link = input . nextElementSibling as HTMLAnchorElement ;
37- link . classList . add ( 'font-semibold' ) ;
38- }
3+ function expandAllParents ( navItem : HTMLElement ) {
4+ let parent = navItem . closest ( 'li' ) ;
5+ while ( parent ) {
6+ const input = parent . querySelector ( 'input' ) ;
7+ if ( input ) {
8+ ( input as HTMLInputElement ) . checked = true ;
399 }
40- }
41-
42- // expand items previously selected
43- for ( const groupId in navState . selected )
44- {
45- const input = $ ( 'input[type="checkbox"][id=\"' + groupId + '\"]' , nav ) as HTMLInputElement ;
46- input . checked = navState . selected [ groupId ] ;
47- }
48-
49- return ( ) => {
50- // store all expanded groups
51- const inputs = $$ ( 'input[type="checkbox"]:checked' , nav ) ;
52- const selectedMap : Record < string , boolean > = inputs
53- . filter ( input => input . checked )
54- . reduce ( ( state : Record < string , boolean > , input ) => {
55- const key = input . id ;
56- const value = input . checked ;
57- return { ...state , [ key ] : value } ;
58- } , { } ) ;
59- const state = { current : currentNavigation , selected : selectedMap } ;
60- sessionStorage . setItem ( PAGE_NAV_EXPAND_STATE_KEY , JSON . stringify ( state ) ) ;
61- }
62- }
63-
64- type NavScrollPosition = number ;
65- const PAGE_NAV_SCROLL_POSITION_KEY = 'pagesNavScrollPosition' ;
66- const pagesNavScrollPosition : NavScrollPosition = parseInt (
67- sessionStorage . getItem ( PAGE_NAV_SCROLL_POSITION_KEY ) ?? '0'
68- ) ;
69-
70-
71- // Initialize the nav scroll position from the session storage
72- // Return a function to keep the nav scroll position in the session storage that should be called before the page is unloaded
73- function keepNavPosition ( nav : HTMLElement ) : ( ) => void {
74- if ( pagesNavScrollPosition ) {
75- nav . scrollTop = pagesNavScrollPosition ;
76- }
77- return ( ) => {
78- sessionStorage . setItem ( PAGE_NAV_SCROLL_POSITION_KEY , nav . scrollTop . toString ( ) ) ;
10+ parent = parent . parentElement ?. closest ( 'li' ) ;
7911 }
8012}
8113
8214function scrollCurrentNaviItemIntoView ( nav : HTMLElement , delay : number ) {
15+ const currentNavItem = $ ( '.current' , nav ) ;
16+ expandAllParents ( currentNavItem ) ;
8317 setTimeout ( ( ) => {
84- const currentNavItem = $ ( '.current' , nav ) ;
18+
8519 if ( currentNavItem && ! isElementInViewport ( currentNavItem ) ) {
8620 currentNavItem . scrollIntoView ( { behavior : 'smooth' , block : 'center' } ) ;
8721 }
@@ -102,13 +36,12 @@ export function initNav() {
10236 if ( ! pagesNav ) {
10337 return ;
10438 }
105- const keepNavStateCallback = keepNavState ( pagesNav ) ;
106- const keepNavPositionCallback = keepNavPosition ( pagesNav ) ;
39+ const navItems = $$ ( 'a[href="' + window . location . pathname + '"]' , pagesNav ) ;
40+ navItems . forEach ( el => {
41+ el . classList . add ( 'current' ) ;
42+ } ) ;
10743 scrollCurrentNaviItemIntoView ( pagesNav , 100 ) ;
108- window . addEventListener ( 'beforeunload' , ( ) => {
109- keepNavStateCallback ( ) ;
110- keepNavPositionCallback ( ) ;
111- } , true ) ;
11244}
11345
114- initNav ( ) ;
46+
47+ // initNav();
0 commit comments