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