FIX: quote button issues

- disappear when moving to another topic
- disappears when clicking outside of the selection
- works even when selecting the last paragraph of a post
- works on all latest mobile OS
This commit is contained in:
Régis Hanol
2016-11-24 18:23:33 +01:00
parent 054c428ba3
commit af387edeb0
5 changed files with 87 additions and 167 deletions

View File

@ -85,37 +85,27 @@ export function extractDomainFromUrl(url) {
}
export function selectedText() {
var html = '';
const selection = window.getSelection();
if (selection.isCollapsed) { return ""; }
if (typeof window.getSelection !== "undefined") {
var sel = window.getSelection();
if (sel.rangeCount) {
var container = document.createElement("div");
for (var i = 0, len = sel.rangeCount; i < len; ++i) {
container.appendChild(sel.getRangeAt(i).cloneContents());
}
html = container.innerHTML;
}
} else if (typeof document.selection !== "undefined") {
if (document.selection.type === "Text") {
html = document.selection.createRange().htmlText;
}
const $div = $("<div>");
for (let r = 0; r < selection.rangeCount; r++) {
const range = selection.getRangeAt(r);
const $ancestor = $(range.commonAncestorContainer);
// ensure we never quote text in the post menu area
const $postMenuArea = $ancestor.find(".post-menu-area")[0];
if ($postMenuArea) { range.setEndBefore($postMenuArea); }
$div.append(range.cloneContents());
}
html = html.replace(/<br>/g, "\n");
// strip click counters
$div.find(".clicks").remove();
// replace emojis
$div.find("img.emoji").replaceWith(function() { return this.title; });
// Strip out any .click elements from the HTML before converting it to text
const div = document.createElement('div');
div.innerHTML = html;
const $div = $(div);
// Find all emojis and replace with its title attribute.
$div.find('img.emoji').replaceWith(function() { return this.title; });
$('.clicks', $div).remove();
const text = div.textContent || div.innerText || "";
return String(text).trim();
return String($div.text()).trim();
}
// Determine the row and col of the caret in an element