vue中sql编辑器组件及其格式化,运行代码

1、组件SqlEditor

<template>
  <div>
    <codemirror
      ref="myCm"
      :options="cmOptions"
      :value="value"
      @changes="onCmCodeChanges"
      @keydown.native="onKeyDown"
      @mousedown.native="onMouseDown"
    ></codemirror>
  </div>
</template>
<script>
import { codemirror } from 'vue-codemirror'
import 'codemirror/theme/idea.css'
import 'codemirror/lib/codemirror.css'
import 'codemirror/addon/hint/show-hint.css'
import 'codemirror/addon/scroll/simplescrollbars.css'
import 'codemirror/addon/edit/matchbrackets.js'
import 'codemirror/addon/scroll/simplescrollbars.js'
import 'codemirror/addon/selection/active-line.js'
import 'codemirror/mode/sql/sql.js'
import 'codemirror/addon/hint/show-hint.js'
import 'codemirror/addon/hint/sql-hint.js'
export default {
  components: {
    codemirror
  },
  props: ['value'],
  data() {
    return {
      editor: null,
      codemirror: '',
      timer: null,
      cmOptions: {
        theme: 'idea',
        mode: 'text/x-mysql',
        readOnly: false,
        line: true,
        tabSize: 4, // 制表符
        indentUnit: 2, // 缩进位数
        lineNumbers: true,
        lineWiseCopyCut: true,
        viewportMargin: 1000,
        autofocus: true,
        autocorrect: true,
        spellcheck: true,
        specialChars: /[\u0000-\u001f\u007f-\u009f\u00ad\u061c\u200b-\u200f\u2028\u2029\ufeff\ufff9-\ufffc]/g,
        extraKeys: { 'Tab': 'autocomplete' },
        lint: false,
        maxHighlightLength: Infinity,
        // 代码折叠
        gutters: ['CodeMirror-lint-markers', 'CodeMirror-linenumbers', 'CodeMirror-foldgutter'],
        lineWrapping: false,
        foldGutter: true, // 启用行槽中的代码折叠
        autoCloseBrackets: true, // 自动闭合符号
        autoCloseTags: true,
        matchTags: { bothTags: true },
        matchBrackets: true, // 在光标点击紧挨{、]括号左、右侧时,自动突出显示匹配的括号 }、]
        styleSelectedText: true,
        styleActiveLine: true, // 当前背景行高亮
        autoRefresh: true,
        highlightSelectionMatches: {
          minChars: 2,
          trim: true,
          style: 'matchhighlight',
          showToken: false
        },
        hintOptions: {
          completeSingle: false
        }
      }
    }
  },
  methods: {
    resetLint() {
      if (!this.$refs.myCm.codemirror.getValue()) {
        this.$nextTick(() => {
          this.$refs.myCm.codemirror.setOption('lint', false)
        })
        return
      }
      this.$refs.myCm.codemirror.setOption('lint', false)
      this.$nextTick(() => {
        this.$refs.myCm.codemirror.setOption('lint', true)
      })
    },
    // 按下键盘事件处理函数
    onKeyDown(event) {
      const keyCode = event.keyCode || event.which || event.charCode
      const keyCombination = event.ctrlKey || event.altKey || event.metaKey
      if (!keyCombination && keyCode > 64 && keyCode < 123) {
        this.$refs.myCm.codemirror.showHint({ completeSingle: false })
      }
    },
    // 按下鼠标时事件处理函数
    onMouseDown(event) {
      this.$refs.myCm.codemirror.closeHint()
    },
    //向父级组件发送事件
    sendsql() {
      this.timer = setTimeout(() => {
        let cm = this.$refs.myCm.codemirror
        this.$emit('onChangeCode', cm.getValue())
      }, 500)
    },
    onCmCodeChanges(cm, changes) {
      this.editorValue = cm.getValue()
      this.resetLint()
      if (!this.timer) {
        this.sendsql()
      } else {
        clearTimeout(this.timer)
        this.sendsql()
      }
    }
  },
  created() {
    try {
      if (!this.value) {
        this.cmOptions.lint = false
        return
      }
    } catch (e) {
      console.log('初始化codemirror出错' + e)
    }
  }
}
</script>
<style lang="less" scoped>
.CodeMirror-hints {
  z-index: 9999 !important;
}
</style>

2、父组件

  <div class="sql-editor">
    <SqlEditor ref="cmEditor" :value="sql" @onChangeCode="changeCode"></SqlEditor>
  </div>

import SqlEditor from './components/SqlEditor.vue'
import { format } from 'sql-formatter'//用来格式化,这里没放,用法很简单,不过要注意安装的版本,不然会报错

 components: //注册

//接收数据
    changeCode(val) {
      this.sql = val
      console.log(this.sql)
    },
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

waves0001

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值