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