From 1e216319a75ef35cf6b905bbb6e257a5f105e62a Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Sun, 19 May 2013 17:25:58 +0300 Subject: [PATCH 1/3] [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 --- phpBB/styles/prosilver/template/editor.js | 98 +++++++++++++++++++++++ 1 file changed, 98 insertions(+) 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); + From a1993bc0745db8b904bd8c851b2f410ca0cd42d0 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Mon, 20 May 2013 18:47:25 +0300 Subject: [PATCH 2/3] [feature/editor-code-tabs] Correctly calculate end tag location PHPBB3-11557 --- phpBB/styles/prosilver/template/editor.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/phpBB/styles/prosilver/template/editor.js b/phpBB/styles/prosilver/template/editor.js index 9ec8409560..4975c8e5a0 100644 --- a/phpBB/styles/prosilver/template/editor.js +++ b/phpBB/styles/prosilver/template/editor.js @@ -441,9 +441,12 @@ function getCaretPosition(txtarea) { } if (lastStart == -1) return false; - for (i = 0; i < endTags.length; i++) { - index = value.lastIndexOf(endTags[i], start); - lastEnd = Math.max(lastEnd, index); + 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); From c25c17adb7b97956d96d61a11474728732e43604 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Mon, 20 May 2013 18:49:52 +0300 Subject: [PATCH 3/3] [feature/editor-code-tabs] Adjust code style to match guidelines PHPBB3-11557 --- phpBB/styles/prosilver/template/editor.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/phpBB/styles/prosilver/template/editor.js b/phpBB/styles/prosilver/template/editor.js index 4975c8e5a0..fd4c68adfe 100644 --- a/phpBB/styles/prosilver/template/editor.js +++ b/phpBB/styles/prosilver/template/editor.js @@ -433,16 +433,14 @@ function getCaretPosition(txtarea) { for (i = 0; i < startTags.length; i++) { var tagLength = startTags[i].length; - if (start >= tagLength) - { + if (start >= tagLength) { index = value.lastIndexOf(startTags[i], start - tagLength); lastStart = Math.max(lastStart, index); } } if (lastStart == -1) return false; - if (start > 0) - { + if (start > 0) { for (i = 0; i < endTags.length; i++) { index = value.lastIndexOf(endTags[i], start - 1); lastEnd = Math.max(lastEnd, index);