1- import { createUseStyles } from 'react-jss' ;
2- import { ThemingParameters } from '@ui5/webcomponents-react-base' ;
31import React , { useEffect } from 'react' ;
42import tocbot from 'tocbot' ;
3+ import classes from './ToC.module.css' ;
54
6- const styles = {
7- header : {
8- width : '100%' ,
9- borderBottom : '1px solid rgba(0, 0, 0, 0.1)' ,
10- fontSize : '24px' ,
11- '@media(min-width:1330px)' : {
12- display : 'none'
13- }
14- } ,
15- fixedContainer : {
16- '@media(min-width:1330px)' : {
17- width : '200px' ,
18- position : 'fixed' ,
19- top : 75 ,
20- right : 6 ,
21- overflow : 'hidden'
22- }
23- } ,
24- toc : {
25- fontFamily : ThemingParameters . sapFontFamily ,
26- fontSize : ThemingParameters . sapFontSize ,
27- backgroundColor : 'white' ,
28- '& *' : {
29- textDecorationLine : 'none' ,
30- listStyle : 'none'
31- } ,
32- '@media(max-width:1329px)' : {
33- borderBottom : '1px solid rgba(0, 0, 0, 0.1)' ,
34- '& .toc-link' : {
35- textShadow : ThemingParameters . sapContent_TextShadow ,
36- color : ThemingParameters . sapLinkColor ,
37- '&:hover' : {
38- color : ThemingParameters . sapLink_Hover_Color
39- } ,
40- '&:active' : {
41- color : ThemingParameters . sapLink_Active_Color
42- }
43- } ,
44- '& .toc-list-item' : {
45- margin : '4px 0'
46- } ,
47- '& .is-active-link' : {
48- fontWeight : 400 ,
49- '&::before' : {
50- content : 'none'
51- }
52- }
53- } ,
54- '@media(min-width:1330px)' : {
55- '& > .toc-list' : {
56- paddingLeft : '10px'
57- } ,
58- '& .toc-list-item' : {
59- margin : '4px 0'
60- } ,
61- '& .toc-link::before' : {
62- width : '4px'
63- } ,
64- '& .is-active-link' : {
65- '&::before' : {
66- backgroundColor : '#0a6ed1'
67- }
68- }
69- }
70- }
71- } ;
72- const useStyles = createUseStyles ( styles , { name : 'TableOfContent' } ) ;
5+ function makeIds ( headingSelector ) {
6+ const headings = document . querySelector ( '.sbdocs-wrapper' ) . querySelectorAll ( headingSelector ) ;
7+ const headingMap = { } ;
738
74- export const TableOfContent = ( { headingSelector = 'h2.sbdocs-h2, h3.sbdocs-h3, h4.sbdocs-h4' } ) => {
75- const classes = useStyles ( ) ;
9+ headings . forEach ( function ( heading ) {
10+ const id =
11+ heading . id ??
12+ heading . textContent
13+ . trim ( )
14+ . toLowerCase ( )
15+ . split ( ' ' )
16+ . join ( '-' )
17+ . replace ( / [ ! @ # $ % ^ & * ( ) : ] / gi, '' )
18+ . replace ( / \/ / gi, '-' ) ;
19+ headingMap [ id ] = ! isNaN ( headingMap [ id ] ) ? ++ headingMap [ id ] : 0 ;
20+ if ( headingMap [ id ] ) {
21+ heading . id = id + '-' + headingMap [ id ] ;
22+ } else {
23+ heading . id = id ;
24+ }
25+ } ) ;
26+ }
7627
28+ export function TableOfContent ( { headingSelector = 'h2:not(.noAnchor), h3:not(.noAnchor), h4:not(.noAnchor)' } ) {
7729 useEffect ( ( ) => {
30+ makeIds ( headingSelector ) ;
31+
7832 tocbot . init ( {
7933 tocSelector : '.js-toc' ,
8034 contentSelector : '.sbdocs-wrapper' ,
@@ -83,20 +37,20 @@ export const TableOfContent = ({ headingSelector = 'h2.sbdocs-h2, h3.sbdocs-h3,
8337 collapseDepth : 6 ,
8438 hasInnerContainers : true
8539 } ) ;
40+
8641 document . querySelectorAll ( '.toc-link' ) . forEach ( ( x ) => x . setAttribute ( 'target' , '_self' ) ) ;
42+
8743 return ( ) => {
8844 tocbot . destroy ( ) ;
8945 } ;
9046 } , [ headingSelector ] ) ;
9147
9248 return (
9349 < >
94- < h3 className = { classes . header } > Contents</ h3 >
50+ < h3 className = { ` ${ classes . header } noAnchor` } > Contents</ h3 >
9551 < div className = { classes . fixedContainer } >
9652 < div className = { `js-toc ${ classes . toc } ` } id = "toc-container" />
9753 </ div >
9854 </ >
9955 ) ;
100- } ;
101-
102- TableOfContent . displayName = 'TableOfContent' ;
56+ }
0 commit comments