当前位置:   article > 正文

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

vue3 monaco

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

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-project\src\components\MonacoEditor.vue

  1. <script setup>
  2. import {
  3. ref,
  4. onMounted,
  5. onBeforeUnmount,
  6. watch,
  7. defineProps,
  8. defineEmits,
  9. } from "vue";
  10. import loader from "@monaco-editor/loader";
  11. const props = defineProps({
  12. value: String,
  13. language: {
  14. type: String,
  15. default: "java",
  16. },
  17. theme: {
  18. type: String,
  19. default: "vs-dark",
  20. },
  21. });
  22. const emits = defineEmits(["update:value"]);
  23. const editorContainer = ref(null);
  24. let editorInstance = null;
  25. onMounted(() => {
  26. loader.init().then((monaco) => {
  27. editorInstance = monaco.editor.create(editorContainer.value, {
  28. value: props.value || "",
  29. language: props.language,
  30. theme: props.theme,
  31. });
  32. editorInstance.onDidChangeModelContent(() => {
  33. emits("update:value", editorInstance.getValue());
  34. });
  35. });
  36. });
  37. onBeforeUnmount(() => {
  38. if (editorInstance) {
  39. editorInstance.dispose();
  40. }
  41. });
  42. watch(
  43. () => props.language,
  44. (newLanguage) => {
  45. if (editorInstance) {
  46. loader.init().then((monaco) => {
  47. monaco.editor.setModelLanguage(editorInstance.getModel(), newLanguage);
  48. });
  49. }
  50. }
  51. );
  52. watch(
  53. () => props.value,
  54. (newValue) => {
  55. if (editorInstance && editorInstance.getValue() !== newValue) {
  56. editorInstance.setValue(newValue);
  57. }
  58. }
  59. );
  60. </script>
  61. <template>
  62. <div ref="editorContainer" class="editor-container"></div>
  63. </template>
  64. <style>
  65. .editor-container {
  66. width: 100%;
  67. height: 100%;
  68. }
  69. </style>

src\App.vue 

  1. <script setup>
  2. import { ref } from 'vue';
  3. import MonacoEditor from '@/components/MonacoEditor.vue';
  4. const code = ref('// Hello World');
  5. const language = ref('java');
  6. const theme = ref('vs-dark');
  7. const logValue = () => {
  8. console.log(code.value);
  9. };
  10. </script>
  11. <template>
  12. <div id="editor" style="height: 400px">
  13. <MonacoEditor v-model:value="code" :language="language" :theme="theme" />
  14. </div>
  15. <div class="controls">
  16. <label for="language">Select Language: </label>
  17. <select id="language" v-model="language">
  18. <option value="java">Java</option>
  19. <option value="javascript">Javascript</option>
  20. </select>
  21. </div>
  22. <button @click="logValue">Log Value</button>
  23. </template>

3.格式化Java代码

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

npm i prettier prettier-plugin-java

src\App.vue  

  1. <script setup>
  2. import { ref } from 'vue';
  3. import MonacoEditor from '@/components/MonacoEditor.vue';
  4. import prettier from 'prettier/standalone';
  5. import java from 'prettier-plugin-java';
  6. const code = ref('// Hello World');
  7. const language = ref('java');
  8. const theme = ref('vs-dark');
  9. const formatCode = async () => {
  10. const formattedCode = await prettier.format(code.value, {
  11. parser: 'java',
  12. plugins: [java],
  13. });
  14. code.value = formattedCode;
  15. };
  16. const logValue = () => {
  17. console.log(code.value);
  18. };
  19. </script>
  20. <template>
  21. <div id="editor" style="height: 400px">
  22. <MonacoEditor v-model:value="code" :language="language" :theme="theme" />
  23. </div>
  24. <div class="controls">
  25. <label for="language">Select Language: </label>
  26. <select id="language" v-model="language">
  27. <option value="java">Java</option>
  28. <option value="javascript">Javascript</option>
  29. </select>
  30. </div>
  31. <button @click="formatCode">Format Code</button>
  32. <button @click="logValue">Log Value</button>
  33. </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)  

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/1000084
推荐阅读
相关标签
  

闽ICP备14008679号