##// END OF EJS Templates
Reimplemented caret position manipulation. Move caret inside the bbcode tags if inserted
neko259 -
r1783:43080135 default
parent child Browse files
Show More
@@ -1,60 +1,60 b''
1 1 /*
2 2 @licstart The following is the entire license notice for the
3 3 JavaScript code in this page.
4 4
5 5
6 6 Copyright (C) 2013 neko259
7 7
8 8 The JavaScript code in this page is free software: you can
9 9 redistribute it and/or modify it under the terms of the GNU
10 10 General Public License (GNU GPL) as published by the Free Software
11 11 Foundation, either version 3 of the License, or (at your option)
12 12 any later version. The code is distributed WITHOUT ANY WARRANTY;
13 13 without even the implied warranty of MERCHANTABILITY or FITNESS
14 14 FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
15 15
16 16 As additional permission under GNU GPL version 3 section 7, you
17 17 may distribute non-source (e.g., minimized or compacted) forms of
18 18 that code without the copy of the GNU GPL normally required by
19 19 section 4, provided you include this license notice and a URL
20 20 through which recipients can access the Corresponding Source.
21 21
22 22 @licend The above is the entire license notice
23 23 for the JavaScript code in this page.
24 24 */
25 25
26 26 /**
27 27 * Add the desired characters to the start and end of selection.
28 28 *
29 29 * @param start Start (left) text
30 30 * @param end End (right) text
31 31 */
32 32 function addMarkToMsg(start, end) {
33 33 var textareas = getPostTextarea();
34 34
35 35 for (var i = 0; i < textareas.length; i++) {
36 36 var textarea = textareas[i];
37 37
38 if (document.selection) {
39 textarea.focus();
40
41 var sel = document.selection.createRange();
42 sel.text = start + sel.text + end;
43 } else if (textarea.selectionStart || textarea.selectionStart == '0') {
38 if (textarea.selectionStart || textarea.selectionStart == '0') {
44 39 textarea.focus();
45 40
46 41 var startPos = textarea.selectionStart;
47 42 var endPos = textarea.selectionEnd;
48 43
49 44 var oldValue = textarea.value;
50 45 textarea.value = oldValue.substring(0, startPos) + start +
51 46 oldValue.substring(startPos, endPos) + end +
52 47 oldValue.substring(endPos, oldValue.length);
48
49 if (startPos == endPos) {
50 var newPos = startPos + start.length;
51 textarea.setSelectionRange(newPos, newPos);
52 }
53 53 } else {
54 54 textarea.value += start + end;
55 55 }
56 56 }
57 57
58 58 return false;
59 59 }
60 60
@@ -1,168 +1,160 b''
1 1 /*
2 2 @licstart The following is the entire license notice for the
3 3 JavaScript code in this page.
4 4
5 5
6 6 Copyright (C) 2013 neko259
7 7
8 8 The JavaScript code in this page is free software: you can
9 9 redistribute it and/or modify it under the terms of the GNU
10 10 General Public License (GNU GPL) as published by the Free Software
11 11 Foundation, either version 3 of the License, or (at your option)
12 12 any later version. The code is distributed WITHOUT ANY WARRANTY;
13 13 without even the implied warranty of MERCHANTABILITY or FITNESS
14 14 FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
15 15
16 16 As additional permission under GNU GPL version 3 section 7, you
17 17 may distribute non-source (e.g., minimized or compacted) forms of
18 18 that code without the copy of the GNU GPL normally required by
19 19 section 4, provided you include this license notice and a URL
20 20 through which recipients can access the Corresponding Source.
21 21
22 22 @licend The above is the entire license notice
23 23 for the JavaScript code in this page.
24 24 */
25 25
26 26 var CLOSE_BUTTON = '#form-close-button';
27 27 var REPLY_TO_MSG = '.reply-to-message';
28 28 var REPLY_TO_MSG_ID = '#reply-to-message-id';
29 29
30 30 var $html = $("html, body");
31 31
32 32 function moveCaretToEnd(el) {
33 if (typeof el.selectionStart == "number") {
34 el.selectionStart = el.selectionEnd = el.value.length;
35 } else if (typeof el.createTextRange != "undefined") {
36 el.focus();
37 var range = el.createTextRange();
38 range.collapse(false);
39 range.select();
40 }
33 var newPos = el.val().length;
34 el[0].setSelectionRange(newPos, newPos);
41 35 }
42 36
43 37 function getForm() {
44 38 return $('.post-form-w');
45 39 }
46 40
47 41 function resetFormPosition() {
48 42 var form = getForm();
49 43 form.insertAfter($('.thread'));
50 44
51 45 $(CLOSE_BUTTON).hide();
52 46 $(REPLY_TO_MSG).hide();
53 47 }
54 48
55 49 function showFormAfter(blockToInsertAfter) {
56 50 var form = getForm();
57 51 form.insertAfter(blockToInsertAfter);
58 52
59 53 $(CLOSE_BUTTON).show();
60 54 form.show();
61 55 $(REPLY_TO_MSG_ID).text(blockToInsertAfter.attr('id'));
62 56 $(REPLY_TO_MSG).show();
63 57 }
64 58
65 59 function addQuickReply(postId) {
66 60 // If we click "reply" on the same post, it means "cancel"
67 61 if (getForm().prev().attr('id') == postId) {
68 62 resetFormPosition();
69 63 } else {
70 64 var blockToInsert = null;
71 65 var textAreaJq = getPostTextarea();
72 66 var postLinkRaw = '[post]' + postId + '[/post]'
73 67 var textToAdd = '';
74 68
75 69 if (postId != null) {
76 70 var post = $('#' + postId);
77 71
78 72 // If this is not OP, add reflink to the post. If there already is
79 73 // the same reflink, don't add it again.
80 74 var postText = textAreaJq.val();
81 75 if (!post.is(':first-child') && postText.indexOf(postLinkRaw) < 0) {
82 76 // Insert line break if none is present.
83 77 if (postText.length > 0 && !postText.endsWith('\n') && !postText.endsWith('\r')) {
84 78 textToAdd += '\n';
85 79 }
86 80 textToAdd += postLinkRaw + '\n';
87 81 }
88 82
89 83 textAreaJq.val(textAreaJq.val()+ textToAdd);
90 84 blockToInsert = post;
91 85 } else {
92 86 blockToInsert = $('.thread');
93 87 }
94 88 showFormAfter(blockToInsert);
95 89
96 90 textAreaJq.focus();
97 91
98 var textarea = document.getElementsByTagName('textarea')[0];
99 moveCaretToEnd(textarea);
92 moveCaretToEnd(textAreaJq);
100 93 }
101 94 }
102 95
103 96 function addQuickQuote() {
104 97 var textAreaJq = getPostTextarea();
105 98
106 99 var quoteButton = $("#quote-button");
107 100 var postId = quoteButton.attr('data-post-id');
108 101 if (postId != null && getForm().prev().attr('id') != postId) {
109 102 addQuickReply(postId);
110 103 }
111 104
112 105 var textToAdd = '';
113 106 var selection = window.getSelection().toString();
114 107 if (selection.length == 0) {
115 108 selection = quoteButton.attr('data-text');
116 109 }
117 110 if (selection.length > 0) {
118 111 textToAdd += '[quote]' + selection + '[/quote]\n';
119 112 }
120 113
121 114 textAreaJq.val(textAreaJq.val() + textToAdd);
122 115
123 116 textAreaJq.focus();
124 117
125 var textarea = document.getElementsByTagName('textarea')[0];
126 moveCaretToEnd(textarea);
118 moveCaretToEnd(textAreaJq);
127 119 }
128 120
129 121 function scrollToBottom() {
130 122 $html.animate({scrollTop: $html.height()}, "fast");
131 123 }
132 124
133 125 function showQuoteButton() {
134 126 var selection = window.getSelection().getRangeAt(0).getBoundingClientRect();
135 127 var quoteButton = $("#quote-button");
136 128 if (selection.width > 0) {
137 129 // quoteButton.offset({ top: selection.top - selection.height, left: selection.left });
138 130 quoteButton.css({top: selection.top + $(window).scrollTop() - 30, left: selection.left});
139 131 quoteButton.show();
140 132
141 133 var text = window.getSelection().toString();
142 134 quoteButton.attr('data-text', text);
143 135
144 136 var rect = window.getSelection().getRangeAt(0).getBoundingClientRect();
145 137 var element = $(document.elementFromPoint(rect.x, rect.y));
146 138 var postId = null;
147 139 if (element.hasClass('post')) {
148 140 postId = element.attr('id');
149 141 } else {
150 142 var postParent = element.parents('.post');
151 143 if (postParent.length > 0) {
152 144 postId = postParent.attr('id');
153 145 }
154 146 }
155 147 quoteButton.attr('data-post-id', postId);
156 148 } else {
157 149 quoteButton.hide();
158 150 }
159 151 }
160 152
161 153 $(document).ready(function() {
162 154 $('body').on('mouseup', function() {
163 155 showQuoteButton();
164 156 });
165 157 $("#quote-button").click(function() {
166 158 addQuickQuote();
167 159 })
168 160 });
General Comments 0
You need to be logged in to leave comments. Login now