Blame view

ant-design-vue-jeecg/src/components/jeecg/JCodeEditor.vue 11.6 KB
肖超群 authored
1
2
3
4
5
6
<template>
  <div v-bind="fullScreenParentProps">
    <a-icon v-if="fullScreen" class="full-screen-icon" :type="iconType" @click="()=>fullCoder=!fullCoder"/>

    <div class="code-editor-cust full-screen-child">
      <textarea ref="textarea"></textarea>
肖超群 authored
7
8
      <span @click="nullTipClick" class="null-tip" :class="{'null-tip-hidden':hasCode}"
            :style="nullTipStyle">{{ placeholderShow }}</span>
肖超群 authored
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
      <template v-if="languageChange">
        <a-select v-model="mode" size="small" class="code-mode-select" @change="changeMode" placeholder="请选择主题">
          <a-select-option
            v-for="mode in modes"
            :key="mode.value"
            :value="mode.value">
            {{ mode.label }}
          </a-select-option>
        </a-select>
      </template>

    </div>
  </div>
</template>

<script type="text/ecmascript-6">
肖超群 authored
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
// 引入全局实例
import _CodeMirror from 'codemirror'

// 核心样式
import 'codemirror/lib/codemirror.css'
// 引入主题后还需要在 options 中指定主题才会生效 darcula  gruvbox-dark hopscotch  monokai
import 'codemirror/theme/panda-syntax.css'
//提示css
import "codemirror/addon/hint/show-hint.css";

// 需要引入具体的语法高亮库才会有对应的语法高亮效果
// codemirror 官方其实支持通过 /addon/mode/loadmode.js 和 /mode/meta.js 来实现动态加载对应语法高亮库
// 但 vue 貌似没有无法在实例初始化后再动态加载对应 JS ,所以此处才把对应的 JS 提前引入
import 'codemirror/mode/javascript/javascript.js'
import 'codemirror/mode/css/css.js'
import 'codemirror/mode/xml/xml.js'
import 'codemirror/mode/clike/clike.js'
import 'codemirror/mode/markdown/markdown.js'
import 'codemirror/mode/python/python.js'
import 'codemirror/mode/r/r.js'
import 'codemirror/mode/shell/shell.js'
import 'codemirror/mode/sql/sql.js'
import 'codemirror/mode/swift/swift.js'
import 'codemirror/mode/vue/vue.js'

import {isIE11, isIE} from '@/utils/browser'

// 尝试获取全局实例
const CodeMirror = window.CodeMirror || _CodeMirror

export default {
  name: 'JCodeEditor',
  props: {
    // 外部传入的内容,用于实现双向绑定
    value: {
      type: String,
      default: ''
肖超群 authored
62
    },
肖超群 authored
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
    // 外部传入的语法类型
    language: {
      type: String,
      default: null
    },
    languageChange: {
      type: Boolean,
      default: false,
      required: false
    },
    placeholder: {
      type: String,
      default: null
    },
    // 显示行号
    lineNumbers: {
      type: Boolean,
      default: true
    },
    // 是否显示全屏按钮
    fullScreen: {
      type: Boolean,
      default: false
    },
    // 全屏以后的z-index
    zIndex: {
      type: [Number, String],
      default: 999
    },
    // 是否自适应高度,可以传String或Boolean
    // 传 String 类型只能写"!ie" ,
    // 填写这个字符串,代表其他浏览器自适应高度
    // 唯独IE下不自适应高度,因为IE下不支持min、max-height样式
    // 如果填写的不是"!ie"就视为true
    autoHeight: {
      type: [String, Boolean],
      default: true
    },
    // 不自适应高度的情况下生效的固定高度
    height: {
      type: [String, Number],
      default: '240px'
肖超群 authored
105
    },
肖超群 authored
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
  },
  data() {
    return {
      // 内部真实的内容
      code: '',
      iconType: 'fullscreen',
      hasCode: false,
      // 默认的语法类型
      mode: 'javascript',
      // 编辑器实例
      coder: null,
      // 默认配置
      options: {
        // 缩进格式
        tabSize: 2,
        // 主题,对应主题库 JS 需要提前引入
        theme: 'panda-syntax',
        line: true,
        // extraKeys: {'Ctrl': 'autocomplete'},//自定义快捷键
        hintOptions: {
          tables: {
            users: ['name', 'score', 'birthDate'],
            countries: ['name', 'population', 'size']
肖超群 authored
129
          }
肖超群 authored
130
        },
肖超群 authored
131
      },
肖超群 authored
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
      // 支持切换的语法高亮类型,对应 JS 已经提前引入
      // 使用的是 MIME-TYPE ,不过作为前缀的 text/ 在后面指定时写死了
      modes: [{
        value: 'css',
        label: 'CSS'
      }, {
        value: 'javascript',
        label: 'Javascript'
      }, {
        value: 'html',
        label: 'XML/HTML'
      }, {
        value: 'x-java',
        label: 'Java'
      }, {
        value: 'x-objectivec',
        label: 'Objective-C'
      }, {
        value: 'x-python',
        label: 'Python'
      }, {
        value: 'x-rsrc',
        label: 'R'
      }, {
        value: 'x-sh',
        label: 'Shell'
      }, {
        value: 'x-sql',
        label: 'SQL'
      }, {
        value: 'x-swift',
        label: 'Swift'
      }, {
        value: 'x-vue',
        label: 'Vue'
      }, {
        value: 'markdown',
        label: 'Markdown'
      }],
      // code 编辑器 是否全屏
      fullCoder: false
    }
  },
  watch: {
    fullCoder: {
      handler(value) {
        if (value) {
          this.iconType = "fullscreen-exit"
        } else {
          this.iconType = "fullscreen"
肖超群 authored
182
183
184
        }
      }
    },
肖超群 authored
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
    // value: {
    //   immediate: false,
    //   handler(value) {
    //     this._getCoder().then(() => {
    //       this.coder.setValue(value)
    //     })
    //   }
    // },
    language: {
      immediate: true,
      handler(language) {
        this._getCoder().then(() => {
          // 尝试从父容器获取语法类型
          if (language) {
            // 获取具体的语法类型对象
            let modeObj = this._getLanguage(language)

            // 判断父容器传入的语法是否被支持
            if (modeObj) {
              this.mode = modeObj.label
              this.coder.setOption('mode', `text/${modeObj.value}`)
            }
          }
        })
      }
    }
  },
  computed: {
    placeholderShow() {
      if (this.placeholder == null) {
        return `请在此输入${this.language}代码`
      } else {
        return this.placeholder
      }
    },
    nullTipStyle() {
      if (this.lineNumbers) {
        return {left: '36px'}
      } else {
        return {left: '12px'}
      }
    },
    // coder 配置
    coderOptions() {
      return {
        tabSize: this.options.tabSize,
        theme: this.options.theme,
        lineNumbers: this.lineNumbers,
        line: true,
        hintOptions: this.options.hintOptions
      }
    },
    isAutoHeight() {
      let {autoHeight} = this
      if (typeof autoHeight === 'string' && autoHeight.toLowerCase().trim() === '!ie') {
        autoHeight = !(isIE() || isIE11())
      } else {
        autoHeight = true
      }
      return autoHeight
    },
    fullScreenParentProps() {
      let props = {
        class: {
          'full-screen-parent': true,
          'full-screen': this.fullCoder,
          'auto-height': this.isAutoHeight
        },
        style: {}
      }
      if (isIE() || isIE11()) {
        props.style['height'] = '240px'
      }
      if (this.fullCoder) {
        props.style['z-index'] = this.zIndex
      }
      if (!this.isAutoHeight) {
        props.style['height'] = (typeof this.height === 'number' ? this.height + 'px' : this.height)
      }
      return props
    }
  },
  mounted() {
    // 初始化
    this._initialize()
  },
  methods: {
    // 初始化
    _initialize() {
      // 初始化编辑器实例,传入需要被实例化的文本域对象和默认配置
      this.coder = CodeMirror.fromTextArea(this.$refs.textarea, this.coderOptions)
      // 编辑器赋值
      if (this.value || this.code) {
        this.hasCode = true
        //this.coder.setValue(this.value || this.code)
        this.setCodeContent(this.value || this.code)
      } else {
        this.coder.setValue('')
        this.hasCode = false
      }
      // 支持双向绑定
      this.coder.on('change', (coder) => {
        this.code = coder.getValue()
        if (this.code) {
          this.hasCode = true
肖超群 authored
290
        } else {
肖超群 authored
291
          this.hasCode = false
肖超群 authored
292
        }
肖超群 authored
293
294
        if (this.$emit) {
          this.$emit('input', this.code)
肖超群 authored
295
        }
肖超群 authored
296
297
298
299
300
301
302
      })
      this.coder.on('focus', () => {
        this.hasCode = true
      })
      this.coder.on('blur', () => {
        if (this.code) {
          this.hasCode = true
肖超群 authored
303
        } else {
肖超群 authored
304
          this.hasCode = false
肖超群 authored
305
        }
肖超群 authored
306
307
308
309
310
311
      })

      /* this.coder.on('cursorActivity',()=>{
         this.coder.showHint()
       })*/
肖超群 authored
312
    },
肖超群 authored
313
314
    getCodeContent() {
      return this.code
肖超群 authored
315
    },
肖超群 authored
316
317
318
    setCodeContent(val) {
      setTimeout(() => {
        if (!val) {
肖超群 authored
319
          this.coder.setValue('')
肖超群 authored
320
321
        } else {
          this.coder.setValue(val)
肖超群 authored
322
        }
肖超群 authored
323
324
325
326
327
328
329
330
331
332
      }, 300)
    },
    // 获取当前语法类型
    _getLanguage(language) {
      // 在支持的语法类型列表中寻找传入的语法类型
      return this.modes.find((mode) => {
        // 所有的值都忽略大小写,方便比较
        let currentLanguage = language.toLowerCase()
        let currentLabel = mode.label.toLowerCase()
        let currentValue = mode.value.toLowerCase()
肖超群 authored
333
肖超群 authored
334
335
336
337
338
339
340
341
342
343
344
345
        // 由于真实值可能不规范,例如 java 的真实值是 x-java ,所以讲 value 和 label 同时和传入语法进行比较
        return currentLabel === currentLanguage || currentValue === currentLanguage
      })
    },
    _getCoder() {
      let _this = this
      return new Promise((resolve) => {
        (function get() {
          if (_this.coder) {
            resolve(_this.coder)
          } else {
            setTimeout(get, 10)
肖超群 authored
346
          }
肖超群 authored
347
348
349
350
351
352
353
        })()
      })
    },
    // 更改模式
    changeMode(val) {
      // 修改编辑器的语法配置
      this.coder.setOption('mode', `text/${val}`)
肖超群 authored
354
肖超群 authored
355
356
      // 获取修改后的语法
      let label = this._getLanguage(val).label.toLowerCase()
肖超群 authored
357
肖超群 authored
358
359
360
361
362
      // 允许父容器通过以下函数监听当前的语法值
      this.$emit('language-change', label)
    },
    nullTipClick() {
      this.coder.focus()
肖超群 authored
363
364
    }
  }
肖超群 authored
365
}
肖超群 authored
366
367
368
</script>

<style lang="less">
肖超群 authored
369
370
371
372
373
.code-editor-cust {
  flex-grow: 1;
  display: flex;
  position: relative;
  height: 100%;
肖超群 authored
374
肖超群 authored
375
376
377
378
379
380
  .CodeMirror {
    flex-grow: 1;
    z-index: 1;

    .CodeMirror-code {
      line-height: 19px;
肖超群 authored
381
    }
肖超群 authored
382
肖超群 authored
383
384
  }
肖超群 authored
385
386
387
388
389
390
391
  .code-mode-select {
    position: absolute;
    z-index: 2;
    right: 10px;
    top: 10px;
    max-width: 130px;
  }
肖超群 authored
392
肖超群 authored
393
394
395
396
  .CodeMirror {
    height: auto;
    min-height: 100%;
  }
肖超群 authored
397
肖超群 authored
398
399
400
401
402
403
404
405
  .null-tip {
    position: absolute;
    top: 4px;
    left: 36px;
    z-index: 10;
    color: #ffffffc9;
    line-height: initial;
  }
肖超群 authored
406
肖超群 authored
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
  .null-tip-hidden {
    display: none;
  }

  /**选中样式偶然出现高度不够的情况*/

  .CodeMirror-selected {
    min-height: 19px !important;
  }
}

/* 全屏样式 */
.full-screen-parent {
  position: relative;

  .full-screen-icon {
    opacity: 0;
    color: black;
    width: 20px;
    height: 20px;
    line-height: 24px;
    background-color: white;
    position: absolute;
    top: 2px;
    right: 2px;
    z-index: 9;
    cursor: pointer;
    transition: opacity 0.3s;
  }

  &:hover {
    .full-screen-icon {
      opacity: 1;

      &:hover {
        background-color: rgba(255, 255, 255, 0.88);
肖超群 authored
443
444
      }
    }
肖超群 authored
445
  }
肖超群 authored
446
肖超群 authored
447
448
449
450
451
452
453
454
  &.full-screen {
    position: fixed;
    top: 10px;
    left: 10px;
    width: calc(100% - 20px);
    height: calc(100% - 20px);
    padding: 10px;
    background-color: #f5f5f5;
肖超群 authored
455
肖超群 authored
456
457
458
    .full-screen-icon {
      top: 12px;
      right: 12px;
肖超群 authored
459
460
461
462
    }

    .full-screen-child {
      height: 100%;
肖超群 authored
463
464
      max-height: 100%;
      min-height: 100%;
肖超群 authored
465
    }
肖超群 authored
466
  }
肖超群 authored
467
肖超群 authored
468
469
470
  .full-screen-child {
    height: 100%;
  }
肖超群 authored
471
肖超群 authored
472
473
474
475
476
477
  &.auto-height {
    .full-screen-child {
      min-height: 120px;
      max-height: 320px;
      height: unset;
      overflow: hidden;
肖超群 authored
478
479
    }
肖超群 authored
480
481
482
483
484
    &.full-screen .full-screen-child {
      height: 100%;
      max-height: 100%;
      min-height: 100%;
    }
肖超群 authored
485
486
  }
肖超群 authored
487
488
489
490
}

.CodeMirror-cursor {
  height: 18.4px !important;
肖超群 authored
491
492
}
</style>