Vue3 基本使用 Monaco Editor Web编辑器 202407

初步教程,如有错误还请指正,如果之后教程过期请参考官方文档使用或者寻找新的依赖替代。

1.安装依赖

复制代码
npm i monaco-editor @monaco-editor/loader

monaco-editor - npm (npmjs.com)

@monaco-editor/loader - npm (npmjs.com)

2024/7/27使用版本

monaco-editor : ^0.50.0

@monaco-editor/loader : ^1.4.0

2.基本使用

vue-projectsrccomponentsMonacoEditor.vue

复制代码
<script setup>
import {
  ref,
  onMounted,
  onBeforeUnmount,
  watch,
  defineProps,
  defineEmits,
} from "vue";
import loader from "@monaco-editor/loader";

const props = defineProps({
  value: String,
  language: {
    type: String,
    default: "java",
  },
  theme: {
    type: String,
    default: "vs-dark",
  },
});

const emits = defineEmits(["update:value"]);

const editorContainer = ref(null);
let editorInstance = null;

onMounted(() => {
  loader.init().then((monaco) => {
    editorInstance = monaco.editor.create(editorContainer.value, {
      value: props.value || "",
      language: props.language,
      theme: props.theme,
    });

    editorInstance.onDidChangeModelContent(() => {
      emits("update:value", editorInstance.getValue());
    });
  });
});

onBeforeUnmount(() => {
  if (editorInstance) {
    editorInstance.dispose();
  }
});

watch(
  () => props.language,
  (newLanguage) => {
    if (editorInstance) {
      loader.init().then((monaco) => {
        monaco.editor.setModelLanguage(editorInstance.getModel(), newLanguage);
      });
    }
  }
);

watch(
  () => props.value,
  (newValue) => {
    if (editorInstance && editorInstance.getValue() !== newValue) {
      editorInstance.setValue(newValue);
    }
  }
);
</script>

<template>
  <div ref="editorContainer" class="editor-container"></div>
</template>

<style>
.editor-container {
  width: 100%;
  height: 100%;
}
</style>

srcApp.vue

复制代码
<script setup>
import { ref } from 'vue';
import MonacoEditor from '@/components/MonacoEditor.vue';

const code = ref('// Hello World');
const language = ref('java');
const theme = ref('vs-dark');

const logValue = () => {
  console.log(code.value);
};
</script>

<template>
  <div id="editor" style="height: 400px">
    <MonacoEditor v-model:value="code" :language="language" :theme="theme" />
  </div>
  <div class="controls">
    <label for="language">Select Language: </label>
    <select id="language" v-model="language">
      <option value="java">Java</option>
      <option value="javascript">Javascript</option>
    </select>
  </div>
  <button @click="logValue">Log Value</button>
</template>

3.格式化Java代码

我目前没有找到 Monaco Editor 自带的格式化Java的功能,我使用了其他的依赖仅供参考。

复制代码
npm i prettier prettier-plugin-java

srcApp.vue

复制代码
<script setup>
import { ref } from 'vue';
import MonacoEditor from '@/components/MonacoEditor.vue';
import prettier from 'prettier/standalone';
import java from 'prettier-plugin-java';

const code = ref('// Hello World');
const language = ref('java');
const theme = ref('vs-dark');

const formatCode = async () => {
  const formattedCode = await prettier.format(code.value, {
    parser: 'java',
    plugins: [java],
  });
  code.value = formattedCode;
};

const logValue = () => {
  console.log(code.value);
};
</script>

<template>
  <div id="editor" style="height: 400px">
    <MonacoEditor v-model:value="code" :language="language" :theme="theme" />
  </div>
  <div class="controls">
    <label for="language">Select Language: </label>
    <select id="language" v-model="language">
      <option value="java">Java</option>
      <option value="javascript">Javascript</option>
    </select>
  </div>
  <button @click="formatCode">Format Code</button>
  <button @click="logValue">Log Value</button>
</template>

4.其他

需要其他功能自行参考官方文档或者中文文档

Monaco Editor (microsoft.github.io)

Monaco Editor Translate (wf0.github.io)

Monaco Editor 中文文档整理(超详细、超全面、带demo示例)_monacoeditor中文文档-CSDN博客

下面这个好像已经封装vue组件了,使用可能更加方便

monaco-editor-vue3 - npm (npmjs.com)

相关推荐
爱滑雪的码农26 分钟前
详细说说React大型项目结构以及日常开发核心语法
前端·javascript·react.js
七牛开发者1 小时前
HTML is the new Markdown:来自 Claude Code 团队的实践
前端·人工智能·语言模型·html
@大迁世界1 小时前
43.HTML 事件处理和 React 事件处理有什么区别?
前端·javascript·react.js·html·ecmascript
CloneCello1 小时前
AI时代程序员认知调整指南
前端
ZC跨境爬虫2 小时前
跟着 MDN 学 HTML day_38:(DocumentFragment 文档片段接口详解)
前端·javascript·ui·html·音视频
@大迁世界3 小时前
41.ShadCN 是什么?它如何和 Tailwind CSS 集成,从而更容易构建可访问且可自定义的 React 组件?
前端·javascript·css·react.js·前端框架
千叶风行3 小时前
Text-to-SQL 技术设计与注意事项
前端·人工智能·后端
软件开发技术深度爱好者4 小时前
HTML5+JavaScript读取DOCX 文档完整内容
前端·html5
幽络源小助理4 小时前
苹果CMS V10 MXPro V4.5模版下载, 自适应视频主题源码, 幽络源源码
前端·开源·源码·php源码
kyriewen4 小时前
坏了,黑客学会用AI写外挂了
前端·程序员·ai编程