1- <script lang="ts">
2- import { loadMonacoEnv , loadWasm } from ' ./env'
3-
4- let init = false
5- </script >
6-
71<script lang="ts" setup>
82import {
93 onMounted ,
104 onBeforeUnmount ,
115 ref ,
126 shallowRef ,
137 nextTick ,
14- watchEffect ,
158 inject ,
169 watch ,
1710 computed ,
1811} from ' vue'
1912import * as monaco from ' monaco-editor-core'
13+ import { initMonaco } from ' ./env'
2014import { getOrCreateModel } from ' ./utils'
2115import { loadGrammars , loadTheme } from ' monaco-volar'
2216import { Store } from ' ../store'
@@ -34,46 +28,16 @@ const props = withDefaults(
3428 }
3529)
3630
37- const emits = defineEmits <{
31+ const emit = defineEmits <{
3832 (e : ' change' , value : string ): void
3933}>()
4034
41- const containerRef = ref <HTMLDivElement | null >()
35+ const containerRef = ref <HTMLDivElement >()
4236const ready = ref (false )
43- const editor = shallowRef <monaco .editor .IStandaloneCodeEditor | undefined >(
44- undefined
45- )
46- const store = inject (' store' ) as Store
37+ const editor = shallowRef <monaco .editor .IStandaloneCodeEditor >()
38+ const store = inject <Store >(' store' )!
4739
48- if (! init ) {
49- init = true
50- loadMonacoEnv (store )
51- loadWasm ()
52- }
53-
54- if (! props .readonly ) {
55- watchEffect (() => {
56- // create a model for each file in the store
57- for (const filename in store .state .files ) {
58- const file = store .state .files [filename ]
59- if (monaco .editor .getModel (monaco .Uri .parse (` file:///${filename } ` )))
60- continue
61- getOrCreateModel (
62- monaco .Uri .parse (` file:///${filename } ` ),
63- file .language ,
64- file .code
65- )
66- }
67-
68- // dispose of any models that are not in the store
69- for (const model of monaco .editor .getModels ()) {
70- if (store .state .files [model .uri .toString ().substring (' file:///' .length )])
71- continue
72- if (model .uri .toString ().startsWith (' file:///node_modules/' )) continue
73- model .dispose ()
74- }
75- })
76- }
40+ initMonaco (store )
7741
7842const lang = computed (() => (props .mode === ' css' ? ' css' : ' javascript' ))
7943
@@ -106,22 +70,6 @@ onMounted(async () => {
10670 })
10771 editor .value = editorInstance
10872
109- // Support for go to definition
110- monaco .editor .registerEditorOpener ({
111- openCodeEditor(_ , resource ) {
112- const path = resource .path
113- if (/ ^ \/ / .test (path ) && ! / ^ \/ node_modules/ .test (path )) {
114- const fileName = path .replace (' /' , ' ' )
115- if (fileName !== store .state .activeFile .filename ) {
116- store .setActive (fileName )
117- return true
118- }
119- }
120-
121- return false
122- },
123- })
124-
12573 // Support for semantic highlighting
12674 const t = (editorInstance as any )._themeService ._theme
12775 t .getTokenStyleMetadata = (
@@ -182,7 +130,7 @@ onMounted(async () => {
182130 })
183131
184132 editorInstance .onDidChangeModelContent (() => {
185- emits (' change' , editorInstance .getValue ())
133+ emit (' change' , editorInstance .getValue ())
186134 })
187135
188136 editorInstance .onDidChangeCursorSelection ((e ) => {
0 commit comments