vue版本的yaml代码编辑器组件开发

目录
文章目录隐藏
  1. 前言
  2. 准备
  3. 组件源码及说明
  4. 组件使用
  5. 效果截图
  6. 结语

前言

最近公司在做 ETL 系统时,有这样一个需求,在 gpss 任务管理内新创建的任务,要求需要完美展示 ymal 格式的文本,并且可以对其修改并提交,关键是还要对 yaml 编辑时进行校验,防止输入语法不正确。虽然网上有很多在线编辑器插件,但都不是 Vue 版本,所以在查阅一些资料以后,自己动手封装一个。

准备

此组件的功能主要依赖于codemirror,另外加入了js-yaml进行语法检查,方便在实时编辑时提示语法不正确的地方,因此首先需要在项目中安装codemirrorjs-yaml

codemirror:

npm install codemirror --save

js-yaml:

npm install js-yaml --save

组件源码及说明

新建@/components/YamlEditor/index.vue文件:

<template>
  <div class="yaml-editor">
    <textarea ref="textarea" />
  </div>
</template>

<script>
import CodeMirror from 'codemirror'
import 'codemirror/addon/lint/lint.css'
import 'codemirror/lib/codemirror.css'
import 'codemirror/theme/monokai.css'
import 'codemirror/mode/yaml/yaml'
import 'codemirror/addon/lint/lint'
import 'codemirror/addon/lint/yaml-lint'

window.jsyaml = require('js-yaml') // 引入 js-yaml 为 codemirror 提高语法检查核心支持

export default {
  name: 'YamlEditor',
  // eslint-disable-next-line vue/require-prop-types
  props: ['value'],
  data() {
    return {
      yamlEditor: false
    }
  },
  watch: {
    value(value) {
      const editorValue = this.yamlEditor.getValue()
      if (value !== editorValue) {
        this.yamlEditor.setValue(this.value)
      }
    }
  },
  mounted() {
    this.yamlEditor = CodeMirror.fromTextArea(this.$refs.textarea, {
      lineNumbers: true, // 显示行号
      mode: 'text/x-yaml', // 语法 model
      gutters: ['CodeMirror-lint-markers'],  // 语法检查器
      theme: 'monokai', // 编辑器主题
      lint: true // 开启语法检查
    })

    this.yamlEditor.setValue(this.value)
    this.yamlEditor.on('change', (cm) => {
      this.$emit('changed', cm.getValue())
      this.$emit('input', cm.getValue())
    })
  },
  methods: {
    getValue() {
      return this.yamlEditor.getValue()
    }
  }
}
</script>

<style scoped>
.yaml-editor{
  height: 100%;
  position: relative;
}
.yaml-editor >>> .CodeMirror {
  height: auto;
  min-height: 300px;
}
.yaml-editor >>> .CodeMirror-scroll{
  min-height: 300px;
}
.yaml-editor >>> .cm-s-rubyblue span.cm-string {
  color: #F08047;
}
</style>

codemirror 的核心配置如下:

this.yamlEditor = CodeMirror.fromTextArea(this.$refs.textarea, {
    lineNumbers: true, // 显示行号
    mode: 'text/x-yaml', // 语法 model
    gutters: ['CodeMirror-lint-markers'],  // 语法检查器
    theme: 'monokai', // 编辑器主题
    lint: true // 开启语法检查
});

这里的配置只有几个简单的参数,个人认为有这些功能已经足够了,更多的详细参数配置可以移步官方文档;如果想让编辑器支持其他语言,可以查看 codemirror 官方文档的语法支持,这里我个人比较倾向下载 codemirror 源码,可以看到对应语法 demo 的源代码,使用不同的语法在本组件中 import 相应的依赖即可。

组件使用

在需要使用的地方引入,我这里是从后台直接获取的,这里只展示主要使用方法:

<template>
  <div>
    <div class="editor-container">
      <yaml-editor v-model="formData.yaml" />
    </div>
  </div>
</template>

<script>
import { post } from "@Axios/api.js";
import YamlEditor from '@/components/YamlEditor/index.vue';

export default {
  name: 'YamlEditorDemo',
  components: { YamlEditor },
  data() {
    return {
      formData:{
        gpssUrl: "GPSS",
        yaml: "",
      }
    };
  },
  methods:{
    // ymal 模板
    templateData() {
      post("/kafka/template", {
        type: this.formData.gpssUrl,
      })
        .then((res) => {
          if (res.code === 0) {
            this.formData.yaml = res.data;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
  }
};
</script>
<style scoped>
.editor-container{
  position: relative;
  height: 100%;
}
</style>

效果截图

使用效果:

vue 版本的 yaml 代码编辑器组件开发

语法检测效果:

语法检测效果

结语

以上就是我在工作中使用的 vue 版本的 yaml 代码编辑器组件,希望对有同样需求的小伙伴提供帮助。

「点点赞赏,手留余香」

2

给作者打赏,鼓励TA抓紧创作!

微信微信 支付宝支付宝

还没有人赞赏,快来当第一个赞赏的人吧!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
码云笔记 » vue版本的yaml代码编辑器组件开发

发表回复