forked from marijnh/Eloquent-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.mjs
More file actions
28 lines (25 loc) · 942 Bytes
/
editor.mjs
File metadata and controls
28 lines (25 loc) · 942 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import {EditorView, keymap, lineNumbers} from "@codemirror/view"
import {EditorState, Compartment} from "@codemirror/state"
import {minimalSetup} from "codemirror"
import {html} from "@codemirror/lang-html"
import {javascript} from "@codemirror/lang-javascript"
import {bracketMatching, syntaxHighlighting} from "@codemirror/language"
import {classHighlighter} from "@lezer/highlight"
let modeCompartment = new Compartment
export function createState(code, mode, extensions = []) {
return EditorState.create({
doc: code,
extensions: [
extensions,
modeCompartment.of(mode == "html" ? html() : javascript()),
minimalSetup,
syntaxHighlighting(classHighlighter),
bracketMatching(),
lineNumbers(),
EditorView.contentAttributes.of({"aria-label": "Code editor"})
]
})
}
export function updateLanguage(mode) {
return modeCompartment.reconfigure(mode == "html" ? html() : javascript())
}