From 3dae0cfcaefc69d5524e03d1b8b282deae9f181d Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Mon, 20 May 2013 19:59:40 +0300 Subject: [feature/editor-code-tabs] Correctly count indentation on first line Correctly count indentation on first line of code tag PHPBB3-11557 --- phpBB/styles/prosilver/template/editor.js | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'phpBB/styles/prosilver/template') diff --git a/phpBB/styles/prosilver/template/editor.js b/phpBB/styles/prosilver/template/editor.js index fd4c68adfe..311c1957ba 100644 --- a/phpBB/styles/prosilver/template/editor.js +++ b/phpBB/styles/prosilver/template/editor.js @@ -401,7 +401,7 @@ function getCaretPosition(txtarea) { */ (function($) { $(document).ready(function() { - var doc, textarea, startTags, endTags; + var doc, textarea, startTags, startTagsEnd, endTags; // find textarea, make sure browser supports necessary functions if (document.forms[form_name]) { @@ -421,6 +421,7 @@ function getCaretPosition(txtarea) { // list of allowed start and end bbcode code tags, in lower case startTags = ['[code]', '[code=']; + startTagsEnd = ']'; endTags = ['[/code]']; function inTag() { @@ -450,11 +451,27 @@ function getCaretPosition(txtarea) { return (lastEnd < lastStart); } - function getLastLine() { + function getLastLine(stripCodeTags) { var start = textarea.selectionStart, value = textarea.value, index = value.lastIndexOf("\n", start - 1); - return value.substring(index + 1, start); + value = value.substring(index + 1, start); + if (stripCodeTags) { + for (var i = 0; i < startTags.length; i++) { + index = value.lastIndexOf(startTags[i]); + if (index >= 0) { + var tagLength = startTags[i].length; + value = value.substring(index + tagLength); + if (startTags[i].lastIndexOf(startTagsEnd) != tagLength) { + index = value.indexOf(startTagsEnd); + if (index >= 0) { + value = value.substr(index + 1); + } + } + } + } + } + return value; } function appendCode(code) { @@ -480,7 +497,7 @@ function getCaretPosition(txtarea) { // intercept new line characters if (key == 13) { if (inTag()) { - var lastLine = getLastLine(), + var lastLine = getLastLine(true), code = '' + /^\s*/g.exec(lastLine); if (code.length > 0) { appendCode("\n" + code); -- cgit v1.2.1 From b2e5cc89344d641394e5c15575379de3c86c0806 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Mon, 20 May 2013 20:29:37 +0300 Subject: [feature/editor-code-tabs] Apply code editor to everything Move code editor from prosilver to assets Apply code editor to subsilver2 Apply code editor to ACP elements where bbcode is allowed PHPBB3-11557 --- phpBB/styles/prosilver/template/editor.js | 91 +------------------------------ 1 file changed, 2 insertions(+), 89 deletions(-) (limited to 'phpBB/styles/prosilver/template') diff --git a/phpBB/styles/prosilver/template/editor.js b/phpBB/styles/prosilver/template/editor.js index 311c1957ba..6eb8c1e0c4 100644 --- a/phpBB/styles/prosilver/template/editor.js +++ b/phpBB/styles/prosilver/template/editor.js @@ -401,7 +401,7 @@ function getCaretPosition(txtarea) { */ (function($) { $(document).ready(function() { - var doc, textarea, startTags, startTagsEnd, endTags; + var doc, textarea; // find textarea, make sure browser supports necessary functions if (document.forms[form_name]) { @@ -419,94 +419,7 @@ function getCaretPosition(txtarea) { return; } - // list of allowed start and end bbcode code tags, in lower case - startTags = ['[code]', '[code=']; - startTagsEnd = ']'; - 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(stripCodeTags) { - var start = textarea.selectionStart, - value = textarea.value, - index = value.lastIndexOf("\n", start - 1); - value = value.substring(index + 1, start); - if (stripCodeTags) { - for (var i = 0; i < startTags.length; i++) { - index = value.lastIndexOf(startTags[i]); - if (index >= 0) { - var tagLength = startTags[i].length; - value = value.substring(index + tagLength); - if (startTags[i].lastIndexOf(startTagsEnd) != tagLength) { - index = value.indexOf(startTagsEnd); - if (index >= 0) { - value = value.substr(index + 1); - } - } - } - } - } - return value; - } - - 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(true), - code = '' + /^\s*/g.exec(lastLine); - if (code.length > 0) { - appendCode("\n" + code); - event.preventDefault(); - return; - } - } - } - }); + phpbb.applyCodeEditor(textarea); }); })(jQuery); -- cgit v1.2.1 From 809c51f30d28e5dc1449f817b7563d12e4050f7e Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Mon, 20 May 2013 20:33:51 +0300 Subject: [feature/editor-code-tabs] Check for browser support in function Check for browser support and valid textarea in function instead of before applying function. PHPBB3-11557 --- phpBB/styles/prosilver/template/editor.js | 3 --- 1 file changed, 3 deletions(-) (limited to 'phpBB/styles/prosilver/template') diff --git a/phpBB/styles/prosilver/template/editor.js b/phpBB/styles/prosilver/template/editor.js index 6eb8c1e0c4..235cc0025b 100644 --- a/phpBB/styles/prosilver/template/editor.js +++ b/phpBB/styles/prosilver/template/editor.js @@ -415,9 +415,6 @@ function getCaretPosition(txtarea) { } textarea = doc.forms[form_name].elements[text_name]; - if (!textarea || typeof textarea.selectionStart !== 'number') { - return; - } phpbb.applyCodeEditor(textarea); }); -- cgit v1.2.1