MonacoEdit在VUE中使用自定义颜色

1.安装

npm install monaco-editor@0.21.2 --save

npm install monaco-editor-webpack-plugin --save

 

2.项目配置

在vue.config.js追加


 

const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin')
module.exports = {
    //忽略部分其他配置代码
    chainWebpack: (config) => {
        config.plugin('monaco').use(new MonacoWebpackPlugin())
    },
};


3.组件代码(在需要的地方引入这个)

<template>
  <div class="container">
    <div id="editor" style="height: 100%"></div> <!--宽高自行设定 -->
  </div>
</template>

<script>
import * as monaco from 'monaco-editor'
export default {
  name: "MonacoEditor.vue",
  data() {
    return {
      editor: null
    };
  },
  mounted() {
    this.initHandler()
  },
  props:{
    value: String,
    options: {        // 可配置属性 可扩展
      type: Object,
      default: () => {
        return {
          language:'javascript',  // 语言
          fontSize: 18,           // 字号
        };
      }
    }
  },
  methods: {
    /**
     * 数据加载
     */
    initHandler() {
      this.initEditor()
    },
    // 初始化编辑器,确保dom已经渲染
    initEditor(){
      // 编辑器配置项 参考文档https://microsoft.github.io/monaco-editor/api/modules/monaco.html
      // 自定义灰色背景主题
      monaco.editor.defineTheme('GreyTheme', {
        base: 'vs',
        inherit: true,
        rules: [{ background: '#F9F9F9' }],
        colors: {
          // 相关颜色属性配置
          'editor.background': '#F9F9F9',     //背景色
        }
      });
      //设置自定义主题
      monaco.editor.setTheme('GreyTheme')
      // 下面属性设置等同于官网DEMO效果
      this.editor = monaco.editor.create(document.getElementById('editor'), {
        value:this.value,//编辑器初始显示文字
        language: this.options.language,//语言支持自行查阅demo
        automaticLayout: false,//自动布局
        theme:'GreyTheme', //官方自带三种主题vs, hc-black, or vs-dark
        selectOnLineNumbers: true,//显示行号
        roundedSelection: false,
        readOnly: false,        // 只读
        cursorStyle: 'line',        //光标样式
        glyphMargin: true,  //字形边缘
        useTabStops: false,
        fontSize: this.options.fontSize,       //字体大小
        autoIndent:false,//自动布局
        renderWhitespace: 'all',
        colorDecorators: true,
        matchBrackets: 'always',
        accessibilitySupport: 'on',
        suggestions: true,
        snippetSuggestions: 'top',
      });
    },
    /**
     * 获取编辑器值
     * @returns {*}
     */
    getValue(){
      return this.editor.getValue(); //获取编辑器中的文本
    }
  },
  destroyed(){
    this.$emit('getEditorValue', this.editor.getValue())
  }
}

</script>

<style scoped>
.monaco-mouse-cursor-text {
  background: red !important;
}
</style>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 使用自定义指令可以方便地扩展 HTML 元素的功能。在 store 使用自定义指令可以让你在状态管理的同时也能够控制应用的 UI。 要在 store 使用自定义指令,首先需要定义一个指令对象,该对象包括 `bind` 和 `unbind` 方法。在 `bind` 方法,你可以访问到指令所绑定的元素、指令的参数、指令的值以及整个 Vue 实例。在 `unbind` 方法,你可以清理指令所绑定的元素。 下面是一个例子: ```javascript // 在 store 定义一个自定义指令 const myDirective = { bind: function (el, binding, vnode) { // 在元素上添加一个属性 el.myProperty = "myValue"; }, unbind: function (el, binding, vnode) { // 在解绑指令时清理元素上的属性 delete el.myProperty; } } // 在 Vue 实例使用自定义指令 new Vue({ el: '#app', store, directives: { myDirective } }); ``` 在上面的例子,我们定义了一个名为 `myDirective` 的自定义指令,并将它添加到了 Vue 实例的 `directives` 属性。在指令的 `bind` 方法,我们在元素上添加了一个自定义属性 `myProperty`,在 `unbind` 方法又将该属性清除。 在 HTML 使用自定义指令可以像使用其他指令一样,只需要在元素上使用 `v-` 前缀即可: ```html <div v-my-directive>My Element</div> ``` 在上面的例子,我们在 `div` 元素上使用自定义指令 `myDirective`。当该元素被渲染时,指令的 `bind` 方法会被调用,将元素上添加一个自定义属性 `myProperty`。当该元素被销毁时,指令的 `unbind` 方法会被调用,清理元素上的自定义属性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值