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

相关推荐
Aphelios38010 小时前
Linux 下 VIM 编辑器学习记录:从基础到进阶(上)
java·linux·编辑器·vim
workflower12 小时前
实例研究:设计一个文档编辑器(24)- 完
java·开发语言·设计模式·编辑器·软件工程·需求分析·软件需求
咩咩大主教13 小时前
VSCode运行Go程序报错:Unable to process `evaluate`: debuggee is running
开发语言·ide·vscode·golang·编辑器
温酒往事·17 小时前
无缝对接[系列2]:在VSCode中继续接入本地DeepSeek的完整指南
ide·vscode·编辑器
徐小夕@趣谈前端17 小时前
tmagic-editor,腾讯开源的基于 Vue3 的页面可视化编辑器
编辑器
徐小夕@趣谈前端18 小时前
支持列表拖拽嵌套,AI流式输出的多模态文档编辑器flowmix/docx: 全面升级
人工智能·编辑器
下雪了 ~18 小时前
使用sublime_text中,TAB键无效怎么解决???
编辑器·sublime text
want_water_fish19 小时前
2步破解官方sublime4最新版本 4192
编辑器·sublime text
m0_748241121 天前
Node.js使用教程
node.js·编辑器·vim