Update how definitions are searched for

This commit is contained in:
toasted-nutbread 2019-10-24 20:21:21 -04:00
parent f927f806ba
commit be27781c15

View File

@ -337,12 +337,18 @@ class Frontend {
} }
async searchSource(textSource, cause) { async searchSource(textSource, cause) {
let hideResults = false; let results = null;
try { try {
this.pendingLookup = true; this.pendingLookup = true;
const focus = (cause === 'mouse'); results = (
hideResults = !await this.searchTerms(textSource, focus) && !await this.searchKanji(textSource, focus); await this.findTerms(textSource) ||
await this.findKanji(textSource)
);
if (results !== null) {
const focus = (cause === 'mouse');
this.showContent(textSource, focus, results.definitions, results.type);
}
} catch (e) { } catch (e) {
if (window.yomichan_orphaned) { if (window.yomichan_orphaned) {
if (textSource && this.options.scanning.modifier !== 'none') { if (textSource && this.options.scanning.modifier !== 'none') {
@ -356,7 +362,7 @@ class Frontend {
this.onError(e); this.onError(e);
} }
} finally { } finally {
if (hideResults && this.options.scanning.autoHideResults) { if (results === null && this.options.scanning.autoHideResults) {
this.searchClear(true); this.searchClear(true);
} }
@ -365,66 +371,46 @@ class Frontend {
} }
} }
async searchTerms(textSource, focus) { showContent(textSource, focus, definitions, type) {
const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt);
const url = window.location.href;
this.lastShowPromise = this.popup.showContent(
textSource.getRect(),
textSource.getWritingMode(),
type,
{definitions, context: {sentence, url, focus}}
);
this.textSourceLast = textSource;
if (this.options.scanning.selectText) {
textSource.select();
}
}
async findTerms(textSource) {
this.setTextSourceScanLength(textSource, this.options.scanning.length); this.setTextSourceScanLength(textSource, this.options.scanning.length);
const searchText = textSource.text(); const searchText = textSource.text();
if (searchText.length === 0) { if (searchText.length === 0) { return null; }
return false;
}
const {definitions, length} = await apiTermsFind(searchText, this.getOptionsContext()); const {definitions, length} = await apiTermsFind(searchText, this.getOptionsContext());
if (definitions.length === 0) { if (definitions.length === 0) { return null; }
return false;
}
textSource.setEndOffset(length); textSource.setEndOffset(length);
const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt); return {definitions, type: 'terms'};
const url = window.location.href;
this.lastShowPromise = this.popup.showContent(
textSource.getRect(),
textSource.getWritingMode(),
'terms',
{definitions, context: {sentence, url, focus}}
);
this.textSourceLast = textSource;
if (this.options.scanning.selectText) {
textSource.select();
}
return true;
} }
async searchKanji(textSource, focus) { async findKanji(textSource) {
this.setTextSourceScanLength(textSource, 1); this.setTextSourceScanLength(textSource, 1);
const searchText = textSource.text(); const searchText = textSource.text();
if (searchText.length === 0) { if (searchText.length === 0) { return null; }
return false;
}
const definitions = await apiKanjiFind(searchText, this.getOptionsContext()); const definitions = await apiKanjiFind(searchText, this.getOptionsContext());
if (definitions.length === 0) { if (definitions.length === 0) { return null; }
return false;
}
const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt); return {definitions, type: 'kanji'};
const url = window.location.href;
this.lastShowPromise = this.popup.showContent(
textSource.getRect(),
textSource.getWritingMode(),
'kanji',
{definitions, context: {sentence, url, focus}}
);
this.textSourceLast = textSource;
if (this.options.scanning.selectText) {
textSource.select();
}
return true;
} }
searchClear(changeFocus) { searchClear(changeFocus) {