Components/CodeEditor

CodeEditor

CodeMirror-based editor for JSON/YAML plus lightweight TOML, INI, and JavaScript editing, with formatting, linting, search, folding, and a toolbar slot.

VerifiedSince 0.3.4

Usage

Use language to select the CodeMirror language package. JSON and YAML receive formatter and linter support; the other languages provide highlighting and editor behavior.

Loading demo...

Best Practices

  • Use JSON or YAML for editable config surfaces that need validation and formatting. Use TOML/INI/JavaScript modes only when highlighting and basic editing are enough.
  • Keep formatOnBlur off for documents where users may intentionally keep partial or invalid syntax while editing.
  • Use readOnly for generated output and examples; do not hide the editor behind disabled form fields.
  • Prefer toolbar actions for discoverability, but keep keyboard shortcuts enabled for power users.
  • Keep custom extensions narrow and local to the feature. They are appended after built-ins and can change editing behavior globally inside the editor.

API Reference

Props

TxCodeEditor

PropTypeDefaultDescription
modelValuestring''Editor document string used by v-model.
language'json' | 'yaml' | 'toml' | 'ini' | 'javascript' | 'js''json'Language mode. js normalizes to javascript.
theme'auto' | 'light' | 'dark' | 'github' | 'dracula' | 'monokai''auto'Editor palette. auto follows document theme markers.
readOnlybooleanfalseMakes the CodeMirror document non-editable and disables formatting.
lineNumbersbooleantrueShows line numbers and active-line gutter highlighting.
lineWrappingbooleanfalseEnables CodeMirror line wrapping.
placeholderstring''Placeholder extension text.
tabSizenumber2Indentation width. Invalid values normalize to 2; valid values are rounded.
formatOnBlurbooleanfalseRuns format() after blur.
formatOnInitbooleanfalseRuns format() once after the runtime editor mounts.
lintbooleantrueEnables lint gutter and diagnostics when the selected language has a linter.
searchbooleantrueEnables search extension and search keymap.
completionbooleantrueEnables autocompletion, close brackets, and related keymaps.
extensionsExtension[][]Additional CodeMirror extensions appended after built-ins.

TxCodeEditorToolbar

PropTypeDefaultDescription
actionsCodeEditorToolbarAction[]built-in actionsToolbar actions. When empty, the built-in format, search, foldAll, unfoldAll, and copy actions are used.
compactbooleanfalseReduces action button padding.

Data Models

CodeEditorToolbarAction

FieldTypeDescription
key'format' | 'search' | 'foldAll' | 'unfoldAll' | 'copy'Action id emitted by the toolbar.
labelstringOptional visible label. Falls back to a built-in English label.
iconTxIconSource | stringOptional TxIcon source or icon name.
activebooleanApplies active styling.
disabledbooleanDisables the button and suppresses action.
shortcutstringOptional shortcut text rendered after the label.

Events

TxCodeEditor

EventPayloadDescription
update:modelValuestringEmitted when the document changes through user editing or formatter changes.
changestringMirrors document changes for change listeners.
focus()Emitted when the CodeMirror view receives focus.
blur()Emitted when the CodeMirror view loses focus.
format{ value: string; language: CodeEditorLanguage }Emitted after a successful formatter change.

TxCodeEditorToolbar

EventPayloadDescription
actionCodeEditorToolbarActionKeyEmitted when an enabled toolbar action is clicked.

Slots

TxCodeEditor

SlotPropsDescription
toolbar{ format, openSearch, foldAll, unfoldAll, copy, getValue }Optional toolbar area rendered above the editor view after the runtime component mounts.

TxCodeEditorToolbar

SlotPropsDescription
leading-Content before the action group.
trailing-Content after the action group.

Exposed Methods

MethodTypeDescription
focus()() => voidFocuses the CodeMirror view when mounted.
blur()() => voidBlurs the editor content DOM when mounted.
format()() => booleanFormats JSON/YAML. Returns false for read-only, unsupported languages, invalid input, unchanged output, or unmounted runtime.
openSearch()() => booleanOpens the search panel when search is enabled.
foldAll()() => booleanRuns CodeMirror fold-all command.
unfoldAll()() => booleanRuns CodeMirror unfold-all command.
copy()() => Promise<boolean>Copies current content through navigator.clipboard when available.
getValue()() => stringReturns the current editor document, or the prop value before runtime mount.
getView()() => EditorView | nullReturns the CodeMirror EditorView after runtime mount.

Toolbar Slot

Use the toolbar slot to connect editor methods to custom controls. TxCodeEditorToolbar provides the standard action button layout.

Loading demo...

Overview

  • TxCodeEditor dynamically imports the runtime editor on mount; before that, exposed methods return safe fallbacks.
  • theme="auto" reads html / body data-theme and light / dark classes, then observes html class and data-theme changes.
  • JSON formatting uses JSON.parse / JSON.stringify with normalized tabSize; YAML formatting uses the yaml package parser/stringifier.
  • JSON and YAML linting are enabled only when lint=true; TOML, INI, and JavaScript do not currently add custom lint diagnostics.
  • External modelValue changes replace the full editor document without re-emitting update/change events.
  • Toolbar slot props are functions from the runtime editor. They are unavailable until the runtime component has mounted.
  • Cmd/Ctrl+Shift+F inside the editor runs format(); the shortcut is always intercepted (the keymap handler always returns true), so it is swallowed even when the current language has no formatter.

Technologies

  • Reviewed against packages/tuffex/packages/components/src/code-editor/src/types.ts, TxCodeEditor.vue, TxCodeEditorRuntime.vue, TxCodeEditorToolbar.vue, code-editor.test.ts, and code-editor-toolbar.test.ts.
  • API headings now use shared Props, Events, and Slots sections with per-component subsections, so coverage checks and readers find both editor and toolbar contracts.
  • Verified source behavior: client-only runtime import, safe pre-mount exposed-method fallbacks, JSON/YAML formatting, TOML/INI local stream parsers, toolbar default actions, and disabled toolbar action guard.
  • Verified coverage: Coverage: packages/tuffex/packages/components/src/code-editor/__tests__/code-editor.test.ts covers update emission, JSON/YAML formatting, and TOML/INI language modes; code-editor-toolbar.test.ts covers toolbar action emission.
  • format() intentionally returns false instead of throwing for read-only, unsupported, invalid, unchanged, or unmounted states.
  • Component source: packages/tuffex/packages/components/src/code-editor/src/TxCodeEditor.vue
  • Runtime source: packages/tuffex/packages/components/src/code-editor/src/TxCodeEditorRuntime.vue
  • Toolbar source: packages/tuffex/packages/components/src/code-editor/src/TxCodeEditorToolbar.vue
  • Types: packages/tuffex/packages/components/src/code-editor/src/types.ts
  • Tests: packages/tuffex/packages/components/src/code-editor/__tests__/code-editor.test.ts, packages/tuffex/packages/components/src/code-editor/__tests__/code-editor-toolbar.test.ts
查看源码
packages/tuffex/packages/components/src/code-editor/index.ts

Customization

VariableWritten byPurpose
--tx-code-editor-bgresolved themeEditor shell and CodeMirror background.
--tx-code-editor-borderresolved themeShell border and toolbar divider color.
--tx-code-editor-toolbar-bgresolved themeToolbar background.
--tx-code-editor-textresolved themeToolbar text color.
--tx-code-editor-focusresolved themeFocus border and ring color.

Use the theme prop for supported palettes. Override these variables only when a host theme must tune the shell around CodeMirror.