1
1
<template >
2
- <div class =" cl-codemirror" >
2
+ <div class =" cl-codemirror" ref = " editorRef " >
3
3
<textarea class =" cl-code" id =" editor" :height =" height" :width =" width" ></textarea >
4
4
</div >
5
5
</template >
6
6
7
7
<script lang="ts">
8
- import { defineComponent , onMounted , watch } from " vue" ;
8
+ import { defineComponent , nextTick , onMounted , ref , watch } from " vue" ;
9
9
import CodeMirror from " codemirror" ;
10
10
import beautifyJs from " js-beautify" ;
11
11
@@ -28,6 +28,8 @@ export default defineComponent({
28
28
emits: [" update:modelValue" , " load" ],
29
29
30
30
setup(props , { emit }) {
31
+ const editorRef = ref <any >(null );
32
+
31
33
let editor: any = null ;
32
34
33
35
// 获取内容
@@ -55,52 +57,48 @@ export default defineComponent({
55
57
);
56
58
57
59
onMounted (function () {
58
- // 实例化
59
- editor = CodeMirror .fromTextArea (document .getElementById (" editor" ), {
60
- mode: " javascript" ,
61
- theme: " ambiance" ,
62
- styleActiveLine: true ,
63
- lineNumbers: true ,
64
- lineWrapping: true ,
65
- indentUnit: 4 ,
66
- ... props .options
67
- });
68
-
69
- // 输入监听
70
- editor .on (" change" , (e : any ) => {
71
- emit (" update:modelValue" , e .getValue ().replace (/ \s / g , " " ));
60
+ nextTick (() => {
61
+ // 实例化
62
+ editor = CodeMirror .fromTextArea (editorRef .value .querySelector (" #editor" ), {
63
+ mode: " javascript" ,
64
+ theme: " ambiance" ,
65
+ styleActiveLine: true ,
66
+ lineNumbers: true ,
67
+ lineWrapping: true ,
68
+ indentUnit: 4 ,
69
+ ... props .options
70
+ });
71
+
72
+ // 输入监听
73
+ editor .on (" change" , (e : any ) => {
74
+ emit (" update:modelValue" , e .getValue ().replace (/ \s / g , " " ));
75
+ });
76
+
77
+ // 设置内容
78
+ setValue (props .modelValue );
79
+
80
+ // 加载回调
81
+ emit (" load" , editor );
82
+
83
+ // 设置编辑框大小
84
+ editor .setSize (props .width || " auto" , props .height || " auto" );
85
+
86
+ // shift + alt + f 格式化
87
+ editor .display .wrapper .onkeydown = (e : any ) => {
88
+ const keyCode = e .keyCode || e .which || e .charCode ;
89
+ const altKey = e .altKey || e .metaKey ;
90
+ const shiftKey = e .shiftKey || e .metaKey ;
91
+
92
+ if (altKey && shiftKey && keyCode == 70 ) {
93
+ setValue ();
94
+ }
95
+ };
72
96
});
73
-
74
- // 加载回调
75
- emit (" load" , editor );
76
-
77
- // 设置编辑框样式
78
- const el = editor .display .wrapper ;
79
-
80
- if (el ) {
81
- if (props .height ) {
82
- el .style .height = props .height || " 50px" ;
83
- }
84
-
85
- if (props .width ) {
86
- el .style .width = props .width ;
87
- }
88
- }
89
-
90
- // 设置内容
91
- setValue (props .modelValue );
92
-
93
- // shift + alt + f 格式化
94
- el .onkeydown = (e : any ) => {
95
- const keyCode = e .keyCode || e .which || e .charCode ;
96
- const altKey = e .altKey || e .metaKey ;
97
- const shiftKey = e .shiftKey || e .metaKey ;
98
-
99
- if (altKey && shiftKey && keyCode == 70 ) {
100
- setValue ();
101
- }
102
- };
103
97
});
98
+
99
+ return {
100
+ editorRef
101
+ };
104
102
}
105
103
});
106
104
</script >
0 commit comments