前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >编辑器中SQL关键字提示

编辑器中SQL关键字提示

作者头像
码客说
发布2023-09-22 08:38:27
3000
发布2023-09-22 08:38:27
举报
文章被收录于专栏:码客码客

前言

官方 https://github.com/surmon-china/vue-codemirror/tree/v4.0.1

安装依赖

代码语言:javascript
复制
npm install --save sql-formatter@2.3.3
npm install --save vue-codemirror@4.0.1
npm install --save vue-highlightjs

注意

需要注意vue-codemirror不能选取新版本,新版本需要vue3支持

自定义组件

SqlEditor.vue

代码语言:javascript
复制
<template>
  <div>
    <textarea ref="mycode" :value="sqlValue"> </textarea>
  </div>
</template>
<script>
import "codemirror/theme/ambiance.css";
import "codemirror/lib/codemirror.css";
import "codemirror/addon/hint/show-hint.css";
import CodeMirror from "codemirror";
import "codemirror/addon/edit/matchbrackets";
import "codemirror/addon/selection/active-line";
import "codemirror/mode/sql/sql";
import "codemirror/addon/hint/show-hint";
import "codemirror/addon/hint/sql-hint";
export default {
  data() {
    return {
      editor: null,
    };
  },
  props: {
    sqlValue: {
      type: String,
      default: "",
    },
    sqlStyle: {
      type: String,
      default: "default",
    },
    readOnly: {
      type: [Boolean, String],
    },
  },
  watch: {
    newVal() {
      if (this.editor) {
        this.$emit("changeTextarea", this.editor.getValue());
      }
    },
  },
  computed: {
    newVal() {
      if (this.editor) {
        return this.editor.getValue();
      } else {
        return "";
      }
    },
  },
  mounted() {
    let mime = "text/x-mariadb";
    this.editor = CodeMirror.fromTextArea(this.$refs.mycode, {
      value: this.sqlValue,
      mode: mime, //选择对应代码编辑器的语言,我这边选的是数据库,根据个人情况自行设置即可
      indentWithTabs: true,
      smartIndent: true,
      lineNumbers: true,
      matchBrackets: true,
      cursorHeight: 1,
      lineWrapping: true,
      readOnly: this.readOnly,
      theme: this.sqlStyle, //ambiance
      // autofocus: true,
      extraKeys: { Ctrl: "autocomplete" }, //自定义快捷键
      hintOptions: {
        //自定义提示选项
        // 当匹配只有一项的时候是否自动补全
        completeSingle: false,
      },
    });
    //代码自动提示功能,记住使用cursorActivity事件不要使用change事件,这是一个坑,那样页面直接会卡死
    this.editor.on("inputRead", () => {
      this.editor.showHint();
    });
  },
  methods: {
    setVal() {
      if (this.editor) {
        if (this.sqlValue === "") {
          this.editor.setValue("");
        } else {
          this.editor.setValue(this.sqlValue);
        }
      }
    },
  },
};
</script>
<style>
.CodeMirror {
  color: black;
  direction: ltr;
  line-height: 22px;
  text-align: left;
  border: 1px solid #f3f3f3;
}
.CodeMirror-hints {
  z-index: 9999 !important;
}
</style>

引用组件

代码语言:javascript
复制
<template>
  <div class="home">
    <SqlEditor
      ref="sqleditor"
      :sql-value="mForm.mSqlStr"
      @changeTextarea="changeTextarea($event)"
    />
    <input
      type="button"
      class="sql-btn"
      @click="formaterSql(mForm.mSqlStr)"
      value="格式化"
    />
  </div>
</template>
<script>
import sqlFormatter from "sql-formatter";
import SqlEditor from "@/components/SqlEditor";
export default {
  components: {
    SqlEditor,
  },
  data() {
    return {
      mForm: {
        mSqlStr: "",
      },
    };
  },
  methods: {
    changeTextarea(obj) {
      this.$set(this.mForm, "mSqlStr", obj);
    },
    formaterSql() {
      let sqleditor = this.$refs.sqleditor;
      sqleditor.editor.setValue(
        sqlFormatter.format(sqleditor.editor.getValue())
      );
    },
  },
};
</script>
<style>
.home {
  text-align: left;
}

.sql-btn {
  margin-top: 10px;
  cursor: pointer;
}
</style>

效果

image-20230921180130222
image-20230921180130222
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 安装依赖
  • 自定义组件
  • 引用组件
  • 效果
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档