1
1
import throttle from 'lodash.throttle'
2
2
import { uuidToId } from 'notion-utils'
3
- import { useCallback , useEffect , useRef , useState } from 'react'
3
+ import { useEffect , useRef , useState } from 'react'
4
4
import Progress from './Progress'
5
5
6
6
/**
@@ -9,7 +9,7 @@ import Progress from './Progress'
9
9
* @returns {JSX.Element }
10
10
* @constructor
11
11
*/
12
- const Catalog = ( { toc, className } ) => {
12
+ const Catalog = ( { post , toc, className } ) => {
13
13
const tocIds = [ ]
14
14
15
15
// 目录自动滚动
@@ -19,43 +19,45 @@ const Catalog = ({ toc, className }) => {
19
19
20
20
// 监听滚动事件
21
21
useEffect ( ( ) => {
22
- window . addEventListener ( 'scroll' , actionSectionScrollSpy )
23
- actionSectionScrollSpy ( )
22
+ if ( toc && toc . length > 1 ) {
23
+ actionSectionScrollSpy ( )
24
+ window . addEventListener ( 'scroll' , actionSectionScrollSpy )
25
+ }
26
+ setTimeout ( ( ) => {
27
+ console . log ( '目录' , post , toc )
28
+ } , 1000 )
24
29
return ( ) => {
25
30
window . removeEventListener ( 'scroll' , actionSectionScrollSpy )
26
31
}
27
32
} , [ ] )
28
33
29
34
const throttleMs = 200
30
- const actionSectionScrollSpy = useCallback (
31
- throttle ( ( ) => {
32
- const sections = document . getElementsByClassName ( 'notion-h' )
33
- let prevBBox = null
34
- let currentSectionId = activeSection
35
- for ( let i = 0 ; i < sections . length ; ++ i ) {
36
- const section = sections [ i ]
37
- if ( ! section || ! ( section instanceof Element ) ) continue
38
- if ( ! currentSectionId ) {
39
- currentSectionId = section . getAttribute ( 'data-id' )
40
- }
41
- const bbox = section . getBoundingClientRect ( )
42
- const prevHeight = prevBBox ? bbox . top - prevBBox . bottom : 0
43
- const offset = Math . max ( 150 , prevHeight / 4 )
44
- // GetBoundingClientRect returns values relative to viewport
45
- if ( bbox . top - offset < 0 ) {
46
- currentSectionId = section . getAttribute ( 'data-id' )
47
- prevBBox = bbox
48
- continue
49
- }
50
- // No need to continue loop, if last element has been detected
51
- break
35
+ const actionSectionScrollSpy = throttle ( ( ) => {
36
+ const sections = document . getElementsByClassName ( 'notion-h' )
37
+ let prevBBox = null
38
+ let currentSectionId = activeSection
39
+ for ( let i = 0 ; i < sections . length ; ++ i ) {
40
+ const section = sections [ i ]
41
+ if ( ! section || ! ( section instanceof Element ) ) continue
42
+ if ( ! currentSectionId ) {
43
+ currentSectionId = section . getAttribute ( 'data-id' )
52
44
}
53
- setActiveSection ( currentSectionId )
54
- const index = tocIds . indexOf ( currentSectionId ) || 0
55
- tRef ?. current ?. scrollTo ( { top : 28 * index , behavior : 'smooth' } )
56
- console . log ( tRef ?. current )
57
- } , throttleMs )
58
- )
45
+ const bbox = section . getBoundingClientRect ( )
46
+ const prevHeight = prevBBox ? bbox . top - prevBBox . bottom : 0
47
+ const offset = Math . max ( 150 , prevHeight / 4 )
48
+ // GetBoundingClientRect returns values relative to viewport
49
+ if ( bbox . top - offset < 0 ) {
50
+ currentSectionId = section . getAttribute ( 'data-id' )
51
+ prevBBox = bbox
52
+ continue
53
+ }
54
+ // No need to continue loop, if last element has been detected
55
+ break
56
+ }
57
+ setActiveSection ( currentSectionId )
58
+ const index = tocIds . indexOf ( currentSectionId ) || 0
59
+ tRef ?. current ?. scrollTo ( { top : 28 * index , behavior : 'smooth' } )
60
+ } , throttleMs )
59
61
60
62
// 无目录就直接返回空
61
63
if ( ! toc || toc . length < 1 ) {
@@ -68,10 +70,10 @@ const Catalog = ({ toc, className }) => {
68
70
< Progress />
69
71
</ div >
70
72
< div
71
- className = 'overflow-y-auto scroll-hidden lg:max-h-dvh max-h-44'
73
+ className = 'overflow-y-auto scroll-hidden lg:max-h-96 max-h-44'
72
74
ref = { tRef } >
73
75
< nav className = 'h-full text-black' >
74
- { toc . map ( tocItem => {
76
+ { toc ? .map ( tocItem => {
75
77
const id = uuidToId ( tocItem . id )
76
78
tocIds . push ( id )
77
79
return (
0 commit comments