|
45 | 45 | if (evt.type === 'touchstart') {
|
46 | 46 | evt.pageX = evt.originalEvent.touches[0].pageX;
|
47 | 47 | evt.pageY = evt.originalEvent.touches[0].pageY;
|
| 48 | + } else if (evt.type === 'selectionchange') { |
| 49 | + return self._getSelectionCoords(); |
48 | 50 | }
|
49 | 51 |
|
50 | 52 | return {
|
|
53 | 55 | };
|
54 | 56 | };
|
55 | 57 |
|
| 58 | + /** |
| 59 | + * Returns an object containing the coordinates for the end of current text selection. |
| 60 | + * Original author: Tim Down (https://stackoverflow.com/a/6847328) |
| 61 | + * |
| 62 | + * @returns {{x: number, y: number}} |
| 63 | + */ |
| 64 | + self._getSelectionCoords = function() { |
| 65 | + var sel = document.selection, range, rects, rect; |
| 66 | + var x = 0, y = 0; |
| 67 | + |
| 68 | + if (sel) { |
| 69 | + if (sel.type !== "Control") { |
| 70 | + range = sel.createRange(); |
| 71 | + range.collapse(false); |
| 72 | + x = range.boundingLeft; |
| 73 | + y = range.boundingBottom; |
| 74 | + } |
| 75 | + } else if (window.getSelection) { |
| 76 | + sel = window.getSelection(); |
| 77 | + if (sel.rangeCount) { |
| 78 | + range = sel.getRangeAt(sel.rangeCount - 1).cloneRange(); |
| 79 | + if (range.getClientRects) { |
| 80 | + range.collapse(false); |
| 81 | + rects = range.getClientRects(); |
| 82 | + if (rects.length > 0) { |
| 83 | + rect = rects[rects.length - 1]; |
| 84 | + x = rect.left; |
| 85 | + y = rect.bottom; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + return { x: x ? x + $(document).scrollLeft() : 0, y: y ? y + $(document).scrollTop() : 0 }; |
| 91 | + }; |
| 92 | + |
56 | 93 | /**
|
57 | 94 | * Sets new dropdown and adds body event handler.
|
58 | 95 | *
|
|
173 | 210 | }
|
174 | 211 |
|
175 | 212 | var coordinates = self._getCoordinates(evt);
|
| 213 | + if (!coordinates.x && !coordinates.y) { |
| 214 | + // Prevent possible positioning bugs (e.g. in IE8) |
| 215 | + return; |
| 216 | + } |
| 217 | + |
176 | 218 | // Which mouse button is pressed?
|
177 | 219 | var key = evt.button || evt.which || null; // IE || FF || Unknown
|
178 | 220 |
|
179 | 221 | self._postID = quickreply.style.getPostId($element);
|
180 | 222 |
|
181 | 223 | setTimeout(function() { // Timeout prevents popup when clicking on selected text
|
182 | 224 | self._getSelection();
|
| 225 | + self._removeDropdown(); |
183 | 226 |
|
184 | 227 | if (self._selection && key <= 1) { // If text selected && right mouse button not pressed
|
185 |
| - self._removeDropdown(); |
186 |
| - self._setDropdown(quickreply.style.quickQuoteDropdown(coordinates.x, coordinates.y) |
187 |
| - .mousedown(insertQuickQuote)); |
| 228 | + var $dropdown = quickreply.style.quickQuoteDropdown(coordinates.x, coordinates.y) |
| 229 | + .mousedown(insertQuickQuote); |
| 230 | + self._setDropdown($dropdown); |
188 | 231 | }
|
189 | 232 | }, 0);
|
190 | 233 | }
|
|
195 | 238 | * @param {event} evt jQuery Event object
|
196 | 239 | */
|
197 | 240 | function handleQuickQuote(evt) {
|
198 |
| - addQuickQuote(evt, $(this)); |
| 241 | + var $content = $(this); |
| 242 | + addQuickQuote(evt, $content); |
199 | 243 |
|
200 | 244 | if (!quickQuoteCancelEvent) {
|
201 |
| - quickreply.$.qrPosts.on('mousemove', '.content', addQuickQuote); |
202 |
| - |
203 |
| - $(document.body).one('mouseup', function() { |
204 |
| - quickreply.$.qrPosts.off('mousemove', '.content', addQuickQuote); |
205 |
| - quickQuoteCancelEvent = false; |
206 |
| - }); |
| 245 | + if ('onselectionchange' in document) { |
| 246 | + $(document).on('selectionchange.quickreply', function (evt) { |
| 247 | + addQuickQuote(evt, $content); |
| 248 | + }); |
| 249 | + $(document.body).one('mouseup', function() { |
| 250 | + $(document).off('selectionchange.quickreply'); |
| 251 | + quickQuoteCancelEvent = false; |
| 252 | + }); |
| 253 | + } else { |
| 254 | + // Fall back to mouse events for older browsers - this method has drawbacks on mobile devices |
| 255 | + quickreply.$.qrPosts.on('mousemove', '.content', addQuickQuote); |
| 256 | + $(document.body).one('mouseup', function() { |
| 257 | + quickreply.$.qrPosts.off('mousemove', '.content', addQuickQuote); |
| 258 | + quickQuoteCancelEvent = false; |
| 259 | + }); |
| 260 | + } |
207 | 261 |
|
208 | 262 | quickQuoteCancelEvent = true;
|
209 | 263 | }
|
|
214 | 268 | */
|
215 | 269 | self.init = function() {
|
216 | 270 | if ('ontouchstart' in window) {
|
217 |
| - quickreply.$.qrPosts.on('mousedown touchstart', '.content', handleQuickQuote); |
| 271 | + quickreply.$.qrPosts.on('touchstart', '.content', handleQuickQuote); |
218 | 272 | }
|
219 | 273 |
|
220 |
| - quickreply.$.qrPosts.on('mouseup', '.content', addQuickQuote); |
| 274 | + quickreply.$.qrPosts.on('mousedown', '.content', handleQuickQuote) |
| 275 | + .on('mouseup', '.content', addQuickQuote); |
221 | 276 | };
|
222 | 277 | }
|
223 | 278 |
|
|
0 commit comments