-
Notifications
You must be signed in to change notification settings - Fork 0
/
setupEditor.js
44 lines (38 loc) · 1.17 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
40
41
42
43
44
import { EditorState, basicSetup } from "@codemirror/basic-setup"
import { defaultTabBinding } from "@codemirror/commands"
import { EditorView, keymap } from "@codemirror/view"
import { json } from "@codemirror/lang-json"
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 }
}