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)

相关推荐
xixichensh8 小时前
前端转做AI程序员第一步——开发一个AI流式对话Demo
前端
小羊没烦恼!9 小时前
系统内部模块(子系统)之间的耦合以及模块(子系统)划分
java·开发语言·前端·数据库·算法·c#
Leslie1659 小时前
聚合不只是 GROUP BY:HashAggregate 与 SortAggregate 的内存账本
前端
ITOM运维行者9 小时前
PHP性能监控怎么做?从响应时间到慢函数的6个关键指标
前端·javascript·后端
成旭先生9 小时前
AI 文本审核 API:一段中文文本判风险等级、命中标签与处置建议
java·前端·人工智能·api接口·内容风控·文本审核·ugc审核
liangshanbo121510 小时前
前端面试题:AI 对话中超长消息导致内存溢出,怎么解决?
java·开发语言·前端
静默回滚10 小时前
PDF能打开却不能复制,原来是权限密码
前端
律宏阔10 小时前
Dart FFI 内存管理:用 using + Arena 替代嵌套 try-finally
前端·flutter
guslegend10 小时前
脚手架入门:必要性、核心功能与执行原理
前端·架构·node.js·脚手架·前端工程化
律宏阔10 小时前
Flutter 调用 Go:从 c-shared + ffigen 到 @Native + Native Assets 踩坑记录
前端·flutter