From c789240a26c7337590f1a72cdf6aa196e6cb5e93 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Fri, 30 Sep 2016 21:40:44 -0400 Subject: [PATCH] Clean up stuff --- main.js | 188 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 98 insertions(+), 90 deletions(-) diff --git a/main.js b/main.js index 34f7ba5..849ae92 100644 --- a/main.js +++ b/main.js @@ -52,97 +52,12 @@ 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) => { - event.preventDefault(); - editor.focus(); - }); - - 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) { - markInvalid(pos); - } - 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) { - var newLine = pos.line; - while (true) { - newLine++; - if (newLine >= editor.doc.size) { // go to end of last line - editor.setCursor(getEndPos()); - break; - } else { // try go to next line - let newText = editor.doc.getLine(newLine); - let newTrimmed = newText.trim(); - if (newTrimmed.length != 0) { // line is not empty (whitespace-only) - let ch = newText.indexOf(newTrimmed); - editor.setCursor({ line: newLine, ch: ch }); - break; - } - } - } - updateIncompleteMark(); - } - } - } - - if (result) { - event.preventDefault(); - } - } - }); + document.addEventListener("keypress", handleKeyPress); + document.addEventListener("keydown", handleKeyDown); }) .catch((e) => { @@ -150,6 +65,99 @@ function setup(data, mime) { }); } +function handleFocus() { + focused = true; +} + +function handleBlur() { + focused = false; +} + +function handleMouseDown(instance, event) { + event.preventDefault(); + editor.focus(); +} + +function handleKeyPress(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) { + markInvalid(pos); + } + editor.setCursor({ line: pos.line, ch: pos.ch + 1 }); + updateIncompleteMark(); + } +} + +function handleKeyDown(event) { + if (focused) { + if (event.keyCode == 8) { // delete + 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(); + } 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(); +} + +function handleEnter(event) { + 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) { + var newLine = pos.line; + while (true) { + newLine++; + if (newLine >= editor.doc.size) { // go to end of last line + editor.setCursor(getEndPos()); + break; + } else { // try go to next line + let newText = editor.doc.getLine(newLine); + let newTrimmed = newText.trim(); + if (newTrimmed.length != 0) { // line is not empty (whitespace-only) + let ch = newText.indexOf(newTrimmed); + editor.setCursor({ line: newLine, ch: ch }); + break; + } + } + } + updateIncompleteMark(); + } + } +} + function moveToEndOfPreviousLine() { let pos = editor.getCursor(); if (pos.line > 0) {