Skip to content

Editor 编辑器 ​

基础用法 ​

代码编辑器,默认支持多种语法,内部实现了按需引入,使用的时候只需要设置lang就可以,具体参数配置看以下代码

基础示例 ​

vue
<template>
  <fei-editor
    v-model="jsonStr"
    lang="json"
    width="100%"
    height="300"
    :theme="theme"
    :readonly="readonly"
    :font-size="fontSize"
  />
</template>

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

const jsonStr = ref('{"name": "fei-editor", "version": "0.0.2"}')
const theme = ref('chrome')
const readonly = ref(false)
const fontSize = ref(12)
</script>

支持的语言 ​

  • JavaScript
  • JSON
  • HTML
  • CSS
  • SQL
  • Java
  • Markdown
  • Elixir
  • Stylus

支持的主题 ​

  • chrome
  • xcode
  • clouds
  • crimson_editor
  • sqlserver
  • github
  • textmate
  • tomorrow
  • solarized_light
  • cobalt
  • dracula
  • monokai
  • solarized_dark
  • terminal
  • vibrant_ink

属性配置 ​

参数说明类型可选值默认值
v-model绑定值string——
lang语言类型stringjavascript/json/html/css/sql/java/markdown/elixir/stylusjson
theme主题stringchrome/xcode/clouds/crimson_editor/sqlserver/github/textmate/tomorrow/solarized_light/cobalt/dracula/monokai/solarized_dark/terminal/vibrant_inkchrome
height编辑器高度string/number—350
width编辑器宽度string/number—100%
fontSize字体大小number—12
readonly是否只读boolean—false
wrap是否换行boolean—false
snippets是否启用代码片段boolean—true
options其他配置项object——

事件 ​

事件名称说明回调参数
change内容改变时触发(value: string)
blur失去焦点时触发(event: Event)
init编辑器初始化完成时触发(editor: EditorInstance)

方法 ​

方法名说明参数
getValue获取编辑器内容—

高级用法 ​

自定义配置 ​

vue
<template>
  <fei-editor
    v-model="code"
    lang="javascript"
    :options="editorOptions"
    @init="onEditorInit"
  />
</template>

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

const code = ref('console.log("Hello World!")')
const editorOptions = ref({
  enableBasicAutocompletion: true,
  enableSnippets: true,
  enableLiveAutocompletion: true,
  showLineNumbers: true,
  showGutter: true,
  highlightActiveLine: true
})

const onEditorInit = (editor) => {
  console.log('Editor initialized:', editor)
}
</script>

动态切换语言和主题 ​

vue
<template>
  <div>
    <a-select v-model:value="currentLang" @change="onLangChange">
      <a-select-option value="javascript">JavaScript</a-select-option>
      <a-select-option value="json">JSON</a-select-option>
      <a-select-option value="html">HTML</a-select-option>
      <a-select-option value="css">CSS</a-select-option>
    </a-select>
    
    <a-select v-model:value="currentTheme" @change="onThemeChange">
      <a-select-option value="chrome">Chrome</a-select-option>
      <a-select-option value="monokai">Monokai</a-select-option>
      <a-select-option value="dracula">Dracula</a-select-option>
    </a-select>
    
    <fei-editor
      v-model="code"
      :lang="currentLang"
      :theme="currentTheme"
      height="400"
    />
  </div>
</template>

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

const code = ref('')
const currentLang = ref('javascript')
const currentTheme = ref('chrome')

const onLangChange = () => {
  // 语言切换逻辑
}

const onThemeChange = () => {
  // 主题切换逻辑
}
</script>

基于 MIT 许可发布