Vue uses the method in the reference library with source code

Vue uses the method in the reference library with source code

The official source code of monaco-editor-vue is as follows

Index.js

import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';

function noop() { }

export { monaco };

export default {
  name: 'MonacoEditor',
  props: {
    diffEditor: { type: Boolean, default: false }, //Whether to use diff mode width: { type: [String, Number], default: '100%' },
    height: {type: [String, Number], default: '100%'},
    original: String, //Only valid in diff mode value: String,
    language: {type: String, default: 'javascript'},
    theme: {type: String, default: 'vs'},
    options: {type: Object, default() {return {};}},
    editorMounted: {type: Function, default: noop},
    editorBeforeMount: {type: Function, default: noop}
  },

  watch:
    options:
      deep: true,
      handler(options) {
        this.editor && this.editor.updateOptions(options);
      }
    },

    value() {
      this.editor && this.value !== this._getValue() && this._setValue(this.value);
    },

    language() {
      if(!this.editor) return;
      if(this.diffEditor){ //Update language in diff mode
        const { original, modified } = this.editor.getModel();
        monaco.editor.setModelLanguage(original, this.language);
        monaco.editor.setModelLanguage(modified, this.language);
      }else
        monaco.editor.setModelLanguage(this.editor.getModel(), this.language);
    },

    theme() {
      this.editor && monaco.editor.setTheme(this.theme);
    },

    style() {
      this.editor && this.$nextTick(() => {
        this.editor.layout();
      });
    }
  },

  computed: {
    style() {
      return {
        width: !/^\d+$/.test(this.width) ? this.width : `${this.width}px`,
        height: !/^\d+$/.test(this.height) ? this.height : `${this.height}px`
      }
    }
  },

  mounted () {
    this.initMonaco();
  },

  beforeDestroy() {
    this.editor && this.editor.dispose();
  },

  render (h) {
    return (
      <div class="monaco_editor_container" style={this.style}></div>
    );
  },

  methods: {
    initMonaco() {
      const { value, language, theme, options } = this;
      Object.assign(options, this._editorBeforeMount()); //Before editor initialization this.editor = monaco.editor[this.diffEditor ? 'createDiffEditor' : 'create'](this.$el, {
        value: value,
        language: language,
        theme: theme,
        ...options
      });
      this.diffEditor && this._setModel(this.value, this.original);
      this._editorMounted(this.editor); //After the editor is initialized},

    _getEditor() {
      if(!this.editor) return null;
      return this.diffEditor ? this.editor.modifiedEditor : this.editor;
    },

    _setModel(value, original) { //Set model in diff mode
      const { language } = this;
      const originalModel = monaco.editor.createModel(original, language);
      const modifiedModel = monaco.editor.createModel(value, language);
      this.editor.setModel({
        original: originalModel,
        modified: modifiedModel
      });
    },

    _setValue(value) {
      let editor = this._getEditor();
      if(editor) return editor.setValue(value);
    },

    _getValue() {
      let editor = this._getEditor();
      if(!editor) return '';
      return editor.getValue();
    },

    _editorBeforeMount() {
      const options = this.editorBeforeMount(monaco);
      return options || {};
    },

    _editorMounted(editor) {
      this.editorMounted(editor, monaco);
      if(this.diffEditor){
        editor.onDidUpdateDiff((event) => {
          const value = this._getValue();
          this._emitChange(value, event);
        });
      }else{
        editor.onDidChangeModelContent(event => {
          const value = this._getValue();
          this._emitChange(value, event);
        });
      }
    },

    _emitChange(value, event) {
      this.$emit('change', value, event);
      this.$emit('input', value);
    }
  }
}

How to use the _getValue method in the above library when using Vue?

Define ref = "", use this.$refs.exampleEditor._setValue('')

Refer to the following code

test.vue

<template>
  <div>
    <MonacoEditor ref="exampleEditor" width="100%" height="300" theme="vs-dark" language="javascript" :options="options" @change="codeInput" />
  </div>
</template>
<script>
import MonacoEditor from 'monaco-editor-vue'
export default {
  components:
    MonacoEditor
  },
  data() {
    return {
    }
  },
  beforeCreate() {
  },

  mounted() {
  },
  methods: {
    this.$refs.exampleEditor._setValue('')
  }
}

This is the end of this article about vue using the methods in the reference library with source code. For more relevant vue using the reference library content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to change fonts in Vue, locally store fonts without referencing online font libraries
  • How to reference element-ui component library in vue2.0

<<:  Docker installation steps for Redmine

>>:  How to use DQL commands to query data in MySQL

Recommend

Detailed examples of Linux disk device and LVM management commands

Preface In the Linux operating system, device fil...

A brief analysis of HTML space code

How much do you know about HTML? If you are learni...

How to use react-color to implement the front-end color picker

background We can use react-color to implement th...

Six inheritance methods in JS and their advantages and disadvantages

Table of contents Preface Prototype chain inherit...

About nginx to implement jira reverse proxy

Summary: Configure nginx reverse proxy jira and i...

MySQL date and time addition and subtraction sample code

Table of contents 1.MySQL adds or subtracts a tim...

How to set up virtual directories and configure virtual paths in Tomcat 7.0

Tomcat7.0 sets virtual directory (1) Currently, o...

When you enter a URL, what exactly happens in the background?

As a software developer, you must have a complete...

Clever use of webkit-box-reflect to achieve various dynamic effects (summary)

In an article a long time ago, I talked about the...

The use of MySQL triggers and what to pay attention to

Table of contents About Triggers Use of triggers ...

Detailed explanation of CSS style cascading rules

CSS style rule syntax style is the basic unit of ...

Getting Started Guide to MySQL Sharding

Preface Relational databases are more likely to b...

How to modify the master-slave replication options in MySQL online

Preface: The most commonly used architecture of M...