1
- import * as monaco from 'monaco-editor' ;
1
+ import { editor , languages , KeyCode , KeyMod , Uri } from 'monaco-editor' ;
2
2
import { decode , encode } from './snippet-encoder' ;
3
- import './monaco -config' ;
3
+ import './worker -config' ;
4
4
5
5
import googleMapsDTSSource from '../../node_modules/@types/google.maps/index.d.ts?raw' ;
6
6
import markerExampleSource from '../../examples/00.default.ts?raw' ;
@@ -12,14 +12,17 @@ const modules: Record<string, string> = {
12
12
13
13
export async function initEditor (
14
14
runCallback : ( jsCode : string ) => void
15
- ) : Promise < monaco . editor . IStandaloneCodeEditor > {
16
- let { typescriptDefaults} = monaco . languages . typescript ;
15
+ ) : Promise < editor . IStandaloneCodeEditor > {
16
+ const { typescript} = languages ;
17
+ const { typescriptDefaults, ScriptTarget, ModuleKind, ModuleResolutionKind} =
18
+ typescript ;
19
+
17
20
typescriptDefaults . setEagerModelSync ( true ) ;
18
21
typescriptDefaults . setCompilerOptions ( {
19
- target : monaco . languages . typescript . ScriptTarget . ES2020 ,
22
+ target : ScriptTarget . ES2020 ,
20
23
allowNonTsExtensions : true ,
21
- moduleResolution : monaco . languages . typescript . ModuleResolutionKind . NodeJs ,
22
- module : monaco . languages . typescript . ModuleKind . CommonJS ,
24
+ moduleResolution : ModuleResolutionKind . NodeJs ,
25
+ module : ModuleKind . CommonJS ,
23
26
typeRoots : [ 'node_modules/@types' ]
24
27
} ) ;
25
28
@@ -35,7 +38,7 @@ export async function initEditor(
35
38
}
36
39
37
40
const container = document . querySelector ( '#editor' ) as HTMLElement ;
38
- const fileUri = monaco . Uri . parse ( 'file:///main.ts' ) ;
41
+ const fileUri = Uri . parse ( 'file:///main.ts' ) ;
39
42
40
43
let source = markerExampleSource ;
41
44
if ( location . hash ) {
@@ -58,20 +61,20 @@ export async function initEditor(
58
61
source = decoded . code ;
59
62
}
60
63
61
- const model = monaco . editor . createModel ( source , 'typescript' , fileUri ) ;
62
- const editor = monaco . editor . create ( container , {
64
+ const model = editor . createModel ( source , 'typescript' , fileUri ) ;
65
+ const editorInstance = editor . create ( container , {
63
66
theme : 'vs-dark' ,
64
67
minimap : { enabled : false } ,
65
68
fontSize : import . meta. env . PROD ? 20 : 12
66
69
} ) ;
67
- editor . setModel ( model ) ;
70
+ editorInstance . setModel ( model ) ;
68
71
69
- editor . addAction ( {
72
+ editorInstance . addAction ( {
70
73
id : 'compile-and-run' ,
71
74
label : 'Compile and run' ,
72
- keybindings : [ monaco . KeyMod . CtrlCmd | monaco . KeyCode . Enter ] ,
75
+ keybindings : [ KeyMod . CtrlCmd | KeyCode . Enter ] ,
73
76
async run ( editor ) {
74
- const worker = await monaco . languages . typescript . getTypeScriptWorker ( ) ;
77
+ const worker = await typescript . getTypeScriptWorker ( ) ;
75
78
const proxy = await worker ( model . uri ) ;
76
79
77
80
const { outputFiles} = await proxy . getEmitOutput ( model . uri . toString ( ) ) ;
@@ -83,18 +86,18 @@ export async function initEditor(
83
86
84
87
const runButton = document . querySelector ( '#btn-compile-and-run' ) ! ;
85
88
runButton . addEventListener ( 'click' , ( ) => {
86
- editor
89
+ editorInstance
87
90
. getAction ( 'compile-and-run' )
88
91
. run ( )
89
92
. then ( ( ) => {
90
93
console . log ( 'compie and run completed.' ) ;
91
94
} ) ;
92
95
} ) ;
93
96
94
- editor . addAction ( {
97
+ editorInstance . addAction ( {
95
98
id : 'save-to-url' ,
96
99
label : 'Save to URL' ,
97
- keybindings : [ monaco . KeyMod . CtrlCmd | monaco . KeyCode . KeyS ] ,
100
+ keybindings : [ KeyMod . CtrlCmd | KeyCode . KeyS ] ,
98
101
run ( editor ) {
99
102
const tsCode = editor . getModel ( ) ?. getValue ( ) ;
100
103
@@ -105,7 +108,7 @@ export async function initEditor(
105
108
}
106
109
} ) ;
107
110
108
- editor . focus ( ) ;
111
+ editorInstance . focus ( ) ;
109
112
110
- return editor ;
113
+ return editorInstance ;
111
114
}
0 commit comments