Components/CodeEditor
Since 1.0.0BETA

CodeEditor

CodeMirror-based editor for JSON and YAML

This page was migrated by AI, please review carefully

Migration is complete, but please validate against source code and manual review.

CodeEditor

A CodeMirror 6 based editor with formatting, linting, completion, folding, and search for JSON/YAML.

Basic Usage

Demo loads on client.

Formatting

Enable formatOnBlur or call the instance method format() to format content.

Themes

Use theme to switch the built-in themes; auto follows the external theme data attributes.

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

Toolbar

Use the toolbar slot, commonly paired with 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

NameTypeDefaultDescription
modelValuestring''Content (v-model)
language'json' | 'yaml' | 'toml' | 'ini' | 'javascript' | 'js''json'Language mode
theme'auto' | 'light' | 'dark' | 'github' | 'dracula' | 'monokai''auto'Theme
readOnlybooleanfalseRead-only mode
lineNumbersbooleantrueLine numbers
lineWrappingbooleanfalseLine wrapping
placeholderstring''Placeholder text
tabSizenumber2Tab size
formatOnBlurbooleanfalseFormat on blur
formatOnInitbooleanfalseFormat on init
lintbooleantrueLinting
searchbooleantrueSearch panel (Cmd/Ctrl+F)
completionbooleantrueCompletion
extensionsExtension[][]Custom CodeMirror extensions

Note: formatting and linting are available for json / yaml. Other languages provide highlighting and basic editing only.

Slots

SlotParamsDescription
toolbar{ format, openSearch, foldAll, unfoldAll, copy, getValue }Custom toolbar

Events

EventParamsDescription
update:modelValue(value: string)v-model update
change(value: string)Content change
focus()Focus
blur()Blur
format({ value, language })Format success

Expose

MethodTypeDescription
focus()() => voidFocus editor
blur()() => voidBlur editor
format()() => booleanRun formatter
openSearch()() => booleanOpen search panel
foldAll()() => booleanFold all
unfoldAll()() => booleanUnfold all
copy()() => Promise<boolean>Copy content
getValue()() => stringGet value
getView()() => EditorView | nullGet editor instance

TxCodeEditorToolbar

NameTypeDefaultDescription
actionsCodeEditorToolbarAction[]built-inToolbar actions
compactbooleanfalseCompact mode

TxCodeEditorToolbar Events

EventParamsDescription
action(key)Action clicked
Was this helpful?