vue yaml代碼編輯器組件問題
一、前期準(zhǔn)備
此組件的功能主要依賴于codemirror,另外加入了js-yaml進(jìn)行語法檢查,方便在實時編輯時提示語法不正確的地方。
因此首先需要在項目中安裝codemirror與js-yaml:
- codemirror:
npm install codemirror
- 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 // 開啟語法檢查 })
這里的配置只有幾個簡單的參數(shù),個人認(rèn)為有這些功能已經(jīng)足夠了,更多的詳細(xì)參數(shù)配置可以移步官方文檔;
如果想讓編輯器支持其他語言,可以查看codemirror官方文檔的語法支持,這里我個人比較傾向下載codemirror源碼,可以看到對應(yīng)語法demo的源代碼,使用不同的語法在本組件中import
相應(yīng)的依賴即可。
三、組件使用
<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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vite配置別名并處理報錯:找不到模塊“xxx”或其相應(yīng)的類型聲明方法詳解
我在學(xué)習(xí)vue3+vite+ts的時候,在配置別名這一步的時候遇到了一個問題,這篇文章主要給大家介紹了關(guān)于vite配置別名并處理報錯:找不到模塊“xxx”或其相應(yīng)的類型聲明的相關(guān)資料,需要的朋友可以參考下2022-11-11Vue實現(xiàn)電商網(wǎng)站商品放大鏡效果示例
這篇文章主要為大家介紹了Vue實現(xiàn)電商網(wǎng)站商品放大鏡效果示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10VScode中配置ESlint+Prettier詳細(xì)步驟(附圖文介紹)
這篇文章主要介紹了VScode中配置ESlint+Prettier詳細(xì)步驟,文中通過代碼示例講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-12-12vue源碼解析computed多次訪問會有死循環(huán)原理
這篇文章主要為大家介紹了vue源碼解析computed多次訪問會有死循環(huán)原理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04