This commit is contained in:
Alex Yatskov 2016-09-11 19:47:40 -07:00
parent 94dccfd875
commit 6183864f16

View File

@ -23,6 +23,7 @@ class Client {
this.audio = {}; this.audio = {};
this.lastMousePos = null; this.lastMousePos = null;
this.lastTextSource = null; this.lastTextSource = null;
this.pendingLookup = false;
this.activateKey = 16; this.activateKey = 16;
this.activateBtn = 2; this.activateBtn = 2;
this.enabled = false; this.enabled = false;
@ -36,8 +37,8 @@ class Client {
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('scroll', (e) => this.hidePopup()); window.addEventListener('scroll', e => this.hidePopup());
window.addEventListener('resize', (e) => this.hidePopup()); window.addEventListener('resize', e => this.hidePopup());
} }
onKeyDown(e) { onKeyDown(e) {
@ -89,11 +90,16 @@ class Client {
return; return;
} }
if (this.pendingLookup) {
return;
}
textSource.setEndOffset(this.options.scanLength); textSource.setEndOffset(this.options.scanLength);
let defs = []; let defs = [];
let seq = -1; let seq = -1;
this.pendingLookup = true;
bgFindTerm(textSource.text()) bgFindTerm(textSource.text())
.then(({definitions, length}) => { .then(({definitions, length}) => {
if (length === 0) { if (length === 0) {
@ -103,7 +109,7 @@ class Client {
textSource.setEndOffset(length); textSource.setEndOffset(length);
const sentence = Client.extractSentence(textSource, this.options.sentenceExtent); const sentence = Client.extractSentence(textSource, this.options.sentenceExtent);
definitions.forEach((definition) => { definitions.forEach(definition => {
definition.url = window.location.href; definition.url = window.location.href;
definition.sentence = sentence; definition.sentence = sentence;
}); });
@ -113,17 +119,21 @@ class Client {
return bgRenderText({definitions, root: this.fgRoot, options: this.options, sequence: seq}, 'term-list.html'); return bgRenderText({definitions, root: this.fgRoot, options: this.options, sequence: seq}, 'term-list.html');
}) })
.then((content) => { .then(content => {
this.definitions = defs; this.definitions = defs;
this.showPopup(textSource, content); this.showPopup(textSource, content);
return bgCanAddDefinitions(defs, ['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: seq}));
} }
}, () => this.hidePopup()); }, () => {
this.pendingLookup = false;
this.hidePopup();
});
} }
showPopup(textSource, content) { showPopup(textSource, content) {
@ -159,7 +169,7 @@ class Client {
api_addNote({index, mode}) { api_addNote({index, mode}) {
const state = {[mode]: false}; const state = {[mode]: false};
bgAddDefinition(this.definitions[index], mode).then((success) => { bgAddDefinition(this.definitions[index], mode).then(success => {
if (success) { if (success) {
this.popup.sendMessage('setActionState', {index, state, sequence: this.sequence}); this.popup.sendMessage('setActionState', {index, state, sequence: this.sequence});
} else { } else {
@ -192,21 +202,21 @@ class Client {
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}));
} }
@ -217,7 +227,7 @@ class Client {
const element = document.elementFromPoint(point.x, point.y); const element = document.elementFromPoint(point.x, point.y);
if (element !== null) { if (element !== null) {
const names = ['IMG', 'INPUT', 'BUTTON', 'TEXTAREA']; const names = ['IMG', 'INPUT', 'BUTTON', 'TEXTAREA'];
if (names.indexOf(element.nodeName) !== -1) { if (names.includes(element.nodeName)) {
return new TextSourceElement(element); return new TextSourceElement(element);
} }
} }
@ -246,7 +256,7 @@ class Client {
for (let i = position; i >= startPos; --i) { for (let i = position; i >= startPos; --i) {
const c = content[i]; const c = content[i];
if (quoteStack.length === 0 && (terminators.indexOf(c) !== -1 || c in quotesFwd)) { if (quoteStack.length === 0 && (terminators.includes(c) || c in quotesFwd)) {
startPos = i + 1; startPos = i + 1;
break; break;
} }
@ -265,7 +275,7 @@ class Client {
const c = content[i]; const c = content[i];
if (quoteStack.length === 0) { if (quoteStack.length === 0) {
if (terminators.indexOf(c) !== -1) { if (terminators.includes(c)) {
endPos = i + 1; endPos = i + 1;
break; break;
} }