@@ -16,6 +16,10 @@ import { getOrCreateModel } from './utils'
16
16
import { loadGrammars , loadTheme } from ' monaco-volar'
17
17
import { Store } from ' ../store'
18
18
import type { PreviewMode } from ' ../editor/types'
19
+ import parserBabel from ' prettier/parser-babel'
20
+ import parserHtml from ' prettier/parser-html'
21
+ import parserPostcss from ' prettier/parser-postcss'
22
+ import prettier from ' prettier/standalone'
19
23
20
24
const props = withDefaults (
21
25
defineProps <{
@@ -41,6 +45,7 @@ const store = inject<Store>('store')!
41
45
initMonaco (store )
42
46
43
47
const lang = computed (() => (props .mode === ' css' ? ' css' : ' javascript' ))
48
+ const extension = computed (() => props .filename .split (' .' ).at (- 1 ))
44
49
45
50
const replTheme = inject <Ref <' dark' | ' light' >>(' theme' )!
46
51
onMounted (async () => {
@@ -146,6 +151,31 @@ onMounted(async () => {
146
151
emit (' change' , editorInstance .getValue ())
147
152
})
148
153
154
+ editorInstance .onDidBlurEditorWidget (() => {
155
+ const parser = {
156
+ vue: ' html' ,
157
+ html: ' html' ,
158
+ css: ' css' ,
159
+ js: ' babel' ,
160
+ ts: ' babel' ,
161
+ }[extension .value ] || props .mode
162
+
163
+ const options = {
164
+ parser ,
165
+ plugins: [parserBabel , parserHtml , parserPostcss ],
166
+ semi: false ,
167
+ singleQuote: true ,
168
+ arrowParens: ' avoid' as const ,
169
+ }
170
+
171
+ let code = editorInstance .getValue ()
172
+ try {
173
+ code = prettier .format (code , options )
174
+ } catch (err ) {}
175
+
176
+ emit (' change' , code )
177
+ })
178
+
149
179
// update theme
150
180
watch (replTheme , (n ) => {
151
181
editorInstance .updateOptions ({
0 commit comments