2012-08-27 15:01:08 +02:00
|
|
|
/*
|
|
|
|
* quote-selection.js
|
|
|
|
*
|
2013-07-20 20:05:39 +02:00
|
|
|
* This is a little buggy.
|
|
|
|
* Allows you to quote a post by just selecting some text, then beginning to type.
|
|
|
|
*
|
2012-08-27 15:01:08 +02:00
|
|
|
* Released under the MIT license
|
|
|
|
* Copyright (c) 2012 Michael Save <savetheinternet@tinyboard.org>
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
* $config['additional_javascript'][] = 'js/jquery.min.js';
|
|
|
|
* $config['additional_javascript'][] = 'js/quote-selection.js';
|
|
|
|
*/
|
|
|
|
|
2024-09-19 22:56:23 +02:00
|
|
|
$(document).ready(function() {
|
|
|
|
if (!window.getSelection) {
|
2012-08-27 15:01:08 +02:00
|
|
|
return;
|
2024-09-19 22:56:23 +02:00
|
|
|
}
|
2024-09-19 22:51:31 +02:00
|
|
|
|
2012-08-27 15:01:08 +02:00
|
|
|
$.fn.selectRange = function(start, end) {
|
|
|
|
return this.each(function() {
|
|
|
|
if (this.setSelectionRange) {
|
|
|
|
this.focus();
|
|
|
|
this.setSelectionRange(start, end);
|
|
|
|
} else if (this.createTextRange) {
|
2024-09-19 22:56:23 +02:00
|
|
|
let range = this.createTextRange();
|
2012-08-27 15:01:08 +02:00
|
|
|
range.collapse(true);
|
|
|
|
range.moveEnd('character', end);
|
|
|
|
range.moveStart('character', start);
|
|
|
|
range.select();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2024-09-19 22:51:31 +02:00
|
|
|
|
2024-09-19 22:56:23 +02:00
|
|
|
let altKey = false;
|
|
|
|
let ctrlKey = false;
|
|
|
|
let metaKey = false;
|
2024-09-19 22:51:31 +02:00
|
|
|
|
2012-08-27 15:01:08 +02:00
|
|
|
$(document).keyup(function(e) {
|
2024-09-19 22:56:23 +02:00
|
|
|
if (e.keyCode == 18) {
|
2012-08-27 15:01:08 +02:00
|
|
|
altKey = false;
|
2024-09-19 22:56:23 +02:00
|
|
|
} else if (e.keyCode == 17) {
|
2012-08-27 15:01:08 +02:00
|
|
|
ctrlKey = false;
|
2024-09-19 22:56:23 +02:00
|
|
|
} else if (e.keyCode == 91) {
|
2013-07-20 20:05:39 +02:00
|
|
|
metaKey = false;
|
2024-09-19 22:56:23 +02:00
|
|
|
}
|
2012-08-27 15:01:08 +02:00
|
|
|
});
|
2024-09-19 22:51:31 +02:00
|
|
|
|
2012-08-27 15:01:08 +02:00
|
|
|
$(document).keydown(function(e) {
|
2024-09-19 22:56:23 +02:00
|
|
|
if (e.altKey) {
|
2012-08-27 15:01:08 +02:00
|
|
|
altKey = true;
|
2024-09-19 22:56:23 +02:00
|
|
|
} else if (e.ctrlKey) {
|
2012-08-27 15:01:08 +02:00
|
|
|
ctrlKey = true;
|
2024-09-19 22:56:23 +02:00
|
|
|
} else if (e.metaKey) {
|
2013-07-20 20:05:39 +02:00
|
|
|
metaKey = true;
|
2024-09-19 22:56:23 +02:00
|
|
|
}
|
2024-09-19 22:51:31 +02:00
|
|
|
|
2013-07-20 20:05:39 +02:00
|
|
|
if (altKey || ctrlKey || metaKey) {
|
2012-08-27 15:01:08 +02:00
|
|
|
return;
|
|
|
|
}
|
2024-09-19 22:51:31 +02:00
|
|
|
|
2024-09-19 22:56:23 +02:00
|
|
|
if (e.keyCode < 48 || e.keyCode > 90) {
|
2012-08-27 15:01:08 +02:00
|
|
|
return;
|
2024-09-19 22:56:23 +02:00
|
|
|
}
|
2024-09-19 22:51:31 +02:00
|
|
|
|
2024-09-19 22:56:23 +02:00
|
|
|
let selection = window.getSelection();
|
|
|
|
let post = $(selection.anchorNode).parents('.post');
|
|
|
|
if (post.length == 0) {
|
2012-08-27 15:01:08 +02:00
|
|
|
return;
|
|
|
|
}
|
2024-09-19 22:51:31 +02:00
|
|
|
|
2024-09-19 22:56:23 +02:00
|
|
|
let postID = post.find('.post_no:eq(1)').text();
|
2024-09-19 22:51:31 +02:00
|
|
|
|
2012-08-27 15:01:08 +02:00
|
|
|
if (postID != $(selection.focusNode).parents('.post').find('.post_no:eq(1)').text()) {
|
|
|
|
return;
|
|
|
|
}
|
2024-09-19 22:51:31 +02:00
|
|
|
|
2024-09-19 22:56:23 +02:00
|
|
|
let selectedText = selection.toString();
|
2024-09-19 22:51:31 +02:00
|
|
|
|
2024-09-19 22:56:23 +02:00
|
|
|
if ($('body').hasClass('debug')) {
|
2012-08-27 15:01:08 +02:00
|
|
|
alert(selectedText);
|
2024-09-19 22:56:23 +02:00
|
|
|
}
|
2024-09-19 22:51:31 +02:00
|
|
|
|
2024-09-19 22:56:23 +02:00
|
|
|
if (selectedText.length == 0) {
|
2012-08-27 15:01:08 +02:00
|
|
|
return;
|
2024-09-19 22:56:23 +02:00
|
|
|
}
|
2024-09-19 22:51:31 +02:00
|
|
|
|
2024-09-19 22:56:23 +02:00
|
|
|
let body = $('textarea#body')[0];
|
2024-09-19 22:51:31 +02:00
|
|
|
|
2024-09-19 22:56:23 +02:00
|
|
|
let last_quote = body.value.match(/[\S.]*(^|[\S\s]*)>>(\d+)/);
|
|
|
|
if (last_quote) {
|
2012-08-27 15:01:08 +02:00
|
|
|
last_quote = last_quote[2];
|
2024-09-19 22:56:23 +02:00
|
|
|
}
|
2024-09-19 22:51:31 +02:00
|
|
|
|
2012-08-27 15:01:08 +02:00
|
|
|
/* to solve some bugs on weird browsers, we need to replace \r\n with \n and then undo that after */
|
2024-09-19 22:56:23 +02:00
|
|
|
let quote = (last_quote != postID ? '>>' + postID + '\r\n' : '') + $.trim(selectedText).replace(/\r\n/g, '\n').replace(/^/mg, '>').replace(/\n/g, '\r\n') + '\r\n';
|
2024-09-19 22:51:31 +02:00
|
|
|
|
2012-08-27 15:01:08 +02:00
|
|
|
selection.removeAllRanges();
|
2024-09-19 22:51:31 +02:00
|
|
|
|
2024-09-19 22:54:04 +02:00
|
|
|
if (body.selectionStart || body.selectionStart == '0') {
|
2024-09-19 22:56:23 +02:00
|
|
|
let start = body.selectionStart;
|
|
|
|
let end = body.selectionEnd;
|
2024-09-19 22:51:31 +02:00
|
|
|
|
2012-08-27 15:01:08 +02:00
|
|
|
if (!body.value.substring(0, start).match(/(^|\n)$/)) {
|
|
|
|
quote = '\r\n\r\n' + quote;
|
|
|
|
}
|
2024-09-19 22:51:31 +02:00
|
|
|
|
|
|
|
body.value = body.value.substring(0, start) + quote + body.value.substring(end, body.value.length);
|
2012-08-27 15:01:08 +02:00
|
|
|
$(body).selectRange(start + quote.length, start + quote.length);
|
|
|
|
} else {
|
|
|
|
// ???
|
|
|
|
body.value += quote;
|
|
|
|
body.focus();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|