aboutsummaryrefslogtreecommitdiff
path: root/ext/js/language/text-scanner.js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/js/language/text-scanner.js')
-rw-r--r--ext/js/language/text-scanner.js93
1 files changed, 40 insertions, 53 deletions
diff --git a/ext/js/language/text-scanner.js b/ext/js/language/text-scanner.js
index d78c4c74..5b125063 100644
--- a/ext/js/language/text-scanner.js
+++ b/ext/js/language/text-scanner.js
@@ -395,11 +395,10 @@ export class TextScanner extends EventDispatcher {
/**
* @param {import('text-source').TextSource} textSource
* @param {import('text-scanner').InputInfoDetail} [inputDetail]
- * @returns {Promise<?import('text-scanner').SearchedEventDetails>}
*/
async search(textSource, inputDetail) {
const inputInfo = this._createInputInfo(null, 'script', 'script', true, [], [], inputDetail);
- return await this._search(textSource, this._searchTerms, this._searchKanji, inputInfo);
+ await this._search(textSource, this._searchTerms, this._searchKanji, inputInfo);
}
// Private
@@ -422,23 +421,8 @@ export class TextScanner extends EventDispatcher {
* @param {boolean} searchTerms
* @param {boolean} searchKanji
* @param {import('text-scanner').InputInfo} inputInfo
- * @returns {Promise<?import('text-scanner').SearchedEventDetails>}
*/
async _search(textSource, searchTerms, searchKanji, inputInfo) {
- /** @type {?import('dictionary').DictionaryEntry[]} */
- let dictionaryEntries = null;
- /** @type {?import('display').HistoryStateSentence} */
- let sentence = null;
- /** @type {?import('display').PageType} */
- let type = null;
- /** @type {?Error} */
- let error = null;
- let searched = false;
- /** @type {?import('settings').OptionsContext} */
- let optionsContext = null;
- /** @type {?import('text-scanner').SearchResultDetail} */
- let detail = null;
-
try {
const inputInfoDetail = inputInfo.detail;
const selectionRestoreInfo = (
@@ -448,56 +432,59 @@ export class TextScanner extends EventDispatcher {
);
if (this._textSourceCurrent !== null && this._textSourceCurrent.hasSameStart(textSource)) {
- return null;
+ return;
}
const getSearchContextPromise = this._getSearchContext();
const getSearchContextResult = getSearchContextPromise instanceof Promise ? await getSearchContextPromise : getSearchContextPromise;
- const {detail: detail2} = getSearchContextResult;
- if (typeof detail2 !== 'undefined') { detail = detail2; }
- optionsContext = this._createOptionsContextForInput(getSearchContextResult.optionsContext, inputInfo);
-
- searched = true;
-
- let valid = false;
+ const {detail} = getSearchContextResult;
+ const optionsContext = this._createOptionsContextForInput(getSearchContextResult.optionsContext, inputInfo);
+
+ /** @type {?import('dictionary').DictionaryEntry[]} */
+ let dictionaryEntries = null;
+ /** @type {?import('display').HistoryStateSentence} */
+ let sentence = null;
+ /** @type {'terms'|'kanji'} */
+ let type = 'terms';
const result = await this._findDictionaryEntries(textSource, searchTerms, searchKanji, optionsContext);
if (result !== null) {
({dictionaryEntries, sentence, type} = result);
- valid = true;
} else if (textSource !== null && textSource instanceof TextSourceElement && await this._hasJapanese(textSource.fullContent)) {
dictionaryEntries = [];
sentence = {text: '', offset: 0};
- type = 'terms';
- valid = true;
}
- if (valid) {
+ if (dictionaryEntries !== null && sentence !== null) {
this._inputInfoCurrent = inputInfo;
this.setCurrentTextSource(textSource);
- if (typeof selectionRestoreInfo !== 'undefined') {
- this._selectionRestoreInfo = selectionRestoreInfo;
- }
+ this._selectionRestoreInfo = selectionRestoreInfo;
+
+ this.trigger('searchSuccess', {
+ type,
+ dictionaryEntries,
+ sentence,
+ inputInfo,
+ textSource,
+ optionsContext,
+ detail
+ });
+ } else {
+ this._triggerSearchEmpty(inputInfo);
}
- } catch (e) {
- error = e instanceof Error ? e : new Error(`A search error occurred: ${e}`);
+ } catch (error) {
+ this.trigger('searchError', {
+ error: error instanceof Error ? error : new Error(`A search error occurred: ${error}`),
+ textSource,
+ inputInfo
+ });
}
+ }
- if (!searched) { return null; }
-
- /** @type {import('text-scanner').SearchedEventDetails} */
- const results = {
- textScanner: this,
- type,
- dictionaryEntries,
- sentence,
- inputInfo,
- textSource,
- optionsContext,
- detail,
- error
- };
- this.trigger('searched', results);
- return results;
+ /**
+ * @param {import('text-scanner').InputInfo} inputInfo
+ */
+ _triggerSearchEmpty(inputInfo) {
+ this.trigger('searchEmpty', {inputInfo});
}
/** */
@@ -1287,10 +1274,10 @@ export class TextScanner extends EventDispatcher {
try {
await this._search(textSource, searchTerms, searchKanji, inputInfo);
} finally {
- if (textSource !== null) {
- textSource.cleanup();
- }
+ textSource.cleanup();
}
+ } else {
+ this._triggerSearchEmpty(inputInfo);
}
} catch (e) {
log.error(e);