@@ -6,24 +6,30 @@ const Fullscreen: React.FC<{ command: ICommand; editorProps: IMarkdownEditor & T
66 const { editorProps } = props ;
77 const $height = useRef < number > ( 0 ) ;
88 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' ) ;
2220 }
2321 }
24- } ) ;
25- } , [ ] ) ;
22+ }
23+ robserver . current ?. disconnect ( ) ;
24+ robserver . current = undefined ;
25+ } ;
26+
27+ const robserver = useRef < ResizeObserver | undefined > ( new ResizeObserver ( entriesHandle ) ) ;
28+
2629 useEffect ( ( ) => {
30+ if ( ! robserver . current ) {
31+ robserver . current = new ResizeObserver ( entriesHandle ) ;
32+ }
2733 if (
2834 editorProps . containerEditor &&
2935 editorProps . containerEditor . current &&
@@ -33,7 +39,13 @@ const Fullscreen: React.FC<{ command: ICommand; editorProps: IMarkdownEditor & T
3339 const parentElement = editorProps . containerEditor . current . parentElement ;
3440 robserver . current . observe ( parentElement ) ;
3541 }
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 ] ) ;
3749
3850 useEffect ( ( ) => {
3951 if ( ! document ) return ;
@@ -57,8 +69,13 @@ const Fullscreen: React.FC<{ command: ICommand; editorProps: IMarkdownEditor & T
5769 }
5870 } , [ full , editorProps ] ) ;
5971
72+ const click = ( ) => {
73+ fullRef . current = ! full ;
74+ setFull ( ! full ) ;
75+ } ;
76+
6077 return (
61- < button onClick = { ( ) => setFull ( ! full ) } type = "button" className = { full ? 'active' : '' } >
78+ < button onClick = { click } type = "button" className = { full ? 'active' : '' } >
6279 { props . command . icon }
6380 </ button >
6481 ) ;
0 commit comments