Load files from GitHub

This commit is contained in:
Shadowfacts 2016-09-29 11:31:59 -04:00
parent 6fedff4466
commit 3e7c2d2d3a
No known key found for this signature in database
GPG Key ID: A7A3BA2CFC135F32
1 changed files with 122 additions and 98 deletions

220
main.js
View File

@ -1,107 +1,131 @@
let editor = new CodeMirror(document.body, {
value: "function myScript() {\t\n\treturn 100;\n}",
mode: "javascript",
readOnly: true,
autofocus: true,
extraKeys: {
Up: () => {},
Down: () => {},
Left: () => {},
Right: () => {}
}
});
var incompleteMark = editor.doc.markText({ line: 0, ch: 0 }, getEndPos(), {
className: "incomplete"
});
var focused = true;
editor.on("focus", (instance, event) => {
focused = true;
});
editor.on("blur", (instance, event) => {
focused = false;
})
editor.on("mousedown", (instance, event) => {
event.preventDefault();
editor.focus();
});
// global vars
var editor; // code mirror instance
var incompleteMark;
var focused = false;
let invalids = [];
document.addEventListener("keypress", (event) => {
if (focused) {
let extensions = {
js: "javascript",
kt: "kotlin"
};
// fetch file and setup
let file = window.location.hash.substring(1);
$.get({
url: `https://raw.githubusercontent.com/${file}`,
success: (data) => {
let parts = file.split(".");
let fileExtension = parts[parts.length - 1];
let extension = extensions.hasOwnProperty(fileExtension) ? extensions[fileExtension] : fileExtension;
setup(data, extension);
}
});
// setup
function setup(data, extension) {
editor = new CodeMirror(document.body, {
value: data,
mode: extension,
readOnly: true,
autofocus: true,
extraKeys: {
Up: () => {},
Down: () => {},
Left: () => {},
Right: () => {}
}
});
incompleteMark = editor.doc.markText({ line: 0, ch: 0 }, getEndPos(), {
className: "incomplete"
});
focused = true;
editor.on("focus", (instance, event) => {
focused = true;
});
editor.on("blur", (instance, event) => {
focused = false;
});
editor.on("mousedown", (instance, event) => {
event.preventDefault();
editor.focus();
});
let pos = editor.getCursor();
let line = editor.doc.getLine(pos.line);
let char = line.charCodeAt(pos.ch);
if (event.charCode != char) {
let mark = editor.doc.markText(pos, {line: pos.line, ch: pos.ch + 1}, {
className: "invalid"
});
if (!invalids[pos.line]) invalids[pos.line] = [];
invalids[pos.line][pos.ch] = mark;
}
editor.setCursor({ line: pos.line, ch: pos.ch + 1 });
updateIncompleteMark();
}
});
document.addEventListener("keydown", (event) => {
if (focused) {
var result = false;
if (event.keyCode == 8) { // delete
result = true;
let pos = editor.getCursor();
if (pos.ch == 0) { // move up 1 line {
moveToEndOfPreviousLine();
} else { // move back 1 char
let line = editor.doc.getLine(pos.line);
if (line.hasOnlyWhiteSpaceBeforeIndex(pos.ch)) {
moveToEndOfPreviousLine();
} else {
editor.setCursor({ line: pos.line, ch: pos.ch - 1 });
}
}
let newPos = editor.getCursor();
let lineInvalids = invalids[newPos.line];
if (lineInvalids) {
let mark = lineInvalids[newPos.ch];
if (mark) {
mark.clear();
lineInvalids.splice(newPos.ch, 1);
}
}
updateIncompleteMark();
} else if (event.keyCode == 13) { // enter
result = true;
let pos = editor.getCursor();
if (pos.line != editor.doc.size - 1) {
let currentLine = editor.doc.getLine(pos.line);
let trimmed = currentLine.trim();
if (editor.getCursor().ch >= currentLine.indexOf(trimmed) + trimmed.length) {
let newLine = pos.line + 1;
let text = editor.doc.getLine(newLine);
let ch = text.indexOf(text.trim());
editor.setCursor({ line: newLine, ch: ch });
updateIncompleteMark();
}
}
}
if (result) {
document.addEventListener("keypress", (event) => {
if (focused) {
event.preventDefault();
let pos = editor.getCursor();
let line = editor.doc.getLine(pos.line);
let char = line.charCodeAt(pos.ch);
if (event.charCode != char) {
let mark = editor.doc.markText(pos, {line: pos.line, ch: pos.ch + 1}, {
className: "invalid"
});
if (!invalids[pos.line]) invalids[pos.line] = [];
invalids[pos.line][pos.ch] = mark;
}
editor.setCursor({ line: pos.line, ch: pos.ch + 1 });
updateIncompleteMark();
}
}
});
});
document.addEventListener("keydown", (event) => {
if (focused) {
var result = false;
if (event.keyCode == 8) { // delete
result = true;
let pos = editor.getCursor();
if (pos.ch == 0) { // move up 1 line {
moveToEndOfPreviousLine();
} else { // move back 1 char
let line = editor.doc.getLine(pos.line);
if (line.hasOnlyWhiteSpaceBeforeIndex(pos.ch)) {
moveToEndOfPreviousLine();
} else {
editor.setCursor({ line: pos.line, ch: pos.ch - 1 });
}
}
let newPos = editor.getCursor();
let lineInvalids = invalids[newPos.line];
if (lineInvalids) {
let mark = lineInvalids[newPos.ch];
if (mark) {
mark.clear();
lineInvalids.splice(newPos.ch, 1);
}
}
updateIncompleteMark();
} else if (event.keyCode == 13) { // enter
result = true;
let pos = editor.getCursor();
if (pos.line != editor.doc.size - 1) {
let currentLine = editor.doc.getLine(pos.line);
let trimmed = currentLine.trim();
if (editor.getCursor().ch >= currentLine.indexOf(trimmed) + trimmed.length) {
let newLine = pos.line + 1;
let text = editor.doc.getLine(newLine);
let ch = text.indexOf(text.trim());
editor.setCursor({ line: newLine, ch: ch });
updateIncompleteMark();
}
}
}
if (result) {
event.preventDefault();
}
}
});
}
function moveToEndOfPreviousLine() {
let pos = editor.getCursor();