Skip to content

Instantly share code, notes, and snippets.

@haskellcamargo
Created June 19, 2015 01:34
Show Gist options
  • Save haskellcamargo/8afd79bf27754ddc78dc to your computer and use it in GitHub Desktop.
Save haskellcamargo/8afd79bf27754ddc78dc to your computer and use it in GitHub Desktop.
Identize
<!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