This commit is contained in:
Alex Yatskov 2016-04-23 10:10:34 -07:00
parent b8df875e03
commit 8b097221db
3 changed files with 42 additions and 35 deletions

View File

@ -87,14 +87,13 @@ class Client {
return; return;
} }
const text = range.text(); if (this.lastRange !== null && this.lastRange.equalTo(range)) {
if (this.lastRange !== null && this.lastRange.text() == text) {
return; return;
} }
findTerm(popupQuery, ({results, length}) => { findTerm(range.text(), ({results, length}) => {
if (length === 0) { if (length === 0) {
this.popup.hide(); this.hidePopup();
} else { } else {
const params = { const params = {
defs: results, defs: results,
@ -104,13 +103,13 @@ class Client {
renderText( renderText(
params, params,
'term-list.html', 'term-list.html',
(html) => this.showPopup(range, html, popupQuery, length) (content) => this.showPopup(range, length, content)
); );
} }
}); });
} }
showPopup(range, html, popupQuery, length) { showPopup(range, length, content) {
if (this.options.highlightText) { if (this.options.highlightText) {
range.setEnd(range.endContainer, range.startOffset + length); range.setEnd(range.endContainer, range.startOffset + length);
@ -119,30 +118,17 @@ class Client {
selection.addRange(range); selection.addRange(range);
} }
const pos = getPopupPositionForRange(this.popup, range, this.popupOffset); this.popup.showNextTo(range, content);
if (this.popup.getAttribute('srcdoc') !== html) {
this.popup.setAttribute('srcdoc', html);
}
this.popup.style.left = pos.x + 'px';
this.popup.style.top = pos.y + 'px';
this.popup.style.visibility = 'visible';
this.popupQuery = popupQuery;
} }
hidePopup() { hidePopup() {
if (this.popup.style.visibility === 'hidden') { this.popup.hide();
return;
if (this.options.highlightText && this.lastRange !== null) {
this.lastRange.deselect();
} }
if (this.options.highlightText) { this.lastRange = null;
const selection = window.getSelection();
selection.removeAllRanges();
}
this.popup.style.visibility = 'hidden';
this.popupQuery = '';
} }
setEnabled(enabled) { setEnabled(enabled) {

View File

@ -33,7 +33,7 @@ class Popup {
this.setContent(content); this.setContent(content);
} }
showBy(element, content) { showNextTo(element, content) {
this.inject(); this.inject();
const elementRect = element.getBoundingClientRect(); const elementRect = element.getBoundingClientRect();

View File

@ -45,17 +45,38 @@ class Range {
} }
paddedRect() { paddedRect() {
const node = this.range.startContainer; const range = this.range.cloneRange();
const startOffset = this.range.startOffset; const startOffset = range.startOffset;
const endOffset = this.range.endOffset; const endOffset = range.endOffset;
const node = range.startContainer;
this.range.setStart(node, Math.max(0, startOffset - 1)); range.setStart(node, Math.max(0, startOffset - 1));
this.range.setEnd(node, Math.min(node.length, endOffset + 1)); range.setEnd(node, Math.min(node.length, endOffset + 1));
const rect = range.getBoundingClientRect();
this.range.setStart(node, startOffset); return range.getBoundingClientRect();
this.range.setEnd(node, endOffset); }
select(length) {
const range = this.range.cloneRange();
range.setEnd(range.startContainer, range.startOffset + length);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
}
deselect() {
const selection = window.getSelection();
selection.removeRange(this.range);
}
equalTo(range) {
const equal =
range.compareBoundaryPoints(Range.END_TO_END, this.range) === 0 &&
range.compareBoundaryPoints(Range.START_TO_START, this.range) === 0;
return equal;
return rect;
} }
static fromPos(point) { static fromPos(point) {