diff --git a/js/quote-selection.js b/js/quote-selection.js index 0722c0b1..ba67fa7c 100644 --- a/js/quote-selection.js +++ b/js/quote-selection.js @@ -10,20 +10,20 @@ * Usage: * $config['additional_javascript'][] = 'js/jquery.min.js'; * $config['additional_javascript'][] = 'js/quote-selection.js'; - * */ -$(document).ready(function(){ - if (!window.getSelection) +$(document).ready(function() { + if (!window.getSelection) { return; - + } + $.fn.selectRange = function(start, end) { return this.each(function() { if (this.setSelectionRange) { this.focus(); this.setSelectionRange(start, end); } else if (this.createTextRange) { - var range = this.createTextRange(); + let range = this.createTextRange(); range.collapse(true); range.moveEnd('character', end); range.moveStart('character', start); @@ -31,88 +31,81 @@ $(document).ready(function(){ } }); }; - - var altKey = false; - var ctrlKey = false; - var metaKey = false; - + + let altKey = false; + let ctrlKey = false; + let metaKey = false; + $(document).keyup(function(e) { - if (e.keyCode == 18) + if (e.keyCode == 18) { altKey = false; - else if (e.keyCode == 17) + } else if (e.keyCode == 17) { ctrlKey = false; - else if (e.keyCode == 91) + } else if (e.keyCode == 91) { metaKey = false; + } }); - + $(document).keydown(function(e) { - if (e.altKey) + if (e.altKey) { altKey = true; - else if (e.ctrlKey) + } else if (e.ctrlKey) { ctrlKey = true; - else if (e.metaKey) + } else if (e.metaKey) { metaKey = true; - + } + if (altKey || ctrlKey || metaKey) { - // console.log('CTRL/ALT/Something used. Ignoring'); return; } - - if (e.keyCode < 48 || e.keyCode > 90) - return; - - var selection = window.getSelection(); - var $post = $(selection.anchorNode).parents('.post'); - if ($post.length == 0) { - // console.log('Start of selection was not post div', $(selection.anchorNode).parent()); + + if (e.keyCode < 48 || e.keyCode > 90) { return; } - - var postID = $post.find('.post_no:eq(1)').text(); - + + let selection = window.getSelection(); + let post = $(selection.anchorNode).parents('.post'); + if (post.length == 0) { + return; + } + + let postID = post.find('.post_no:eq(1)').text(); + if (postID != $(selection.focusNode).parents('.post').find('.post_no:eq(1)').text()) { - // console.log('Selection left post div', $(selection.focusNode).parent()); return; } - - ; - var selectedText = selection.toString(); - // console.log('Selected text: ' + selectedText.replace(/\n/g, '\\n').replace(/\r/g, '\\r')); - - if ($('body').hasClass('debug')) + + let selectedText = selection.toString(); + + if ($('body').hasClass('debug')) { alert(selectedText); - - if (selectedText.length == 0) + } + + if (selectedText.length == 0) { return; - - var body = $('textarea#body')[0]; - - var last_quote = body.value.match(/[\S.]*(^|[\S\s]*)>>(\d+)/); - if (last_quote) + } + + let body = $('textarea#body')[0]; + + let last_quote = body.value.match(/[\S.]*(^|[\S\s]*)>>(\d+)/); + if (last_quote) { last_quote = last_quote[2]; - + } + /* to solve some bugs on weird browsers, we need to replace \r\n with \n and then undo that after */ - var quote = (last_quote != postID ? '>>' + postID + '\r\n' : '') + $.trim(selectedText).replace(/\r\n/g, '\n').replace(/^/mg, '>').replace(/\n/g, '\r\n') + '\r\n'; - - // console.log('Deselecting text'); + let quote = (last_quote != postID ? '>>' + postID + '\r\n' : '') + $.trim(selectedText).replace(/\r\n/g, '\n').replace(/^/mg, '>').replace(/\n/g, '\r\n') + '\r\n'; + selection.removeAllRanges(); - - if (document.selection) { - // IE - body.focus(); - var sel = document.selection.createRange(); - sel.text = quote; - body.focus(); - } else if (body.selectionStart || body.selectionStart == '0') { - // Mozilla - var start = body.selectionStart; - var end = body.selectionEnd; - + + if (body.selectionStart || body.selectionStart == '0') { + let start = body.selectionStart; + let end = body.selectionEnd; + if (!body.value.substring(0, start).match(/(^|\n)$/)) { quote = '\r\n\r\n' + quote; } - - body.value = body.value.substring(0, start) + quote + body.value.substring(end, body.value.length); + + body.value = body.value.substring(0, start) + quote + body.value.substring(end, body.value.length); $(body).selectRange(start + quote.length, start + quote.length); } else { // ??? @@ -121,4 +114,3 @@ $(document).ready(function(){ } }); }); - diff --git a/templates/main.js b/templates/main.js index 13c80c33..96d2fb3f 100644 --- a/templates/main.js +++ b/templates/main.js @@ -71,7 +71,7 @@ var datelocale = function alert(a, do_confirm, confirm_ok_action, confirm_cancel_action) { - var handler, div, bg, closebtn, okbtn; + let handler, div, bg, closebtn, okbtn; let close = function() { handler.fadeOut(400, function() { handler.remove(); }); return false; @@ -133,11 +133,11 @@ function changeStyle(styleName, link) { {% verbatim %} if (!document.getElementById('stylesheet')) { - var s = document.createElement('link'); + let s = document.createElement('link'); s.rel = 'stylesheet'; s.type = 'text/css'; s.id = 'stylesheet'; - var x = document.getElementsByTagName('head')[0]; + let x = document.getElementsByTagName('head')[0]; x.appendChild(s); } @@ -145,8 +145,8 @@ function changeStyle(styleName, link) { selectedstyle = styleName; if (document.getElementsByClassName('styles').length != 0) { - var styleLinks = document.getElementsByClassName('styles')[0].childNodes; - for (var i = 0; i < styleLinks.length; i++) { + let styleLinks = document.getElementsByClassName('styles')[0].childNodes; + for (let i = 0; i < styleLinks.length; i++) { styleLinks[i].className = ''; } } @@ -171,7 +171,7 @@ function changeStyle(styleName, link) { var stylesheet_choices = JSON.parse(localStorage.board_stylesheets); if (board_name && stylesheet_choices[board_name]) { - for (var styleName in styles) { + for (let styleName in styles) { if (styleName == stylesheet_choices[board_name]) { changeStyle(styleName); break; @@ -182,7 +182,7 @@ function changeStyle(styleName, link) { {% else %} {% verbatim %} if (localStorage.stylesheet) { - for (var styleName in styles) { + for (let styleName in styles) { if (styleName == localStorage.stylesheet) { changeStyle(styleName); break; @@ -194,11 +194,11 @@ function changeStyle(styleName, link) { {% verbatim %} function initStyleChooser() { - var newElement = document.createElement('div'); + let newElement = document.createElement('div'); newElement.className = 'styles'; for (styleName in styles) { - var style = document.createElement('a'); + let style = document.createElement('a'); style.innerHTML = '[' + styleName + ']'; style.onclick = function() { changeStyle(this.innerHTML.substring(1, this.innerHTML.length - 1), this); @@ -259,8 +259,7 @@ function highlightReply(id) { } let divs = document.getElementsByTagName('div'); - for (var i = 0; i < divs.length; i++) - { + for (let i = 0; i < divs.length; i++) { if (divs[i].className.indexOf('post') != -1) { divs[i].className = divs[i].className.replace(/highlighted/, ''); } @@ -308,12 +307,7 @@ function citeReply(id, with_link) { return false; } - if (document.selection) { - // IE - textarea.focus(); - let sel = document.selection.createRange(); - sel.text = '>>' + id + '\n'; - } else if (textarea.selectionStart || textarea.selectionStart == '0') { + if (textarea.selectionStart || textarea.selectionStart == '0') { let start = textarea.selectionStart; let end = textarea.selectionEnd; textarea.value = textarea.value.substring(0, start) + '>>' + id + '\n' + textarea.value.substring(end, textarea.value.length); @@ -435,7 +429,7 @@ onReady(init); {% if config.google_analytics %}{% verbatim %} -var _gaq = _gaq || [];_gaq.push(['_setAccount', '{% endverbatim %}{{ config.google_analytics }}{% verbatim %}']);{% endverbatim %}{% if config.google_analytics_domain %}{% verbatim %}_gaq.push(['_setDomainName', '{% endverbatim %}{{ config.google_analytics_domain }}{% verbatim %}']){% endverbatim %}{% endif %}{% if not config.google_analytics_domain %}{% verbatim %}_gaq.push(['_setDomainName', 'none']){% endverbatim %}{% endif %}{% verbatim %};_gaq.push(['_trackPageview']);(function() {var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);})();{% endverbatim %}{% endif %} +var _gaq = _gaq || [];_gaq.push(['_setAccount', '{% endverbatim %}{{ config.google_analytics }}{% verbatim %}']);{% endverbatim %}{% if config.google_analytics_domain %}{% verbatim %}_gaq.push(['_setDomainName', '{% endverbatim %}{{ config.google_analytics_domain }}{% verbatim %}']){% endverbatim %}{% endif %}{% if not config.google_analytics_domain %}{% verbatim %}_gaq.push(['_setDomainName', 'none']){% endverbatim %}{% endif %}{% verbatim %};_gaq.push(['_trackPageview']);(function() {var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';let s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);})();{% endverbatim %}{% endif %} {% if config.statcounter_project and config.statcounter_security %} var sc = document.createElement('script');