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

AI 概述
前言准备组件源码及说明组件使用效果截图结语 前言 最近公司在做 ETL 系统时,有这样一个需求,在 gpss 任务管理内新创建的任务,要求需要完美展示 ymal 格式的文本,并且可以对其修改并提交,关键是还要对 yaml 编辑时进行校验,防止输入语法不正确。虽然网上有很多在线编辑器插件,但都不是 Vue 版本...
目录
文章目录隐藏
  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 代码编辑器组件,希望对有同样需求的小伙伴提供帮助。

以上关于vue版本的yaml代码编辑器组件开发的文章就介绍到这了,更多相关内容请搜索码云笔记以前的文章或继续浏览下面的相关文章,希望大家以后多多支持码云笔记。

「点点赞赏,手留余香」

2

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

微信微信 支付宝支付宝

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

声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 admin@mybj123.com 进行投诉反馈,一经查实,立即处理!
重要:如软件存在付费、会员、充值等,均属软件开发者或所属公司行为,与本站无关,网友需自行判断
码云笔记 » vue版本的yaml代码编辑器组件开发

发表回复