Clean up stuff

This commit is contained in:
Shadowfacts 2016-09-30 21:40:44 -04:00
parent 821edca54c
commit c789240a26
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 98 additions and 90 deletions

64
main.js
View File

@ -52,19 +52,33 @@ function setup(data, mime) {
focused = true;
editor.on("focus", (instance, event) => {
focused = true;
});
editor.on("blur", (instance, event) => {
focused = false;
});
editor.on("focus", handleFocus);
editor.on("blur", handleBlur);
editor.on("mousedown", handleMouseDown);
editor.on("mousedown", (instance, event) => {
document.addEventListener("keypress", handleKeyPress);
document.addEventListener("keydown", handleKeyDown);
})
.catch((e) => {
throw e;
});
}
function handleFocus() {
focused = true;
}
function handleBlur() {
focused = false;
}
function handleMouseDown(instance, event) {
event.preventDefault();
editor.focus();
});
}
document.addEventListener("keypress", (event) => {
function handleKeyPress(event) {
if (focused) {
event.preventDefault();
@ -77,15 +91,21 @@ function setup(data, mime) {
editor.setCursor({ line: pos.line, ch: pos.ch + 1 });
updateIncompleteMark();
}
});
}
document.addEventListener("keydown", (event) => {
function handleKeyDown(event) {
if (focused) {
var result = false;
if (event.keyCode == 8) { // delete
result = true;
event.preventDefault();
handleDelete(event);
} else if (event.keyCode == 13) { // enter
event.preventDefault();
handleEnter(event);
}
}
}
function handleDelete(event) {
let pos = editor.getCursor();
if (pos.ch == 0) { // move up 1 line {
moveToEndOfPreviousLine();
@ -109,9 +129,9 @@ function setup(data, mime) {
}
updateIncompleteMark();
} else if (event.keyCode == 13) { // enter
result = true;
}
function handleEnter(event) {
let pos = editor.getCursor();
if (pos.line != editor.doc.size - 1) {
let currentLine = editor.doc.getLine(pos.line);
@ -136,18 +156,6 @@ function setup(data, mime) {
updateIncompleteMark();
}
}
}
if (result) {
event.preventDefault();
}
}
});
})
.catch((e) => {
throw e;
});
}
function moveToEndOfPreviousLine() {