Merge pull request #811 from Zankaria/main-js-let

Minor JS update
This commit is contained in:
Lorenzo Yario 2024-09-20 18:42:01 -07:00 committed by GitHub
commit 380ae8c675
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 67 additions and 81 deletions

View File

@ -10,20 +10,20 @@
* Usage: * Usage:
* $config['additional_javascript'][] = 'js/jquery.min.js'; * $config['additional_javascript'][] = 'js/jquery.min.js';
* $config['additional_javascript'][] = 'js/quote-selection.js'; * $config['additional_javascript'][] = 'js/quote-selection.js';
*
*/ */
$(document).ready(function(){ $(document).ready(function() {
if (!window.getSelection) if (!window.getSelection) {
return; return;
}
$.fn.selectRange = function(start, end) { $.fn.selectRange = function(start, end) {
return this.each(function() { return this.each(function() {
if (this.setSelectionRange) { if (this.setSelectionRange) {
this.focus(); this.focus();
this.setSelectionRange(start, end); this.setSelectionRange(start, end);
} else if (this.createTextRange) { } else if (this.createTextRange) {
var range = this.createTextRange(); let range = this.createTextRange();
range.collapse(true); range.collapse(true);
range.moveEnd('character', end); range.moveEnd('character', end);
range.moveStart('character', start); range.moveStart('character', start);
@ -31,88 +31,81 @@ $(document).ready(function(){
} }
}); });
}; };
var altKey = false; let altKey = false;
var ctrlKey = false; let ctrlKey = false;
var metaKey = false; let metaKey = false;
$(document).keyup(function(e) { $(document).keyup(function(e) {
if (e.keyCode == 18) if (e.keyCode == 18) {
altKey = false; altKey = false;
else if (e.keyCode == 17) } else if (e.keyCode == 17) {
ctrlKey = false; ctrlKey = false;
else if (e.keyCode == 91) } else if (e.keyCode == 91) {
metaKey = false; metaKey = false;
}
}); });
$(document).keydown(function(e) { $(document).keydown(function(e) {
if (e.altKey) if (e.altKey) {
altKey = true; altKey = true;
else if (e.ctrlKey) } else if (e.ctrlKey) {
ctrlKey = true; ctrlKey = true;
else if (e.metaKey) } else if (e.metaKey) {
metaKey = true; metaKey = true;
}
if (altKey || ctrlKey || metaKey) { if (altKey || ctrlKey || metaKey) {
// console.log('CTRL/ALT/Something used. Ignoring');
return; return;
} }
if (e.keyCode < 48 || e.keyCode > 90) 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());
return; 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()) { if (postID != $(selection.focusNode).parents('.post').find('.post_no:eq(1)').text()) {
// console.log('Selection left post div', $(selection.focusNode).parent());
return; return;
} }
; let selectedText = selection.toString();
var selectedText = selection.toString();
// console.log('Selected text: ' + selectedText.replace(/\n/g, '\\n').replace(/\r/g, '\\r')); if ($('body').hasClass('debug')) {
if ($('body').hasClass('debug'))
alert(selectedText); alert(selectedText);
}
if (selectedText.length == 0)
if (selectedText.length == 0) {
return; return;
}
var body = $('textarea#body')[0];
let body = $('textarea#body')[0];
var last_quote = body.value.match(/[\S.]*(^|[\S\s]*)>>(\d+)/);
if (last_quote) let last_quote = body.value.match(/[\S.]*(^|[\S\s]*)>>(\d+)/);
if (last_quote) {
last_quote = last_quote[2]; 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 */ /* 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'; let 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');
selection.removeAllRanges(); selection.removeAllRanges();
if (document.selection) { if (body.selectionStart || body.selectionStart == '0') {
// IE let start = body.selectionStart;
body.focus(); let end = body.selectionEnd;
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.value.substring(0, start).match(/(^|\n)$/)) { if (!body.value.substring(0, start).match(/(^|\n)$/)) {
quote = '\r\n\r\n' + quote; 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); $(body).selectRange(start + quote.length, start + quote.length);
} else { } else {
// ??? // ???
@ -121,4 +114,3 @@ $(document).ready(function(){
} }
}); });
}); });

View File

@ -71,7 +71,7 @@ var datelocale =
function alert(a, do_confirm, confirm_ok_action, confirm_cancel_action) { 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() { let close = function() {
handler.fadeOut(400, function() { handler.remove(); }); handler.fadeOut(400, function() { handler.remove(); });
return false; return false;
@ -133,11 +133,11 @@ function changeStyle(styleName, link) {
{% verbatim %} {% verbatim %}
if (!document.getElementById('stylesheet')) { if (!document.getElementById('stylesheet')) {
var s = document.createElement('link'); let s = document.createElement('link');
s.rel = 'stylesheet'; s.rel = 'stylesheet';
s.type = 'text/css'; s.type = 'text/css';
s.id = 'stylesheet'; s.id = 'stylesheet';
var x = document.getElementsByTagName('head')[0]; let x = document.getElementsByTagName('head')[0];
x.appendChild(s); x.appendChild(s);
} }
@ -145,8 +145,8 @@ function changeStyle(styleName, link) {
selectedstyle = styleName; selectedstyle = styleName;
if (document.getElementsByClassName('styles').length != 0) { if (document.getElementsByClassName('styles').length != 0) {
var styleLinks = document.getElementsByClassName('styles')[0].childNodes; let styleLinks = document.getElementsByClassName('styles')[0].childNodes;
for (var i = 0; i < styleLinks.length; i++) { for (let i = 0; i < styleLinks.length; i++) {
styleLinks[i].className = ''; styleLinks[i].className = '';
} }
} }
@ -171,7 +171,7 @@ function changeStyle(styleName, link) {
var stylesheet_choices = JSON.parse(localStorage.board_stylesheets); var stylesheet_choices = JSON.parse(localStorage.board_stylesheets);
if (board_name && stylesheet_choices[board_name]) { if (board_name && stylesheet_choices[board_name]) {
for (var styleName in styles) { for (let styleName in styles) {
if (styleName == stylesheet_choices[board_name]) { if (styleName == stylesheet_choices[board_name]) {
changeStyle(styleName); changeStyle(styleName);
break; break;
@ -182,7 +182,7 @@ function changeStyle(styleName, link) {
{% else %} {% else %}
{% verbatim %} {% verbatim %}
if (localStorage.stylesheet) { if (localStorage.stylesheet) {
for (var styleName in styles) { for (let styleName in styles) {
if (styleName == localStorage.stylesheet) { if (styleName == localStorage.stylesheet) {
changeStyle(styleName); changeStyle(styleName);
break; break;
@ -194,11 +194,11 @@ function changeStyle(styleName, link) {
{% verbatim %} {% verbatim %}
function initStyleChooser() { function initStyleChooser() {
var newElement = document.createElement('div'); let newElement = document.createElement('div');
newElement.className = 'styles'; newElement.className = 'styles';
for (styleName in styles) { for (styleName in styles) {
var style = document.createElement('a'); let style = document.createElement('a');
style.innerHTML = '[' + styleName + ']'; style.innerHTML = '[' + styleName + ']';
style.onclick = function() { style.onclick = function() {
changeStyle(this.innerHTML.substring(1, this.innerHTML.length - 1), this); changeStyle(this.innerHTML.substring(1, this.innerHTML.length - 1), this);
@ -259,8 +259,7 @@ function highlightReply(id) {
} }
let divs = document.getElementsByTagName('div'); 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) { if (divs[i].className.indexOf('post') != -1) {
divs[i].className = divs[i].className.replace(/highlighted/, ''); divs[i].className = divs[i].className.replace(/highlighted/, '');
} }
@ -308,12 +307,7 @@ function citeReply(id, with_link) {
return false; return false;
} }
if (document.selection) { if (textarea.selectionStart || textarea.selectionStart == '0') {
// IE
textarea.focus();
let sel = document.selection.createRange();
sel.text = '>>' + id + '\n';
} else if (textarea.selectionStart || textarea.selectionStart == '0') {
let start = textarea.selectionStart; let start = textarea.selectionStart;
let end = textarea.selectionEnd; let end = textarea.selectionEnd;
textarea.value = textarea.value.substring(0, start) + '>>' + id + '\n' + textarea.value.substring(end, textarea.value.length); 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 %} {% 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 %} {% if config.statcounter_project and config.statcounter_security %}
var sc = document.createElement('script'); var sc = document.createElement('script');