aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB
diff options
context:
space:
mode:
authorVjacheslav Trushkin <cyberalien@gmail.com>2013-05-19 17:25:58 +0300
committerVjacheslav Trushkin <cyberalien@gmail.com>2013-05-19 17:25:58 +0300
commit1e216319a75ef35cf6b905bbb6e257a5f105e62a (patch)
tree71cb0788a4ae9325f375a5a7e4bb8b833a50309b /phpBB
parentd8ed228ddf2f4117a5a8044e9903ef22febdeb4a (diff)
downloadforums-1e216319a75ef35cf6b905bbb6e257a5f105e62a.tar
forums-1e216319a75ef35cf6b905bbb6e257a5f105e62a.tar.gz
forums-1e216319a75ef35cf6b905bbb6e257a5f105e62a.tar.bz2
forums-1e216319a75ef35cf6b905bbb6e257a5f105e62a.tar.xz
forums-1e216319a75ef35cf6b905bbb6e257a5f105e62a.zip
[feature/editor-code-tabs] Allow tabs when typing code
Allow tabs when typing code in textarea Keep indentation when typing code in textarea PHPBB3-11557
Diffstat (limited to 'phpBB')
-rw-r--r--phpBB/styles/prosilver/template/editor.js98
1 files changed, 98 insertions, 0 deletions
diff --git a/phpBB/styles/prosilver/template/editor.js b/phpBB/styles/prosilver/template/editor.js
index 0a749347ad..9ec8409560 100644
--- a/phpBB/styles/prosilver/template/editor.js
+++ b/phpBB/styles/prosilver/template/editor.js
@@ -394,3 +394,101 @@ function getCaretPosition(txtarea) {
return caretPos;
}
+
+/**
+* Allow to use tab character when typing code
+* Keep indentation of last line of code when typing code
+*/
+(function($) {
+ $(document).ready(function() {
+ var doc, textarea, startTags, endTags;
+
+ // find textarea, make sure browser supports necessary functions
+ if (document.forms[form_name]) {
+ doc = document;
+ } else {
+ doc = opener.document;
+ }
+
+ if (!doc.forms[form_name]) {
+ return;
+ }
+
+ textarea = doc.forms[form_name].elements[text_name];
+ if (!textarea || typeof textarea.selectionStart !== 'number') {
+ return;
+ }
+
+ // list of allowed start and end bbcode code tags, in lower case
+ startTags = ['[code]', '[code='];
+ endTags = ['[/code]'];
+
+ function inTag() {
+ var start = textarea.selectionStart,
+ lastEnd = -1,
+ lastStart = -1,
+ i, index, value;
+
+ value = textarea.value.toLowerCase();
+
+ for (i = 0; i < startTags.length; i++) {
+ var tagLength = startTags[i].length;
+ if (start >= tagLength)
+ {
+ index = value.lastIndexOf(startTags[i], start - tagLength);
+ lastStart = Math.max(lastStart, index);
+ }
+ }
+ if (lastStart == -1) return false;
+
+ for (i = 0; i < endTags.length; i++) {
+ index = value.lastIndexOf(endTags[i], start);
+ lastEnd = Math.max(lastEnd, index);
+ }
+
+ return (lastEnd < lastStart);
+ }
+
+ function getLastLine() {
+ var start = textarea.selectionStart,
+ value = textarea.value,
+ index = value.lastIndexOf("\n", start - 1);
+ return value.substring(index + 1, start);
+ }
+
+ function appendCode(code) {
+ var start = textarea.selectionStart,
+ end = textarea.selectionEnd,
+ value = textarea.value;
+ textarea.value = value.substr(0, start) + code + value.substr(end);
+ textarea.selectionStart = textarea.selectionEnd = start + code.length;
+ }
+
+ $(textarea).on('keydown', function(event) {
+ var key = event.keyCode || event.which;
+
+ // intercept tabs
+ if (key == 9) {
+ if (inTag()) {
+ appendCode("\t");
+ event.preventDefault();
+ return;
+ }
+ }
+
+ // intercept new line characters
+ if (key == 13) {
+ if (inTag()) {
+ var lastLine = getLastLine(),
+ code = '' + /^\s*/g.exec(lastLine);
+ if (code.length > 0) {
+ appendCode("\n" + code);
+ event.preventDefault();
+ return;
+ }
+ }
+ }
+ });
+ });
+})(jQuery);
+