Example: Basic Editor
To create a CodeMirror editor, you instantiate an object of the
EditorView
class. Usually, you'll want to
specify a parent element to put the editor in right away, but it is
also possible to place view.dom
into the document after
initialization.
import {basicSetup} from "codemirror"
import {EditorView} from "@codemirror/view"
const view = new EditorView({
doc: "Start document",
parent: document.body,
extensions: [basicSetup]
})
This will put a text editor with the content “Start document” at the
end of the page.
At any point, view.state
will hold the
current editor state. state.doc.toString()
will give you the
document as a string.
The extensions
field configures the extensions loaded into the
editor. Most functionality—even basic stuff like key bindings, a line
number gutter and syntax highlighting—is implemented as extension. It
is possible to create an editor without extensions, but that'd be a
rather useless editor. The basicSetup
value bundles together a collection of extensions that make a good
default. If you want to more precisely configure your editor, you
could start by inlining its content, as in the example below, and
tweak the result.
import {Extension, EditorState} from "@codemirror/state"
import {
EditorView, keymap, highlightSpecialChars, drawSelection,
highlightActiveLine, dropCursor, rectangularSelection,
crosshairCursor, lineNumbers, highlightActiveLineGutter
} from "@codemirror/view"
import {
defaultHighlightStyle, syntaxHighlighting, indentOnInput,
bracketMatching, foldGutter, foldKeymap
} from "@codemirror/language"
import {
defaultKeymap, history, historyKeymap
} from "@codemirror/commands"
import {
searchKeymap, highlightSelectionMatches
} from "@codemirror/search"
import {
autocompletion, completionKeymap, closeBrackets,
closeBracketsKeymap
} from "@codemirror/autocomplete"
import {lintKeymap} from "@codemirror/lint"
const view = new EditorView({
doc: "Start document",
parent: document.body,
extensions: [
lineNumbers(),
foldGutter(),
highlightSpecialChars(),
history(),
drawSelection(),
dropCursor(),
EditorState.allowMultipleSelections.of(true),
indentOnInput(),
syntaxHighlighting(defaultHighlightStyle),
bracketMatching(),
closeBrackets(),
autocompletion(),
rectangularSelection(),
crosshairCursor(),
highlightActiveLine(),
highlightActiveLineGutter(),
highlightSelectionMatches(),
keymap.of([
...closeBracketsKeymap,
...defaultKeymap,
...searchKeymap,
...historyKeymap,
...foldKeymap,
...completionKeymap,
...lintKeymap
])
]
})
Extensions are also the way you load additional functionality, such as
a language mode or your own custom code, into your editor. There's a
list of extensions provided by the library
that you can scan through to look for a specific feature. Most
language packages expose a main function, named after the language,
that takes a configuration object and returns the extension that
provides the language support.
import {javascript} from "@codemirror/lang-javascript"
import {EditorView, basicSetup} from "codemirror"
const view = new EditorView({
doc: "Start document",
parent: document.body,
extensions: [
basicSetup,
javascript({typescript: true})
]
})
By default, a CodeMirror editor is just a borderless element that will
grow to match its content. You can style it to match
your desired look.