-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetupEditor.js
More file actions
74 lines (67 loc) · 2.35 KB
/
Copy pathsetupEditor.js
File metadata and controls
74 lines (67 loc) · 2.35 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
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
// JSON Text Editors
import { EditorState } from "@codemirror/state"
import { EditorView, keymap, lineNumbers, highlightActiveLineGutter, highlightSpecialChars, drawSelection, dropCursor, rectangularSelection, crosshairCursor, highlightActiveLine } from "@codemirror/view"
import { history, historyKeymap, indentWithTab } from "@codemirror/commands"
import { json } from "@codemirror/lang-json"
import { syntaxHighlighting, defaultHighlightStyle, bracketMatching, foldGutter, foldKeymap } from "@codemirror/language"
import { closeBrackets, closeBracketsKeymap, completionKeymap } from "@codemirror/autocomplete"
import { highlightSelectionMatches, searchKeymap } from "@codemirror/search"
import { lintKeymap } from "@codemirror/lint"
// get our actual elements
export default function setupEditors() {
const jsonRequestBody = document.querySelector("[data-json-request-body]")
const jsonResponseBody = document.querySelector("[data-json-response-body]")
const basicExtensions = [
lineNumbers(),
highlightActiveLineGutter(),
highlightSpecialChars(),
history(),
foldGutter(),
drawSelection(),
dropCursor(),
EditorState.allowMultipleSelections.of(true),
syntaxHighlighting(defaultHighlightStyle, { fallback: true }),
bracketMatching(),
closeBrackets(),
rectangularSelection(),
crosshairCursor(),
highlightActiveLine(),
highlightSelectionMatches(),
keymap.of([
...closeBracketsKeymap,
indentWithTab,
...historyKeymap,
...foldKeymap,
...completionKeymap,
...searchKeymap,
...lintKeymap,
]),
json(),
EditorState.tabSize.of(2),
]
const requestEditor = new EditorView({
state: EditorState.create({
doc: "{\n\t\n}", // doc is like text content
extensions: basicExtensions,
}),
parent: jsonRequestBody,
})
const responseEditor = new EditorView({
state: EditorState.create({
doc: "{}", //empty JS object
extensions: [...basicExtensions, EditorView.editable.of(false)],
}),
parent: jsonResponseBody,
})
// update the contents inside the Response Editor
function updateResponseEditor(value) {
responseEditor.dispatch({
changes: {
from: 0,
to: responseEditor.state.doc.length,
insert: JSON.stringify(value, null, 2),
},
})
}
return { requestEditor, updateResponseEditor }
}