Skip to content

Commit

Permalink
Add customizable indentation and unindent
Browse files Browse the repository at this point in the history
You can now press shift+tab to unindent.
  • Loading branch information
jeroenvisser101 committed Dec 15, 2016
1 parent 862bced commit 253b127
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/codeflask.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function CodeFlask() {

function CodeFlask(indent) {
this.indent = indent || " ";
}

CodeFlask.prototype.run = function(selector, opts) {
Expand Down Expand Up @@ -109,11 +109,19 @@ CodeFlask.prototype.handleInput = function(textarea, highlightCode, highlightPre
inputVal = input.value;

// If TAB pressed, insert four spaces
if(e.keyCode === 9){
input.value = inputVal.substring(0, selStartPos) + " " + inputVal.substring(selStartPos, input.value.length);
input.selectionStart = selStartPos + 4;
input.selectionEnd = selStartPos + 4;
e.preventDefault();
if (e.keyCode === 9) {
e.preventDefault();

// Allow shift-tab if the 4 characters in front are spaces
if (e.shiftKey && inputVal.substring(selStartPos - self.indent.length, selStartPos) == self.indent) {
input.value = inputVal.substring(0, selStartPos - self.indent.length) + inputVal.substring(selStartPos, input.value.length);
input.selectionStart = selStartPos - self.indent.length;
input.selectionEnd = selStartPos - self.indent.length;
} else {
input.value = inputVal.substring(0, selStartPos) + self.indent + inputVal.substring(selStartPos, input.value.length);
input.selectionStart = selStartPos + self.indent.length;
input.selectionEnd = selStartPos + self.indent.length;
}

highlightCode.innerHTML = input.value.replace(/&/g, "&")
.replace(/</g, "&lt;")
Expand Down Expand Up @@ -168,3 +176,7 @@ CodeFlask.prototype.update = function(string) {
evt.initEvent("input", false, true);
this.textarea.dispatchEvent(evt);
}

CodeFlask.prototype.unbind = function (textarea) {
textarea
}

0 comments on commit 253b127

Please sign in to comment.