Vue3中使用Monaco-editor的教程详解

 更新时间:2023年11月01日 13:58:40   作者:会说法语的猪  
Monaco-editor,一个vs code 编辑器,需要将其继承到项目,这篇文章主要为大家详细介绍了如何在vue中安装和使用Monaco-editor,有需要的小伙伴可以参考下

Monaco-editor,一个vs code 编辑器,需要将其继承到项目。不说闲话了,直接上代码。 

npm地址:https://www.npmjs.com/package/monaco-editor

中文文档:https://aydk.site/editor/

安装

pnpm add monaco-editor -S
pnpm add vite-plugin-monaco-editor -D

配置

vite.config.ts

import { defineConfig} from 'vite'
 
// vs code 编辑器配置
import monacoEditorPlugin from 'vite-plugin-monaco-editor'
 
// https://vitejs.dev/config/
export default ({ mode }) => {
  return defineConfig({
    plugins: [
      monacoEditorPlugin({
        languageWorkers: ['editorWorkerService', 'typescript', 'json', 'html']
      })
    ]
  })
}

封装

首先先封装个hook如下:

@/hooks/useMonacoEditor.hook.ts 

import * as monaco from 'monaco-editor'
import useCommonStore from '@/store/common'
import { ref, nextTick, onBeforeUnmount } from 'vue'
 
export function useMonacoEditor(language: string = 'javascript') {
  // 编辑器示例
  let monacoEditor: monaco.editor.IStandaloneCodeEditor | null = null
  // 目标元素
  const monacoEditorRef = ref<HTMLElement | null>(null)
 
  // 创建实例
  function createEditor(editorOption: monaco.editor.IStandaloneEditorConstructionOptions = {}) {
    if(!monacoEditorRef.value) return
    monacoEditor = monaco.editor.create(monacoEditorRef.value, {
      // 初始模型
      model: monaco.editor.createModel('', language),
      // 是否启用预览图
      minimap: { enabled: true },
      // 圆角
      roundedSelection: true,
      // 主题
      theme: useCommonStore().mode == 'dark' ? 'vs-dark' : 'vs',
      // 主键
      multiCursorModifier: 'ctrlCmd',
      // 滚动条
      scrollbar: {
        verticalScrollbarSize: 8,
        horizontalScrollbarSize: 8
      },
      // 行号
      lineNumbers: 'on',
      // tab大小
      tabSize: 2,
      //字体大小
      fontSize: 16,
      // 控制编辑器在用户键入、粘贴、移动或缩进行时是否应自动调整缩进
      autoIndent: 'advanced',
      // 自动布局
      automaticLayout: true,
      ...editorOption
    })
    return monacoEditor
  }
 
  // 格式化
  async function formatDoc() {
    await monacoEditor?.getAction('editor.action.formatDocument')?.run()
  }
 
  // 数据更新
  function updateVal(val: string) {
    nextTick(() => {
      if(getOption(monaco.editor.EditorOption.readOnly)) {
        updateOptions({ readOnly: false })
      }
      monacoEditor?.setValue(val)
      setTimeout(async () => {
        await formatDoc()
      }, 10)
    })
  }
 
  // 配置更新
  function updateOptions(opt: monaco.editor.IStandaloneEditorConstructionOptions) {
    monacoEditor?.updateOptions(opt)
  }
 
  // 获取配置
  function getOption(name: monaco.editor.EditorOption) {
    return monacoEditor?.getOption(name)
  }
 
  // 获取实例
  function getEditor() {
    return monacoEditor
  }
 
  // 页面离开 销毁
  onBeforeUnmount(() => {
    if(monacoEditor) {
      monacoEditor.dispose()
    }
  })
 
  return {
    monacoEditorRef,
    createEditor,
    getEditor,
    updateVal,
    updateOptions,
    getOption,
    formatDoc
  }
}

然后调用上面 useMonacoEditor 封装editor编辑器组件

@/components/MonacoEditor/index.vue 

<template>
  <div ref="monacoEditorRef" :style="monacoEditorStyle"></div>
</template>
<script setup lang="ts">
import { useMonacoEditor } from '@/hooks'
import { onMounted, computed, watch } from 'vue'
 
const props = withDefaults(defineProps<{
  width?: string | number,
  height?: string | number,
  language?: string,
  editorOption?: Object,
  modelValue: string
}>(), {
  width: '100%',
  height: '100%',
  language: 'javascript',
  editorOption: () => ({}),
  modelValue: ''
})
 
const emits = defineEmits<{
  (e: 'blue'): void,
  (e: 'update:modelValue', val: string): void
}>()
 
const monacoEditorStyle = computed(() => {
  return { 
    width: typeof props.width === 'string' ? props.width : props.width + 'px', 
    height: typeof props.height === 'string' ? props.height : props.height + 'px'
  }
})
 
const { monacoEditorRef, createEditor, updateVal, updateOptions, getEditor } = useMonacoEditor(props.language)
 
onMounted(() => {
  const monacoEditor = createEditor(props.editorOption)
  updateMonacoVal(props.modelValue)
  monacoEditor?.onDidChangeModelContent(() => {
    emits('update:modelValue', monacoEditor!.getValue())
  })
  monacoEditor?.onDidBlurEditorText(() => {
    emits('blue')
  })
})
 
watch(() => props.modelValue, () => {
  updateMonacoVal(props.modelValue)
})
 
function updateMonacoVal(val: string) {
  if(val !== getEditor()?.getValue()) {
    updateVal(val)
  }
}
 
defineExpose({ updateOptions })
</script>

组件使用: 

<div class="edit-container">
  <MonacoEditor ref="MonacoEditRef" v-model="editJson" language="json" />
</div>
<script setup lang="ts">
import MonacoEditor from '@/components/MonacoEditor/index.vue'
import { ref } from 'vue'
 
let editJson = ref('')
 
const MonacoEditRef = ref<InstanceType<typeof MonacoEditor>>()
 
//MonacoEditRef.value!.updateOptions({ theme: 'vs' }) 调用子组件方法修改配置
</script>

到此这篇关于Vue3中使用Monaco-editor的教程详解的文章就介绍到这了,更多相关Vue3使用Monaco-editor内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Vue组件通信入门之Provide和Inject机制

    Vue组件通信入门之Provide和Inject机制

    这篇文章主要给大家介绍了关于Vue组件通信入门之Provide和Inject机制的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Vue组件通信具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-12-12
  • vue移动端轻量级的轮播组件实现代码

    vue移动端轻量级的轮播组件实现代码

    这篇文章主要介绍了vue移动端轻量级的轮播组件实现代码,一个简单的移动端卡片滑动轮播组件,适用于Vue2.x。本文给大家带来了实例代码,需要的朋友参考下吧
    2018-07-07
  • vue项目如何部署运行到tomcat上

    vue项目如何部署运行到tomcat上

    这篇文章主要介绍了vue项目如何部署运行到tomcat上的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • vue项目安装使用vconsole方式

    vue项目安装使用vconsole方式

    这篇文章主要介绍了vue项目安装使用vconsole方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-11-11
  • vue-autoui自匹配webapi的UI控件的实现

    vue-autoui自匹配webapi的UI控件的实现

    这篇文章主要介绍了vue-autoui自匹配webapi的UI控件的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-03-03
  • vue使用keep-alive如何实现多页签并支持强制刷新

    vue使用keep-alive如何实现多页签并支持强制刷新

    这篇文章主要介绍了vue使用keep-alive如何实现多页签并支持强制刷新,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • 一文带你理解 Vue 中的生命周期

    一文带你理解 Vue 中的生命周期

    在我们实际项目开发过程中,会非常频繁地和 Vue 组件的生命周期打交道,接下来我们就从源码的角度来看一下这些生命周期的钩子函数是如何被执行的,需要的朋友可以参考下面文章内容
    2021-09-09
  • vue实现右键点击弹框信息功能

    vue实现右键点击弹框信息功能

    这篇文章主要介绍了vue实现右键点击弹框信息功能方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • vue查询数据el-table不更新数据的解决方案

    vue查询数据el-table不更新数据的解决方案

    这篇文章主要介绍了vue查询数据el-table不更新数据的问题及解决方案,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-12-12
  • vue-router的HTML5 History 模式设置

    vue-router的HTML5 History 模式设置

    这篇文章主要介绍了vue-router的HTML5 History模式设置,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-09-09

最新评论