diff options
Diffstat (limited to 'template/admin/rsrc/main.js')
-rw-r--r-- | template/admin/rsrc/main.js | 135 |
1 files changed, 71 insertions, 64 deletions
diff --git a/template/admin/rsrc/main.js b/template/admin/rsrc/main.js index a93b8be..f6f7884 100644 --- a/template/admin/rsrc/main.js +++ b/template/admin/rsrc/main.js @@ -7,10 +7,10 @@ var next = document.getElementById("next-site"); //============================================================================== // Handle arrow keys and change the location to the desired direction //============================================================================== -document.addEventListener("keyup", function(event) { - if(!event.ctrlKey && !event.shiftKey) { - (event.keyCode === 37 && prev) && (window.location.href = prev.getAttribute("href")); - (event.keyCode === 39 && next) && (window.location.href = next.getAttribute("href")); +document.addEventListener("keyup", function(e) { + if(!e.ctrlKey && !e.shiftKey) { + (e.keyCode === 37 && prev) && (window.location.href = prev.getAttribute("href")); + (e.keyCode === 39 && next) && (window.location.href = next.getAttribute("href")); } }, false); @@ -30,124 +30,131 @@ var markdownTags = { }; //============================================================================== +// Timeout function for delayed execution of code +//============================================================================== +function delayed(callback) { + window.setTimeout(callback, 20); +} + +//============================================================================== // Set caret position in editor //============================================================================== function setCaretPosition(position) { - window.setTimeout(function() { - document.getElementById("content-editor").focus(); - document.getElementById("content-editor").setSelectionRange(position, position); - }, 50); - + document.getElementById("content-editor").setSelectionRange(position, position); + document.getElementById("content-editor").focus(); } //============================================================================== -// Insert markdown around text in editor +// Insert emoticon after cursor in editor //============================================================================== -function markdownReplace(tagname) { - var element = document.activeElement; +function insertEmoticon(target, emoticon) { + var selectionStart = target.selectionStart; + var selectionEnd = target.selectionEnd; - if(element.nodeName === 'TEXTAREA') { - var selectionStart = element.selectionStart; - var selectionEnd = element.selectionEnd; + var content = target.value; + target.value = content.slice(0, selectionStart) + emoticon + content.slice(selectionEnd); - var selectedText = element.value.substring(selectionStart, selectionEnd); - - var content = element.value; - element.value = content.slice(0, selectionStart) + markdownTags[tagname][0] + selectedText + markdownTags[tagname][1] + content.slice(selectionEnd); - - setCaretPosition(selectionStart + markdownTags[tagname][0].length + selectedText.length + markdownTags[tagname][1].length); - } + delayed(function() { + setCaretPosition(selectionStart + emoticon.length); + }); } //============================================================================== -// Insert emoticon after cursor in editor +// Insert markdown around text in editor //============================================================================== -function emoticonReplace(emoticon) { - var element = document.activeElement; +function insertMarkdown(target, markdown) { + var selectionStart = target.selectionStart; + var selectionEnd = target.selectionEnd; - if(element.nodeName === 'TEXTAREA') { - var selectionStart = element.selectionStart; - var selectionEnd = element.selectionEnd; + var selectedText = target.value.substring(selectionStart, selectionEnd); - var content = element.value; - element.value = content.slice(0, selectionStart) + emoticon + content.slice(selectionEnd); + var content = target.value; + target.value = content.slice(0, selectionStart) + markdownTags[markdown][0] + selectedText + markdownTags[markdown][1] + content.slice(selectionEnd); - setCaretPosition(selectionStart + emoticon.length); - } + delayed(function() { + setCaretPosition(selectionStart + markdownTags[markdown][0].length + selectedText.length + markdownTags[markdown][1].length); + }); } //============================================================================== // Keep server-side session active if the user is writing a long text //============================================================================== -addEventListener("DOMContentLoaded", function() { - setInterval(function() { - var Request = new XMLHttpRequest(); - Request.open("HEAD", "", true); - Request.send(); - }, 300000); -}, false); +setInterval(function() { + var Request = new XMLHttpRequest(); + Request.open("HEAD", "", true); + Request.send(); +}, 300000); //============================================================================== -// Insert tab indent into editor if <tab> is pressed +// Confirmation message for delete button //============================================================================== -addEventListener("DOMContentLoaded", function() { +if(document.getElementById("delete-button")) { + document.getElementById("delete-button").onclick = function(e) { + return confirm(e.target.getAttribute('data-text')); + }; +} + +//============================================================================== +// Insert or remove tab indent in editor if [<shift>+]<tab> is pressed +//============================================================================== +(function() { if(document.getElementById("content-editor")) { var element = document.getElementById("content-editor"); element.addEventListener('keydown', function(e) { - if(e.keyCode === 9 && !e.ctrlKey && !e.shiftKey) { + if(e.keyCode === 9 && !e.ctrlKey) { var selectionStart = element.selectionStart; var selectionEnd = element.selectionEnd; var content = element.value; - element.value = content.substring(0, selectionStart) + "\t" + content.substring(selectionEnd); + if(e.shiftKey) { + if(content.substring(selectionStart, selectionStart -1) === "\t") { + element.value = content.substring(0, selectionStart - 1) + content.substring(selectionEnd); + setCaretPosition(selectionStart - 1); + } + } + + else { + element.value = content.substring(0, selectionStart) + "\t" + content.substring(selectionEnd); + setCaretPosition(selectionStart + 1); + } - setCaretPosition(selectionStart + 1); e.preventDefault(); } }, false); } -}, false); +})(); //============================================================================== -// Confirmation message for delete buttons +// Emoticon button list //============================================================================== -addEventListener("DOMContentLoaded", function() { - if(document.getElementById("delete-button")) { - document.getElementById("delete-button").onclick = function(e) { - return confirm(e.target.getAttribute('data-text')); - }; - } -}, false); - -//============================================================================== -// Emoticon listener -//============================================================================== -addEventListener("DOMContentLoaded", function() { +(function() { if(document.getElementById("emoticon-list")) { var list = document.getElementById("emoticon-list"); + var node = document.getElementById("content-editor"); var items = list.getElementsByTagName("li"); for(var i = 0; i < items.length; ++i) { items[i].onmousedown = function(e) { - emoticonReplace(e.target.getAttribute('data-emoticon')); + insertEmoticon(node, e.target.getAttribute('data-emoticon')); }; } } -}, false); +})(); //============================================================================== -// Markdown listener +// Markdown button list //============================================================================== -addEventListener("DOMContentLoaded", function() { +(function() { if(document.getElementById("markdown-list")) { var list = document.getElementById("markdown-list"); + var node = document.getElementById("content-editor"); var items = list.getElementsByTagName("li"); for(var i = 0; i < items.length; ++i) { items[i].onmousedown = function(e) { - markdownReplace(e.target.getAttribute('data-markdown')); + insertMarkdown(node, e.target.getAttribute('data-markdown')); }; } } -}, false);
\ No newline at end of file +})();
\ No newline at end of file |