This commit is contained in:
Alex Yatskov 2017-01-07 18:39:36 -08:00
parent dc4162eeed
commit fab7a03b6c
4 changed files with 45 additions and 40 deletions

View File

@ -32,11 +32,11 @@ class Driver {
window.addEventListener('mousedown', this.onMouseDown.bind(this)); window.addEventListener('mousedown', this.onMouseDown.bind(this));
window.addEventListener('mousemove', this.onMouseMove.bind(this)); window.addEventListener('mousemove', this.onMouseMove.bind(this));
window.addEventListener('keydown', this.onKeyDown.bind(this)); window.addEventListener('keydown', this.onKeyDown.bind(this));
window.addEventListener('resize', e => this.hidePopup()); window.addEventListener('resize', e => this.searchClear());
getOptions().then(opts => { getOptions().then(opts => {
this.options = opts; this.options = opts;
return getEnabled(); return isEnabled();
}).then(enabled => { }).then(enabled => {
this.enabled = enabled; this.enabled = enabled;
}); });
@ -60,7 +60,7 @@ class Driver {
if (this.enabled && this.lastMousePos !== null && e.keyCode === 16 /* shift */) { if (this.enabled && this.lastMousePos !== null && e.keyCode === 16 /* shift */) {
this.searchAt(this.lastMousePos, true); this.searchAt(this.lastMousePos, true);
} else if (e.keyCode === 27 /* esc */) { } else if (e.keyCode === 27 /* esc */) {
this.hidePopup(); this.searchClear();
} }
} }
@ -87,7 +87,7 @@ class Driver {
} }
const searcher = () => this.searchAt(this.lastMousePos, false); const searcher = () => this.searchAt(this.lastMousePos, false);
if (!this.popup.visible() || e.shiftKey || e.which === 2 /* mmb */) { if (!this.popup.isVisible() || e.shiftKey || e.which === 2 /* mmb */) {
searcher(); searcher();
} else { } else {
this.popupTimerSet(searcher); this.popupTimerSet(searcher);
@ -97,7 +97,7 @@ class Driver {
onMouseDown(e) { onMouseDown(e) {
this.lastMousePos = {x: e.clientX, y: e.clientY}; this.lastMousePos = {x: e.clientX, y: e.clientY};
this.popupTimerClear(); this.popupTimerClear();
this.hidePopup(); this.searchClear();
} }
onBgMessage({action, params}, sender, callback) { onBgMessage({action, params}, sender, callback) {
@ -117,14 +117,14 @@ class Driver {
const textSource = textSourceFromPoint(point); const textSource = textSourceFromPoint(point);
if (textSource === null || !textSource.containsPoint(point)) { if (textSource === null || !textSource.containsPoint(point)) {
if (hideNotFound) { if (hideNotFound) {
this.hidePopup(); this.searchClear();
} }
return; return;
} }
if (this.lastTextSource !== null && this.lastTextSource.equals(textSource)) { if (this.lastTextSource !== null && this.lastTextSource.equals(textSource)) {
return true; return;
} }
this.pendingLookup = true; this.pendingLookup = true;
@ -132,12 +132,12 @@ class Driver {
if (!found) { if (!found) {
this.searchKanji(textSource).then(found => { this.searchKanji(textSource).then(found => {
if (!found && hideNotFound) { if (!found && hideNotFound) {
this.hidePopup(); this.searchClear();
} }
}); });
} }
}).catch(error => { }).catch(error => {
alert('Error: ' + error); window.alert('Error: ' + error);
}).then(() => { }).then(() => {
this.pendingLookup = false; this.pendingLookup = false;
}); });
@ -159,12 +159,13 @@ class Driver {
}); });
this.popup.showNextTo(textSource.getRect()); this.popup.showNextTo(textSource.getRect());
this.popup.invoke('showTermDefs', {definitions, options: this.options}); this.popup.showTermDefs(definitions, this.options);
return true; return true;
} }
}).catch(error => { }).catch(error => {
alert('Error: ' + error); window.alert('Error: ' + error);
return false;
}); });
} }
@ -178,16 +179,17 @@ class Driver {
definitions.forEach(definition => definition.url = window.location.href); definitions.forEach(definition => definition.url = window.location.href);
this.popup.showNextTo(textSource.getRect()); this.popup.showNextTo(textSource.getRect());
this.popup.invoke('showKanjiDefs', {definitions, options: this.options}); this.popup.showKanjiDefs(definitions, this.options);
return true; return true;
} }
}).catch(error => { }).catch(error => {
alert('Error: ' + error); window.alert('Error: ' + error);
return false;
}); });
} }
hidePopup() { searchClear() {
this.popup.hide(); this.popup.hide();
if (this.options.selectMatchedText && this.lastTextSource !== null) { if (this.options.selectMatchedText && this.lastTextSource !== null) {
@ -195,7 +197,6 @@ class Driver {
} }
this.lastTextSource = null; this.lastTextSource = null;
this.definitions = null;
} }
api_setOptions(opts) { api_setOptions(opts) {
@ -204,7 +205,7 @@ class Driver {
api_setEnabled(enabled) { api_setEnabled(enabled) {
if (!(this.enabled = enabled)) { if (!(this.enabled = enabled)) {
this.hidePopup(); this.searchClear();
} }
} }
} }

View File

@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
class FrameContext { class Frame {
constructor() { constructor() {
this.definitions = []; this.definitions = [];
this.audioCache = {}; this.audioCache = {};
@ -156,4 +156,4 @@ class FrameContext {
} }
} }
window.frameContext = new FrameContext(); window.frame = new Frame();

View File

@ -30,7 +30,7 @@ class Popup {
document.body.appendChild(this.container); document.body.appendChild(this.container);
} }
show(rect) { showAt(rect) {
this.container.style.left = rect.x + 'px'; this.container.style.left = rect.x + 'px';
this.container.style.top = rect.y + 'px'; this.container.style.top = rect.y + 'px';
this.container.style.height = rect.height + 'px'; this.container.style.height = rect.height + 'px';
@ -59,22 +59,26 @@ class Popup {
y = elementRect.top - height - this.offset; y = elementRect.top - height - this.offset;
} }
this.show({x, y, width, height}); this.showAt({x, y, width, height});
}
visible() {
return this.container !== null && this.container.style.visibility !== 'hidden';
} }
hide() { hide() {
if (this.container !== null) { this.container.style.visibility = 'hidden';
this.container.style.visibility = 'hidden';
}
} }
invoke(action, params) { isVisible() {
if (this.container !== null) { return this.container.style.visibility !== 'hidden';
this.container.contentWindow.postMessage({action, params}, '*'); }
}
showTermDefs(definitions, options) {
this.invokeApi('showTermDefs', {definitions, options});
}
showKanjiDefs(definitions, options) {
this.invokeApi('showKanjiDefs', {definitions, options});
}
invokeApi(action, params) {
this.container.contentWindow.postMessage({action, params}, '*');
} }
} }

View File

@ -17,7 +17,7 @@
*/ */
function invokeApiBg(action, params) { function invokeBgApi(action, params) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
chrome.runtime.sendMessage({action, params}, ({result, error}) => { chrome.runtime.sendMessage({action, params}, ({result, error}) => {
if (error) { if (error) {
@ -29,32 +29,32 @@ function invokeApiBg(action, params) {
}); });
} }
function getEnabled() { function isEnabled() {
return invokeApiBg('getEnabled', {}); return invokeBgApi('getEnabled', {});
} }
function getOptions() { function getOptions() {
return invokeApiBg('getOptions', {}); return invokeBgApi('getOptions', {});
} }
function findTerm(text) { function findTerm(text) {
return invokeApiBg('findTerm', {text}); return invokeBgApi('findTerm', {text});
} }
function findKanji(text) { function findKanji(text) {
return invokeApiBg('findKanji', {text}); return invokeBgApi('findKanji', {text});
} }
function renderText(data, template) { function renderText(data, template) {
return invokeApiBg('renderText', {data, template}); return invokeBgApi('renderText', {data, template});
} }
function canAddDefinitions(definitions, modes) { function canAddDefinitions(definitions, modes) {
return invokeApiBg('canAddDefinitions', {definitions, modes}).catch(() => null); return invokeBgApi('canAddDefinitions', {definitions, modes}).catch(() => null);
} }
function addDefinition(definition, mode) { function addDefinition(definition, mode) {
return invokeApiBg('addDefinition', {definition, mode}); return invokeBgApi('addDefinition', {definition, mode});
} }
function textSourceFromPoint(point) { function textSourceFromPoint(point) {