11/* eslint-disable no-empty */
22import { useEffect , useState , useRef , CSSProperties } from 'react' ;
33import loader from '@monaco-editor/loader' ;
4-
5- loader . config ( {
6- paths : {
7- vs : 'https://g.alicdn.com/code/lib/monaco-editor/0.31.1/min/vs' ,
8- } ,
9- } ) ;
4+ import { getMonaco } from './monaco' ;
105
116type IAmbigousFn = ( ...args : any [ ] ) => any ;
127
@@ -52,7 +47,7 @@ export interface IMonacoInstance {
5247 [ otherKeys : string ] : any ;
5348}
5449
55- type ICodeEditorViewState = {
50+ interface ICodeEditorViewState {
5651 contributionsState : any ;
5752 cursorState : any ;
5853 viewState : any ;
@@ -154,7 +149,7 @@ export const useEditor = (type: 'single' | 'diff', props: IGeneralManacoEditorPr
154149 const editorWillMountRef = useRef < ISingleMonacoEditorProps [ 'editorWillMount' ] > ( ) ;
155150
156151 const decomposeRef = useRef ( false ) ;
157- const viewStatusRef = useRef < Map < any , ICodeEditorViewState > > ( new Map ( ) )
152+ const viewStatusRef = useRef < Map < any , ICodeEditorViewState > > ( new Map ( ) ) ;
158153
159154 useEffect ( ( ) => {
160155 editorDidMountRef . current = editorDidMount ;
@@ -185,7 +180,7 @@ export const useEditor = (type: 'single' | 'diff', props: IGeneralManacoEditorPr
185180 loader . config ( requireConfigRef . current ) ;
186181 }
187182
188- loader . init ( )
183+ getMonaco ( requireConfigRef . current )
189184 . then ( ( monaco : any ) => {
190185 // 兼容旧版本 monaco-editor 写死 MonacoEnvironment 的问题
191186 ( window as any ) . MonacoEnvironment = undefined ;
@@ -209,7 +204,7 @@ export const useEditor = (type: 'single' | 'diff', props: IGeneralManacoEditorPr
209204 monaco ,
210205 valueRef . current ?? defaultValueRef . current ?? '' ,
211206 languageRef . current ,
212- pathRef . current
207+ pathRef . current ,
213208 ) ;
214209 editor = monaco . editor . create ( containerRef . current , {
215210 automaticLayout : true ,
@@ -268,7 +263,7 @@ export const useEditor = (type: 'single' | 'diff', props: IGeneralManacoEditorPr
268263 ? editorRef . current . getModifiedEditor ( )
269264 : editorRef . current ;
270265
271- const nextValue = value ?? defaultValueRef . current ?? ''
266+ const nextValue = value ?? defaultValueRef . current ?? '' ;
272267 if ( editor ?. getOption ?.( monacoRef . current ?. editor . EditorOption . readOnly ) ) {
273268 editor ?. setValue ( nextValue ) ;
274269 } else if ( value !== editor ?. getValue ( ) ) {
@@ -330,7 +325,7 @@ export const useEditor = (type: 'single' | 'diff', props: IGeneralManacoEditorPr
330325 if ( valueRef . current !== null && valueRef . current !== undefined && model . getValue ( ) !== valueRef . current ) {
331326 model . setValue ( valueRef . current ) ;
332327 }
333-
328+
334329 if ( model !== editorRef . current . getModel ( ) ) {
335330 saveViewState && viewStatusRef . current . set ( previousPath , editorRef . current . saveViewState ( ) ) ;
336331 editorRef . current . setModel ( model ) ;
@@ -365,9 +360,9 @@ function getOrCreateModel(monaco: IMonacoInstance, value?: string, language?: st
365360}
366361
367362function usePrevious < T > ( value : T ) {
368- const ref = useRef < T > ( )
363+ const ref = useRef < T > ( ) ;
369364 useEffect ( ( ) => {
370- ref . current = value
371- } , [ value ] )
372- return ref . current
365+ ref . current = value ;
366+ } , [ value ] ) ;
367+ return ref . current ;
373368}
0 commit comments