当前位置:   article > 正文

vue3-element-admin集成monaco-editor_elementui vue3 monaco

elementui vue3 monaco

安装依赖

执行命令:

pnpm install monaco-editor
  • 1

配置 vite.config.ts

在 optimizeDeps 中新增monaco所需组件

// 预加载项目必需的组件
    optimizeDeps: {
      include: [
        "monaco-editor/esm/vs/language/json/json.worker",
        "monaco-editor/esm/vs/language/css/css.worker",
        "monaco-editor/esm/vs/language/html/html.worker",
        "monaco-editor/esm/vs/language/typescript/ts.worker",
        "monaco-editor/esm/vs/editor/editor.worker",
      ],
    },
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

自定义组件

在src/components下新增目录 MonacoEditor,在该目录下新建 index.vue , 内容如下:

<template>
  <div class="editor" ref="dom"></div>
</template>

<script lang="ts">
import { onMounted, ref, defineComponent, watch, onUnmounted, shallowRef } from "vue";
import * as monaco from "monaco-editor";
import EditorWorker from "monaco-editor/esm/vs/editor/editor.worker?worker";

//https://github.com/vitejs/vite/discussions/1791
export default defineComponent({
  name: "vsCode",
  props: {
    modelValue: {
      type: String,
      default: ""
    },
    theme: {
      type: String,
      default: "vs"
    },
    language: { type: String, default: "json" },
    options: Object
  },
  emits: ["change", "update:modelValue"],
  setup(props, { emit }) {
    const dom = ref();
    const vs = shallowRef();
    const self: any = window;

    self.MonacoEnvironment = {
      getWorker() {
        return new EditorWorker();
      }
    };

    onMounted(() => {
      const editor = monaco.editor.create(dom.value, {
        model: monaco.editor.createModel(props.modelValue, props.language),
        tabSize: 2,
        automaticLayout: true,
        scrollBeyondLastLine: false,
        theme: props.theme,
        ...props.options
      });
      vs.value = editor;
      editor.onDidChangeModelContent(() => {
        const value = editor.getValue();
        emit("update:modelValue", value);
        emit("change", value);
      });
      watch(
        () => props.modelValue,
        (vl) => {
          if (vl !== editor.getValue()) {
            editor.setValue(vl);
          }
        }
      );
      watch(
        () => props.theme,
        (vl) => {
          monaco.editor.setTheme(vl);
        }
      );
      watch(
        () => props.language,
        (vl) => {
          monaco.editor.setModelLanguage(editor.getModel() as monaco.editor.ITextModel, vl);
        }
      );
      watch(
        () => props.options,
        (vl) => {
          editor.updateOptions(vl as monaco.editor.IEditorOptions);
        }
      );
    });

    onUnmounted(() => {
      vs.value && vs.value.dispose();
    });

    return { dom };
  }
});
</script>

<style scoped>
.editor {
  height: 100%;
}
</style>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94

全局注册组件

在 components.d.ts 增加如下代码:

 declare module "vue" {
  export interface GlobalComponents {
    MonacoEditor: (typeof import("../components/MonacoEditor/index.vue"))["default"];
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5

使用组件

<template>
	<MonacoEditor class="editor" v-model="formData.content" theme="vs" language="java"
	                            style="width:100%; height: 420px; border: 1px solid #ccc" />
</template>
<script setup lang="ts">
/**
 * 引入 MonacoEditor 组件
 * */
import MonacoEditor from "@/components/MonacoEditor/index.vue";
const formData = reactive({
  content: "", // 存储编辑器内容
});
</script>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/710211
推荐阅读
相关标签
  

闽ICP备14008679号