9
9
watch ,
10
10
computed ,
11
11
type Ref ,
12
+ watchEffect ,
12
13
} from ' vue'
13
14
import * as monaco from ' monaco-editor-core'
14
15
import { initMonaco } from ' ./env'
@@ -44,7 +45,6 @@ const store = inject<Store>('store')!
44
45
45
46
initMonaco (store )
46
47
47
- const lang = computed (() => (props .mode === ' css' ? ' css' : ' javascript' ))
48
48
const extension = computed (() => props .filename .split (' .' ).at (- 1 ))
49
49
50
50
const replTheme = inject <Ref <' dark' | ' light' >>(' theme' )!
@@ -58,9 +58,6 @@ onMounted(async () => {
58
58
}
59
59
60
60
const editorInstance = monaco .editor .create (containerRef .value , {
61
- ... (props .readonly
62
- ? { value: props .value , language: lang .value }
63
- : { model: null }),
64
61
fontSize: 13 ,
65
62
theme: replTheme .value === ' light' ? theme .light : theme .dark ,
66
63
readOnly: props .readonly ,
@@ -100,55 +97,42 @@ onMounted(async () => {
100
97
}
101
98
}
102
99
103
- watch (
104
- () => props .value ,
105
- (value ) => {
106
- if (editorInstance .getValue () === value ) return
107
- editorInstance .setValue (value || ' ' )
108
- },
109
- { immediate: true }
110
- )
100
+ watchEffect (() => {
101
+ if (editorInstance .getValue () !== props .value )
102
+ editorInstance .setValue (props .value || ' ' )
111
103
112
- watch (lang , (lang ) =>
113
- monaco .editor .setModelLanguage (editorInstance .getModel ()! , lang )
114
- )
115
-
116
- if (! props .readonly ) {
117
- watch (
118
- () => props .filename ,
119
- (_ , oldFilename ) => {
120
- if (! editorInstance ) return
121
- const file = store .state .files [props .filename ]
122
- if (! file ) return null
123
- const model = getOrCreateModel (
124
- monaco .Uri .parse (` file:///${props .filename } ` ),
125
- file .language ,
126
- file .code
127
- )
128
-
129
- const oldFile = oldFilename ? store .state .files [oldFilename ] : null
130
- if (oldFile ) {
131
- oldFile .editorViewState = editorInstance .saveViewState ()
132
- }
133
-
134
- editorInstance .setModel (model )
135
-
136
- if (file .editorViewState ) {
137
- editorInstance .restoreViewState (file .editorViewState )
138
- editorInstance .focus ()
139
- }
140
- },
141
- { immediate: true }
142
- )
143
- }
104
+ editorInstance .updateOptions ({
105
+ readOnly: props .readonly ,
106
+ wordWrap: store .state .wordWrap ? ' on' : ' off' ,
107
+ theme: replTheme .value === ' light' ? theme .light : theme .dark ,
108
+ })
109
+ })
144
110
145
111
watch (
146
- () => store .state .wordWrap ,
147
- () => {
148
- editorInstance .updateOptions ({
149
- wordWrap: store .state .wordWrap ? ' on' : ' off' ,
150
- })
151
- }
112
+ () => props .filename ,
113
+ (_ , oldFilename ) => {
114
+ if (! editorInstance ) return
115
+ const file = store .state .files [props .filename ]
116
+ if (! file ) return null
117
+ const model = getOrCreateModel (
118
+ monaco .Uri .parse (` file:///${props .filename } ` ),
119
+ file .language ,
120
+ file .code
121
+ )
122
+
123
+ const oldFile = oldFilename ? store .state .files [oldFilename ] : null
124
+ if (oldFile ) {
125
+ oldFile .editorViewState = editorInstance .saveViewState ()
126
+ }
127
+
128
+ editorInstance .setModel (model )
129
+
130
+ if (file .editorViewState ) {
131
+ editorInstance .restoreViewState (file .editorViewState )
132
+ editorInstance .focus ()
133
+ }
134
+ },
135
+ { immediate: true }
152
136
)
153
137
154
138
await loadGrammars (monaco , editorInstance )
@@ -197,13 +181,6 @@ onMounted(async () => {
197
181
emit (' change' , code )
198
182
}
199
183
})
200
-
201
- // update theme
202
- watch (replTheme , (n ) => {
203
- editorInstance .updateOptions ({
204
- theme: n === ' light' ? theme .light : theme .dark ,
205
- })
206
- })
207
184
})
208
185
209
186
onBeforeUnmount (() => {
0 commit comments