mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-28 06:08:52 +00:00
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
This commit is contained in:
commit
778658f787
1 changed files with 99 additions and 0 deletions
|
@ -394,3 +394,102 @@ function getCaretPosition(txtarea) {
|
||||||
|
|
||||||
return caretPos;
|
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);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue