Advertisement
Guest User

quote

a guest
Jul 18th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. /*
  2. * quote-selection.js
  3. *
  4. * This is a little buggy.
  5. * Allows you to quote a post by just selecting some text, then beginning to type.
  6. *
  7. * Released under the MIT license
  8. * Copyright (c) 2012 Michael Save <savetheinternet@tinyboard.org>
  9. *
  10. * Usage:
  11. * $config['additional_javascript'][] = 'js/jquery.min.js';
  12. * $config['additional_javascript'][] = 'js/quote-selection.js';
  13. *
  14. */
  15.  
  16. $(document).ready(function(){
  17. if (!window.getSelection)
  18. return;
  19.  
  20. $.fn.selectRange = function(start, end) {
  21. return this.each(function() {
  22. if (this.setSelectionRange) {
  23. this.focus();
  24. this.setSelectionRange(start, end);
  25. } else if (this.createTextRange) {
  26. var range = this.createTextRange();
  27. range.collapse(true);
  28. range.moveEnd('character', end);
  29. range.moveStart('character', start);
  30. range.select();
  31. }
  32. });
  33. };
  34.  
  35. var altKey = true
  36. var ctrlKey = true
  37. var metaKey = true
  38.  
  39. $(document).keyup(function(e) {
  40. if (e.keyCode == 18)
  41. altKey = false;
  42. else if (e.keyCode == 17)
  43. ctrlKey = false;
  44. else if (e.keyCode == 91)
  45. metaKey = false;
  46. });
  47.  
  48. $(document).keydown(function(e) {
  49. if (e.altKey)
  50. altKey = true;
  51. else if (e.ctrlKey)
  52. ctrlKey = true;
  53. else if (e.metaKey)
  54. metaKey = true;
  55.  
  56. if (altKey || ctrlKey || metaKey) {
  57. // console.log('CTRL/ALT/Something used. Ignoring');
  58. return;
  59. }
  60.  
  61. if (e.keyCode < 48 || e.keyCode > 90)
  62. return;
  63.  
  64. var selection = window.getSelection();
  65. var $post = $(selection.anchorNode).parents('.post');
  66. if ($post.length == 0) {
  67. // console.log('Start of selection was not post div', $(selection.anchorNode).parent());
  68. return;
  69. }
  70.  
  71. var postID = $post.find('.post_no:eq(1)').text();
  72.  
  73. if (postID != $(selection.focusNode).parents('.post').find('.post_no:eq(1)').text()) {
  74. // console.log('Selection left post div', $(selection.focusNode).parent());
  75. return;
  76. }
  77.  
  78. ;
  79. var selectedText = selection.toString();
  80. // console.log('Selected text: ' + selectedText.replace(/\n/g, '\\n').replace(/\r/g, '\\r'));
  81.  
  82. if ($('body').hasClass('debug'))
  83. alert(selectedText);
  84.  
  85. if (selectedText.length == 0)
  86. return;
  87.  
  88. var body = $('textarea#body')[0];
  89.  
  90. var last_quote = body.value.match(/[\S.]*(^|[\S\s]*)>>(\d+)/);
  91. if (last_quote)
  92. last_quote = last_quote[2];
  93.  
  94. /* to solve some bugs on weird browsers, we need to replace \r\n with \n and then undo that after */
  95. var quote = (last_quote != postID ? '>>' + postID + '\r\n' : '') + $.trim(selectedText).replace(/\r\n/g, '\n').replace(/^/mg, '>').replace(/\n/g, '\r\n') + '\r\n';
  96.  
  97. // console.log('Deselecting text');
  98. selection.removeAllRanges();
  99.  
  100. if (document.selection) {
  101. // IE
  102. body.focus();
  103. var sel = document.selection.createRange();
  104. sel.text = quote;
  105. body.focus();
  106. } else if (body.selectionStart || body.selectionStart == '0') {
  107. // Mozilla
  108. var start = body.selectionStart;
  109. var end = body.selectionEnd;
  110.  
  111. if (!body.value.substring(0, start).match(/(^|\n)$/)) {
  112. quote = '\r\n\r\n' + quote;
  113. }
  114.  
  115. body.value = body.value.substring(0, start) + quote + body.value.substring(end, body.value.length);
  116. $(body).selectRange(start + quote.length, start + quote.length);
  117. } else {
  118. // ???
  119. body.value += quote;
  120. body.focus();
  121. }
  122. });
  123. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement