This commit is contained in:
Alex Yatskov 2016-09-11 20:18:34 -07:00
parent 6183864f16
commit 8f776cf759

View File

@ -24,8 +24,6 @@ class Client {
this.lastMousePos = null;
this.lastTextSource = null;
this.pendingLookup = false;
this.activateKey = 16;
this.activateBtn = 2;
this.enabled = false;
this.options = {};
this.definitions = null;
@ -42,21 +40,21 @@ class Client {
}
onKeyDown(e) {
if (this.enabled && this.lastMousePos !== null && (e.keyCode === this.activateKey || e.charCode === this.activateKey)) {
if (this.enabled && this.lastMousePos !== null && (e.keyCode === 16 || e.charCode === 16)) {
this.searchAt(this.lastMousePos);
}
}
onMouseMove(e) {
this.lastMousePos = {x: e.clientX, y: e.clientY};
if (this.enabled && (e.shiftKey || e.which === this.activateBtn)) {
if (this.enabled && (e.shiftKey || e.which === 2)) {
this.searchAt(this.lastMousePos);
}
}
onMouseDown(e) {
this.lastMousePos = {x: e.clientX, y: e.clientY};
if (this.enabled && (e.shiftKey || e.which === this.activateBtn)) {
if (this.enabled && (e.shiftKey || e.which === 2)) {
this.searchAt(this.lastMousePos);
} else {
this.hidePopup();
@ -80,6 +78,10 @@ class Client {
}
searchAt(point) {
if (this.pendingLookup) {
return;
}
const textSource = Client.textSourceFromPoint(point);
if (textSource === null || !textSource.containsPoint(point)) {
this.hidePopup();
@ -90,22 +92,13 @@ class Client {
return;
}
if (this.pendingLookup) {
return;
}
textSource.setEndOffset(this.options.scanLength);
let defs = [];
let seq = -1;
this.pendingLookup = true;
bgFindTerm(textSource.text())
.then(({definitions, length}) => {
bgFindTerm(textSource.text()).then(({definitions, length}) => {
if (length === 0) {
return Promise.reject();
}
this.hidePopup();
} else {
textSource.setEndOffset(length);
const sentence = Client.extractSentence(textSource, this.options.sentenceExtent);
@ -114,27 +107,19 @@ class Client {
definition.sentence = sentence;
});
defs = definitions;
seq = ++this.sequence;
return bgRenderText({definitions, root: this.fgRoot, options: this.options, sequence: seq}, 'term-list.html');
})
.then(content => {
this.definitions = defs;
const sequence = ++this.sequence;
return bgRenderText({definitions, sequence, root: this.fgRoot, options: this.options}, 'term-list.html').then((content) => {
this.definitions = definitions;
this.showPopup(textSource, content);
return bgCanAddDefinitions(defs, ['term_kanji', 'term_kana']);
})
.then(states => {
this.pendingLookup = false;
return bgCanAddDefinitions(definitions, ['term_kanji', 'term_kana']);
}).then(states => {
if (states !== null) {
states.forEach((state, index) => this.popup.sendMessage('setActionState', {index, state, sequence: seq}));
states.forEach((state, index) => this.popup.sendMessage('setActionState', {index, state, sequence }));
}
}, () => {
this.pendingLookup = false;
this.hidePopup();
});
}
}).then(() => this.pendingLookup = false);
}
showPopup(textSource, content) {
this.popup.showNextTo(textSource.getRect(), content);
@ -201,22 +186,19 @@ class Client {
let defs = [];
let seq = -1;
bgFindKanji(kanji)
.then(definitions => {
bgFindKanji(kanji).then(definitions => {
definitions.forEach(definition => definition.url = window.location.href);
defs = definitions;
seq = ++this.sequence;
return bgRenderText({definitions, root: this.fgRoot, options: this.options, sequence: seq}, 'kanji-list.html');
})
.then(content => {
}).then(content => {
this.definitions = defs;
this.popup.setContent(content, defs);
return bgCanAddDefinitions(defs, ['kanji']);
})
.then(states => {
}).then(states => {
if (states !== null) {
states.forEach((state, index) => this.popup.sendMessage('setActionState', {index, state, sequence: seq}));
}