suerprisePlus
2024-05-30 d4c810f9803aa13d806724a5f4f71fe85abac38e
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
119
120
121
122
123
124
125
126
127
128
<template>
  <div class="json-editor">
    <textarea ref="textarea" />
  </div>
</template>
 
<script>
// import sqlFormatter from "sql-formatter";
import CodeMirror from "codemirror";
import "codemirror/lib/codemirror.css";
import "codemirror/mode/sql/sql.js";
// 替换主题这里需修改名称
import "codemirror/theme/idea.css";
// 支持代码自动补全
import "codemirror/addon/hint/show-hint.css";
import "codemirror/addon/hint/show-hint.js";
import "codemirror/addon/hint/anyword-hint.js";
 
// JSON代码高亮需要由JavaScript插件支持
import "codemirror/mode/javascript/javascript.js";
 
// 支持各种代码折叠
import "codemirror/addon/fold/foldgutter.css";
import "codemirror/addon/fold/foldcode.js";
import "codemirror/addon/fold/foldgutter.js";
import "codemirror/addon/fold/brace-fold.js";
import "codemirror/addon/fold/comment-fold.js";
 
// 支持括号自动匹配
import "codemirror/addon/edit/matchbrackets.js";
import "codemirror/addon/edit/closebrackets.js";
 
// 行注释
import "codemirror/addon/comment/comment.js";
// JSON错误检查
import "codemirror/addon/lint/lint.css";
import "codemirror/addon/lint/lint.js";
 
export default {
  props: {
    value: '',
    height: {
      type: String,
      required: true,
    },
    myMode: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      editor: false,
    };
  },
  watch: {
    value(value) {
      const editorValue = this.editor.getValue();
      if (value !== editorValue) {
        if (typeof this.value !== "undefined") {
          this.editor.setValue(this.value);
        } else {
          this.editor.setValue("");
        }
      }
    },
  },
  mounted() {
    this.editor = CodeMirror.fromTextArea(this.$refs.textarea, {
      mode: this.myMode, //语言
      smartIndent: true, // 是否智能缩进
      styleActiveLine: true, // 当前行高亮
      lineNumbers: true, // 是否显示行数
      indentUnit: 2, // 缩进单位,默认2
      gutters: [
        "CodeMirror-linenumbers",
        "CodeMirror-foldgutter",
        "CodeMirror-lint-markers", // CodeMirror-lint-markers是实现语法报错功能
      ],
      lint: true,
      //lineWrapping: true, // 自动换行
      matchBrackets: true, // 括号匹配显示
      autoCloseBrackets: true, // 输入和退格时成对
      readOnly: false, // 只读
      foldGutter: true,
      autoRefresh: true,
    });
    //代码自动提示功能,记住使用cursorActivity事件不要使用change事件,这是一个坑,那样页面直接会卡死
    this.editor.on("inputRead", () => {
      this.editor.showHint();
    });
    this.editor.setSize("auto", this.height);
    if (typeof this.value !== "undefined") {
      this.editor.setValue(this.value);
    } else {
      this.editor.setValue("");
    }
  },
  methods: {
    getValue() {
      return this.editor.getValue();
    },
    // formatSql() {
    //   /*(sql编辑器内容绑定content参数) 将sql内容进行格式后放入编辑器中*/
    //   this.content = sqlFormatter.format(this.content);
    // },
  },
};
</script>
 
<style scoped>
.json-editor {
  height: 100%;
}
 
.json-editor >>> .CodeMirror {
  font-size: 14px;
  /* overflow-y:auto; */
  font-weight: normal;
}
 
.json-editor >>> .CodeMirror-scroll {
}
 
.json-editor >>> .cm-s-rubyblue span.cm-string {
  color: #f08047;
}
</style>