组件/CodeEditor 代码编辑器
自 1.0.0BETA

CodeEditor 代码编辑器

基于 CodeMirror 6 的代码编辑器

该页面由 AI 迁移生成,请谨慎使用

内容已迁移完成,但仍建议结合源码和人工评审结果使用。

CodeEditor 代码编辑器

基于 CodeMirror 6 的代码编辑器,支持 JSON/YAML 的格式化、校验、补全、折叠与搜索。

基础用法

示例即将加载...
<script setup lang="ts">
import { ref } from 'vue'

const jsonValue = ref('{"name":"Tuffex","version":1,"features":["lint","format","search"]}')
const yamlValue = ref('name: Tuffex\nversion: 1\nfeatures:\n  - lint\n  - format\n  - search\n')
</script>

<template>
  <div class="grid gap-4">
    <TxCodeEditor v-model="jsonValue" language="json" />
    <TxCodeEditor v-model="yamlValue" language="yaml" />
  </div>
</template>

格式化

通过 formatOnBlur 或实例方法 format() 触发格式化。

主题风格

常用主题通过 theme 切换,auto 会跟随外部主题 data-theme/class。

EXAMPLE.VUE
<template>
  <TxCodeEditor v-model="jsonValue" language="json" theme="github" />
  <TxCodeEditor v-model="jsonValue" language="json" theme="dracula" />
</template>

Toolbar

通过 toolbar slot 接入工具条,建议配合 TxCodeEditorToolbar 使用。

EXAMPLE.VUE
<script setup lang="ts">
import { ref } from 'vue'

const value = ref('{"name":"Tuffex"}')
const toolbarActions = [
  { key: 'format', label: 'Format' },
  { key: 'search', label: 'Search', shortcut: 'Cmd/Ctrl+F' },
  { key: 'foldAll', label: 'Fold' },
  { key: 'unfoldAll', label: 'Unfold' },
  { key: 'copy', label: 'Copy' },
]

function handleToolbarAction(key, handlers) {
  if (key === 'format') handlers.format()
  if (key === 'search') handlers.openSearch()
  if (key === 'foldAll') handlers.foldAll()
  if (key === 'unfoldAll') handlers.unfoldAll()
  if (key === 'copy') handlers.copy()
}
</script>

<template>
  <TxCodeEditor v-model="value" language="json">
    <template #toolbar="{ format, openSearch, foldAll, unfoldAll, copy }">
      <TxCodeEditorToolbar
        :actions="toolbarActions"
        @action="(key) => handleToolbarAction(key, { format, openSearch, foldAll, unfoldAll, copy })"
      />
    </template>
  </TxCodeEditor>
</template>

API

TxCodeEditor Props

属性名类型默认值说明
modelValuestring''内容(v-model)
language'json' | 'yaml' | 'toml' | 'ini' | 'javascript' | 'js''json'语言模式
theme'auto' | 'light' | 'dark' | 'github' | 'dracula' | 'monokai''auto'主题风格
readOnlybooleanfalse只读模式
lineNumbersbooleantrue行号
lineWrappingbooleanfalse自动换行
placeholderstring''占位文本
tabSizenumber2Tab 缩进空格数
formatOnBlurbooleanfalse失焦时格式化
formatOnInitbooleanfalse初始化时格式化
lintbooleantrue语法校验
searchbooleantrue搜索面板(Cmd/Ctrl+F)
completionbooleantrue自动补全
extensionsExtension[][]自定义 CodeMirror 扩展

提示:格式化与校验目前对 json / yaml 生效,其余语言仅提供高亮与基础编辑能力。

Slots

名称参数说明
toolbar{ format, openSearch, foldAll, unfoldAll, copy, getValue }自定义工具条

Events

事件名参数说明
update:modelValue(value: string)v-model 更新
change(value: string)文本变更
focus()聚焦
blur()失焦
format({ value, language })格式化成功

Expose

名称类型说明
focus()() => void聚焦
blur()() => void失焦
format()() => boolean执行格式化
openSearch()() => boolean打开搜索面板
foldAll()() => boolean折叠全部
unfoldAll()() => boolean展开全部
copy()() => Promise<boolean>复制内容
getValue()() => string获取内容
getView()() => EditorView | null获取编辑器实例

TxCodeEditorToolbar

属性名类型默认值说明
actionsCodeEditorToolbarAction[]内置默认工具按钮列表
compactbooleanfalse紧凑样式

TxCodeEditorToolbar Events

事件名参数说明
action(key)点击工具按钮