The version of vichan running on lainchan.org
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.2KB

  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. $(document).ready(function(){
  16. if (!window.getSelection)
  17. return;
  18. $.fn.selectRange = function(start, end) {
  19. return this.each(function() {
  20. if (this.setSelectionRange) {
  21. this.focus();
  22. this.setSelectionRange(start, end);
  23. } else if (this.createTextRange) {
  24. var range = this.createTextRange();
  25. range.collapse(true);
  26. range.moveEnd('character', end);
  27. range.moveStart('character', start);
  28. range.select();
  29. }
  30. });
  31. };
  32. var altKey = false;
  33. var ctrlKey = false;
  34. var metaKey = false;
  35. $(document).keyup(function(e) {
  36. if (e.keyCode == 18)
  37. altKey = false;
  38. else if (e.keyCode == 17)
  39. ctrlKey = false;
  40. else if (e.keyCode == 91)
  41. metaKey = false;
  42. });
  43. $(document).keydown(function(e) {
  44. if (e.altKey)
  45. altKey = true;
  46. else if (e.ctrlKey)
  47. ctrlKey = true;
  48. else if (e.metaKey)
  49. metaKey = true;
  50. if (altKey || ctrlKey || metaKey) {
  51. // console.log('CTRL/ALT/Something used. Ignoring');
  52. return;
  53. }
  54. if (e.keyCode < 48 || e.keyCode > 90)
  55. return;
  56. var selection = window.getSelection();
  57. var $post = $(selection.anchorNode).parents('.post');
  58. if ($post.length == 0) {
  59. // console.log('Start of selection was not post div', $(selection.anchorNode).parent());
  60. return;
  61. }
  62. var postID = $post.find('.post_no:eq(1)').text();
  63. if (postID != $(selection.focusNode).parents('.post').find('.post_no:eq(1)').text()) {
  64. // console.log('Selection left post div', $(selection.focusNode).parent());
  65. return;
  66. }
  67. ;
  68. var selectedText = selection.toString();
  69. // console.log('Selected text: ' + selectedText.replace(/\n/g, '\\n').replace(/\r/g, '\\r'));
  70. if ($('body').hasClass('debug'))
  71. alert(selectedText);
  72. if (selectedText.length == 0)
  73. return;
  74. var body = $('textarea#body')[0];
  75. var last_quote = body.value.match(/[\S.]*(^|[\S\s]*)>>(\d+)/);
  76. if (last_quote)
  77. last_quote = last_quote[2];
  78. /* to solve some bugs on weird browsers, we need to replace \r\n with \n and then undo that after */
  79. var quote = (last_quote != postID ? '>>' + postID + '\r\n' : '') + $.trim(selectedText).replace(/\r\n/g, '\n').replace(/^/mg, '>').replace(/\n/g, '\r\n') + '\r\n';
  80. // console.log('Deselecting text');
  81. selection.removeAllRanges();
  82. if (document.selection) {
  83. // IE
  84. body.focus();
  85. var sel = document.selection.createRange();
  86. sel.text = quote;
  87. body.focus();
  88. } else if (body.selectionStart || body.selectionStart == '0') {
  89. // Mozilla
  90. var start = body.selectionStart;
  91. var end = body.selectionEnd;
  92. if (!body.value.substring(0, start).match(/(^|\n)$/)) {
  93. quote = '\r\n\r\n' + quote;
  94. }
  95. body.value = body.value.substring(0, start) + quote + body.value.substring(end, body.value.length);
  96. $(body).selectRange(start + quote.length, start + quote.length);
  97. } else {
  98. // ???
  99. body.value += quote;
  100. body.focus();
  101. }
  102. });
  103. });