1
1
import React , { useCallback , useEffect , useState } from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
import { useTranslation } from 'react-i18next' ;
4
+ import classnames from 'classnames' ;
4
5
import OutlineItem from './outline-item' ;
5
6
import { useScrollContext } from '../../hooks/use-scroll-context' ;
6
7
import { TRANSLATE_NAMESPACE } from '../../constants' ;
@@ -9,6 +10,20 @@ import { INTERNAL_EVENTS } from '../../constants/event-types';
9
10
10
11
import './style.css' ;
11
12
13
+ export const getOutlineSetting = ( ) => {
14
+ const currentValue = localStorage . getItem ( 'sf-editor' ) ;
15
+ const config = currentValue ? JSON . parse ( currentValue ) : { } ;
16
+ const { outlineOpen = false } = config ;
17
+ return outlineOpen ;
18
+ } ;
19
+
20
+ export const setOutlineSetting = ( isShown ) => {
21
+ const currentValue = localStorage . getItem ( 'sf-editor' ) ;
22
+ const config = currentValue ? JSON . parse ( currentValue ) : { } ;
23
+ config [ 'outlineOpen' ] = isShown ;
24
+ localStorage . setItem ( 'sf-editor' , JSON . stringify ( config ) ) ;
25
+ } ;
26
+
12
27
const getHeaderList = ( children ) => {
13
28
const headerList = [ ] ;
14
29
children . forEach ( ( node ) => {
@@ -23,7 +38,6 @@ const Outline = ({ editor }) => {
23
38
const { t } = useTranslation ( TRANSLATE_NAMESPACE ) ;
24
39
const scrollRef = useScrollContext ( ) ;
25
40
const [ headerList , setHeaderList ] = useState ( [ ] ) ;
26
- const [ activeId , setActiveId ] = useState ( '' ) ;
27
41
const [ isShown , setIsShown ] = useState ( false ) ;
28
42
const [ scrollLeft , setScrollLeft ] = useState ( 0 ) ;
29
43
@@ -32,47 +46,19 @@ const Outline = ({ editor }) => {
32
46
setHeaderList ( headerList ) ;
33
47
} , [ editor . children ] ) ;
34
48
35
- const handleScroll = useCallback ( ( e ) => {
36
- const scrollTop = scrollRef . current . scrollTop ;
37
- const styles = getComputedStyle ( scrollRef ?. current ) ;
38
- const paddingTop = parseInt ( styles . paddingTop ) ;
39
- for ( let i = 0 ; i < headerList . length ; i ++ ) {
40
- const headerItem = headerList [ i ] ;
41
- const dom = document . getElementById ( headerItem . id ) ;
42
- const { offsetTop, offsetHeight } = dom ;
43
- const styles = getComputedStyle ( dom ) ;
44
- const marginTop = parseInt ( styles . marginTop ) ;
45
- if ( offsetTop + offsetHeight + marginTop > scrollTop - paddingTop ) {
46
- setActiveId ( headerItem . id ) ;
47
- break ;
48
- }
49
- }
50
- } , [ headerList , scrollRef ] ) ;
51
-
52
- useEffect ( ( ) => {
53
- let observerRefValue = null ;
54
- if ( scrollRef . current ) {
55
- scrollRef . current . addEventListener ( 'scroll' , handleScroll ) ;
56
- observerRefValue = scrollRef . current ;
57
- }
58
-
59
- return ( ) => {
60
- observerRefValue . removeEventListener ( 'scroll' , handleScroll ) ;
61
- } ;
62
- } , [ handleScroll , scrollRef ] ) ;
49
+ const updateOutlineState = useCallback ( ( nextState ) => {
50
+ setOutlineSetting ( nextState ) ;
51
+ setIsShown ( nextState ) ;
52
+ const eventBus = EventBus . getInstance ( ) ;
53
+ eventBus . dispatch ( INTERNAL_EVENTS . OUTLINE_STATE_CHANGED ) ;
54
+ } , [ ] ) ;
63
55
64
56
const toggleShow = useCallback ( ( ) => {
65
- setIsShown ( prevIsShown => {
66
- const newIsShown = ! prevIsShown ;
67
- setTimeout ( ( ) => {
68
- const eventBus = EventBus . getInstance ( ) ;
69
- eventBus . dispatch ( INTERNAL_EVENTS . OUTLINE_STATE_CHANGED , newIsShown ) ;
70
- } , 0 ) ;
71
- return newIsShown ;
72
- } ) ;
73
- } , [ ] ) ;
57
+ const nextState = ! isShown ;
58
+ updateOutlineState ( nextState ) ;
59
+ } , [ isShown , updateOutlineState ] ) ;
74
60
75
- useEffect ( ( ) => {
61
+ useEffect ( ( ) => {
76
62
if ( ! scrollRef . current ) return ;
77
63
78
64
const updateScrollLeft = ( ) => {
@@ -86,35 +72,42 @@ const Outline = ({ editor }) => {
86
72
} ;
87
73
} , [ scrollRef ] ) ;
88
74
75
+ useEffect ( ( ) => {
76
+ const outlineState = getOutlineSetting ( ) ;
77
+ console . log ( outlineState ) ;
78
+ updateOutlineState ( outlineState ) ;
79
+ // eslint-disable-next-line react-hooks/exhaustive-deps
80
+ } , [ ] ) ;
81
+
89
82
return (
90
- < div className = "sf-editor-outline" style = { { left : - scrollLeft } } >
91
- { isShown && (
92
- < >
93
- < div className = "sf-editor-outline-header" >
94
- < h2 className = "sf-editor-outline-header_title" > { t ( 'Outline' ) } </ h2 >
95
- < span className = "sf-editor-outline-header_close iconfont icon-x" onClick = { toggleShow } > </ span >
96
- </ div >
97
- { headerList . length === 0 ? (
98
- < div className = "empty-container" > { t ( 'No_outline' ) } </ div >
99
- ) : (
100
- < div className = "sf-editor-outline-list-container" >
101
- { headerList . map ( ( node , index ) => (
102
- < OutlineItem key = { index } node = { node } activeId = { activeId } />
103
- ) ) }
83
+ < div className = { classnames ( 'sf-editor-outline-wrapper' , { 'active' : isShown } ) } style = { { left : - scrollLeft } } >
84
+ < div className = "sf-editor-outline" >
85
+ { isShown && (
86
+ < >
87
+ < div className = "sf-editor-outline-header" >
88
+ < h2 className = "sf-editor-outline-header_title" > { t ( 'Outline' ) } </ h2 >
89
+ < span className = "sf-editor-outline-header_close iconfont icon-x" onClick = { toggleShow } > </ span >
104
90
</ div >
105
- ) }
106
- </ >
107
- ) }
91
+ { headerList . length === 0 ? (
92
+ < div className = "empty-container" > { t ( 'No_outline' ) } </ div >
93
+ ) : (
94
+ < div className = "sf-editor-outline-list-container" >
95
+ { headerList . map ( ( node , index ) => (
96
+ < OutlineItem key = { index } node = { node } />
97
+ ) ) }
98
+ </ div >
99
+ ) }
100
+ </ >
101
+ ) }
102
+ </ div >
108
103
{ ! isShown && (
109
- < >
110
- < span
111
- id = "sf-editor-outline-menu"
112
- className = "sf-editor-outline-menu sf-edito-tooltip iconfont icon-outline"
113
- onClick = { toggleShow }
114
- >
115
- < span className = "custom-tooltip" > { t ( 'Outline' ) } </ span >
116
- </ span >
117
- </ >
104
+ < span
105
+ id = "sf-editor-outline-menu"
106
+ className = "sf-editor-outline-menu sf-editor-tooltip iconfont icon-outline"
107
+ onClick = { toggleShow }
108
+ >
109
+ < span className = "custom-tooltip" > { t ( 'Outline' ) } </ span >
110
+ </ span >
118
111
) }
119
112
</ div >
120
113
) ;
0 commit comments