溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Vue2怎么安裝使用MonacoEditor

發(fā)布時間:2023-04-17 10:52:06 來源:億速云 閱讀:204 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要講解了“Vue2怎么安裝使用MonacoEditor”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Vue2怎么安裝使用MonacoEditor”吧!

1.安裝MonacoEditor

cnpm install -S editor@1.0.0
cnpm install -S monaco-editor@0.19.3
cnpm install -S monaco-editor-webpack-plugin@1.9.1

2.配置vue.config.js文件

const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
module.exports = {
  configureWebpack: {
    plugins: [
        new MonacoWebpackPlugin()
    ]
}

3.建立組件monaco.vue

<template>
  <div>
    <div class="code-container" ref="container"></div>
  </div>
</template>
 
<script>
import './icon/iconfont.css'
import * as monaco from 'monaco-editor'
import vCompletion from '@/utils/sql.js'//語法提示文件
export default {
  name: 'codeEditor',
  props: {
    opts: {
      type: Object,
      default() {
        return {
          language: 'java', // shell、sql、python
          readOnly: false, // 不能編輯
        }
      },
    },
    value:{
      type:String,
      default:''
    }
  },
  watch: {
    value: {
      handler(n) {
        if(this.showInit){//初次傳值初始化一次
          this.init()
          this.showInit = false
        }
        this.monacoInstance.setValue(n)//剩余全部更新內(nèi)容
      },
      deep: true,
    },
  },
  data() {
    return {
      monacoInstance: null,
      provider: null,
      hints: [],
      color: null,
      showInit:true
    }
  },
  created() {
    this.init()
  },
  mounted() {
    this.init()
  },
  beforeDestroy() {
    this.dispose()
  },
 
  methods: {
    cloneDeep(suggestions) {
      return JSON.parse(JSON.stringify(suggestions))
    },
    dispose() {
      if (this.monacoInstance) {
        if (this.monacoInstance.getModel()) {
          this.monacoInstance.getModel().dispose()
        }
        this.monacoInstance.dispose()
        this.monacoInstance = null
        if (this.provider) {
          this.provider.dispose()
          this.color.dispose()
          this.provider = null
        }
      }
    },
    init() {
      let that = this
      this.dispose()
      this.provider = monaco.languages.registerCompletionItemProvider('sql', {
        provideCompletionItems(model, position) {
          return {
            suggestions: that.cloneDeep(vCompletion),//自定義語法提示,代碼補全
          }
        },
        triggerCharacters: ['.'],//輸入點可觸發(fā)代碼提示
      })
        //自定義語法高亮
      this.color = monaco.languages.setMonarchTokensProvider('sql', {
        ignoreCase: true,
        tokenizer: {//需要什么顏色,就在里面用正則匹配
          root: [
            [
              /currentUser|#@|RsOk|#string|#switch|#case|selectSql|uuid|addOrderBy|addConditionRequired|countSql|addGroupBy|currentDateTime|addFieldExist|formData|simplePage|RsJson|[@]|RsJsonData|Local|select|#set|#include|#render|#end|#for|#if|#else|#elseif|from|where|addField|addConditionExist|table|SqlUtil|GROUP_CONCAT|BY|AND|ADD|Data|page|IF|as|SUM|MAX|min|AVG|COUNT|ROUND|LEFT|JOIN/,
              { token: 'keyword' },
            ], //藍色
            [
              /[+]|[-]|[*]|[/]|[%]|[>]|[<]|[=]|[!]|[:]|[&&]|[||]/,
              { token: 'string' },
            ], //紅色
            [/'.*?'|".*?"/, { token: 'string.escape' }], //橙色
            [/#--.*?\--#/, { token: 'comment' }], //綠色
            [/null/, { token: 'regexp' }], //粉色
            [/[{]|[}]/, { token: 'type' }], //青色
            // [/[\u4e00-\u9fa5]/, { token: 'predefined' }],//亮粉色
            // [/''/, { token: 'invalid' }],//紅色
            // [/[\u4e00-\u9fa5]/, { token: 'number.binary' }],//淺綠
            [/(?!.*[a-zA-Z])[0-9]/, { token: 'number.hex' }], //淺綠
            [/[(]|[)]/, { token: 'number.octal' }], //淺綠
            // [/[\u4e00-\u9fa5]/, { token: 'number.float' }],//淺綠
          ],
        },
      })
      // 初始化編輯器實例
      this.monacoInstance = monaco.editor.create(this.$refs['container'], {
        value: this.value,
        theme: 'vs-dark',
        autoIndex: true,
        ...this.opts,
      })
      // 監(jiān)聽編輯器content變化事件
      this.monacoInstance.onDidChangeModelContent(() => {
        this.$emit('contentChange', this.monacoInstance.getValue())
      })
    },
  },
}
</script>
<style lang="scss" scope>
.code-container {
  width: 100%;
  height: 500px;
  overflow: hidden;
  border: 1px solid #eaeaea;
  .monaco-editor .scroll-decoration {
    box-shadow: none;
  }
}
</style>

4.建立語法提示文件sql.js

export default [
  /**   * 內(nèi)置函數(shù)   */
  {
     label: 'if',//觸發(fā)提示的文本
     kind: monaco.languages.CompletionItemKind.Function,
     insertText: `\n#if()\n\t\n #end`,//補全的文本
     insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
     detail: '流程控制'
   },  {
     label: 'if/else',
     kind: monaco.languages.CompletionItemKind.Function,
     insertText: '\n#if()\n\n #else\n\n #end',
     insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
     detail: '流程控制'
   }
 ]

5.父組件引入monaco.vue

<template>
  <div>
    <moaco v-model="value" :opts="opts" @contentChange="contentChange"></moaco>
  </div>
</template>
 
<script>
import monaco from '@/monaco.vue'
export default {
  components: {
    monaco,
  },
  data() {
    return {
      value: '',
      countent: '',
      opts: {
        value: '',
        readOnly: false, // 是否可編輯
        language: 'sql', // 語言類型
        theme: 'vs-dark', // 編輯器主題
      },
    }
  },
  methods: {
    contentChange(val) {
      //每次改變編輯器內(nèi)容觸發(fā)事件,先用一個值存放數(shù)據(jù)
      this.countent = val
    },
    submit() {
      //在你提交給后臺時將this.countent賦值給value
      this.countent = this.value
    },
  },
}
</script>
 
<style>
</style>

感謝各位的閱讀,以上就是“Vue2怎么安裝使用MonacoEditor”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對Vue2怎么安裝使用MonacoEditor這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI