vue yaml代码编辑器组件问题

 更新时间:2023年07月20日 14:19:01   作者:极值小白  
这篇文章主要介绍了vue yaml代码编辑器组件问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

一、前期准备

此组件的功能主要依赖于codemirror,另外加入了js-yaml进行语法检查,方便在实时编辑时提示语法不正确的地方。

因此首先需要在项目中安装codemirror与js-yaml:

二、组件源码及说明

新建@/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="value" />
    </div>
  </div>
</template>
<script>
import YamlEditor from '@/components/YamlEditor/index.vue';
const yamlData = "- hosts: all\n  become: yes\n  become_method: sudo\n  gather_facts: no\n\n  tasks:\n  - name: \"install {{ package_name }}\"\n    package:\n      name: \"{{ package_name }}\"\n      state: \"{{ state | default('present') }}\"";
export default {
  name: 'YamlEditorDemo',
  components: { YamlEditor },
  data() {
    return {
      value: yamlData,
    };
  },
};
</script>
<style scoped>
.editor-container{
  position: relative;
  height: 100%;
}
</style>

四、效果截图

使用效果:

在这里插入图片描述

语法检测效果:

在这里插入图片描述

在这里插入图片描述

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • vue使用qrcode生成二维码的方法

    vue使用qrcode生成二维码的方法

    这篇文章给大家介绍了vue使用qrcode生成二维码的方法,在Vue中实现二维码生成需要使用第三方库来处理生成二维码的逻辑,常用的库有qrcode和vue-qrcode,所以接下来小编将给大家介绍vue qrcode生成二维码的方法示例,需要的朋友可以参考下
    2024-01-01
  • 一文带你搞懂Vue Loader是如何工作的

    一文带你搞懂Vue Loader是如何工作的

    Vue Loader 作为一个 webpack 的 Loader,扮演着将 .vue 文件转化为浏览器可执行代码的角色,下面就跟随小编一起深入了解Vue Loader 的技术细节与工作机制吧
    2024-12-12
  • vite配置别名并处理报错:找不到模块“xxx”或其相应的类型声明方法详解

    vite配置别名并处理报错:找不到模块“xxx”或其相应的类型声明方法详解

    我在学习vue3+vite+ts的时候,在配置别名这一步的时候遇到了一个问题,这篇文章主要给大家介绍了关于vite配置别名并处理报错:找不到模块“xxx”或其相应的类型声明的相关资料,需要的朋友可以参考下
    2022-11-11
  • Vue实现电商网站商品放大镜效果示例

    Vue实现电商网站商品放大镜效果示例

    这篇文章主要为大家介绍了Vue实现电商网站商品放大镜效果示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • Vue2实现组件延迟加载的示例代码

    Vue2实现组件延迟加载的示例代码

    当一个页面需要加载较多个组件时,并且组件自身又比较复杂,如果一次性加载,可能等待时间较长,体验不好,这个时候就需要延迟加载了,本文为大家介绍了Vue2实现组件延迟加载的示例代码,需要的可以参考下
    2024-01-01
  • VScode中配置ESlint+Prettier详细步骤(附图文介绍)

    VScode中配置ESlint+Prettier详细步骤(附图文介绍)

    这篇文章主要介绍了VScode中配置ESlint+Prettier详细步骤,文中通过代码示例讲解的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下
    2024-12-12
  • vue源码解析computed多次访问会有死循环原理

    vue源码解析computed多次访问会有死循环原理

    这篇文章主要为大家介绍了vue源码解析computed多次访问会有死循环原理,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-04-04
  • vue中使用swiper轮播图的正确姿势(亲测有效)

    vue中使用swiper轮播图的正确姿势(亲测有效)

    最近在用Vue制作移动端项目,碰到了轮播图的制作,接下来就让小编一步一步带大家动作制作吧,下面这篇文章主要给大家介绍了关于vue中使用swiper轮播图的正确姿势,需要的朋友可以参考下
    2022-06-06
  • vue3中单文件组件<script setup>实例详解

    vue3中单文件组件<script setup>实例详解

    <script setup>是vue3中新引入的语法糖,目的是简化使用Composition API时冗长的模板代码,下面这篇文章主要给大家介绍了关于vue3中单文件组件<script setup>的相关资料,需要的朋友可以参考下
    2022-07-07
  • vue3 中v-model语法糖示例详解

    vue3 中v-model语法糖示例详解

    vue3中的v-model相当于vue2中的v-model和v-bind.sync 修饰符组合在一起的产物(择优整合)v-bind.sync 在 vue3 中被移除了可以在组件标签上使用多个 v-model 绑定属性,使用参数区分,这篇文章主要介绍了vue3 中v-model语法糖,需要的朋友可以参考下
    2024-06-06

最新评论