This commit is contained in:
Alex Yatskov 2016-04-22 22:25:12 -07:00
parent 85594f4def
commit b8df875e03
2 changed files with 35 additions and 39 deletions

View File

@ -19,18 +19,12 @@
class Client { class Client {
constructor() { constructor() {
this.popupMousePos = null; this.popup = new Popup();
this.popupQuery = ''; this.lastMousePos = null;
this.popupOffset = 10; this.lastRange = null;
this.enabled = false; this.enabled = false;
this.options = {}; this.options = {};
this.popup = document.createElement('iframe');
this.popup.classList.add('yomichan-popup');
this.popup.addEventListener('mousedown', (e) => e.stopPropagation());
this.popup.addEventListener('scroll', (e) => e.stopPropagation());
document.body.appendChild(this.popup);
chrome.runtime.onMessage.addListener(this.onBgMessage.bind(this)); chrome.runtime.onMessage.addListener(this.onBgMessage.bind(this));
window.addEventListener('message', this.onFrameMessage.bind(this)); window.addEventListener('message', this.onFrameMessage.bind(this));
window.addEventListener('mousedown', this.onMouseDown.bind(this)); window.addEventListener('mousedown', this.onMouseDown.bind(this));
@ -46,24 +40,24 @@ class Client {
} }
onKeyDown(e) { onKeyDown(e) {
if (this.enabled && this.popupMousePos !== null && (e.keyCode === 16 || e.charCode === 16)) { if (this.enabled && this.lastMousePos !== null && (e.keyCode === 16 || e.charCode === 16)) {
this.searchAtPoint(this.popupMousePos); this.searchAt(this.lastMousePos);
} }
} }
onMouseMove(e) { onMouseMove(e) {
this.popupMousePos = {x: e.clientX, y: e.clientY}; this.lastMousePos = {x: e.clientX, y: e.clientY};
if (this.enabled && (e.shiftKey || e.which === 2)) { if (this.enabled && (e.shiftKey || e.which === 2)) {
this.searchAtPoint(this.popupMousePos); this.searchAt(this.lastMousePos);
} }
} }
onMouseDown(e) { onMouseDown(e) {
this.popupMousePos = {x: e.clientX, y: e.clientY}; this.lastMousePos = {x: e.clientX, y: e.clientY};
if (this.enabled && (e.shiftKey || e.which === 2)) { if (this.enabled && (e.shiftKey || e.which === 2)) {
this.searchAtPoint(this.popupMousePos); this.searchAt(this.lastMousePos);
} else { } else {
this.hidePopup(); this.popup.hide();
} }
} }
@ -86,35 +80,32 @@ class Client {
// } // }
} }
searchAtPoint(point) { searchAt(point) {
const range = getRangeAtPoint(point, this.options.scanLength); const range = Range.fromPoint(point);
if (range === null) { if (range === null || !range.containsPoint(point)) {
this.hidePopup(); this.popup.hide();
return; return;
} }
if (this.popup.contains(range.startContainer)) { const text = range.text();
this.hidePopup(); if (this.lastRange !== null && this.lastRange.text() == text) {
return;
}
const rect = getRangePaddedRect(range);
if (point.x < rect.left || point.x > rect.right) {
this.hidePopup();
return;
}
const popupQuery = range.toString();
if (popupQuery === this.popupQuery) {
return; return;
} }
findTerm(popupQuery, ({results, length}) => { findTerm(popupQuery, ({results, length}) => {
if (length === 0) { if (length === 0) {
this.hidePopup(); this.popup.hide();
} else { } else {
const params = {defs: results, root: chrome.extension.getURL('fg')}; const params = {
renderText(params, 'term-list.html', (html) => this.showPopup(range, html, popupQuery, length)); defs: results,
root: chrome.extension.getURL('fg')
};
renderText(
params,
'term-list.html',
(html) => this.showPopup(range, html, popupQuery, length)
);
} }
}); });
} }

View File

@ -39,6 +39,11 @@ class Range {
return length; return length;
} }
containsPoint(point) {
const rect = this.paddedRect();
return point.x >= rect.left && point.x <= rect.right;
}
paddedRect() { paddedRect() {
const node = this.range.startContainer; const node = this.range.startContainer;
const startOffset = this.range.startOffset; const startOffset = this.range.startOffset;
@ -53,7 +58,7 @@ class Range {
return rect; return rect;
} }
static fromPoint(point) { static fromPos(point) {
const range = document.caretRangeFromPoint(point.x, point.y); const range = document.caretRangeFromPoint(point.x, point.y);
return range === null ? null : new Range(range); return range === null ? null : new Range(range);
} }