@@ -23,7 +23,7 @@ import NavStyles from '@/styles/nav-styles';
23
23
import InfoStyles from '@/styles/info-styles' ;
24
24
import CustomStyles from '@/styles/custom-styles' ;
25
25
import {
26
- pathIsInSearch , invalidCharsRegEx , sleep , rapidocApiKey , advanceSearch ,
26
+ pathIsInSearch , invalidCharsRegEx , sleep , rapidocApiKey , advanceSearch , hasValidPathInUrlHash ,
27
27
} from '@/utils/common-utils' ;
28
28
import ProcessSpec from '@/utils/spec-parser' ;
29
29
import mainBodyTemplate from '@/templates/main-body-template' ;
@@ -599,6 +599,27 @@ export default class RapiDoc extends LitElement {
599
599
}
600
600
}
601
601
602
+ resetSelectedContentId ( ) {
603
+ // No content is selected at start
604
+ this . selectedContentId = '' ;
605
+ // If there is hash in url then check if hash belong to any of the path in spec
606
+ if ( window . location . hash ) {
607
+ this . selectedContentId = window . location . hash . substring ( 1 ) . startsWith ( 'overview--' )
608
+ ? 'overview' : hasValidPathInUrlHash ( this . resolvedSpec . tags )
609
+ ? window . location . hash . substring ( 1 ) : '' ;
610
+ }
611
+ // If there is no matching hash to path, check if there is sufficient data to display overview otherwise just display first path from first tag
612
+ if ( ! this . selectedContentId ) {
613
+ if ( this . showInfo === 'true' && ( this . resolvedSpec . info ?. description || this . resolvedSpec . info ?. title ) ) {
614
+ this . selectedContentId = 'overview' ;
615
+ } else {
616
+ this . selectedContentId = `${ this . resolvedSpec . tags [ 0 ] ?. paths [ 0 ] ?. method } -${ this . resolvedSpec . tags [ 0 ] ?. paths [ 0 ] ?. path } ` ;
617
+ }
618
+ }
619
+ // Set url back in address bar
620
+ window . location . hash = `${ this . selectedContentId } ` ;
621
+ }
622
+
602
623
async afterSpecParsedAndValidated ( spec ) {
603
624
this . resolvedSpec = spec ;
604
625
if ( this . defaultApiServerUrl ) {
@@ -616,13 +637,7 @@ export default class RapiDoc extends LitElement {
616
637
this . selectedServer = this . resolvedSpec . servers [ 0 ] ;
617
638
}
618
639
}
619
- if ( this . showInfo === 'true' && ! window . location . hash && ( this . resolvedSpec . info ?. description || this . resolvedSpec . info ?. title ) ) {
620
- this . selectedContentId = 'overview' ;
621
- } else if ( window . location . hash ) {
622
- this . selectedContentId = window . location . hash . substring ( 1 ) . startsWith ( 'overview--' ) ? 'overview' : window . location . hash . substring ( 1 ) ;
623
- } else {
624
- this . selectedContentId = `${ this . resolvedSpec . tags [ 0 ] ?. paths [ 0 ] ?. method } -${ this . resolvedSpec . tags [ 0 ] ?. paths [ 0 ] ?. path } ` ;
625
- }
640
+ this . resetSelectedContentId ( ) ;
626
641
this . requestUpdate ( ) ;
627
642
const specLoadedEvent = new CustomEvent ( 'spec-loaded' , { detail : spec } ) ;
628
643
this . dispatchEvent ( specLoadedEvent ) ;
@@ -751,7 +766,9 @@ export default class RapiDoc extends LitElement {
751
766
752
767
// Public Method (scrolls to a given path and highlights the left-nav selection)
753
768
async scrollTo ( path , expandPath = true ) {
754
- this . selectedContentId = path . startsWith ( 'overview--' ) ? 'overview' : path ;
769
+ if ( path ) {
770
+ this . selectedContentId = path . startsWith ( 'overview--' ) ? 'overview' : path ;
771
+ }
755
772
await sleep ( 0 ) ;
756
773
const gotoEl = this . shadowRoot . getElementById ( path ) ;
757
774
if ( gotoEl ) {
0 commit comments