Usage
Two-way binding with v-model
The document is bound through modelValue (not value), so v-model works as with any Vue component.
When modelValue is changed from outside, the editor content is replaced. Two props control how the view behaves at that moment:
scroll-into-view(defaulttrue) scrolls the editor to the updated selection.preserve-scroll-position(defaultfalse) keeps the current scroll position, which is useful when text is appended continuously (logs, streaming output, and so on).
Initial text from a slot
Instead of v-model, you can place the initial text inside the component. Only the text of the first child node is used.
- Slot content overwrites the
v-modelvalue, so do not combine the two. The slot method is intended for simple read-only highlighting. - Wrap the text in
<pre>so that whitespace is not collapsed. - The slot method does not work with Vue 2.7.
Read-only and disabled
Use disabled when the editor is only a syntax highlighter.
Editing behavior
The tab prop traps the Tab key inside the editor. Keyboard users can still leave the editor by pressing Escape followed by Tab.
Custom key bindings
Pass an array of KeyBinding objects to keymap. If run returns true, the default behavior for that key is skipped.
Listening to events
See Events for the payload of each event.
Translating the editor UI
Use phrases to translate the text shown by CodeMirror (search panel, lint panel, folding markers, …). See the CodeMirror translation example for the available keys.
When combining with vue-i18n, make phrases reactive (for example a computed) so the editor updates when the locale changes. See Internationalization for a full translation table and a vue-i18n example.