@@ -280,6 +280,15 @@ export const createEditor = async (options: EditorOptions): Promise<CodeEditor>
280280 } ) ;
281281 setModel ( editor , options . value , language ) ;
282282
283+ const getOrCreateModel = ( value : string , lang : string | undefined , uri : Monaco . Uri ) => {
284+ const model = monaco . editor . getModel ( uri ) ;
285+ if ( model ) {
286+ model . setValue ( value ) ;
287+ return model ;
288+ }
289+ return monaco . editor . createModel ( value , lang , uri ) ;
290+ } ;
291+
283292 const contentEditors : Array < EditorOptions [ 'editorId' ] > = [ 'markup' , 'style' , 'script' , 'tests' ] ;
284293 if ( contentEditors . includes ( editorId ) ) {
285294 editors . push ( editor ) ;
@@ -393,24 +402,24 @@ export const createEditor = async (options: EditorOptions): Promise<CodeEditor>
393402 } ) ;
394403 if ( ! scriptEditor ) return ;
395404 const ext = scriptLanguage === 'typescript' ? 'tsx' : 'jsx' ;
396- const createModel = ( ) => {
397- scriptModel = monaco . editor . createModel (
398- scriptEditor . getValue ( ) ,
399- scriptLanguage ,
400- monaco . Uri . parse ( 'script.' + ext ) ,
401- ) ;
402- } ;
403- if ( scriptModel ) {
404- scriptModel . dispose ( ) ;
405- setTimeout ( ( ) => {
406- createModel ( ) ;
407- } , 300 ) ;
408- } else {
409- createModel ( ) ;
410- }
405+ scriptModel = getOrCreateModel (
406+ scriptEditor . getValue ( ) ,
407+ scriptLanguage ,
408+ monaco . Uri . parse ( 'script.' + ext ) ,
409+ ) ;
411410 } ;
412411 createScriptModel ( ) ;
413412
413+ const addDeclarations = ( ) => {
414+ if ( editorId !== 'script' ) return ;
415+ const declarations = `
416+ declare module 'https://*';
417+ declare module './*';
418+ ` ;
419+ getOrCreateModel ( declarations , undefined , monaco . Uri . parse ( 'file:///declarations.d.ts' ) ) ;
420+ } ;
421+ addDeclarations ( ) ;
422+
414423 const clearTypes = ( allTypes = true ) => {
415424 scriptModel ?. dispose ( ) ;
416425 if ( editorId === 'tests' ) return ;
0 commit comments