Components/CodeEditor

CodeEditor

A CodeMirror-based editor for configuration files and code.

VerifiedSince 0.3.4

Usage

Languages

language selects the language. JSON and YAML get formatting and linting; the others get highlighting and basic editing only.

Loading demo...

Toolbar

The toolbar slot exposes the editor methods; TxCodeEditorToolbar provides the standard button layout.

Loading demo...

Best Practices

  • Use JSON or YAML for configuration that needs validation and formatting; TOML, INI, and JavaScript suit highlighting and basic editing only.
  • Leave formatOnBlur off when users may keep invalid syntax mid-edit.
  • Show generated output and examples with readOnly, not inside disabled form fields.
  • Use the toolbar for discoverability, and keep the keyboard shortcuts.
  • Keep custom extensions narrow: they are appended after the built-ins and affect the whole editor.

API Reference

TxCodeEditor

Props

PropTypeDefaultDescription
modelValuestring''The document, bound with v-model.
language'json' | 'yaml' | 'toml' | 'ini' | 'javascript' | 'js''json'Language; js is an alias of javascript.
theme'auto' | 'light' | 'dark' | 'github' | 'dracula' | 'monokai''auto'Palette; auto follows the page theme.
readOnlybooleanfalseBlocks editing and disables formatting.
lineNumbersbooleantrueShows line numbers and highlights the active line's number.
lineWrappingbooleanfalseWraps long lines.
placeholderstring''Text shown while the document is empty.
tabSizenumber2Indent width; invalid values fall back to 2, others are rounded.
formatOnBlurbooleanfalseRuns format() on blur.
formatOnInitbooleanfalseRuns format() once after the editor mounts.
lintbooleantrueShows diagnostics when the language has a linter.
searchbooleantrueEnables the search panel and its shortcuts.
completionbooleantrueEnables autocompletion, bracket closing, and their shortcuts.
extensionsExtension[][]CodeMirror extensions appended after the built-ins.

Events

EventPayloadDescription
update:modelValuestringFires when editing or formatting changes the document.
changestringFires together with update:modelValue.
focus()Fires when the editor gains focus.
blur()Fires when the editor loses focus.
format{ value: string; language: CodeEditorLanguage }Fires when formatting changes the document.

Slots

SlotPropsDescription
toolbar{ format, openSearch, foldAll, unfoldAll, copy, getValue }Toolbar above the editor, rendered once the runtime mounts.

Exposed Methods

MethodTypeDescription
focus()() => voidFocuses the editor.
blur()() => voidBlurs the editor.
format()() => booleanFormats JSON/YAML; returns false when it can't or nothing changes.
openSearch()() => booleanOpens the search panel when search is on.
foldAll()() => booleanFolds every block.
unfoldAll()() => booleanUnfolds every block.
copy()() => Promise<boolean>Copies the document through navigator.clipboard.
getValue()() => stringThe current document; modelValue before the runtime mounts.
getView()() => EditorView | nullThe CodeMirror EditorView; null before mount.

TxCodeEditorToolbar

Props

PropTypeDefaultDescription
actionsCodeEditorToolbarAction[]built-in actionsButtons; when empty, format, search, foldAll, unfoldAll, and copy.
compactbooleanfalseTightens button padding.

Events

EventPayloadDescription
actionCodeEditorToolbarActionKeyFires when an enabled button is clicked.

Slots

SlotPropsDescription
leading-Content before the buttons.
trailing-Content after the buttons.

Types

CodeEditorToolbarAction

FieldTypeDescription
key'format' | 'search' | 'foldAll' | 'unfoldAll' | 'copy'Id emitted with action on click.
labelstringButton text; falls back to a built-in English label.
iconTxIconSource | stringA TxIcon source or icon name.
activebooleanApplies the active style.
disabledbooleanDisables the button and suppresses action.
shortcutstringShortcut text shown after the label.

Overview

  • The runtime editor loads dynamically after mount. Until then, exposed methods return safe fallbacks and the toolbar slot does not render.
  • theme="auto" reads data-theme and the dark / light classes on html / body, and follows changes on html.
  • Only JSON and YAML format and lint; indentation follows tabSize.
  • Writing modelValue from outside replaces the whole document without emitting update:modelValue or change.
  • Cmd/Ctrl+Shift+F runs format(). The shortcut is always intercepted, so it is swallowed even for languages without a formatter.

Technologies

  • JSON formats through JSON.stringify and YAML through the yaml package; TOML and INI highlight with local stream parsers.
  • Source: packages/tuffex/packages/components/src/code-editor/.
查看源码
packages/tuffex/packages/components/src/code-editor/index.ts

Customization

CSS variableUsed for
--tx-code-editor-bgShell and editor background.
--tx-code-editor-borderShell border and toolbar divider.
--tx-code-editor-toolbar-bgToolbar background.
--tx-code-editor-textToolbar text.
--tx-code-editor-focusFocus border and ring.

The resolved theme writes these variables on the root. Prefer theme for palettes; override only when a host theme must tune the shell.