Skip to content

代码编辑器组件

概述

CodeEditor 是基于 CodeMirror 6 的代码编辑器组件,支持多种编程语言语法高亮、自动补全、代码折叠等功能,主题自动跟随系统明暗模式。

组件路径web/apps/web-ele/src/components/zq-form/code-editor/

组件名称CodeEditor

功能特性

  • 多语言支持:JavaScript、TypeScript、Python、SQL、JSON、HTML、CSS、Markdown、XML、YAML
  • 语法高亮:基于 CodeMirror 语言包
  • 主题切换:支持 light/dark/auto(跟随系统)
  • 代码折叠:支持折叠代码块
  • 括号匹配:自动高亮匹配的括号
  • 自动补全:基于语言的智能补全
  • 行号显示:可配置显示/隐藏
  • 自动换行:可配置启用/禁用
  • 历史记录:支持撤销/重做

Props

属性类型默认值说明
modelValuestring''代码内容
languageCodeLanguage'javascript'语言类型
theme'light' | 'dark' | 'auto''auto'主题
readonlybooleanfalse是否只读
disabledbooleanfalse是否禁用
heightnumber | string'auto'高度
minHeightnumber | string'100px'最小高度
maxHeightnumber | string-最大高度
placeholderstring-占位符
tabSizenumber2Tab 缩进大小
lineNumbersbooleantrue是否显示行号
lineWrappingbooleanfalse是否自动换行
foldGutterbooleantrue是否显示代码折叠
highlightActiveLinebooleantrue是否高亮当前行
bracketMatchingbooleantrue是否显示括号匹配
autocompletionbooleantrue是否启用自动补全
indentGuidebooleantrue是否显示缩进指南

支持的语言

typescript
type CodeLanguage =
  | 'javascript'
  | 'typescript'
  | 'python'
  | 'sql'
  | 'json'
  | 'html'
  | 'css'
  | 'markdown'
  | 'xml'
  | 'yaml';

Events

事件参数说明
update:modelValuestring代码内容更新
changestring代码内容变化
focus-编辑器获得焦点
blur-编辑器失去焦点
readyEditorView编辑器初始化完成

Expose 方法

方法参数返回值说明
getView()-EditorView | null获取 CodeMirror EditorView 实例
focus()-void聚焦编辑器
getValue()-string获取代码内容
setValue(value)stringvoid设置代码内容
format()-void格式化代码(仅支持 JSON)

使用示例

基础用法

vue
<template>
  <CodeEditor v-model="code" language="javascript" />
</template>

<script setup>
import { ref } from 'vue';

const code = ref('console.log("Hello World");');
</script>

JSON 编辑器

vue
<template>
  <CodeEditor
    ref="editorRef"
    v-model="jsonCode"
    language="json"
    :height="300"
    placeholder="请输入 JSON 数据"
  />
  <button @click="formatJson">格式化</button>
</template>

<script setup>
import { ref } from 'vue';

const editorRef = ref();
const jsonCode = ref('{"name":"test","value":123}');

function formatJson() {
  editorRef.value?.format();
}
</script>

SQL 编辑器

vue
<template>
  <CodeEditor
    v-model="sql"
    language="sql"
    :height="200"
    :line-wrapping="true"
    placeholder="请输入 SQL 语句"
  />
</template>

<script setup>
import { ref } from 'vue';

const sql = ref('SELECT * FROM users WHERE status = 1');
</script>

只读模式

vue
<template>
  <CodeEditor
    :model-value="readonlyCode"
    language="python"
    :readonly="true"
    :height="150"
  />
</template>

<script setup>
const readonlyCode = `def hello():
    print("Hello, World!")

hello()`;
</script>

自定义配置

vue
<template>
  <CodeEditor
    v-model="code"
    language="typescript"
    theme="dark"
    :height="400"
    :tab-size="4"
    :line-numbers="true"
    :fold-gutter="true"
    :bracket-matching="true"
    :autocompletion="true"
    @ready="handleReady"
    @change="handleChange"
  />
</template>

<script setup>
import { ref } from 'vue';

const code = ref('');

function handleReady(view) {
  console.log('编辑器已就绪', view);
}

function handleChange(value) {
  console.log('代码已变更', value);
}
</script>

与表单设计器集成

在表单设计器中,该组件作为 code-editor 类型的表单项使用:

json
{
  "type": "code-editor",
  "field": "sqlQuery",
  "label": "SQL 查询",
  "props": {
    "language": "sql",
    "height": 200,
    "placeholder": "请输入 SQL 语句"
  }
}

技术实现

  • 基于 CodeMirror 6 构建
  • 使用 Compartment 实现配置动态更新
  • 语言包按需加载,减少初始包体积
  • 主题通过 @codemirror/theme-one-dark 实现暗色模式

Released under the MIT License.