forked from edrys-org/module-code
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
311 lines (251 loc) · 10 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<!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" />
<meta name="description" content="A live code editor module that can be connected to different edrys-modules" />
<meta name="show-in" content="station" />
<title>Code Editor</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cross-lab-project.github.io/edrys-Lite/module/edrys.js"></script>
<link rel="stylesheet" href="https://cross-lab-project.github.io/edrys-Lite/module/vendor/water.min.css" />
<link rel="stylesheet" href="https://cross-lab-project.github.io/edrys-Lite/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>
<script src="https://unpkg.com/vue@3"></script>
</head>
<body>
<div id="app">
<div style="height: 640px;" v-if="!multiFileMode">
<div id="editor" style="height: 100%; max-height: inherit; max-width: inherit;"></div>
<button id="run" style="z-index: 100; position: absolute; right: 5px; bottom: 5px;" @click="runCode">
<span class="oi" data-glyph="media-play"></span>
Run Code
</button>
</div>
<div style="height: 100%;" v-if="multiFileMode">
<button id="run" style="z-index: 100; position: absolute; right: 5px; bottom: 5px;" @click="runCode">
<span class="oi" data-glyph="media-play"></span>
Run Code
</button>
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<li class="nav-item" v-for="(name, index) in filename">
<a class="nav-link" :class="{ 'active': index === 0 }" data-bs-toggle="tab" :href="'#'+identifier(name)">{{name}}</a>
</li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div v-for="(name, index) in filename"
:id="identifier(name)"
class="container tab-pane"
:class="{ 'active': index === 0 }"
style="height: 600px; max-height: 600px; max-width: inherit;">
</div>
</div>
</div>
<script>
const CONTENT = "// Type code here...\n// Right click or Ctrl + S to upload your code.\n"
const EDITOR = 'editor'
var sendMsgNext = true
const client = new Date().getTime()
var editor = {}
function identifier(id) {
return id.replace(".", "_").replace("/", "__")
}
function run() {
Edrys.sendMessage('run', '')
}
function initEditor({
id,
content,
language,
theme
}) {
const editor_ = monaco.editor.create(document.getElementById(id), {
value: content,
language: language,
theme: theme,
automaticLayout: true,
})
function save() {
const value = editor_.getValue()
if (sendMsgNext) {
Edrys.sendMessage('update', JSON.stringify({ id, value, client }))
} else {
sendMsgNext = !sendMsgNext
}
Edrys.setItem(`editorText_${id}`, value)
}
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) {
if (Edrys.module) {
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)
return editor_
}
const {
createApp
} = Vue
const app = createApp({
data() {
return {
multiFileMode: null,
filename: []
}
},
methods: {
runCode() {
run()
},
identifier(id) {
return identifier(id)
},
init({id, content, language, theme}) {
if (typeof content === "string") {
// in this case only a single file will be generated
this.multiFileMode = false
editor = {}
editor[this.identifier(id)] = initEditor({id, content, language, theme})
} else {
this.multiFileMode = true
for(const name in content) {
this.filename.push(name)
}
}
},
initFiles({id, content, language, theme}) {
editor = {}
for(const filename in content) {
let id = this.identifier(filename)
editor[id] = initEditor({
id,
content: content[filename],
language,
theme
})
}
}
}
})
const ui = app.mount("#app")
Edrys.onReady(() => {
const language = Edrys?.module?.config?.language || 'cpp'
const theme = Edrys?.module?.config?.theme || (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'vs-dark' : 'vs-light')
if (Edrys.module.config.file) {
const content = Edrys.module.config.file
for(key in content) {
content[key] = Edrys.getItem(`editorText_${identifier(key)}`) || content[key] || CONTENT
}
ui.init({id: "editor", content, language, theme})
setTimeout(function(){
ui.initFiles({id: "editor", content, language, theme})
}, 1000)
}
else {
const content = Edrys.getItem(`editorText_${EDITOR}`) || Edrys.module.config.editorText || CONTENT
ui.init({id: EDITOR, content, language, theme})
}
})
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[b.id].setValue(b.value)
}
else if (subject == 'run') { // station-less editor && Edrys.role == 'station') {
body = ""
if(ui.multiFileMode) {
let file = {}
for (const name of ui.filename) {
file[name] = editor[identifier(name)].getValue()
}
body = JSON.stringify({ file })
}
else {
body = editor[EDITOR].getValue()
}
if (Edrys.module.config.runCommand) {
Edrys.sendMessage(Edrys.module.config.runCommand, body)
}
}
})
</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>