-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
editors.ts
122 lines (108 loc) · 2.77 KB
/
editors.ts
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
import * as monaco from 'monaco-editor';
// NOTE: using loader syntax becuase Yaml worker imports editor.worker directly and that
// import shouldn't go through loader syntax.
// @ts-ignore
import EditorWorker from 'worker-loader!monaco-editor/esm/vs/editor/editor.worker';
// @ts-ignore
import JSONWorker from 'worker-loader!monaco-editor/esm/vs/language/json/json.worker';
// @ts-ignore
import GraphQLWorker from 'worker-loader!monaco-graphql/esm/graphql.worker';
const GRAPHQL_LANGUAGE_ID = 'graphql';
// @ts-ignore
window.MonacoEnvironment = {
getWorker(_workerId: string, label: string) {
if (label === GRAPHQL_LANGUAGE_ID) {
return new GraphQLWorker();
}
if (label === 'json') {
return new JSONWorker();
}
return new EditorWorker();
},
};
const operationString =
localStorage.getItem('operations') ??
`
# right click to view context menu
# F1 for command palette
# enjoy prettier formatting, autocompletion,
# validation, hinting and more for GraphQL SDL and operations!
query Example(
$owner: String!
$name: String!
$reviewEvent: PullRequestReviewEvent!
$user: FollowUserInput!
) {
repository(owner: $owner, name: $name) {
stargazerCount
}
}
`;
const variablesString =
localStorage.getItem('variables') ??
`{
"reviewEvent": "graphql",
"name": true
}`;
const resultsString = `{}`;
const THEME = 'vs-dark';
export function createEditors() {
const variablesModel = monaco.editor.createModel(
variablesString,
'json',
monaco.Uri.file('/1/variables.json'),
);
const variablesEditor = monaco.editor.create(
document.getElementById('variables') as HTMLElement,
{
model: variablesModel,
language: 'json',
formatOnPaste: true,
formatOnType: true,
theme: THEME,
comments: {
insertSpace: true,
ignoreEmptyLines: true,
},
},
);
const operationModel = monaco.editor.createModel(
operationString,
GRAPHQL_LANGUAGE_ID,
monaco.Uri.file('/1/operation.graphql'),
);
const operationEditor = monaco.editor.create(
document.getElementById('operation') as HTMLElement,
{
model: operationModel,
formatOnPaste: true,
formatOnType: true,
folding: true,
theme: THEME,
language: GRAPHQL_LANGUAGE_ID,
},
);
const resultsModel = monaco.editor.createModel(
resultsString,
'json',
monaco.Uri.file('/1/results.json'),
);
const resultsEditor = monaco.editor.create(
document.getElementById('results') as HTMLElement,
{
model: resultsModel,
language: 'json',
theme: THEME,
wordWrap: 'on',
readOnly: true,
showFoldingControls: 'always',
},
);
return {
operationEditor,
variablesEditor,
resultsEditor,
operationModel,
variablesModel,
};
}