@@ -37,8 +37,8 @@ function ReactCodeMirror(props = {}, ref) {
3737
3838 // http://codemirror.net/doc/manual.html#config
3939 async function setOptions ( instance , opt = { } ) {
40- if ( typeof opt === 'object' ) {
41- const mode = CodeMirror . findModeByName ( opt . mode ) ;
40+ if ( typeof opt === 'object' && window ) {
41+ const mode = CodeMirror . findModeByName ( opt . mode || '' ) ;
4242 if ( mode && mode . mode ) {
4343 await import ( `codemirror/mode/${ mode . mode } /${ mode . mode } .js` ) ;
4444 }
@@ -54,7 +54,7 @@ function ReactCodeMirror(props = {}, ref) {
5454 }
5555
5656 useEffect ( ( ) => {
57- if ( ! editor ) {
57+ if ( ! editor && window ) {
5858 // 生成codemirror实例
5959 const instance = CodeMirror . fromTextArea ( textareaRef . current , { ...defaultOptions , ...options } ) ;
6060 const eventDict = getEventHandleFromProps ( ) ;
@@ -71,29 +71,29 @@ function ReactCodeMirror(props = {}, ref) {
7171 setOptions ( instance , { ...defaultOptions , ...options } ) ;
7272 }
7373 return ( ) => {
74- if ( editor ) {
74+ if ( editor && window ) {
7575 editor . toTextArea ( ) ;
7676 setEditor ( undefined ) ;
7777 }
7878 }
7979 } , [ ] ) ;
8080
8181 useMemo ( ( ) => {
82- if ( ! editor ) return ;
82+ if ( ! editor || ! window ) return ;
8383 const val = editor . getValue ( ) ;
8484 if ( value !== undefined && value !== val ) {
8585 editor . setValue ( value ) ;
8686 }
8787 } , [ value ] ) ;
8888
8989 useMemo ( ( ) => {
90- if ( ! editor ) return ;
90+ if ( ! editor || ! window ) return ;
9191 editor . setSize ( width , height ) ;
9292 } , [ width , height ] ) ;
9393
9494
9595 useMemo ( ( ) => {
96- if ( ! editor ) return ;
96+ if ( ! editor || ! window ) return ;
9797 setOptions ( editor , { ...defaultOptions , ...options } ) ;
9898 } , [ options ] ) ;
9999
0 commit comments