vue3+vite+ts使用monaco-editor编辑器的简单步骤

 更新时间:2023年01月28日 11:48:24   作者:Best_卡卡  
因为毕设需要用到代码编辑器,根据调研,我选择使用monaco-editor代码编辑器,下面这篇文章主要给大家介绍了关于vue3+vite+ts使用monaco-editor编辑器的简单步骤,需要的朋友可以参考下

前言

近期要完成一个代码编辑器的内容,用的vue3.0+ts+vite架构,学习尚浅, 常在插件上遇坑

特此记录下在monaco-editor的使用

需求:yaml和sql的文件的高亮、补全

实现

1.安装

// ^0.34.1
yarn add monaco-editor

2.在vite.config.js中配置(如果不需要ts\js\html就不需要这么做)

// 强制预构建插件包
   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`
    ], 
  },

3.组件封装与使用

monacoEditor.vue组件

<template>
  <div
    ref="codeEditBox"
    class="codeEditBox"
    :class="hightChange&&'codeEditBox1'"
  />
</template>
<script lang="ts">
	import {
	  defineComponent, onBeforeUnmount, onMounted, ref, watch,
	} from 'vue'
	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'
	import EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker'
	import * as monaco from 'monaco-editor'
	import { language as sqlLanguage } from 'monaco-editor/esm/vs/basic-languages/sql/sql.js';
	import { language as yamlLanguage } from 'monaco-editor/esm/vs/basic-languages/yaml/yaml.js';
	import 'monaco-editor/esm/vs/basic-languages/sql/sql.contribution'
	import { editorProps } from './monacoEditorType'
	export default defineComponent({
	  name: 'MonacoEditor',
	  props: editorProps,
	  emits: ['update:modelValue', 'change', 'editor-mounted'],
	  setup(props, { emit }) {
	    (self as any).MonacoEnvironment = {
	      getWorker(_: string, label: string) {
	        if (label === 'json') {
	          return new JsonWorker()
	        }
	        if (['css', 'scss', 'less'].includes(label)) {
	          return new CssWorker()
	        }
	        if (['html', 'handlebars', 'razor'].includes(label)) {
	          return new HtmlWorker()
	        }
	        if (['typescript', 'javascript'].includes(label)) {
	          return new TsWorker()
	        }
	        return new EditorWorker()
	      },
	    }
	    let editor: any
	    const codeEditBox = ref()
	
	    const init = () => {
	      monaco.languages.typescript.javascriptDefaults.setDiagnosticsOptions({
	        noSemanticValidation: true,
	        noSyntaxValidation: false,
	      })
	      monaco.languages.typescript.javascriptDefaults.setCompilerOptions({
	        target: monaco.languages.typescript.ScriptTarget.ES2020,
	        allowNonTsExtensions: true,
	      })
	      monaco.languages.registerCompletionItemProvider('sql', {
	        provideCompletionItems() {
	          const suggestions:any = [];
	          // 这个keywords就是sql.js文件中有的
	          sqlLanguage.keywords.forEach((item:any) => {
	            suggestions.push({
	              label: item,
	              kind: monaco.languages.CompletionItemKind.Keyword,
	              insertText: item,
	            });
	          })
	          sqlLanguage.operators.forEach((item:any) => {
	            suggestions.push({
	              label: item,
	              kind: monaco.languages.CompletionItemKind.Operator,
	              insertText: item,
	            });
	          })
	          sqlLanguage.builtinFunctions.forEach((item:any) => {
	            suggestions.push({
	              label: item,
	              kind: monaco.languages.CompletionItemKind.Function,
	              insertText: item,
	            });
	          })
	          sqlLanguage.builtinVariables.forEach((item:any) => {
	            suggestions.push({
	              label: item,
	              kind: monaco.languages.CompletionItemKind.Variable,
	              insertText: item,
	            });
	          })
	          return {
	            // 最后要返回一个数组
	            suggestions,
	          };
	        },
	      })
	      monaco.languages.registerCompletionItemProvider('yaml', {
	        provideCompletionItems() {
	          const suggestions:any = [];
	          // 这个keywords就是python.js文件中有的
	          yamlLanguage.keywords.forEach((item:any) => {
	            suggestions.push({
	              label: item,
	              kind: monaco.languages.CompletionItemKind.Keyword,
	              insertText: item,
	            });
	          })
	          return {
	            // 最后要返回一个数组
	            suggestions,
	          };
	        },
	      })
	
	      editor = monaco.editor.create(codeEditBox.value, {
	        value: props.modelValue,
	        language: props.language,
	        readOnly: props.readOnly,
	        theme: props.theme,
	        ...props.options,
	      })

      // 监听值的变化
      editor.onDidChangeModelContent(() => {
        const value = editor.getValue() // 给父组件实时返回最新文本
        emit('update:modelValue', value)
        emit('change', value)
      })

      emit('editor-mounted', editor)
    }
    watch(
      () => props.modelValue,
      (newValue) => {
        if (editor) {
          const value = editor.getValue()
          if (newValue !== value) {
            editor.setValue(newValue)
          }
        }
      },
    )

    watch(
      () => props.options,
      (newValue) => {
        editor.updateOptions(newValue)
      },
      { deep: true },
    )
    watch(
      () => props.readOnly,
      () => {
        console.log('props.readOnly', props.readOnly)
        editor.updateOptions({ readOnly: props.readOnly })
      },
      { deep: true },
    )

    watch(
      () => props.language,
      (newValue) => {
        monaco.editor.setModelLanguage(editor.getModel()!, newValue)
      },
    )

    onBeforeUnmount(() => {
      editor.dispose()
    })

    onMounted(() => {
      init()
    })

    return { codeEditBox }
  },
})
</script>
  <style lang="scss" scoped>
  .codeEditBox {
    width: 100%;
    flex: 1;
    min-height: 100px;
    // height: 200px;
    overflow-y: auto;
  }
  .codeEditBox1{
    height: calc(100% - 323px);
  }
  </style>

4.monacoEditorType.ts类型定义文件

	import { PropType } from 'vue'
	
	export type Theme = 'vs' | 'hc-black' | 'vs-dark'
	export type FoldingStrategy = 'auto' | 'indentation'
	export type RenderLineHighlight = 'all' | 'line' | 'none' | 'gutter'
	export interface Options {
	  automaticLayout: boolean // 自适应布局
	  foldingStrategy: FoldingStrategy // 折叠方式  auto | indentation
	  renderLineHighlight: RenderLineHighlight // 行亮
	  selectOnLineNumbers: boolean // 显示行号
	  placeholder:string
	  minimap: {
	    // 关闭小地图
	    enabled: boolean
	  }
	  // readOnly: Boolean // 只读
	  fontSize: number // 字体大小
	  scrollBeyondLastLine: boolean // 取消代码后面一大段空白
	  overviewRulerBorder: boolean // 不要滚动条的边框
	}
	
	export const editorProps = {
	  modelValue: {
	    type: String as PropType<string>,
	    default: null,
	  },
	  hightChange: {
	    type: Boolean,
	    default: false,
	  },
	  width: {
	    type: [String, Number] as PropType<string | number>,
	    default: '100%',
	  },
	  height: {
	    type: [String, Number] as PropType<string | number>,
	    default: '100%',
	  },
	  language: {
	    type: String as PropType<string>,
	    default: 'javascript',
	  },
	  readOnly: {
	    type: Boolean,
	    default: false,
	  },
	  theme: {
	    type: String as PropType<Theme>,
	    validator(value: string): boolean {
	      return ['vs', 'hc-black', 'vs-dark', 'hc-light'].includes(value)
	    },
	    default: 'vs',
	  },
	  options: {
	    type: Object as PropType<Options>,
	    default() {
	      return {
	        automaticLayout: true,
	        // foldingStrategy: 'indentation',
	        foldingStrategy: 'indentation', // 折叠方式  auto | indentation
	        // renderLineHighlight: 'all',
	        renderLineHighlight: 'all' || 'line' || 'none' || 'gutter', // 行亮
	        selectOnLineNumbers: true, // 显示行号
	        minimap: {
	          // 关闭小地图
	          enabled: false,
	        },
	        placeholder: 'ss',
	        // readOnly: false, // 只读
	        fontSize: 16, // 字体大小
	        scrollBeyondLastLine: false, // 取消代码后面一大段空白
	        overviewRulerBorder: false, // 不要滚动条的边框
	      }
	    },
	  },
	}

5.在父组件中使用

	<monacoEditor
   	v-model="value"
     :language="language"
     :hight-change="hightChange"
     :read-only="tablist.length===0"
     width="100%"
     height="100%"
     @editor-mounted="editorMounted"
   />
   import monacoEditor from './monacoEditor.vue'
   const value = ref('-- select * from infrastructure;')
   const language = ref('sql')
   const hightChange = ref<any>(false)
   const editorMounted = (editor: any) => {
     console.log('editor实例加载完成', editor)
   }

    value:编辑器代码显示的值
    language:要加载的语言类型
    hightChange:改变编辑器的高度(可去掉,我这边有个sql查表的实现,需要在编辑区下面加一个sql查询的表格,所以需要这个参数)
    read-only:编辑区是否是只读的,当左侧文件树为空时没有文件,编辑区不可写
    editorMounted:加载完成操作

//记得在env.d.ts配置包的
declare module 'monaco-editor';
declare module 'monaco-editor/esm/vs/language/typescript/ts.worker?worker'
declare module 'monaco-editor/esm/vs/basic-languages/sql/sql.js';
declare module 'monaco-editor/esm/vs/basic-languages/yaml/yaml.js';
declare module 'monaco-editor/esm/vs/language/json/json.worker?worker'
declare module 'monaco-editor/esm/vs/language/css/css.worker?worker'
declare module 'monaco-editor/esm/vs/language/html/html.worker?worker'
declare module 'monaco-editor/esm/vs/editor/editor.worker?worker'
declare module 'monaco-editor/esm/vs/basic-languages/javascript/javascript.js'

打包报错的处理

yarn build 时会发生下述报错

我在这里看到了解决办法https://github.com/microsoft/monaco-editor/blob/main/docs/integrate-esm.md

在monacoEditor.vue组件,重写getWorker方法

<template>
  <div
    ref="codeEditBox"
    class="codeEditBox"
    :class="hightChange&&'codeEditBox1'"
  />
</template>

<script lang="ts">
import {
  defineComponent, onBeforeUnmount, onMounted, ref, watch,
} from 'vue'
// 减去以下包
--  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'
--  import EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker'

import * as monaco from 'monaco-editor'
import { language as sqlLanguage } from 'monaco-editor/esm/vs/basic-languages/sql/sql.js';
import { language as yamlLanguage } from 'monaco-editor/esm/vs/basic-languages/yaml/yaml.js';
import 'monaco-editor/esm/vs/basic-languages/sql/sql.contribution'
import 'monaco-editor/esm/vs/basic-languages/yaml/yaml.contribution'
import 'monaco-editor/esm/vs/basic-languages/javascript/javascript.contribution'
import { editorProps } from './monacoEditorType'

export default defineComponent({
  name: 'MonacoEditor',
  props: editorProps,
  emits: ['update:modelValue', 'change', 'editor-mounted'],
  setup(props, { emit }) {
    (self as any).MonacoEnvironment = {
     ++ getWorker: (_: string, label: string) => {
     ++   const getWorkerModule = (moduleUrl:string, label:string) => new Worker((self as any).MonacoEnvironment.getWorkerUrl(moduleUrl), {
     ++     name: label,
     ++     type: 'module',
     ++   });

     ++   switch (label) {
     ++     case 'json':
     ++       return getWorkerModule('/monaco-editor/esm/vs/language/json/json.worker?worker', label);
     ++     case 'css':
     ++     case 'scss':
     ++     case 'less':
     ++       return getWorkerModule('/monaco-editor/esm/vs/language/css/css.worker?worker', label);
     ++     case 'html':
     ++     case 'handlebars':
     ++     case 'razor':
     ++       return getWorkerModule('/monaco-editor/esm/vs/language/html/html.worker?worker', label);
     ++     case 'typescript':
     ++     case 'javascript':
     ++       return getWorkerModule('/monaco-editor/esm/vs/language/typescript/ts.worker?worker', label);
     ++     default:
     ++       return getWorkerModule('/monaco-editor/esm/vs/editor/editor.worker?worker', label);
     ++   }
     ++ },
      // 原来的减去
      -- getWorker(_: string, label: string) {
      --   if (label === 'json') {
      --     return new JsonWorker()
      --   }
      --   if (['css', 'scss', 'less'].includes(label)) {
      --     return new CssWorker()
      --   }
      --   if (['html', 'handlebars', 'razor'].includes(label)) {
      --     return new HtmlWorker()
      --   }
      --   if (['typescript', 'javascript'].includes(label)) {
      --     return new TsWorker()
      --   }
      --   return new EditorWorker()
      -- },
    }
    let editor: any
    const codeEditBox = ref()

    const init = () => {
      monaco.languages.typescript.javascriptDefaults.setDiagnosticsOptions({
        noSemanticValidation: true,
        noSyntaxValidation: false,
      })
      monaco.languages.typescript.javascriptDefaults.setCompilerOptions({
        target: monaco.languages.typescript.ScriptTarget.ES2020,
        allowNonTsExtensions: true,
      })
      monaco.languages.registerCompletionItemProvider('sql', {
        provideCompletionItems() {
          const suggestions:any = [];
          // 这个keywords就是sql.js文件中有的
          sqlLanguage.keywords.forEach((item:any) => {
            suggestions.push({
              label: item,
              kind: monaco.languages.CompletionItemKind.Keyword,
              insertText: item,
            });
          })
          sqlLanguage.operators.forEach((item:any) => {
            suggestions.push({
              label: item,
              kind: monaco.languages.CompletionItemKind.Operator,
              insertText: item,
            });
          })
          sqlLanguage.builtinFunctions.forEach((item:any) => {
            suggestions.push({
              label: item,
              kind: monaco.languages.CompletionItemKind.Function,
              insertText: item,
            });
          })
          sqlLanguage.builtinVariables.forEach((item:any) => {
            suggestions.push({
              label: item,
              kind: monaco.languages.CompletionItemKind.Variable,
              insertText: item,
            });
          })
          return {
            // 最后要返回一个数组
            suggestions,
          };
        },
      })
      monaco.languages.registerCompletionItemProvider('yaml', {
        provideCompletionItems() {
          const suggestions:any = [];
          // 这个keywords就是python.js文件中有的
          yamlLanguage.keywords.forEach((item:any) => {
            suggestions.push({
              label: item,
              kind: monaco.languages.CompletionItemKind.Keyword,
              insertText: item,
            });
          })
          return {
            // 最后要返回一个数组
            suggestions,
          };
        },
      })

      editor = monaco.editor.create(codeEditBox.value, {
        value: props.modelValue,
        language: props.language,
        readOnly: props.readOnly,
        theme: props.theme,
        ...props.options,
      })

      // 监听值的变化
      editor.onDidChangeModelContent(() => {
        const value = editor.getValue() // 给父组件实时返回最新文本
        emit('update:modelValue', value)
        emit('change', value)
      })

      emit('editor-mounted', editor)
    }
    watch(
      () => props.modelValue,
      (newValue) => {
        if (editor) {
          const value = editor.getValue()
          if (newValue !== value) {
            editor.setValue(newValue)
          }
        }
      },
    )

    watch(
      () => props.options,
      (newValue) => {
        editor.updateOptions(newValue)
      },
      { deep: true },
    )
    watch(
      () => props.readOnly,
      () => {
        console.log('props.readOnly', props.readOnly)
        editor.updateOptions({ readOnly: props.readOnly })
      },
      { deep: true },
    )

    watch(
      () => props.language,
      (newValue) => {
        monaco.editor.setModelLanguage(editor.getModel()!, newValue)
      },
    )

    onBeforeUnmount(() => {
      editor.dispose()
    })

    onMounted(() => {
      init()
    })

    return { codeEditBox }
  },
})
</script>

  <style lang="scss" scoped>
  .codeEditBox {
    width: 100%;
    flex: 1;
    min-height: 100px;
    // height: 200px;
    overflow-y: auto;
  }
  .codeEditBox1{
    height: calc(100% - 323px);
  }
  </style>

总结 

到此这篇关于vue3+vite+ts使用monaco-editor编辑器的文章就介绍到这了,更多相关vue3+vite+ts使用monaco-editor内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • vue3-组合式api-provide/inject详解

    vue3-组合式api-provide/inject详解

    provide/inject 适用于跨级通信,在孙组件中通过依赖注入的方式能获取到父组件中改变的这个值,下面通过实例代码介绍vue3-组合式api-provide/inject的相关知识,需要的朋友可以参考下
    2022-11-11
  • vue实现从外部修改组件内部的变量的值

    vue实现从外部修改组件内部的变量的值

    这篇文章主要介绍了vue实现从外部修改组件内部的变量的值,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • vue 动态组件component

    vue 动态组件component

    这篇文章主要介绍了 vue 动态组件component ,vue提供了一个内置的<component>,专门用来实现动态组件的渲染,这个标签就相当于一个占位符,需要使用is属性指定绑定的组件,想了解更多详细内容的小伙伴请参考下面文章的具体内容
    2021-11-11
  • 实现一个Vue版Upload组件

    实现一个Vue版Upload组件

    这篇文章主要介绍了实现一个Vue版Upload组件,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-08-08
  • 基于Vue实现树形穿梭框的示例代码

    基于Vue实现树形穿梭框的示例代码

    这篇文章主要为大家介绍了如何利用Vue实现一个树形穿梭框,elementUI和ant-d组件库的穿梭框组件效果都不是很好,所以本文将利用一个新的插件来实现,需要的可以参考一下
    2022-04-04
  • Vue.js的Mixins使用方式

    Vue.js的Mixins使用方式

    Vue.js的Mixins功能允许封装可复用的组件选项,实现代码复用和模块化,Mixins可以包含数据、方法、生命周期钩子等组件选项,使用时,Mixins中的选项会被混入组件中,优先级低于组件自身选项,优点包括代码复用、高灵活性和简单易用
    2024-09-09
  • vue + socket.io实现一个简易聊天室示例代码

    vue + socket.io实现一个简易聊天室示例代码

    本篇文章主要介绍了vue + socket.io实现一个简易聊天室示例代码,具有一定的参考价值,有兴趣的可以了解一下。
    2017-03-03
  • vue封装el-upload批量上传只请求一次接口

    vue封装el-upload批量上传只请求一次接口

    本文主要介绍了vue封装el-upload批量上传只请求一次接口,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-02-02
  • 图文详解Vue3没有代码提示问题的解决办法

    图文详解Vue3没有代码提示问题的解决办法

    最近在使用Vue.js时候没有自动提示,就很难受,下面这篇文章主要给大家介绍了关于Vue3没有代码提示问题的解决办法,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2023-01-01
  • vue-列表下详情的展开与折叠案例

    vue-列表下详情的展开与折叠案例

    这篇文章主要介绍了vue-列表下详情的展开与折叠案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07

最新评论