1
1
<script setup lang='ts'>
2
- import CodeMirror from ' codemirror'
2
+ import type { editor as MonacoEditor } from ' monaco-editor'
3
+ import * as monaco from ' monaco-editor'
3
4
import { useStore } from ' @/stores'
4
5
import { removeLeft } from ' @/utils'
5
6
@@ -18,22 +19,46 @@ const code = useLocalStorage(`formCustomConfig`, removeLeft(`
18
19
})
19
20
` ).trim ())
20
21
21
- const formCustomTextarea = useTemplateRef <HTMLTextAreaElement >(` formCustomTextarea ` )
22
+ const editorRef = useTemplateRef <HTMLDivElement >(` editorRef ` )
22
23
23
- const editor = ref <CodeMirror . EditorFromTextArea | null >(null )
24
+ const editor = ref <MonacoEditor . IStandaloneCodeEditor | null >(null )
24
25
25
26
onMounted (() => {
26
- editor .value = markRaw (CodeMirror .fromTextArea (formCustomTextarea .value ! , {
27
- mode: ` javascript ` ,
28
- theme: store .isDark ? ` darcula ` : ` xq-light ` ,
29
- lineNumbers: true ,
30
- }))
27
+ editor .value = monaco .editor .create (editorRef .value ! , {
28
+ value: code .value ,
29
+ language: ` javascript ` ,
30
+ theme: store .isDark ? ` vs-dark ` : ` vs ` ,
31
+ lineNumbers: ` on ` ,
32
+ automaticLayout: true ,
33
+ fontSize: 12 ,
34
+ lineHeight: 18 ,
35
+ })
36
+ })
31
37
32
- // 嵌套使用 nextTick 才能确保生效,具体原因未知
33
- nextTick (() => {
34
- nextTick (() => {
35
- editor .value ?.setValue (code .value )
38
+ function updateEditorStyle(fontSize : number , lineHeight : number ) {
39
+ if (editor .value ) {
40
+ editor .value .updateOptions ({
41
+ fontSize ,
42
+ lineHeight ,
36
43
})
44
+ }
45
+ }
46
+
47
+ onMounted (() => {
48
+ const updateStyle = () => {
49
+ if (window .innerWidth >= 768 ) {
50
+ updateEditorStyle (14 , 24 )
51
+ }
52
+ else {
53
+ updateEditorStyle (12 , 20 )
54
+ }
55
+ }
56
+
57
+ window .addEventListener (` resize ` , updateStyle )
58
+ updateStyle ()
59
+
60
+ onUnmounted (() => {
61
+ window .removeEventListener (` resize ` , updateStyle )
37
62
})
38
63
})
39
64
@@ -46,10 +71,10 @@ function formCustomSave() {
46
71
47
72
<template >
48
73
<div class =" space-y-4 min-w-0" >
49
- <div class =" h-60 border flex flex-col" >
50
- <textarea
51
- ref =" formCustomTextarea "
52
- placeholder = " Your custom code here. "
74
+ <div class =" h-60 border flex flex-col rounded-md " >
75
+ <div
76
+ ref =" editorRef "
77
+ class = " flex-1 "
53
78
/>
54
79
</div >
55
80
<Button
@@ -68,15 +93,4 @@ function formCustomSave() {
68
93
</template >
69
94
70
95
<style scoped>
71
- /* 覆盖全局的 overflow-x: hidden 设置 */
72
- :deep(.CodeMirror-scroll ) {
73
- overflow-x : auto !important ;
74
- overflow-y : auto !important ;
75
- }
76
-
77
- @media (max-width : 768px ) {
78
- :deep(.CodeMirror ) {
79
- font-size : 12px ;
80
- }
81
- }
82
96
</style >
0 commit comments