Repository navigation
Expand file tree
/
Copy patheditor.old.html
More file actions
428 lines (403 loc) · 13 KB
/
Copy patheditor.old.html
File metadata and controls
428 lines (403 loc) · 13 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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
<!DOCTYPE html>
<html lang="pt">
<head>
<title>Marvi - Editor</title>
<meta charset="UTF-8" />
<link rel="stylesheet" href="/css/index.css" />
<link rel="stylesheet" href="/css/master.css" />
<link rel="stylesheet" href="/css/panic.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script
src="/src-min/ace.js"
type="text/javascript"
charset="utf-8"
></script>
<link rel="shortcut icon" href="assets/favicon.ico" />
<script src="./helper.js" charset="utf-8"></script>
</head>
<body>
<div id="saveDialog" style="display:none">
<div class="dialog">
<button id="closeButton">×</button>
<div id="upload" style="display:none">
<h2>Como você deseja salvar seu código?</h2>
<div class="buttons">
<button onclick="download()" class="purple">
Baixar <span class="icon">⬇</span>
</button>
<button onclick="github()" class="purple">
Github <img src="assets/github.svg" alt="" />
</button>
</div>
<div class="githubPath" hidden>
<input
type="text"
name="githubPath"
placeholder="caminho e nome do arquivo"
/>
<br />
<input
type="text"
name="message"
placeholder="mensagem do commit"
/>
<br />
<br />
<button class="green" onclick="saveGithub()">Salvar</button>
</div>
<br />
<p style="color: darkred;">
LEMBRE-SE DE NÃO INCLUIR A LINHA DE INVOCAÇÃO DA FUNÇÃO
</p>
</div>
<div id="githubLoad" style="display:none">
<h2>Qual arquvo você quer abrir?</h2>
<select id="file">
<option disabled selected value></option>
</select>
<div class="buttons">
<button onclick="loadFile()" class="green">Abrir</button>
</div>
</div>
<div class="buttons" id="custommsg" hidden></div>
</div>
</div>
<div class="container">
<div class="half">
<header class="header editor">
<div>
<a class="white_back" href="/home.html"> ⬅ </a>
<p>Editor javascript</p>
<button onclick="toggleGithub()" id="github-file">
<img src="assets/github.svg" alt="" />
</button>
</div>
<div>
<a href="/editorBlockly.html"> <img src="assets/block.svg" alt="" /> </a>
<a href="/editor2.html"> <img src="assets/arrows.svg" alt="" /> </a>
</div>
</header>
<div id="editor"></div>
<footer class="footer">
<button onclick="run()" class="green">
Executar <span class="icon">▶</span>
</button>
<button onclick="reset()" class="rose">
Reiniciar <span class="icon">↺</span>
</button>
<button onclick="save()" class="purple">
Salvar arquivo <span class="icon">⬇</span>
</button>
</footer>
</div>
<div class="half">
<header class="header console">
Saída do console
<button onclick="clearConsole()" class="wine">Limpar</button>
</header>
<div id="log"></div>
<a id="panic" href="#" onclick="panic()">
<div class="button">pânico</div>
<div class="body">
<div class="center"></div>
<div class="bottom"></div>
</div>
<div class="base"></div>
</a>
</div>
</div>
<script>
const editor = ace.edit("editor");
const logger = document.getElementById("log");
editor.setTheme("ace/theme/monokai");
editor.session.setMode("ace/mode/javascript");
editor.setBehavioursEnabled(false);
editor.focus();
const files = [];
document.onkeyup = function(e) {
if (e.ctrlKey && e.which === 13) {
run();
} else if (e.altKey && e.which === 83) {
e.preventDefault();
save();
}
};
function save() {
$("#saveDialog").show();
$("#upload").show();
$.ajax({
url:
url +
"courses/" +
localStorage.courseId +
"?filter[include]=activities&access_token=" +
localStorage.access_token,
crossDomain: true,
success: res => {
res.activities.map(a => {
if (a.exercises) {
a.exercises.map(e => {
files.push(e.file);
});
}
});
}
});
}
function panic() {
const sure = confirm("O botão de pânico custa 420 moedas. Tem certeza que quer continuar?")
if (!sure) {
return false;
}
const realySure = confirm("VOCÊ TEM MUITA CERTEZA MESMO QUE QUER GASTAR 420 MOEDAS PARA ACIONAR O BOTÃO DE PÂNICO?")
if (!realySure) {
return false;
}
$.ajax({
type: "patch",
url:
url +
"students/" +
localStorage.userId +
"?access_token=" +
localStorage.access_token,
crossDomain: true,
data: {
panic: true
},
success: function(responseData, textStatus, jqXHR) {
alert("Pronto, um facilitador irá te ajudar em breve!");
},
error: function(responseData, textStatus, errorThrown) {
alert("Algo deu errado " + responseData.responseJSON.error.message);
}
});
}
$("#closeButton").click(() => {
$("#saveDialog").toggle();
$("input[name=githubPath]").val("");
$("input[name=message]").val("");
$(".githubPath").toggle();
$("#upload").hide();
$("#githubLoad").hide();
});
function github() {
$(".githubPath").toggle();
}
function pushToGit(code, path) {
const msg = $("input[name=message]").val();
$.ajax({
type: "post",
url:
url +
"students/" +
localStorage.userId +
"/pushToGit?access_token=" +
localStorage.access_token,
crossDomain: true,
data: {
content: btoa(code),
path: path,
message: msg
},
success: function(res, txt, jsXHR) {
alert("Commit criado com sucesso! Veja em seu repositório");
$("#saveDialog").toggle();
$(".githubPath").toggle();
$("#upload").toggle();
$("input[name=githubPath]").val("");
$("input[name=message]").val("");
},
error: function(res, txt, jsXHR) {
if (res.responseJSON.error.statusCode === 401) {
const goToLink =
"<h3>Algo deu errado no upload para o github. Você deve autorizar o acesso novamemte.</h3>" +
'<button onclick="linkToGit()" class="green">Clique aqui para continuar</button>';
$("#upload").hide();
$("#custommsg").append(goToLink);
$("#custommsg").show();
}
}
});
}
function checkFileIsCurrent(obj) {
return (
obj.mode === "100644" &&
obj.path.includes(
"fase0" + JSON.parse(localStorage.user).activityNumber
)
);
}
function toggleGithub() {
const user = JSON.parse(localStorage.user);
$("#saveDialog").show();
$("#githubLoad").show();
$.ajax({
url:
"https://api.github.com/repos/" +
user.username +
"/marvin/commits?access_token=" +
JSON.parse(localStorage.user).githubAccessToken,
crossDomain: true,
success: function(res, txt, jsXHR) {
$.ajax({
url:
"https://api.github.com/repos/" +
user.username +
"/marvin/git/trees/" +
res[0].sha +
"?recursive=1&access_token=" +
JSON.parse(localStorage.user).githubAccessToken,
crossDomain: true,
success: function(r, t) {
const files = r.tree.filter(checkFileIsCurrent);
if ($("select#file option").length === 1) {
for (var i = 0; i < files.length; i++) {
$("select#file").append(
"<option value=" +
files[i].path +
">" +
files[i].path +
"</option>"
);
}
}
}
});
},
error: function(res, txt, jsXHR) {
console.log(res);
}
});
}
function loadFile() {
const user = JSON.parse(localStorage.user);
const path = $("select#file").val();
$.ajax({
url:
"https://api.github.com/repos/" +
user.username +
"/marvin/contents/" +
path,
crossDomain: true,
success: function(res) {
editor.getSession().setValue(atob(res.content));
$("#closeButton").trigger("click");
$("input[name=githubPath]").val(path);
}
});
}
function saveGithub() {
const code = editor.getValue();
const path = $("input[name=githubPath]")
.val()
.replace(/\s/g, "");
if (!$("input[name=message]").val())
return alert("Você precisa colocar uma mensagem de commit!");
else if (
(files.includes(path) ||
(!files.includes(path) &&
confirm(
"Parece que você digitou um caminho de arquivo que não corresponde a nenhum exercício. Tem certeza que quer continuar?"
))) &&
(code.match(/(fun.*{[\s\S]*}[\s]*$)/g) ||
(!code.match(/(fun.*{[\s\S]*}[\s]*$)/g) &&
confirm(
"Oops, parece que seu código não segue as normas de envio, tem certeza que quer continuar?"
)))
)
pushToGit(code, path);
else {
$("#saveDialog").toggle();
$("#upload").toggle();
$(".githubPath").hide();
}
}
function linkToGit() {
window.open("/github-link.html", "_blank");
$("#closeButton").trigger("click");
}
function addToLog(msg, err) {
if (err) {
logger.innerHTML =
logger.innerHTML + '<p class="error log">' + msg + "</p>";
} else {
logger.innerHTML =
logger.innerHTML + '<p class="log">' + msg + "</p>";
}
}
(function() {
console.log = function(message) {
if (typeof message === "string") {
addToLog(message.replace(/\n/g, "<br> "));
} else if (typeof message == "object") {
addToLog(
JSON && JSON.stringify ? JSON.stringify(message) : message
);
} else {
addToLog(message);
}
};
})();
window.onerror = function(msg) {
if (typeof msg == "object") {
addToLog(JSON && JSON.stringify ? JSON.stringify(msg) : msg, true);
} else {
addToLog(msg, true);
}
};
function saveCode() {
var code = editor.getValue();
localStorage.setItem("code", code);
}
function reset() {
location.reload();
}
function clearConsole() {
logger.innerHTML = "";
}
function run() {
saveCode();
var code = editor.getValue();
$("#script").remove();
var script = document.createElement("script");
script.setAttribute("id", "script");
try {
script.appendChild(document.createTextNode(code));
document.body.appendChild(script);
} catch (e) {
script.text = code;
document.body.appendChild(script);
}
}
function download() {
const code = editor.getValue();
const element = document.createElement("a");
element.setAttribute(
"href",
"data:text/javascript;charset=utf-8," + encodeURIComponent(code)
);
element.setAttribute("download", "file.js");
element.style.display = "none";
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
$(document).ready(function() {
let code = location.search.substring(1);
let run;
try {
run = atob(code)
} catch (e) {
}
editor.setValue(run || localStorage.code || "");
setInterval(function() {
saveCode();
}, 30000);
});
</script>
</body>
</html>