Vue2怎么安装使用MonacoEditor

其他教程   发布日期:2025年02月25日   浏览次数:213

这篇文章主要讲解了“Vue2怎么安装使用MonacoEditor”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Vue2怎么安装使用MonacoEditor”吧!

1.安装MonacoEditor

  1. cnpm install -S editor@1.0.0
  2. cnpm install -S monaco-editor@0.19.3
  3. cnpm install -S monaco-editor-webpack-plugin@1.9.1

2.配置vue.config.js文件

  1. const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
  2. module.exports = {
  3. configureWebpack: {
  4. plugins: [
  5. new MonacoWebpackPlugin()
  6. ]
  7. }

3.建立组件monaco.vue

  1. <template>
  2. <div>
  3. <div class="code-container" ref="container"></div>
  4. </div>
  5. </template>
  6. <script>
  7. import './icon/iconfont.css'
  8. import * as monaco from 'monaco-editor'
  9. import vCompletion from '@/utils/sql.js'//语法提示文件
  10. export default {
  11. name: 'codeEditor',
  12. props: {
  13. opts: {
  14. type: Object,
  15. default() {
  16. return {
  17. language: 'java', // shell、sql、python
  18. readOnly: false, // 不能编辑
  19. }
  20. },
  21. },
  22. value:{
  23. type:String,
  24. default:''
  25. }
  26. },
  27. watch: {
  28. value: {
  29. handler(n) {
  30. if(this.showInit){//初次传值初始化一次
  31. this.init()
  32. this.showInit = false
  33. }
  34. this.monacoInstance.setValue(n)//剩余全部更新内容
  35. },
  36. deep: true,
  37. },
  38. },
  39. data() {
  40. return {
  41. monacoInstance: null,
  42. provider: null,
  43. hints: [],
  44. color: null,
  45. showInit:true
  46. }
  47. },
  48. created() {
  49. this.init()
  50. },
  51. mounted() {
  52. this.init()
  53. },
  54. beforeDestroy() {
  55. this.dispose()
  56. },
  57. methods: {
  58. cloneDeep(suggestions) {
  59. return JSON.parse(JSON.stringify(suggestions))
  60. },
  61. dispose() {
  62. if (this.monacoInstance) {
  63. if (this.monacoInstance.getModel()) {
  64. this.monacoInstance.getModel().dispose()
  65. }
  66. this.monacoInstance.dispose()
  67. this.monacoInstance = null
  68. if (this.provider) {
  69. this.provider.dispose()
  70. this.color.dispose()
  71. this.provider = null
  72. }
  73. }
  74. },
  75. init() {
  76. let that = this
  77. this.dispose()
  78. this.provider = monaco.languages.registerCompletionItemProvider('sql', {
  79. provideCompletionItems(model, position) {
  80. return {
  81. suggestions: that.cloneDeep(vCompletion),//自定义语法提示,代码补全
  82. }
  83. },
  84. triggerCharacters: ['.'],//输入点可触发代码提示
  85. })
  86. //自定义语法高亮
  87. this.color = monaco.languages.setMonarchTokensProvider('sql', {
  88. ignoreCase: true,
  89. tokenizer: {//需要什么颜色,就在里面用正则匹配
  90. root: [
  91. [
  92. /currentUser|#@|RsOk|#string|#switch|#case|selectSql|uuid|addOrderBy|addConditionRequired|countSql|addGroupBy|currentDateTime|addFieldExist|formData|simplePage|RsJson|[@]|RsJsonData|Local|select|#set|#include|#render|#end|#for|#if|#else|#elseif|from|where|addField|addConditionExist|table|SqlUtil|GROUP_CONCAT|BY|AND|ADD|Data|page|IF|as|SUM|MAX|min|AVG|COUNT|ROUND|LEFT|JOIN/,
  93. { token: 'keyword' },
  94. ], //蓝色
  95. [
  96. /[+]|[-]|[*]|[/]|[%]|[>]|[<]|[=]|[!]|[:]|[&&]|[||]/,
  97. { token: 'string' },
  98. ], //红色
  99. [/'.*?'|".*?"/, { token: 'string.escape' }], //橙色
  100. [/#--.*?--#/, { token: 'comment' }], //绿色
  101. [/null/, { token: 'regexp' }], //粉色
  102. [/[{]|[}]/, { token: 'type' }], //青色
  103. // [/[u4e00-u9fa5]/, { token: 'predefined' }],//亮粉色
  104. // [/''/, { token: 'invalid' }],//红色
  105. // [/[u4e00-u9fa5]/, { token: 'number.binary' }],//浅绿
  106. [/(?!.*[a-zA-Z])[0-9]/, { token: 'number.hex' }], //浅绿
  107. [/[(]|[)]/, { token: 'number.octal' }], //浅绿
  108. // [/[u4e00-u9fa5]/, { token: 'number.float' }],//浅绿
  109. ],
  110. },
  111. })
  112. // 初始化编辑器实例
  113. this.monacoInstance = monaco.editor.create(this.$refs['container'], {
  114. value: this.value,
  115. theme: 'vs-dark',
  116. autoIndex: true,
  117. ...this.opts,
  118. })
  119. // 监听编辑器content变化事件
  120. this.monacoInstance.onDidChangeModelContent(() => {
  121. this.$emit('contentChange', this.monacoInstance.getValue())
  122. })
  123. },
  124. },
  125. }
  126. </script>
  127. <style lang="scss" scope>
  128. .code-container {
  129. width: 100%;
  130. height: 500px;
  131. overflow: hidden;
  132. border: 1px solid #eaeaea;
  133. .monaco-editor .scroll-decoration {
  134. box-shadow: none;
  135. }
  136. }
  137. </style>

4.建立语法提示文件sql.js

  1. export default [
  2. /** * 内置函数 */
  3. {
  4. label: 'if',//触发提示的文本
  5. kind: monaco.languages.CompletionItemKind.Function,
  6. insertText: `
  7. #if()
  8. #end`,//补全的文本
  9. insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
  10. detail: '流程控制'
  11. }, {
  12. label: 'if/else',
  13. kind: monaco.languages.CompletionItemKind.Function,
  14. insertText: '
  15. #if()
  16. #else
  17. #end',
  18. insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
  19. detail: '流程控制'
  20. }
  21. ]

5.父组件引入monaco.vue

  1. <template>
  2. <div>
  3. <moaco v-model="value" :opts="opts" @contentChange="contentChange"></moaco>
  4. </div>
  5. </template>
  6. <script>
  7. import monaco from '@/monaco.vue'
  8. export default {
  9. components: {
  10. monaco,
  11. },
  12. data() {
  13. return {
  14. value: '',
  15. countent: '',
  16. opts: {
  17. value: '',
  18. readOnly: false, // 是否可编辑
  19. language: 'sql', // 语言类型
  20. theme: 'vs-dark', // 编辑器主题
  21. },
  22. }
  23. },
  24. methods: {
  25. contentChange(val) {
  26. //每次改变编辑器内容触发事件,先用一个值存放数据
  27. this.countent = val
  28. },
  29. submit() {
  30. //在你提交给后台时将this.countent赋值给value
  31. this.countent = this.value
  32. },
  33. },
  34. }
  35. </script>
  36. <style>
  37. </style>

以上就是Vue2怎么安装使用MonacoEditor的详细内容,更多关于Vue2怎么安装使用MonacoEditor的资料请关注九品源码其它相关文章!