介绍

vue-codemirror是一款优秀的编辑器。支持很多种格式的语言编辑,如vue、js、css等。

如何扩展到json

1、安装依赖 npm install vue-codemirror --save

2、在main.js中引入

1
2
3
4
import { codemirror } from 'vue-codemirror'
import 'codemirror/lib/codemirror.css'

Vue.use(VueCodemirror)

3、编辑公共组件 code-editor.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<template>
<codemirror class="code-editor-wrapper" :options="cmOptions" v-bind="$attrs" v-on="$listeners" />
</template>

<script>
import { codemirror } from "vue-codemirror";

import "codemirror/theme/blackboard.css";
import "codemirror/theme/eclipse.css";
import "codemirror/mode/javascript/javascript.js";
import "codemirror/mode/xml/xml.js";
import "codemirror/mode/htmlmixed/htmlmixed.js";
import "codemirror/mode/css/css.js";
import "codemirror/mode/yaml/yaml.js";
import "codemirror/mode/sql/sql.js";
import "codemirror/mode/python/python.js";
import "codemirror/mode/markdown/markdown.js";
import "codemirror/addon/hint/show-hint.css";
import "codemirror/addon/hint/show-hint.js";
import "codemirror/addon/hint/javascript-hint.js";
import "codemirror/addon/hint/xml-hint.js";
import "codemirror/addon/hint/css-hint.js";
import "codemirror/addon/hint/html-hint.js";
import "codemirror/addon/hint/sql-hint.js";
import "codemirror/addon/hint/anyword-hint.js";
import "codemirror/addon/lint/lint.css";
import "codemirror/addon/lint/lint.js";
import "codemirror/addon/lint/json-lint";

require("script-loader!jsonlint");
import "codemirror/addon/lint/css-lint.js";
import "codemirror/addon/lint/javascript-lint.js";
import "codemirror/addon/fold/foldcode.js";
import "codemirror/addon/fold/foldgutter.js";
import "codemirror/addon/fold/foldgutter.css";
import "codemirror/addon/fold/brace-fold.js";
import "codemirror/addon/fold/xml-fold.js";
import "codemirror/addon/fold/comment-fold.js";
import "codemirror/addon/fold/markdown-fold.js";
import "codemirror/addon/fold/indent-fold.js";
import "codemirror/addon/edit/closebrackets.js";
import "codemirror/addon/edit/closetag.js";
import "codemirror/addon/edit/matchtags.js";
import "codemirror/addon/edit/matchbrackets.js";
import "codemirror/addon/selection/active-line.js";
import "codemirror/addon/search/jump-to-line.js";
import "codemirror/addon/dialog/dialog.js";
import "codemirror/addon/dialog/dialog.css";
import "codemirror/addon/search/searchcursor.js";
import "codemirror/addon/search/search.js";
import "codemirror/addon/display/autorefresh.js";
import "codemirror/addon/selection/mark-selection.js";
import "codemirror/addon/search/match-highlighter.js";
import "codemirror/mode/vue/vue.js";
import "codemirror/theme/monokai.css";
import "codemirror/addon/scroll/annotatescrollbar.js";
import "codemirror/addon/search/matchesonscrollbar.js";
import "codemirror/mode/clike/clike.js";
import "codemirror/addon/comment/comment.js";
import "codemirror/keymap/sublime.js";

export default {
name: "CodeEditor",
components: {
codemirror,
},
inheritAttrs: false,
props: {
options: Object,
mode: String,
},
computed: {
cmOptions() {
return {
tabSize: 2,
line: true,
mode: this.mode,
lineWrapping: false,
lineNumbers: true,
autofocus: false,
smartIndent: false,
autocorrect: true,
spellcheck: true,
lint: true,
foldGutter: true,
autoCloseBrackets: true,
autoCloseTags: true,
matchTags: { bothTags: true },
matchBrackets: true,
styleActiveLine: false,
autoRefresh: true,
highlightSelectionMatches: {
minChars: 2,
style: "matchhighlight",
showToken: true,
},
styleSelectedText: true,
enableAutoFormatJson: true,
defaultJsonIndentation: 2,
...this.options,
};
},
},
};
</script>

<style lang="scss" scoped>
.code-editor-wrapper {
height: 100%;
text-align: left;
overflow: auto;
border: 1px solid #ccc;

::v-deep .CodeMirror {
height: 100%;
}
}
</style>

4、在其他组件中使用

1
2
3
<code-editor v-model="jsonEg" mode="application/json" />

import CodeEditor from './code-editor.vue';

资源

文档:https://github.surmon.me/vue-codemirror/