Optimization

This commit is contained in:
Alex Yatskov 2016-04-14 21:49:34 -07:00
parent 5f74c473ce
commit 0cd7830281

View File

@ -20,7 +20,7 @@
class Client { class Client {
constructor() { constructor() {
this.lastMosePos = null; this.lastMosePos = null;
this.popupText = ''; this.popupQuery = '';
this.popupOffset = 10; this.popupOffset = 10;
this.enabled = false; this.enabled = false;
this.options = null; this.options = null;
@ -38,9 +38,7 @@ class Client {
getOptions((opts) => { getOptions((opts) => {
this.setOptions(opts); this.setOptions(opts);
getState((state) => { getState((state) => this.setEnabled(state === 'enabled'));
this.setEnabled(state === 'enabled');
});
}); });
} }
@ -97,26 +95,25 @@ class Client {
return; return;
} }
const text = range.toString(); const popupQuery = range.toString();
if (text === this.popupText) { if (popupQuery === this.popupQuery) {
return; return;
} }
findTerm(text, ({results, length}) => { findTerm(popupQuery, ({results, length}) => {
if (length === 0) { if (length === 0) {
this.hidePopup(); this.hidePopup();
} else { } else {
range.setEnd(range.endContainer, range.startOffset + length); const params = {defs: results, root: chrome.extension.getURL('fg')};
renderText({defs: results, root: chrome.extension.getURL('fg')}, 'defs.html', (html) => { renderText(params, 'defs.html', (html) => this.showPopup(range, html, popupQuery, length));
this.popup.setAttribute('srcdoc', html);
this.showPopup(range);
});
} }
}); });
} }
showPopup(range) { showPopup(range, html, popupQuery, length) {
if (this.options.highlightText) { if (this.options.highlightText) {
range.setEnd(range.endContainer, range.startOffset + length);
const selection = window.getSelection(); const selection = window.getSelection();
selection.removeAllRanges(); selection.removeAllRanges();
selection.addRange(range); selection.addRange(range);
@ -124,10 +121,14 @@ class Client {
const pos = getPopupPositionForRange(this.popup, range, this.popupOffset); const pos = getPopupPositionForRange(this.popup, range, this.popupOffset);
this.popupText = range.toString(); if (this.popup.getAttribute('srcdoc') !== html) {
this.popup.setAttribute('srcdoc', html);
}
this.popup.style.left = pos.x + 'px'; this.popup.style.left = pos.x + 'px';
this.popup.style.top = pos.y + 'px'; this.popup.style.top = pos.y + 'px';
this.popup.style.visibility = 'visible'; this.popup.style.visibility = 'visible';
this.popupQuery = popupQuery;
} }
hidePopup() { hidePopup() {
@ -140,8 +141,8 @@ class Client {
selection.removeAllRanges(); selection.removeAllRanges();
} }
this.popupText = '';
this.popup.style.visibility = 'hidden'; this.popup.style.visibility = 'hidden';
this.popupQuery = '';
} }
setEnabled(enabled) { setEnabled(enabled) {