-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson-editor.js
More file actions
42 lines (37 loc) · 1.21 KB
/
Copy pathjson-editor.js
File metadata and controls
42 lines (37 loc) · 1.21 KB
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
import {EditorState ,basicSetup } from "@codemirror/basic-setup";
import { EditorView, keymap } from "@codemirror/view";
import { defaultTabBinding } from "@codemirror/commands";
import { json } from "@codemirror/lang-json";
export default function setEditor(){
const jsonRequestBody= document.querySelector('[data-json-request]')
const jsonResponseBody=document.querySelector('[data-json-response-body]');
const basicExtensions= [ basicSetup, //does 95% of basic setup
keymap.of(defaultTabBinding), //allow tab inside the editor
json(), //language
EditorState.tabSize.of(2) //spacing
]
const reqEditor= new EditorView({
state: EditorState.create({
doc:"{\n\t\n}",
extensions: basicExtensions,
}),
parent: jsonRequestBody,
})
const resEditor= new EditorView({
state: EditorState.create({
doc:"{}",
extensions: [...basicExtensions,EditorView.editable.of(false)],
}),
parent: jsonResponseBody,
})
function updateResponseEditor(value){
resEditor.dispatch({
changes:{
from: 0,
to: resEditor.state.doc.length,
insert: JSON.stringify(value,null,2),
}
})
}
return { reqEditor, updateResponseEditor }
}