Created
September 18, 2012 16:31
-
-
Save joshbetz/3744131 to your computer and use it in GitHub Desktop.
Add basic <tab>ing ability
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
<?php | |
class Pretty_Code_Editor { | |
function __construct() { | |
add_action( 'admin_print_styles', array( $this, 'editor_styles' ) ); | |
add_action( 'admin_print_footer_scripts', array( $this, 'tabs' ) ); | |
} | |
function editor_styles() { ?> | |
<style> | |
#wp_mce_fullscreen, .wp-editor-area { | |
font-family: "Baskerville", Georgia, Serif !important; | |
font-size: 16px; | |
} | |
</style> | |
<?php } | |
function tabs() { ?> | |
<script> | |
(function($){ | |
$("textarea").keydown(function(e) { | |
// tab was pressed | |
if(e.keyCode === 9) { | |
var start = this.selectionStart, | |
end = this.selectionEnd, | |
$this = $(this); | |
// set textarea value to: text before caret + tab + text after caret | |
$this.val($this.val().substring(0, start) | |
+ "\t" | |
+ $this.val().substring(end)); | |
// put caret at right position again | |
this.selectionStart = this.selectionEnd = start + 1; | |
// prevent the focus lose | |
return false; | |
} | |
// return was pressed | |
else if(e.keyCode === 13) { | |
var start = this.selectionStart, | |
end = this.selectionEnd, | |
$this = $(this), | |
numTabs = countTabs($this.val(), start), | |
tmp = ""; | |
// set up the value of the textarea | |
tmp += $this.val().substring(0, start); | |
tmp += "\n"; | |
for( i = 0; i < numTabs; i++) | |
tmp += "\t"; | |
tmp += $this.val().substring(end); | |
$this.val(tmp); | |
// put caret at right position again | |
this.selectionStart = this.selectionEnd = start + numTabs + 1; | |
// prevent from adding an extra \n at the end | |
return false; | |
} | |
}); | |
function countTabs(string, cursor) { | |
var end = cursor, start; | |
for (start = cursor; start >= 0 && string[start] != "\n"; --start); | |
return strspn(string, "\t", start + 1, end - start + 1); | |
} | |
function strspn(str1, str2, start, lgth) { | |
// http://kevin.vanzonneveld.net | |
// + original by: Valentina De Rosa | |
// + improved by: Brett Zamir (http://brett-zamir.me) | |
// * example 1: strspn('42 is the answer, what is the question ...', '1234567890'); | |
// * returns 1: 2 | |
// * example 2: strspn('foo', 'o', 1, 2); | |
// * returns 2: 2 | |
var found; | |
var stri; | |
var strj; | |
var j = 0; | |
var i = 0; | |
start = start ? (start < 0 ? (str1.length + start) : start) : 0; | |
lgth = lgth ? ((lgth < 0) ? (str1.length + lgth - start) : lgth) : str1.length - start; | |
str1 = str1.substr(start, lgth); | |
for (i = 0; i < str1.length; i++) { | |
found = 0; | |
stri = str1.substring(i, i + 1); | |
for (j = 0; j <= str2.length; j++) { | |
strj = str2.substring(j, j + 1); | |
if (stri == strj) { | |
found = 1; | |
break; | |
} | |
} | |
if (found != 1) { | |
return i; | |
} | |
} | |
return i; | |
} | |
})(jQuery); | |
</script> | |
<?php } | |
} | |
new Pretty_Code_Editor(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment