Vue3 + Monaco Editor Web编辑器

yarn add monaco-editor @monaco-editor/loader

Index.vue:

javascript 复制代码
<script setup>
import { ref, onMounted } from 'vue'
import MonacoEditor from './MonacoEditor.vue'

const code = ref('// Hello World')
const language = ref('java')
const theme = ref('default-theme') // default-theme vs-dark

const logValue = () => {
  console.log(code.value)
}

onMounted(() => {
  code.value = 
`
Here is the chat histories between human and assistant, inside <histories></histories> XML tags.

\`\`\`java

示例代码
package com.cool.modules.base.entity.sys;


import com.cool.core.base.BaseEntity;
import com.tangzc.autotable.annotation.enums.IndexTypeEnum;


import com.tangzc.mybatisflex.autotable.annotation.ColumnDefine;
import com.mybatisflex.annotation.Table;
import com.tangzc.autotable.annotation.Index;
import lombok.Getter;
import lombok.Setter;


/**
 * 系统配置
 */
@Getter
@Setter
@Table(value = "base_sys_conf", comment = "系统配置表")
public class BaseSysConfEntity extends BaseEntity<BaseSysConfEntity> {
    @Index(type = IndexTypeEnum.UNIQUE)
    @ColumnDefine(comment = "配置键", notNull = true)
    private String cKey;


    @ColumnDefine(comment = "值", notNull = true, type = "text")
    private String cValue;
}


\`\`\`

<histories>
{{#histories#}}
</histories>


Human: {{#sys.query#}}

Assistant:
`
})
</script>

<template>
  <div id="editor" style="height: 400px">
    <MonacoEditor
      v-model:value="code"
      :language="language"
      :theme="theme"
    />
  </div>
  <div class="controls">
    <label for="language">Select Language: </label>
    <select id="language" v-model="language">
      <option value="java">Java</option>
      <option value="javascript">Javascript</option>
    </select>
  </div>
  <button @click="logValue">Log Value</button>
</template>

MonacoEditor.vue:

javascript 复制代码
<script setup>
import { ref, onMounted, onBeforeUnmount, watch, defineProps, defineEmits } from 'vue'
import loader from '@monaco-editor/loader'

const props = defineProps({
  value: String,
  language: {
    type: String,
    default: 'java'
  },
  theme: {
    type: String,
    default: 'vs-dark'
  }
})

const emits = defineEmits(['update:value'])

const editorContainer = ref(null)
let editorInstance = null

onMounted(() => {
  loader.init().then((monaco) => {
    editorInstance = monaco.editor.create(editorContainer.value, {
      value: props.value || '',
      language: props.language,
      theme: props.theme,
      readOnly: true,
      domReadOnly: true,
      quickSuggestions: false,
      minimap: { enabled: false },
      lineNumbersMinChars: 1,
      lineNumbers: 'off',
      wordWrap: 'on',
      unicodeHighlight: {
        ambiguousCharacters: false
      }
    })

    editorInstance.onDidChangeModelContent(() => {
      emits('update:value', editorInstance.getValue())
    })
  })
})

onBeforeUnmount(() => {
  if (editorInstance) {
    editorInstance.dispose()
  }
})

watch(
  () => props.language,
  (newLanguage) => {
    if (editorInstance) {
      loader.init().then((monaco) => {
        monaco.editor.setModelLanguage(editorInstance.getModel(), newLanguage)
      })
    }
  }
)

watch(
  () => props.value,
  (newValue) => {
    if (editorInstance && editorInstance.getValue() !== newValue) {
      editorInstance.setValue(newValue)
    }
  }
)
</script>

<template>
  <div ref="editorContainer" class="editor-container"></div>
</template>

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

参考链接

https://blog.csdn.net/weixin_67711401/article/details/140729746

https://aydk.site/

https://microsoft.github.io/monaco-editor/

人工智能学习网站

https://chat.xutongbao.top

相关推荐
向宇it2 小时前
【从零开始入门unity游戏开发之——unity篇02】unity6基础入门——软件下载安装、Unity Hub配置、安装unity编辑器、许可证管理
开发语言·unity·c#·编辑器·游戏引擎
徐小夕@趣谈前端2 小时前
可视化大屏编辑器, 开源!
编辑器
天天进步20152 小时前
TipTap编辑器:现代化的富文本编辑解决方案
编辑器
weixin_423196176 小时前
VSCode+WSL作为IDE开发和管理深度学习项目
ide·vscode·编辑器
乐闻x7 小时前
VSCode 插件开发实战(八):创建和管理任务 Task
ide·vscode·编辑器
神洛华9 小时前
Y3地图制作1:水果缤纷乐、密室逃脱
编辑器·游戏引擎·游戏程序
一棵开花的树,枝芽无限靠近你20 小时前
【PPTist】组件结构设计、主题切换
前端·笔记·学习·编辑器
0xdadream20 小时前
typora数学符号
编辑器
带电的小王1 天前
VSCode:VSCode安装 -- 最简洁的VSCode安装教程
ide·vscode·编辑器