How to use JS code compiler Monaco

How to use JS code compiler Monaco

Preface

My needs are syntax highlighting, function prompts, automatic line wrapping, and code folding.

Monaco

Monaco is from Microsoft, it supports many languages ​​and has thumbnail maps, but sometimes the prompts are not easy to use and the package size is very large.
The Monaco Editor is the code editor that powers VS Code.

How to use official website

[Official Documentation](https://microsoft.github.io/monaco-editor/index.html)
[Online demo](https://github.com/Microsoft/monaco-editor-samples)
[github](https://github.com/Microsoft/monaco-editor)

Install

yarn add monaco-editor | npm install monaco-editor

Introduction

import * as monaco from 'monaco-editor' // The package is big but the demo can run // Customize some suggestion functions const suggestions = [
  {
    label: 'split_chinese',
    insertText: 'split_chinese(inputString,language);', // Not displayed when not written. .
    Details:
      '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'
  }
]

initialization

mounted() {
    monaco.languages.registerCompletionItemProvider('JavaScript', {
      provideCompletionItems() {
        return {
          suggestions: suggestions
        }
      },
      triggerCharacters: [' ', '.'] // Write the characters that trigger the prompt, there can be multiple })
    let self = this
    setTimeout(function () {
      self.init()
    }, 50) //Because the parent component has not passed the parameter, the child component has been rendered}
  
 // Initialization method 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 // Use fixed attributes to display widgets that exceed the editor size})
  editor.onDidChangeModelContent(function () {
    self.$emit('update:code', editor.getValue()) //Used to monitor editor content changes and pass the content to the parent component})
}

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>

Operation effect

shortcoming

Mine was overturned and I don't want to run it again. I'll just write a demo while the code is still there. It runs OK (some customers also reported that it is not easy to use, it is my own fault, I am not worthy of using Monaco). However, it is really difficult to use, especially the prompt function, which usually does not have any prompts. Then one package was huge, like 3.9G (seriously). It may not be introduced as needed, but if it is not introduced, there will be no prompt function, custom function prompt. There is also webpack configuration, tossing back and forth!

The above is the detailed content of the tutorial on how to use the JS compiler Monaco. For more information about the JS compiler Monaco, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Quickly solve the problem of Baidu compiler json error
  • Introduction to AngularJS HTML Compiler
  • Detailed explanation of the basic syntax of the JSP compiler
  • Vue uses monaco to achieve code highlighting
  • Implementing Monaco Editor custom prompt function in vue

<<:  Detailed tutorial for installing mysql 8.0.12 under Windows

>>:  Detailed steps to change the default password when installing MySQL in Ubuntu

Recommend

How to solve the problem of MySQL query character set mismatch

Find the problem I recently encountered a problem...

Nginx operation and maintenance domain name verification method example

When configuring the interface domain name, each ...

Four data type judgment methods in JS

Table of contents 1. typeof 2. instanceof 3. Cons...

Implementation of Grid common layout

No gaps on both sides, gaps between each column w...

Details of the order in which MySQL reads my.cnf

Table of contents The order in which MySQL reads ...

Detailed explanation of the life cycle of Angular components (Part 2)

Table of contents 1. View hook 1. Things to note ...

Examples of correct use of interface and type methods in TypeScript

Table of contents Preface interface type Appendix...

TypeScript installation and use and basic data types

The first step is to install TypeScript globally ...

MySQL grouping queries and aggregate functions

Overview I believe we often encounter such scenar...

How to detect if the current browser is a headless browser with JavaScript

Table of contents What is a headless browser? Why...

MySQL query statement grouped by time

MySQL query by year, month, week, day group 1. Qu...

Detailed explanation of the pitfalls of MySQL 8.0

I updated MySQL 8.0 today. The first problem: Nav...