do not select the marker in Chrome

This commit is contained in:
Régis Hanol 2013-10-21 10:20:47 +02:00
parent d9a16079a5
commit 9c820443fa
1 changed files with 13 additions and 12 deletions

View File

@ -39,6 +39,7 @@ Discourse.QuoteButtonController = Discourse.Controller.extend({
var selection = window.getSelection();
// no selections
if (selection.rangeCount === 0) return;
// retrieve the selected range
var range = selection.getRangeAt(0),
cloned = range.cloneRange(),
@ -55,7 +56,6 @@ Discourse.QuoteButtonController = Discourse.Controller.extend({
if (this.get('buffer') === selectedText) return;
// we need to retrieve the post data from the posts collection in the topic controller
var postStream = this.get('controllers.topic.postStream');
this.set('post', postStream.findLoadedPost(postId));
this.set('buffer', selectedText);
@ -64,31 +64,32 @@ Discourse.QuoteButtonController = Discourse.Controller.extend({
// (ie. moves the end point to the start point)
range.collapse(true);
// create a marker element containing a single invisible character
// create a marker element
var markerElement = document.createElement("span");
// containing a single invisible character
markerElement.appendChild(document.createTextNode("\ufeff"));
// insert it at the beginning of our range
// and insert it at the beginning of our selection range
range.insertNode(markerElement);
// work around chrome that would sometimes lose the selection
// retrieve the position of the market
var markerOffset = $(markerElement).offset(),
$quoteButton = $('.quote-button');
// remove the marker
markerElement.parentNode.removeChild(markerElement);
// work around Chrome that would sometimes lose the selection
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(cloned);
// move the quote button at the beginning of the selection
var markerOffset = $(markerElement).offset(),
$quoteButton = $('.quote-button');
// move the quote button above the marker
Em.run.schedule('afterRender', function() {
$quoteButton.offset({
top: markerOffset.top - $quoteButton.outerHeight() - 5,
left: markerOffset.left
});
});
// remove the marker
markerElement.parentNode.removeChild(markerElement);
},
/**