-
Notifications
You must be signed in to change notification settings - Fork 11
/
editor.js
165 lines (150 loc) · 3.65 KB
/
editor.js
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import * as CodeMirror from "codemirror";
import { javascript } from "@codemirror/lang-javascript";
import { Compartment, EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { indentWithTab } from "@codemirror/commands";
import { html } from "@codemirror/lang-html";
import { json } from "@codemirror/lang-json";
import { xml } from "@codemirror/lang-xml";
import { css } from "@codemirror/lang-css";
import { oneDark } from "@codemirror/theme-one-dark";
import {
lineNumbers,
highlightActiveLineGutter,
highlightSpecialChars,
drawSelection,
dropCursor,
rectangularSelection,
crosshairCursor,
highlightActiveLine,
keymap,
} from "@codemirror/view";
import {
foldGutter,
indentOnInput,
syntaxHighlighting,
defaultHighlightStyle,
bracketMatching,
foldKeymap,
} from "@codemirror/language";
import { history, defaultKeymap, historyKeymap } from "@codemirror/commands";
import { highlightSelectionMatches, searchKeymap } from "@codemirror/search";
import {
closeBrackets,
autocompletion,
closeBracketsKeymap,
completionKeymap,
} from "@codemirror/autocomplete";
const theme = new Compartment();
const language = new Compartment();
const listener = new Compartment();
const readOnly = new Compartment();
const lineWrapping = new Compartment();
const SUPPORTED_LANGUAGES_MAP = {
javascript,
json,
html,
css,
xml,
txt: () => [],
};
const baseTheme = EditorView.baseTheme({
"&light": {
backgroundColor: "white", // the default codemirror light theme doesn't set this up
"color-scheme": "light",
},
"&dark": {
"color-scheme": "dark",
},
});
const editorView = new CodeMirror.EditorView({
doc: "",
extensions: [
lineNumbers(),
highlightActiveLineGutter(),
highlightSpecialChars(),
history(),
foldGutter(),
drawSelection(),
dropCursor(),
indentOnInput(),
syntaxHighlighting(defaultHighlightStyle, { fallback: true }),
bracketMatching(),
closeBrackets(),
autocompletion(),
rectangularSelection(),
crosshairCursor(),
highlightActiveLine(),
highlightSelectionMatches(),
keymap.of([
...closeBracketsKeymap,
...defaultKeymap,
...searchKeymap,
...historyKeymap,
...foldKeymap,
...completionKeymap,
indentWithTab,
]),
readOnly.of([]),
lineWrapping.of([]),
baseTheme,
theme.of(oneDark),
language.of(json()),
listener.of([]),
],
parent: document.body,
});
function getSupportedLanguages() {
return Object.keys(SUPPORTED_LANGUAGES_MAP);
}
function setDarkMode(active) {
editorView.dispatch({
effects: theme.reconfigure(active ? [oneDark] : []),
});
}
function setLanguage(lang) {
let langFn = SUPPORTED_LANGUAGES_MAP[lang];
editorView.dispatch({
effects: language.reconfigure(langFn ? langFn() : []),
});
}
function setContent(text) {
editorView.dispatch({
changes: { from: 0, to: editorView.state.doc.length, insert: text },
});
}
function getContent() {
return editorView.state.doc.toString();
}
function setListener(fn) {
editorView.dispatch({
effects: listener.reconfigure(
EditorView.updateListener.of((v) => {
if (v.docChanged) {
fn();
}
})
),
});
}
function setReadOnly(value) {
editorView.dispatch({
effects: readOnly.reconfigure(value ? EditorState.readOnly.of(true) : []),
});
}
function setLineWrapping(enabled) {
editorView.dispatch({
effects: lineWrapping.reconfigure(enabled ? EditorView.lineWrapping : []),
});
}
export {
setDarkMode,
setLanguage,
getSupportedLanguages,
setContent,
getContent,
setListener,
setReadOnly,
setLineWrapping,
editorView,
};