-
Notifications
You must be signed in to change notification settings - Fork 1
/
setupEditor.js
39 lines (33 loc) · 1.16 KB
/
setupEditor.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
import { basicSetup, EditorState } from "@codemirror/basic-setup";
import { defaultTabBinding } from "@codemirror/commands";
import { json } from "@codemirror/lang-json";
import { EditorView, keymap } from "@codemirror/view";
export default function setupEditors() {
const jsonRequestBody = document.querySelector("[data-json-request-body]");
const jsonResponseBody = document.querySelector("[data-json-response-body]");
const basicExtensions = [basicSetup, keymap.of([defaultTabBinding]), json(), EditorState.tabSize.of(2)];
const requestEditor = new EditorView({
state: EditorState.create({
doc: "{\n\t\n}",
extensions: basicExtensions,
}),
parent: jsonRequestBody,
});
const responseEditor = new EditorView({
state: EditorState.create({
doc: "{}",
extensions: [...basicExtensions, EditorView.editable.of(false)],
}),
parent: jsonResponseBody,
});
function updateResponseEditor(value) {
responseEditor.dispatch({
changes: {
from: 0,
to: responseEditor.state.doc.length,
insert: JSON.stringify(value, null, 2),
},
});
}
return { requestEditor, updateResponseEditor };
}