最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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 // 開啟語法檢查
    })

這里的配置只有幾個簡單的參數(shù),個人認為有這些功能已經(jīng)足夠了,更多的詳細參數(shù)配置可以移步官方文檔

如果想讓編輯器支持其他語言,可以查看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>

四、效果截圖

使用效果:

在這里插入圖片描述

語法檢測效果:

在這里插入圖片描述

在這里插入圖片描述

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論

全椒县| 浙江省| 渭南市| 福鼎市| 肥乡县| 浮山县| 大城县| 永安市| 四川省| 忻城县| 田阳县| 平度市| 海城市| 沙湾县| 苍山县| 垦利县| 台北市| 措勤县| 临江市| 子长县| 灵璧县| 鞍山市| 天等县| 临猗县| 上蔡县| 佛山市| 辽中县| 保康县| 山东省| 达州市| 兴山县| 呼伦贝尔市| 奉新县| 墨江| 丹巴县| 古交市| 忻城县| 龙口市| 潮州市| 长阳| 新闻|