@@ -6,24 +6,30 @@ const Fullscreen: React.FC<{ command: ICommand; editorProps: IMarkdownEditor & T
6
6
const { editorProps } = props ;
7
7
const $height = useRef < number > ( 0 ) ;
8
8
const [ full , setFull ] = useState ( false ) ;
9
- const robserver = useRef < ResizeObserver > ( ) ;
10
- useEffect ( ( ) => {
11
- robserver . current = new ResizeObserver ( ( entries ) => {
12
- for ( const entry of entries ) {
13
- if ( ! $height . current ) {
14
- $height . current = entry . target . clientHeight ;
15
- }
16
- if ( editorProps . editor ?. current ?. view ?. dom ) {
17
- if ( full ) {
18
- editorProps . editor . current . view . dom . style . height = `${ entry . target . clientHeight } px` ;
19
- } else {
20
- editorProps . editor . current . view . dom . removeAttribute ( 'style' ) ;
21
- }
9
+ const fullRef = useRef ( full ) ;
10
+ const entriesHandle : ResizeObserverCallback = ( entries : ResizeObserverEntry [ ] ) => {
11
+ for ( const entry of entries ) {
12
+ if ( ! $height . current ) {
13
+ $height . current = entry . target . clientHeight ;
14
+ }
15
+ if ( editorProps . editor ?. current ?. view ?. dom ) {
16
+ if ( fullRef . current ) {
17
+ editorProps . editor . current . view . dom . style . height = `${ entry . target . clientHeight } px` ;
18
+ } else {
19
+ editorProps . editor . current . view . dom . removeAttribute ( 'style' ) ;
22
20
}
23
21
}
24
- } ) ;
25
- } , [ ] ) ;
22
+ }
23
+ robserver . current ?. disconnect ( ) ;
24
+ robserver . current = undefined ;
25
+ } ;
26
+
27
+ const robserver = useRef < ResizeObserver | undefined > ( new ResizeObserver ( entriesHandle ) ) ;
28
+
26
29
useEffect ( ( ) => {
30
+ if ( ! robserver . current ) {
31
+ robserver . current = new ResizeObserver ( entriesHandle ) ;
32
+ }
27
33
if (
28
34
editorProps . containerEditor &&
29
35
editorProps . containerEditor . current &&
@@ -33,7 +39,13 @@ const Fullscreen: React.FC<{ command: ICommand; editorProps: IMarkdownEditor & T
33
39
const parentElement = editorProps . containerEditor . current . parentElement ;
34
40
robserver . current . observe ( parentElement ) ;
35
41
}
36
- } , [ editorProps . containerEditor , editorProps . editor , full , robserver ] ) ;
42
+ return ( ) => {
43
+ if ( robserver . current ) {
44
+ robserver . current . disconnect ( ) ;
45
+ robserver . current = undefined ;
46
+ }
47
+ } ;
48
+ } , [ editorProps . containerEditor , entriesHandle , editorProps . editor , full , robserver ] ) ;
37
49
38
50
useEffect ( ( ) => {
39
51
if ( ! document ) return ;
@@ -57,8 +69,13 @@ const Fullscreen: React.FC<{ command: ICommand; editorProps: IMarkdownEditor & T
57
69
}
58
70
} , [ full , editorProps ] ) ;
59
71
72
+ const click = ( ) => {
73
+ fullRef . current = ! full ;
74
+ setFull ( ! full ) ;
75
+ } ;
76
+
60
77
return (
61
- < button onClick = { ( ) => setFull ( ! full ) } type = "button" className = { full ? 'active' : '' } >
78
+ < button onClick = { click } type = "button" className = { full ? 'active' : '' } >
62
79
{ props . command . icon }
63
80
</ button >
64
81
) ;
0 commit comments