Created
June 19, 2015 01:34
-
-
Save haskellcamargo/8afd79bf27754ddc78dc to your computer and use it in GitHub Desktop.
Identize
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title></title> | |
</head> | |
<body> | |
<textarea cols="50" rows="10" id="ide"></textarea> | |
</body> | |
<script type="text/javascript"> | |
/** | |
* Identize | |
* @author Marcelo Camargo | |
* https://github.com/haskellcamargo | |
*/ | |
HTMLTextAreaElement.prototype.identize = function(spaceNum) { | |
this.onkeydown = function(e) { | |
// Verify if tab | |
if ((e.keyCode | e.which) === 9) { | |
// Get content on the right and left | |
var left = this.value.substr(0, this.selectionStart); | |
var right = this.value.substr(this.selectionEnd); | |
this.value = left + " ".repeat(spaceNum) + right; | |
// Put the cursor in the correct position | |
selectionPosition = left.length + spaceNum | |
this.selectionStart = selectionPosition; | |
this.selectionEnd = selectionPosition; | |
// Prevent the default browser action to jump for another form | |
e.preventDefault(); | |
} | |
} | |
}; | |
document.getElementById("ide").identize(4); | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment