Improvements

This commit is contained in:
Alex Yatskov 2016-04-24 11:04:24 -07:00
parent 75d5d84811
commit 709094455e
3 changed files with 25 additions and 29 deletions

View File

@ -87,16 +87,17 @@ class Client {
return; return;
} }
range.setLength(this.options.scanLength); if (this.lastRange !== null && this.lastRange.compareOrigin(range) === 0) {
if (this.lastRange !== null && this.lastRange.equals(range)) {
return; return;
} }
range.setLength(this.options.scanLength);
findTerm(range.text(), ({results, length}) => { findTerm(range.text(), ({results, length}) => {
if (length === 0) { if (length === 0) {
this.hidePopup(); this.hidePopup();
} else { } else {
range.setLength(length);
const params = { const params = {
defs: results, defs: results,
root: chrome.extension.getURL('fg') root: chrome.extension.getURL('fg')
@ -105,17 +106,21 @@ class Client {
renderText( renderText(
params, params,
'term-list.html', 'term-list.html',
(content) => this.showPopup(range, length, content) (content) => this.showPopup(range, content)
); );
} }
}); });
} }
showPopup(range, length, content) { showPopup(range, content) {
this.popup.showNextTo(range, content); this.popup.showNextTo(range.getRect(), content);
if (this.options.highlightText) { if (this.options.highlightText) {
range.select(length); if (this.lastRange !== null) {
this.lastRange.deselect();
}
range.select();
} }
this.lastRange = range; this.lastRange = range;

View File

@ -33,10 +33,9 @@ class Popup {
this.setContent(content); this.setContent(content);
} }
showNextTo(element, content) { showNextTo(elementRect, content) {
this.inject(); this.inject();
const elementRect = element.getBoundingClientRect();
const popupRect = this.popup.getBoundingClientRect(); const popupRect = this.popup.getBoundingClientRect();
let posX = elementRect.left; let posX = elementRect.left;
@ -49,11 +48,7 @@ class Popup {
posY = elementRect.top - popupRect.height - this.offset; posY = elementRect.top - popupRect.height - this.offset;
} }
this.popup.style.left = posX + 'px'; this.showAt({x: posX, y: posY}, content);
this.popup.style.top = posY + 'px';
this.popup.style.visibility = 'visible';
this.setContent(content);
} }
hide() { hide() {

View File

@ -40,11 +40,15 @@ class Range {
} }
containsPoint(point) { containsPoint(point) {
const rect = this.getBoundingClientRect(); const rect = this.getPaddedRect();
return point.x >= rect.left && point.x <= rect.right; return point.x >= rect.left && point.x <= rect.right;
} }
getBoundingClientRect() { getRect() {
return this.rng.getBoundingClientRect();
}
getPaddedRect() {
const range = this.rng.cloneRange(); const range = this.rng.cloneRange();
const startOffset = range.startOffset; const startOffset = range.startOffset;
const endOffset = range.endOffset; const endOffset = range.endOffset;
@ -56,13 +60,9 @@ class Range {
return range.getBoundingClientRect(); return range.getBoundingClientRect();
} }
select(length) { select() {
const range = this.rng.cloneRange();
range.setEnd(range.startContainer, range.startOffset + length);
const selection = window.getSelection(); const selection = window.getSelection();
selection.removeAllRanges(); selection.addRange(this.rng);
selection.addRange(range);
} }
deselect() { deselect() {
@ -70,12 +70,8 @@ class Range {
selection.removeAllRanges(); selection.removeAllRanges();
} }
equals(range) { compareOrigin(range) {
const equal = return range.rng.compareBoundaryPoints(Range.END_TO_END, this.rng);
range.rng.compareBoundaryPoints(Range.END_TO_END, this.rng) === 0 &&
range.rng.compareBoundaryPoints(Range.START_TO_START, this.rng) === 0;
return equal;
} }