溫馨提示×

溫馨提示×

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

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

JS代碼編譯器中Monaco怎么用

發(fā)布時間:2021-06-11 11:28:04 來源:億速云 閱讀:265 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)JS代碼編譯器中Monaco怎么用的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

前言

我的需求是可以語法高亮、函數(shù)提示功能、自動換行、代碼折疊

Monaco

Monaco是微軟家的,支持的語言很多,還有縮略地圖,有時候提示不好用然后包體很大。
The Monaco Editor is the code editor that powers VS Code.

JS代碼編譯器中Monaco怎么用

使用方法官網(wǎng)

[官方文檔](https://microsoft.github.io/monaco-editor/index.html)
[在線demo](https://github.com/Microsoft/monaco-editor-samples)
[github](https://github.com/Microsoft/monaco-editor)

安裝

yarn add monaco-editor | npm install monaco-editor

引入

import * as monaco from 'monaco-editor' // 包體很大了 但是demo可以跑起來

//自定義一些提示函數(shù)
const suggestions = [
  {
    label: 'split_chinese',
    insertText: 'split_chinese(inputString,language);', // 不寫的時候不展示。。
    detail:
      'inputString:need split string\n' +
      'language:\nCH_T:traditional Chinese\nCH_S:Chinese Simplified\n HK_T:Hong Kong Traditional\nTW_T:Taiwan Traditional\n'
  },
  {
    label: 'uuid',
    insertText: 'var uuid = uuid();',
    detail: 'generate uuid'
  },
  {
    label: 'HashMap',
    insertText: 'var hashMap = new HashMap();',
    detail: 'create hash object'
  }
]

初始化

mounted() {
    monaco.languages.registerCompletionItemProvider('JavaScript', {
      provideCompletionItems() {
        return {
          suggestions: suggestions
        }
      },
      triggerCharacters: [' ', '.'] // 寫觸發(fā)提示的字符,可以有多個
    })
    let self = this
    setTimeout(function () {
      self.init()
    }, 50) //因為父組件還未傳參 子組件已經(jīng)渲染
  }
  
 //初始化方法
init(script) {
  let self = this
  if (script) this.code = script
  self.$refs.container.innerHTML = ''
  var editor = monaco.editor.create(this.$refs.container, {
    value: this.code,
    language: 'javascript',
    minimap: {
      enabled: false
    },
    fontSize: '12px',
    fixedOverflowWidgets: true // 超出編輯器大小的使用fixed屬性顯示
  })
  editor.onDidChangeModelContent(function () {
    self.$emit('update:code', editor.getValue()) //用來監(jiān)聽編輯器內(nèi)容變化,將內(nèi)容傳給父組件
  })
}

html

<template>
  <div ref="container" class="monaco"></div>
</template>

css

<style scoped>
.monaco {
  width: 95%;
  height: 400px;
  border: 1px solid #dcdfe6;
  text-align: left;
  margin-right: 20px;
  border-radius: 4px;
}
</style>

運行效果

JS代碼編譯器中Monaco怎么用

感謝各位的閱讀!關(guān)于“JS代碼編譯器中Monaco怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

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

AI