3
3
* Licensed under the MIT License. See LICENSE in the package root for license information.
4
4
* ------------------------------------------------------------------------------------------ */
5
5
6
- import React , { type CSSProperties , useCallback , useEffect , useRef } from 'react' ;
6
+ import React , { type CSSProperties , useEffect , useRef } from 'react' ;
7
7
import { MonacoEditorLanguageClientWrapper , type TextContents , type WrapperConfig } from 'monaco-editor-wrapper' ;
8
8
9
9
export type MonacoEditorProps = {
@@ -12,8 +12,7 @@ export type MonacoEditorProps = {
12
12
wrapperConfig : WrapperConfig ,
13
13
onTextChanged ?: ( textChanges : TextContents ) => void ;
14
14
onLoad ?: ( wrapper : MonacoEditorLanguageClientWrapper ) => void ;
15
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
- onError ?: ( e : any ) => void ;
15
+ onError ?: ( e : unknown ) => void ;
17
16
}
18
17
19
18
export const MonacoEditorReactComp : React . FC < MonacoEditorProps > = ( props ) => {
@@ -30,72 +29,52 @@ export const MonacoEditorReactComp: React.FC<MonacoEditorProps> = (props) => {
30
29
const containerRef = useRef < HTMLDivElement > ( null ) ;
31
30
32
31
useEffect ( ( ) => {
33
- return ( ) => {
34
- destroyMonaco ( ) ;
32
+ const destroyMonaco = async ( ) => {
33
+ try {
34
+ await wrapperRef . current . dispose ( ) ;
35
+ } catch {
36
+ // The language client may throw an error during disposal.
37
+ // This should not prevent us from continue working.
38
+ }
35
39
} ;
36
- } , [ ] ) ;
37
-
38
- useEffect ( ( ) => {
39
- handleReInit ( ) ;
40
- } , [ wrapperConfig ] ) ;
41
-
42
- useEffect ( ( ) => {
43
- handleOnTextChanged ( ) ;
44
- } , [ onTextChanged ] ) ;
45
40
46
- useEffect ( ( ) => {
47
- if ( containerRef . current ) {
48
- containerRef . current . className = className ?? '' ;
49
- wrapperConfig . htmlContainer = containerRef . current ;
50
- }
51
- } , [ className ] ) ;
52
-
53
- const handleReInit = useCallback ( async ( ) => {
54
- await destroyMonaco ( ) ;
55
- await initMonaco ( ) ;
56
- await startMonaco ( ) ;
57
- } , [ wrapperConfig ] ) ;
58
-
59
- const initMonaco = useCallback ( async ( ) => {
60
- if ( containerRef . current ) {
61
- wrapperConfig . htmlContainer = containerRef . current ;
62
- await wrapperRef . current . init ( wrapperConfig ) ;
63
- } else {
64
- throw new Error ( 'No htmlContainer found! Aborting...' ) ;
65
- }
66
- } , [ wrapperConfig ] ) ;
41
+ const initMonaco = async ( ) => {
42
+ if ( containerRef . current ) {
43
+ wrapperConfig . htmlContainer = containerRef . current ;
44
+ await wrapperRef . current . init ( wrapperConfig ) ;
45
+ } else {
46
+ throw new Error ( 'No htmlContainer found! Aborting...' ) ;
47
+ }
48
+ } ;
67
49
68
- const startMonaco = useCallback ( async ( ) => {
69
- if ( containerRef . current ) {
70
- try {
71
- wrapperRef . current . registerTextChangeCallback ( onTextChanged ) ;
72
- await wrapperRef . current . start ( ) ;
73
- onLoad ?.( wrapperRef . current ) ;
74
- handleOnTextChanged ( ) ;
75
- } catch ( e ) {
76
- if ( onError ) {
77
- onError ( e ) ;
78
- } else {
79
- throw e ;
50
+ const startMonaco = async ( ) => {
51
+ if ( containerRef . current ) {
52
+ try {
53
+ wrapperRef . current . registerTextChangeCallback ( onTextChanged ) ;
54
+ await wrapperRef . current . start ( ) ;
55
+ onLoad ?.( wrapperRef . current ) ;
56
+ } catch ( e ) {
57
+ if ( onError ) {
58
+ onError ( e ) ;
59
+ } else {
60
+ throw e ;
61
+ }
80
62
}
63
+ } else {
64
+ throw new Error ( 'No htmlContainer found! Aborting...' ) ;
81
65
}
82
- } else {
83
- throw new Error ( 'No htmlContainer found! Aborting...' ) ;
84
- }
85
- } , [ onError , onLoad , onTextChanged ] ) ;
66
+ } ;
86
67
87
- const handleOnTextChanged = useCallback ( ( ) => {
88
- if ( ! onTextChanged ) return ;
89
- } , [ onTextChanged , wrapperConfig ] ) ;
68
+ ( async ( ) => {
69
+ await initMonaco ( ) ;
70
+ await startMonaco ( ) ;
71
+ } ) ( ) ;
90
72
91
- const destroyMonaco = useCallback ( async ( ) => {
92
- try {
93
- await wrapperRef . current . dispose ( ) ;
94
- } catch {
95
- // The language client may throw an error during disposal.
96
- // This should not prevent us from continue working.
97
- }
98
- } , [ ] ) ;
73
+ return ( ) => {
74
+ destroyMonaco ( ) ;
75
+ } ;
76
+
77
+ } , [ wrapperConfig ] ) ;
99
78
100
79
return (
101
80
< div
0 commit comments