diff options
author | Nathan Guse <nathaniel.guse@gmail.com> | 2013-05-20 11:57:32 -0500 |
---|---|---|
committer | Nathan Guse <nathaniel.guse@gmail.com> | 2013-05-20 11:57:32 -0500 |
commit | 778658f7876db462fdc37fdfa91c02ad0c4baea1 (patch) | |
tree | 71867437242b3851c49e3b524a90ea6c1870ff8c /phpBB | |
parent | 421452e6fb0b1ebf6695775f370c2ede9ac81406 (diff) | |
parent | c25c17adb7b97956d96d61a11474728732e43604 (diff) | |
download | forums-778658f7876db462fdc37fdfa91c02ad0c4baea1.tar forums-778658f7876db462fdc37fdfa91c02ad0c4baea1.tar.gz forums-778658f7876db462fdc37fdfa91c02ad0c4baea1.tar.bz2 forums-778658f7876db462fdc37fdfa91c02ad0c4baea1.tar.xz forums-778658f7876db462fdc37fdfa91c02ad0c4baea1.zip |
Merge remote-tracking branch 'remotes/cyberalien/feature/editor-code-tabs' into develop
# By Vjacheslav Trushkin
# Via Vjacheslav Trushkin
* remotes/cyberalien/feature/editor-code-tabs:
[feature/editor-code-tabs] Adjust code style to match guidelines
[feature/editor-code-tabs] Correctly calculate end tag location
[feature/editor-code-tabs] Allow tabs when typing code
Diffstat (limited to 'phpBB')
-rw-r--r-- | phpBB/styles/prosilver/template/editor.js | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/phpBB/styles/prosilver/template/editor.js b/phpBB/styles/prosilver/template/editor.js index 0a749347ad..fd4c68adfe 100644 --- a/phpBB/styles/prosilver/template/editor.js +++ b/phpBB/styles/prosilver/template/editor.js @@ -394,3 +394,102 @@ 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; + + if (start > 0) { + for (i = 0; i < endTags.length; i++) { + index = value.lastIndexOf(endTags[i], start - 1); + 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); + |