vue3 vite使用 monaco-editor 报错

报错:Unexpected usage at EditorSimpleWorker.loadForeignModule

修改配置:

 "monaco-editor-webpack-plugin": "^4.2.0",删除不用

版本:

 "monaco-editor": "^0.28.1",

 修改如下:

 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`,
    ],
  },
  transpileDependencies: true,
  configureWebpack: {
    plugins: [],
  },

 文件中的代码

<template>
  <div
    ref="editorContainer"
    class="editor-container"
    :class="{ 'no-suggestion': !props.suggestion }"
    :style="{ height: `${props.myHeight ? props.myHeight : height + 'px'}` }"
  ></div>
</template>

<script  setup lang="ts">
import { ref, watch, onMounted, onUnmounted, toRaw } from "vue";
//引入monaco-editor
import * as monaco from "monaco-editor";
import editorWorker from "monaco-editor/esm/vs/editor/editor.worker?worker";
import jsonWorker from "monaco-editor/esm/vs/language/json/json.worker?worker";
import cssWorker from "monaco-editor/esm/vs/language/css/css.worker?worker";
import htmlWorker from "monaco-editor/esm/vs/language/html/html.worker?worker";
import tsWorker from "monaco-editor/esm/vs/language/typescript/ts.worker?worker";

const props = defineProps([
  "value",
  "myHeight",
  "suggestion",
  "lineNumbers",
  "fontSize",
  "background",
]);
const emit = defineEmits(["change"]);

const height = ref<any>(0);
const editor = ref<any>(null);
const editorContainer = ref<any>(null);

monaco.editor.defineTheme("define-vs-dark", {
  base: "vs-dark",
  inherit: true,
  rules: [
    {
      foreground: props.background ? props.background : "#2f3031",
      token: "markdown.header",
      fontStyle: "bold",
    },
  ],
  colors: {
    "editor.background": props.background ? props.background : "#2f3031",
    "editorGutter.background": props.background ? props.background : "#2f3031",
  },
});

const cssArr = ["css", "scss", "less"];
const jsonArr = ["json"];
const htmlArr = ["html", "handlebars", "razor"];
const tsArr = ["typescript", "javascript"];
const se: any = self;

onMounted(() => {
//高亮及提示
  se.MonacoEnvironment = {
    getWorker(_: any, label: any) {
      if (jsonArr.includes(label)) {
        return new jsonWorker();
      }
      if (cssArr.includes(label)) {
        return new cssWorker();
      }
      if (htmlArr.includes(label)) {
        return new htmlWorker();
      }
      if (tsArr.includes(label)) {
        return new tsWorker();
      }
      return new editorWorker();
    },
  };
  //创建
  editor.value = monaco.editor.create(editorContainer.value, {
    value: props.value,
    theme: "define-vs-dark",
    folding: false,
    // cursorStyle: "line", //光标样式
    language: "typescript",
    selectOnLineNumbers: true, //显示行号
    roundedSelection: false,
    readOnly: false, // 只读
    automaticLayout: false, //自动布局
    glyphMargin: true, //字形边缘
    useTabStops: false,
    fontSize: props.fontSize ? props.fontSize : 14, //字体大小
    quickSuggestionsDelay: 100, //代码提示延时
    contextmenu: true,
    scrollBeyondLastLine: false,
    acceptSuggestionOnEnter: props.suggestion ? "on" : "off", // 接受输入建议 "on" | "off" | "smart"
    acceptSuggestionOnCommitCharacter: props.suggestion, // 接受关于提交字符的建议
    lineNumbers: props.lineNumbers ? "on" : "off",
    minimap: {
      enabled: false, // 关闭代码缩略图
    },
  });
  // 监听内容变化
  toRaw(editor.value).onDidChangeModelContent((e: any) => {
    sendValue();
    setContainerHeight();
  });
  setContainerHeight();
  // 监听失去焦点事件
  toRaw(editor.value).onDidBlurEditorText(() => {});
});
const reciveValue = () => {
  if (!editor.value) return;
  const currentValue = toRaw(editor.value).getValue();
  if (currentValue === props.value) return;

  toRaw(editor.value).setValue(props.value);
};
const sendValue = () => {
  if (!editor.value) return;
  const content = toRaw(editor.value).getValue()
    ? toRaw(editor.value).getValue()
    : props.value;
  emit("change", content);
};

const setContainerHeight = () => {
  const lineCount = toRaw(editor.value).getModel().getLineCount();
  const lineHeight = toRaw(editor.value).getOption(
    monaco.editor.EditorOption.lineHeight
  );
  height.value = lineCount * lineHeight + monaco.editor.EditorOption.lineHeight;
};
const watchValue = watch(
  () => props.value,
  () => {
    reciveValue();
  }
);
onMounted(() => {
  sendValue();
});
onUnmounted(() => {
  editor.value?.dispose();
  watchValue();
});
</script>

<style scoped lang="less">
.editor-container {
  width: 100%;
}
.no-suggestion {
  .suggest-widget {
    display: none !important;
  }
}
.editor-scrollable .lines-content {
  width: 100% !important;
  height: 100% !important;
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值