This commit is contained in:
Alex Yatskov 2016-03-28 13:00:48 -07:00
parent 43d4abf317
commit d77319e328
2 changed files with 38 additions and 20 deletions

View File

@ -21,36 +21,55 @@ class Client {
constructor() { constructor() {
this.popup = $('<div class="yomichan-popup"/>'); this.popup = $('<div class="yomichan-popup"/>');
this.popupOffset = 10; this.popupOffset = 10;
this.lastMosePos = null;
this.enabled = false; this.enabled = false;
$('body').append(this.popup).click(() => this.hidePopup()); $('body').append(this.popup);
chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); chrome.runtime.onMessage.addListener(this.onMessage.bind(this));
window.addEventListener('mousemove', this.onMouseMove.bind(this)); window.addEventListener('mousedown', this.onMouseAction.bind(this));
window.addEventListener('keydown', this.onKeyDown.bind(this)); window.addEventListener('mousemove', this.onMouseAction.bind(this));
window.addEventListener('keydown', this.onKeyAction.bind(this));
getState((state) => this.setEnabled(state === 'enabled')); getState((state) => this.setEnabled(state === 'enabled'));
} }
onKeyDown(e) { onKeyAction(e) {
if (e.keyCode === 16 || e.charCode === 16) { if (!this.enabled) {
this.hidePopup();
}
}
onMouseMove(e) {
if (!this.enabled || (!e.shiftKey && e.which !== 2)) {
return; return;
} }
const range = getRangeAtCursor(e, 10); if (this.lastMousePos !== null && (e.keyCode === 16 || e.charCode === 16)) {
this.searchAtPoint(this.lastMousePos);
}
}
onMouseAction(e) {
this.lastMousePos = {x: e.clientX, y: e.clientY};
if (!this.enabled) {
return;
}
if (e.shiftKey || e.which === 2) {
this.searchAtPoint(this.lastMousePos);
}
}
onMessage(request, sender, callback) {
this.setEnabled(request === 'enabled');
callback();
}
searchAtPoint(point) {
const range = getRangeAtPoint(point, 10);
if (range === null) { if (range === null) {
this.hidePopup(); this.hidePopup();
return; return;
} }
const rect = getRangePaddedRect(range); const rect = getRangePaddedRect(range);
if (e.clientX < rect.left || e.clientX > rect.right) { if (point.x < rect.left || point.x > rect.right) {
this.hidePopup(); this.hidePopup();
return; return;
} }
@ -68,11 +87,6 @@ class Client {
}); });
} }
onMessage(request, sender, callback) {
this.setEnabled(request === 'enabled');
callback();
}
showPopup(range) { showPopup(range) {
const selection = window.getSelection(); const selection = window.getSelection();
selection.removeAllRanges(); selection.removeAllRanges();
@ -83,6 +97,10 @@ class Client {
} }
hidePopup() { hidePopup() {
if (this.popup.css('visibility') === 'hidden') {
return;
}
const selection = window.getSelection(); const selection = window.getSelection();
selection.removeAllRanges(); selection.removeAllRanges();

View File

@ -17,8 +17,8 @@
*/ */
function getRangeAtCursor(e, lookAhead) { function getRangeAtPoint(point, lookAhead) {
const range = document.caretRangeFromPoint(e.clientX, e.clientY); const range = document.caretRangeFromPoint(point.x, point.y);
if (range === null) { if (range === null) {
return null; return null;
} }