Monaco Editor vue2项目使用包括汉化

使用前置安装依赖

  {
    "@monaco-editor/loader": "1.4.0",
    "monaco-editor": "0.50.0",
    "monaco-editor-webpack-plugin": "7.1.0",
    "sql-formatter": "15.3.2",
    "vue": "2.6.14",
    "xml-formatter": "3.6.3"
  },

webpack配置

const { defineConfig } = require('@vue/cli-service')
const MonacoWebpackPlugin = require("monaco-editor-webpack-plugin");
module.exports = defineConfig({
  transpileDependencies: true,
  configureWebpack: {
    devtool: 'source-map'//调试模式
  },
  chainWebpack(config) {
    config.plugin("monaco").use(new MonacoWebpackPlugin());
  },
});

组件实际代码

<template>
  <div class="editor-container">
    <button @click="formatF">点我手动触发格式化</button>
    <span>&emsp;</span>
    组件内部搜索
    <input
      v-model="searchText"
      @input="searchF(searchText)"
      @keypress.enter="searchF(searchText)"
      placeholder="输入关键字回车搜索"
    />(可使用ctrl+f快速搜索 支持正则 批量替换等 )
    <div class="editor-container" ref="editorContainer"></div>
  </div>
</template>

<script>
import * as monaco from "monaco-editor";
import xmlFormat from "xml-formatter"; // xml 格式化库
import { format } from "sql-formatter"; // sql 格式化库
import loader from '@monaco-editor/loader';// 汉化加载器

export default {
  name: "MonacoEditor",
  props: {
    value: {
      type: String,
      required: true,
    },
    language: {
      type: String,
      default: "javascript",
    },
    theme: {
      type: String,
      default: "vs-dark",
    },
    readOnly: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      editor: null, // 编辑器实例
      searchText: "", // 搜索框输入内容
      lastSearch: "", // 记录上一次搜索的关键字
      searchIndex: 0, // 记录上一次搜索的索引
    };
  },
  watch: {
    language(v) {
      monaco.editor.setModelLanguage(this.editor.getModel(), v);
    },
    theme(v) {
      this.editor.updateOptions({
        theme: v,
      });
    },
    readOnly(v) {
      this.editor.updateOptions({ readOnly: v });
    },
  },

  async mounted() {
    await this.initMonaco(); 
  },
  methods: {
    async initMonaco() {
      // 设置语言为中文并加载语言包
      loader.config({ 'vs/nls': { availableLanguages: { '*': 'zh-cn' } } }); // availableLanguages中可以配置想要的语言,例如de、zh-cn等
      let monacoNow =  await loader.init(); // 加载monaco编辑器
      this.editor = monacoNow.editor.create(this.$refs.editorContainer, {
        value: this.value,
        language: this.language,
        automaticLayout: true, // 自动调整大小
        minimap: {
          enabled: false, // 关闭 右侧minimap
        },
        readOnly: this.readOnly,
        scrollBeyondLastLine: false, // 用滚动到最后一行后继续滚动的功能 开启后会导致滚动条可以滚动到代码在页面上只显示一行
        theme: this.theme,
        wordWrap: "on", // 自动换行
        contextmenu: true, // 禁用右键菜单  后续可拓展自定义右键命令 等
        // formatOnType: true, // 输入时格式化
        // formatOnPaste: true, // 粘贴时格式化
      });
       // json和html自带格式化不用额外写  后续有其他语言支持可以再补充
      // 为 SQL 注册格式化器
      monacoNow.languages.registerDocumentFormattingEditProvider("sql", {
        provideDocumentFormattingEdits: (model) => {
          const formatted = format(model.getValue(), {
            language: "plsql", // 默认一种语言
          });
          return [
            {
              range: model.getFullModelRange(),
              text: formatted,
            },
          ];
        },
      });

      // 为 XML 注册格式化器  对应的格式化方法需要增加trycatch
      monacoNow.languages.registerDocumentFormattingEditProvider("xml", {
        provideDocumentFormattingEdits: (model) => {
          let valueOld = model.getValue();
          let formatted = "";
          try {
            formatted = xmlFormat(model.getValue(), {
              collapseContent: true,
            });
          } catch (error) {
            console.log(error);
            formatted = valueOld;
          }
          return [
            {
              range: model.getFullModelRange(),
              text: formatted,
            },
          ];
        },
      });

      // 监听输入内容变化实现v-model双向绑定
      this.editor.getModel().onDidChangeContent(() => {
        this.$emit("input", this.editor.getValue());
      });
    },
    /**
     * 格式化函数
     *
     * @description 主动触发格式化命令
     * @returns 无返回值
     */
    formatF() {
      // 主动触发格式化命令  格式化在只读不能使用 所以先改为编辑后 格式化完了 再改回原来的只读状态
      // const commands = this.editor.getActions();
      // console.log("Available commands:"); //获取当前支持的命令getAction 后run()可以手动触发
      this.editor.updateOptions({ readOnly: false });
      this.editor
        .getAction("editor.action.formatDocument")
        .run()
        .then(() => {
          this.editor.updateOptions({ readOnly: this.readOnly });
        });
    },

    /**
     * 在编辑器中搜索指定内容
     * @description 支持查询结果循环选中 可在编辑器焦点时使用ctrl+f 功能更全 包括但不限于 搜索框快速搜索 支持正则 批量替换等
     * @param v 要搜索的内容
     * @returns 无返回值
     */
    searchF(v) {
      const model = this.editor.getModel();
      const matches = model.findMatches(v, true, false, false, null, true);
      if (matches.length > 0) {
        if (
          this.lastSearch === v &&
          this.searchIndex !== matches.length - 1 // 匹配到最后一个时,下次搜索从头开始
        ) {
          this.searchIndex++;
        } else {
          this.searchIndex = 0;
        }
        const match = matches[this.searchIndex];
        this.editor.setSelection(match.range);
        this.editor.revealRangeInCenter(match.range);
      }
      this.lastSearch = v;
    },
  },

  /**
   * 组件销毁前钩子函数
   *
   * @description 在组件销毁前,释放编辑器资源
   */
  beforeDestroy() {
    if (this.editor) {
      this.editor.dispose();
    }
  },
};
</script>

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

最后效果

录屏2024-08-06 11.46.52

注意事项

如果你的产品环境是不能连接外网的,无法加载外链的情况时 @monaco-editor/loader默认会加载cdn的js链接加载,需要根据它提供的配置 自己在项目中或者服务器放引用的文件
配置文档

import loader from '@monaco-editor/loader';
loader.config({ paths: { vs: '../path-to-monaco' } });
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue 2项目使用Monaco Editor并进行打包,你可以按照以下步骤进行操作: 1. 首先,安装Monaco Editor的npm包。在终端中运行以下命令: ``` npm install monaco-editor ``` 2. 在你的Vue组件中,引入Monaco Editor的相关代码。可以在你的组件文件中添加以下代码: ```javascript import * as monaco from 'monaco-editor'; export default { mounted() { // 加载Monaco Editor monaco.editor.create(this.$refs.editor, { value: "initial code", language: "javascript" }); } } ``` 这将在`mounted`钩子中创建一个Monaco Editor实例。 3. 添加Monaco Editor的样式。你可以通过在你的Vue组件中添加以下样式代码来引入默认的Monaco Editor样式: ```css @import "~monaco-editor/esm/vs/editor/editor.main.css"; ``` 4. 打包配置。 如果你使用的是Vue CLI来构建项目,你需要在webpack配置中添加一些额外的配置。在`vue.config.js`文件中添加以下代码: ```javascript module.exports = { configureWebpack: { // 在这里添加Monaco Editorwebpack配置 module: { rules: [ { test: /\.css$/, use: ['style-loader', 'css-loader'], }, { test: /\.ttf$/, use: ['file-loader'], }, ], }, resolve: { alias: { 'vs': require.resolve('monaco-editor/esm/vs'), }, }, }, }; ``` 这将确保Monaco Editor的样式和字体文件正确引入,并且可以在打包后的项目中正常使用。 这样,你就可以在Vue 2项目使用Monaco Editor并进行打包了。记得替换`initial code`和`javascript`为你实际需要的初始代码和语言
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值