generated from edrys-org/module-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
241 lines (203 loc) · 7.81 KB
/
index.html
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="A live code editor module for use with stations" />
<meta name="show-in" content="station" />
<title>Code Editor</title>
<script src="https://edrys-org.github.io/edrys/module/edrys.js"></script>
<script src="./split.min.js"></script>
<script defer src="https://edrys-org.github.io/agent-command-runner/command-runner.js"></script>
<link rel="stylesheet" href="https://edrys-org.github.io/edrys/module/vendor/water.min.css" />
<link rel="stylesheet" href="https://edrys-org.github.io/edrys/module/vendor/open-iconic/css/open-iconic.min.css" />
<link rel="stylesheet" data-name="monaco-editor/min/vs/editor/editor.main"
href="monaco-editor/min/vs/editor/editor.main.css">
<script src="./debounce.js"></script>
<script>
var require = { paths: { vs: 'monaco-editor/min/vs' } }
</script>
<script src="./monaco-editor/min/vs/loader.js"></script>
<script src="./monaco-editor/min/vs/editor/editor.main.nls.js"></script>
<script src="./monaco-editor/min/vs/editor/editor.main.js"></script>
</head>
<body>
<button id="run" style="z-index: 100; position: absolute; right: 5px; bottom: 5px;" onclick="run()">
<span class="oi" data-glyph="media-play"></span>
Run Code
</button>
<div class="split">
<div id="commands" style="display: none;"></div>
<div id="editor"></div>
<div id="terminal"></div>
</div>
<script>
let init = false
Edrys.onUpdate((e) => {
if (!init) {
if (Edrys.role == 'station') {
document.getElementById('commands').style.display = 'block'
}
Split(Edrys.role == 'station' ?
['#commands', '#editor', '#terminal'] :
['#editor', '#terminal'], {
direction: 'vertical',
sizes: Edrys.role == 'station' ? [20, 60, 20] : [80, 20]
})
}
init = true
})
let language = Edrys?.module?.config?.language || 'cpp'
let theme = Edrys?.module?.config?.theme || (window.matchMedia &&
window.matchMedia('(prefers-color-scheme: dark)').matches ? 'vs-dark' : 'vs-light')
let running = false;
async function runCommands() {
if (running) return
running = true
document.getElementById('run').disabled = true
terminal.setScrollPosition({ scrollTop: 0 });
let output = 'Running code...\n'
cmds = commands.getValue().split('\n').filter(c => !c.startsWith('#'))
for (const cmd of cmds) {
output += await run_command(cmd.replace('$CODE', btoa(editor.getValue())))
Edrys.sendMessage('output', output)
}
running = false
document.getElementById('run').disabled = false
}
const client = new Date().getTime()
let sendMsgNext = true
function save() {
const value = editor.getValue()
if (sendMsgNext)
Edrys.sendMessage('update', JSON.stringify({ value, client }))
else
sendMsgNext = !sendMsgNext
Edrys.setItem('editorText', value)
}
function run() {
Edrys.sendMessage('run', '')
}
Edrys.onMessage(({ from, subject, body }) => {
if (subject == 'update') {
const b = JSON.parse(body)
if (b.client == client) return
console.log(b.client, client)
sendMsgNext = false
editor.setValue(b.value)
}
else if (subject == 'run' && Edrys.role == 'station') {
runCommands()
}
else if (subject == 'output') {
terminal.setValue(body)
terminal.revealLineInCenter(0);
}
})
const editor = monaco.editor.create(document.getElementById('editor'), {
value: `// Type code here...
// Right click or Ctrl + S to upload your code.`,
language: language,
theme: theme,
automaticLayout: true
})
const terminal = monaco.editor.create(document.getElementById('terminal'), {
value: `Right click or press Ctrl + S to upload code. Press F1 for more options.
Note your code is not saved, so be sure to keep a copy.`,
theme: theme,
readOnly: true,
lineNumbers: false,
minimap: {
enabled: false
},
automaticLayout: true
})
const commands = monaco.editor.create(document.getElementById('commands'), {
value: `# Type commands to be run here, one command per line\n
# The string '$CODE' will be replaced with the submitted code (base64-encoded)
echo "This is the code submitted:"
echo $CODE | base64 --decode`,
language: 'shell',
theme: theme,
lineNumbers: false,
minimap: {
enabled: false
},
automaticLayout: true
})
editor.addAction({
id: 'upload-code',
label: 'Save & upload code...',
keybindings: [
monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_S
],
precondition: null,
keybindingContext: null,
contextMenuGroupId: 'navigation',
contextMenuOrder: 0,
run: function (ed) {
save()
run()
return null
}
});
editor.addAction({
id: 'toggle-theme',
label: 'Toggle dark theme',
precondition: null,
keybindingContext: null,
contextMenuGroupId: 'navigation',
contextMenuOrder: 1,
run: function (ed) {
let _theme = Edrys.getItem('theme') == 'vs-light' ? 'vs-dark' : 'vs-light'
ed.updateOptions({
theme: _theme
})
Edrys.setItem('theme', _theme)
return null
}
});
editor.onDidChangeModelContent(save);
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.Enter, run)
commands.onDidChangeModelContent(() => {
Edrys.setItem('commands', commands.getValue())
});
Edrys.onReady(() => {
editor.setValue(Edrys.getItem('editorText') || Edrys.module.config.editorText || editor.getValue())
terminal.setValue(Edrys.module.config.terminalText || terminal.getValue())
commands.setValue(Edrys.getItem('commands') || Edrys.module.config.commands || commands.getValue())
})
</script>
<style>
html,
body,
.split {
width: 100%;
height: 100%;
max-width: 100%;
overflow: hidden;
margin: 0;
padding: 0;
}
.gutter {
background-repeat: no-repeat;
background-position: 50%;
}
@media (prefers-color-scheme: light) {
.gutter {
background-color: #ececec;
}
}
@media (prefers-color-scheme: dark) {
.gutter {
background-color: #0f0f0f;
}
}
.gutter.gutter-vertical {
background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB4AAAAFAQMAAABo7865AAAABlBMVEVHcEzMzMzyAv2sAAAAAXRSTlMAQObYZgAAABBJREFUeF5jOAMEEAIEEFwAn3kMwcB6I2AAAAAASUVORK5CYII=');
cursor: row-resize;
}
</style>
</body>
</html>