11import { $ , $$ } from "select-dom" ;
2- import { initHighlight } from "./hljs" ;
3- import { initCopyButton } from "./copybutton" ;
4- import { initTabs } from "./tabs" ;
5- import { initTocNav } from "./toc-nav" ;
62
7- type NavExpandState = {
8- current :string ,
9- selected : Record < string , boolean >
10- } ;
11- const PAGE_NAV_EXPAND_STATE_KEY = 'pagesNavState' ;
12-
13- // Initialize the nav state from the session storage
14- // Return a function to keep the nav state in the session storage that should be called before the page is unloaded
15- function keepNavState ( nav : HTMLElement ) : ( ) => void {
16-
17- const currentNavigation = nav . dataset . currentNavigation ;
18- const currentPageId = nav . dataset . currentPageId ;
19-
20- let navState = JSON . parse ( sessionStorage . getItem ( PAGE_NAV_EXPAND_STATE_KEY ) ?? "{}" ) as NavExpandState
21- if ( navState . current !== currentNavigation )
22- {
23- sessionStorage . removeItem ( PAGE_NAV_EXPAND_STATE_KEY ) ;
24- navState = { current : currentNavigation } as NavExpandState ;
25- }
26- if ( currentPageId )
27- {
28- const currentPageLink = $ ( 'a[id="page-' + currentPageId + '"]' , nav ) ;
29- currentPageLink . classList . add ( 'current' ) ;
30- currentPageLink . classList . add ( 'pointer-events-none' ) ;
31- currentPageLink . classList . add ( 'text-blue-elastic!' ) ;
32- currentPageLink . classList . add ( 'font-semibold' ) ;
33-
34- const parentIds = nav . dataset . currentPageParentIds ?. split ( ',' ) ?? [ ] ;
35- for ( const parentId of parentIds )
36- {
37- const input = $ ( 'input[type="checkbox"][id=\"' + parentId + '\"]' , nav ) as HTMLInputElement ;
38- if ( input ) {
39- input . checked = true ;
40- const link = input . nextElementSibling as HTMLAnchorElement ;
41- link . classList . add ( 'font-semibold' ) ;
42- }
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 ;
439 }
44- }
45-
46- // expand items previously selected
47- for ( const groupId in navState . selected )
48- {
49- const input = $ ( 'input[type="checkbox"][id=\"' + groupId + '\"]' , nav ) as HTMLInputElement ;
50- input . checked = navState . selected [ groupId ] ;
51- }
52-
53- return ( ) => {
54- // store all expanded groups
55- const inputs = $$ ( 'input[type="checkbox"]:checked' , nav ) ;
56- const selectedMap : Record < string , boolean > = inputs
57- . filter ( input => input . checked )
58- . reduce ( ( state : Record < string , boolean > , input ) => {
59- const key = input . id ;
60- const value = input . checked ;
61- return { ...state , [ key ] : value } ;
62- } , { } ) ;
63- const state = { current : currentNavigation , selected : selectedMap } ;
64- sessionStorage . setItem ( PAGE_NAV_EXPAND_STATE_KEY , JSON . stringify ( state ) ) ;
65- }
66- }
67-
68- type NavScrollPosition = number ;
69- const PAGE_NAV_SCROLL_POSITION_KEY = 'pagesNavScrollPosition' ;
70- const pagesNavScrollPosition : NavScrollPosition = parseInt (
71- sessionStorage . getItem ( PAGE_NAV_SCROLL_POSITION_KEY ) ?? '0'
72- ) ;
73-
74-
75- // Initialize the nav scroll position from the session storage
76- // Return a function to keep the nav scroll position in the session storage that should be called before the page is unloaded
77- function keepNavPosition ( nav : HTMLElement ) : ( ) => void {
78- if ( pagesNavScrollPosition ) {
79- nav . scrollTop = pagesNavScrollPosition ;
80- }
81- return ( ) => {
82- sessionStorage . setItem ( PAGE_NAV_SCROLL_POSITION_KEY , nav . scrollTop . toString ( ) ) ;
10+ parent = parent . parentElement ?. closest ( 'li' ) ;
8311 }
8412}
8513
8614function scrollCurrentNaviItemIntoView ( nav : HTMLElement , delay : number ) {
15+ const currentNavItem = $ ( '.current' , nav ) ;
16+ expandAllParents ( currentNavItem ) ;
8717 setTimeout ( ( ) => {
88- const currentNavItem = $ ( '.current' , nav ) ;
18+
8919 if ( currentNavItem && ! isElementInViewport ( currentNavItem ) ) {
9020 currentNavItem . scrollIntoView ( { behavior : 'smooth' , block : 'center' } ) ;
9121 }
@@ -106,13 +36,12 @@ export function initNav() {
10636 if ( ! pagesNav ) {
10737 return ;
10838 }
109- const keepNavStateCallback = keepNavState ( pagesNav ) ;
110- const keepNavPositionCallback = keepNavPosition ( pagesNav ) ;
39+ const navItems = $$ ( 'a[href="' + window . location . pathname + '"]' , pagesNav ) ;
40+ navItems . forEach ( el => {
41+ el . classList . add ( 'current' ) ;
42+ } ) ;
11143 scrollCurrentNaviItemIntoView ( pagesNav , 100 ) ;
112- window . addEventListener ( 'beforeunload' , ( ) => {
113- keepNavStateCallback ( ) ;
114- keepNavPositionCallback ( ) ;
115- } , true ) ;
11644}
11745
46+
11847// initNav();
0 commit comments