Skip to content

ProCodeEditor Code Editor

Overview

ProCodeEditor is an advanced code editor component built on CodeMirror 6, supporting multiple programming languages and themes with syntax highlighting, code folding, auto-completion, and more. Ideal for code editing, configuration files, JSON data processing, and similar scenarios.

Basic Usage

vue
<script setup lang="ts">
import ProCodeEditor from "@/components/Pro/ProCodeEditor/index.vue";
import { ref } from "vue";

const code = ref(`{
  "name": "antdv-next-admin",
  "version": "1.0.0"
}`);
</script>

<template>
  <ProCodeEditor v-model="code" language="json" :height="300" />
</template>

Supported Languages

ProCodeEditor supports 14 programming languages:

LanguageValue
JSONjson
JavaScriptjavascript
TypeScripttypescript
HTMLhtml
CSScss
Markdownmarkdown
SQLsql
YAMLyaml
XMLxml
Pythonpython
Javajava
PHPphp
Rustrust
Gogo

Supported Themes

ProCodeEditor supports 12 themes:

ThemeValueDescription
AutoautoFollow system theme (default)
LightlightPure light theme
DarkdarkPure dark theme
GitHub LightgithubGitHub style light
GitHub DarkgithubDarkGitHub style dark
DraculadraculaDracula color scheme
MaterialmaterialMaterial light
Material DarkmaterialDarkMaterial dark
MonokaimonokaiClassic Monokai dark
NordnordNord color scheme
Tokyo NighttokyoNightTokyo Night dark
SolarizedsolarizedSolarized light
Solarized DarksolarizedDarkSolarized dark

Props

PropertyTypeDefaultDescription
modelValuestring''Bound value (code content)
languageSupportedLanguage'json'Editor language type
themeEditorTheme'auto'Editor theme
heightnumber | 'auto'300Editor height (pixels or 'auto')
readonlybooleanfalseRead-only mode
disabledbooleanfalseDisabled state
placeholderstring''Placeholder text
lineNumbersbooleantrueShow line numbers
foldGutterbooleantrueShow code fold buttons
showToolbarbooleanfalseShow default toolbar
showLanguageSelectbooleanfalseShow language selector
formatOnBlurbooleanfalseAuto-format JSON on blur

Events

EventTypeDescription
update:modelValue(value: string) => voidEmitted when code content changes
change(value: string) => voidEmitted when code content changes
focus() => voidEmitted when editor gains focus
blur() => voidEmitted when editor loses focus
languageChange(language: SupportedLanguage) => voidEmitted when language changes

Slots

SlotDescription
toolbarCustom toolbar content

Usage Examples

JSON Editor with Toolbar

vue
<script setup lang="ts">
import ProCodeEditor from "@/components/Pro/ProCodeEditor/index.vue";
import { ref } from "vue";

const jsonData = ref('{\n  "name": "example"\n}');
</script>

<template>
  <ProCodeEditor
    v-model="jsonData"
    language="json"
    :height="400"
    show-toolbar
  />
</template>

Code Preview (Read-only)

vue
<script setup lang="ts">
import ProCodeEditor from "@/components/Pro/ProCodeEditor/index.vue";

const code = `function hello() {
  console.log('Hello, World!')
}`;
</script>

<template>
  <ProCodeEditor
    :model-value="code"
    language="javascript"
    theme="github"
    readonly
    :height="200"
  />
</template>

Custom Toolbar

vue
<script setup lang="ts">
import ProCodeEditor from "@/components/Pro/ProCodeEditor/index.vue";
import { ref } from "vue";

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

<template>
  <ProCodeEditor v-model="code" language="sql" :height="250">
    <template #toolbar>
      <a-space>
        <a-button size="small">Execute Query</a-button>
        <a-button size="small">Format</a-button>
      </a-space>
    </template>
  </ProCodeEditor>
</template>

Theme Switching

vue
<script setup lang="ts">
import ProCodeEditor from "@/components/Pro/ProCodeEditor/index.vue";
import { ref } from "vue";

const code = ref('const theme = "dracula"');
const theme = ref<"light" | "dark" | "dracula">("dark");
</script>

<template>
  <a-space direction="vertical" style="width: 100%">
    <a-radio-group v-model:value="theme" size="small">
      <a-radio-button value="light">Light</a-radio-button>
      <a-radio-button value="dark">Dark</a-radio-button>
      <a-radio-button value="dracula">Dracula</a-radio-button>
    </a-radio-group>

    <ProCodeEditor
      v-model="code"
      language="typescript"
      :theme="theme"
      :height="300"
    />
  </a-space>
</template>

Auto Height

vue
<script setup lang="ts">
import ProCodeEditor from "@/components/Pro/ProCodeEditor/index.vue";
import { ref } from "vue";

const content = ref(`# Markdown Document

This is **Markdown** content.

## Features

- Item 1
- Item 2
`);
</script>

<template>
  <ProCodeEditor
    v-model="content"
    language="markdown"
    height="auto"
    show-language-select
  />
</template>

Features

JSON Formatting and Minification

When language="json", the toolbar automatically shows "Format" and "Minify" buttons:

  • Format: Pretty-print JSON with 2-space indentation
  • Minify: Remove all whitespace and compress to a single line

Auto Theme Detection

When theme="auto", the editor automatically follows system preferences:

  • Uses light theme when system is in light mode
  • Uses dark theme when system is in dark mode

Syntax Validation

Built-in JSON syntax validation with real-time error detection and highlighting.

Copy to Clipboard

Toolbar includes a "Copy" button for one-click copying of editor content.

Type Definitions

typescript
export type SupportedLanguage =
  | "json"
  | "javascript"
  | "typescript"
  | "html"
  | "css"
  | "markdown"
  | "sql"
  | "yaml"
  | "xml"
  | "python"
  | "java"
  | "php"
  | "rust"
  | "go";

export type EditorTheme =
  | "auto"
  | "light"
  | "dark"
  | "github"
  | "githubDark"
  | "dracula"
  | "material"
  | "materialDark"
  | "monokai"
  | "nord"
  | "tokyoNight"
  | "solarized"
  | "solarizedDark";

Released under the MIT License.