electron-vite vue3离线使用monaco-editor

目录

[1.搭建一个 electron-vite 项目](#1.搭建一个 electron-vite 项目)

2.安装monaco-editor和vite-plugin-monaco-editor

3.electron.vite.config.mjs配置

[4.创建 worker.js并在main.js 引入](#4.创建 worker.js并在main.js 引入)

[5.创建组件 MonacoVite.vue 组件](#5.创建组件 MonacoVite.vue 组件)

[6. App.vue中引入组件](#6. App.vue中引入组件)

7.运行测试


1.搭建一个 electron-vite 项目

javascript 复制代码
pnpm create @quick-start/electron

参考链接:

1.Getting Started | electron-vite

  1. Electron⚡️Vite | Electron⚡️Vite
    2.Electron⚡️Vite

然后按照提示操作即可!

javascript 复制代码
✔ Project name: ... <electron-app>
✔ Select a framework: › vue
✔ Add TypeScript? ... No / Yes
✔ Add Electron updater plugin? ... No / Yes
✔ Enable Electron download mirror proxy? ... No / Yes

Scaffolding project in ./<electron-app>...
Done.

2.安装monaco-editor和vite-plugin-monaco-editor

javascript 复制代码
pnpm install monaco-editor
javascript 复制代码
pnpm i vite-plugin-monaco-editor

3.electron.vite.config.mjs配置

javascript 复制代码
import monacoEditorPlugin from 'vite-plugin-monaco-editor';
javascript 复制代码
plugins: [vue(),monacoEditorPlugin ]

electron.vite.config.mjs

javascript 复制代码
import { resolve } from 'path'
import { defineConfig, externalizeDepsPlugin } from 'electron-vite'
import vue from '@vitejs/plugin-vue'
import monacoEditorPlugin from 'vite-plugin-monaco-editor';
export default defineConfig({
  main: {
    plugins: [externalizeDepsPlugin()]
  },
  preload: {
    plugins: [externalizeDepsPlugin()]
  },
  renderer: {
    resolve: {
      alias: {
        '@renderer': resolve('src/renderer/src')
      }
    },
    plugins: [vue(),monacoEditorPlugin ]
  }
})

4.创建 worker.js并在main.js 引入

worker.js

javascript 复制代码
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';

self.MonacoEnvironment = {
    getWorker(_, label) {
        if (label === 'json') {
            return new JsonWorker();
        }
        if (label === 'css' || label === 'scss' || label === 'less') {
            return new CssWorker();
        }
        if (label === 'html' || label === 'handlebars' || label === 'razor') {
            return new HtmlWorker();
        }
        if (label === 'typescript' || label === 'javascript') {
            return new TsWorker();
        }
        return new EditorWorker();
    }
};

monaco.languages.typescript.typescriptDefaults.setEagerModelSync(true);

main.js

javascript 复制代码
import './worker.js'
javascript 复制代码
import './assets/main.css'

import { createApp } from 'vue'
import App from './App.vue'
//引入worker.js
import './worker.js'
createApp(App).mount('#app')

5.创建组件 MonacoVite.vue 组件

javascript 复制代码
<template>
    <div :style="{
         height: height+'px',
    width: width+'px'
    }" ref="editorRef"></div>
  </template>
  
  <script setup>
  import {defineEmits, defineProps, onMounted, ref, toRaw, watch} from 'vue';
  import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
  
  const emits = defineEmits(['update:modelValue']);
  
  const props = defineProps({
    height: {
      type: Number,
      default: 500,
    },
    width: {
      type: Number,
      default: 500,
    },
    modelValue: {
      type: String,
      default: '',
    },
    language: {
      type: String,
      default: 'json',
    },
    theme: {
      type: String,
      default: 'vs-dark',
    }
  });
  
  const editorRef = ref(null);
  const editorInstance = ref(null);
  onMounted(() => {
    if (editorRef.value && !editorInstance.value) {
      editorInstance.value = monaco.editor.create(editorRef.value, {
        value: props.modelValue,
        language: props.language,
        theme: props.theme,
        scrollBeyondLastLine: false,
      });
      editorInstance.value.onDidChangeModelContent((event) => {
        emits('update:modelValue', toRaw(editorInstance.value).getValue());
      });
    }
  });
  
  
  // 监听外部code变化,更新内部状态
  watch(() => props.modelValue, (newVal, oldVal) => {
    let currValue = toRaw(editorInstance.value).getValue();
    if (newVal!==currValue){
      toRaw(editorInstance.value).setValue(newVal)
    }
  }, {deep: true});
  
  </script>

6. App.vue中引入组件

javascript 复制代码
import MonacoVite from './components/MonacoVite.vue'
javascript 复制代码
 <monaco-vite :width="500" :height="500" v-model:="codedata" language="json"></monaco-vite>

App.vue

javascript 复制代码
<script setup>
import {ref} from 'vue'
import Versions from './components/Versions.vue'
const ipcHandle = () => window.electron.ipcRenderer.send('ping')

import MonacoVite from './components/MonacoVite.vue'
const codedata=ref("123")

</script>

<template>
  
  <monaco-vite :width="800" :height="800" v-model:="codedata" language="json"></monaco-vite>
 
  <div class="actions">
    <div class="action">
      <a href="https://electron-vite.org/" target="_blank" rel="noreferrer">Documentation</a>
    </div>
    <div class="action">
      <a target="_blank" rel="noreferrer" @click="ipcHandle">Send IPC</a>
    </div>
  </div>
  <Versions />
</template>

7.运行测试

参考链接:

1.GitHub - microsoft/monaco-editor: A browser based code editor

2.https://www.npmjs.com/package/vite-plugin-monaco-editor

3.Vue3 Vite Monaco Editor - boommanpro

4.在 vite-vue3.x 中的使用 vscode monaco-editor 方法 | NoteZ_技术博客

相关推荐
活宝小娜40 分钟前
vue不刷新浏览器更新页面的方法
前端·javascript·vue.js
程序视点42 分钟前
【Vue3新工具】Pinia.js:提升开发效率,更轻量、更高效的状态管理方案!
前端·javascript·vue.js·typescript·vue·ecmascript
coldriversnow43 分钟前
在Vue中,vue document.onkeydown 无效
前端·javascript·vue.js
我开心就好o44 分钟前
uniapp点左上角返回键, 重复来回跳转的问题 解决方案
前端·javascript·uni-app
开心工作室_kaic2 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端
刚刚好ā2 小时前
js作用域超全介绍--全局作用域、局部作用、块级作用域
前端·javascript·vue.js·vue
沉默璇年3 小时前
react中useMemo的使用场景
前端·react.js·前端框架
yqcoder3 小时前
reactflow 中 useNodesState 模块作用
开发语言·前端·javascript
2401_882727573 小时前
BY组态-低代码web可视化组件
前端·后端·物联网·低代码·数学建模·前端框架
SoaringHeart4 小时前
Flutter进阶:基于 MLKit 的 OCR 文字识别
前端·flutter