From 4e3f23e942252dacb31780de30f0233eccf1d9f8 Mon Sep 17 00:00:00 2001 From: Kuuuube <61125188+Kuuuube@users.noreply.github.com> Date: Wed, 26 Jun 2024 17:49:24 -0400 Subject: Attempt to fallback on kanji or terms dict when no results are found in the other (#1141) * Attempt to fallback on kanji or terms dict when no results are found in the other * Move logic to _findDictionaryEntries and split out _getFindDetails * Fix not falling back both ways properly * Add source to findDetails * Simplify return * Search fallback kanji without wildcard * Avoid double searching kanji * Remove weird newline * make symmetric * Simplify flow --------- Co-authored-by: Stefan Vukovic --- ext/js/display/display.js | 51 +++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 17 deletions(-) (limited to 'ext') diff --git a/ext/js/display/display.js b/ext/js/display/display.js index b5dd4ba6..f86d7b8c 100644 --- a/ext/js/display/display.js +++ b/ext/js/display/display.js @@ -1215,28 +1215,45 @@ export class Display extends EventDispatcher { * @returns {Promise} */ async _findDictionaryEntries(isKanji, source, wildcardsEnabled, optionsContext) { + /** @type {import('dictionary').DictionaryEntry[]} */ + let dictionaryEntries = []; + const {findDetails, source: source2} = this._getFindDetails(source, wildcardsEnabled); if (isKanji) { - return await this._application.api.kanjiFind(source, optionsContext); + dictionaryEntries = await this._application.api.kanjiFind(source, optionsContext); + if (dictionaryEntries.length > 0) { return dictionaryEntries; } + + dictionaryEntries = (await this._application.api.termsFind(source2, findDetails, optionsContext)).dictionaryEntries; } else { - /** @type {import('api').FindTermsDetails} */ - const findDetails = {}; - if (wildcardsEnabled) { - const match = /^([*\uff0a]*)([\w\W]*?)([*\uff0a]*)$/.exec(source); - if (match !== null) { - if (match[1]) { - findDetails.matchType = 'suffix'; - findDetails.deinflect = false; - } else if (match[3]) { - findDetails.matchType = 'prefix'; - findDetails.deinflect = false; - } - source = match[2]; + dictionaryEntries = (await this._application.api.termsFind(source2, findDetails, optionsContext)).dictionaryEntries; + if (dictionaryEntries.length > 0) { return dictionaryEntries; } + + dictionaryEntries = await this._application.api.kanjiFind(source, optionsContext); + } + return dictionaryEntries; + } + + /** + * @param {string} source + * @param {boolean} wildcardsEnabled + * @returns {{findDetails: import('api').FindTermsDetails, source: string}} + */ + _getFindDetails(source, wildcardsEnabled) { + /** @type {import('api').FindTermsDetails} */ + const findDetails = {}; + if (wildcardsEnabled) { + const match = /^([*\uff0a]*)([\w\W]*?)([*\uff0a]*)$/.exec(source); + if (match !== null) { + if (match[1]) { + findDetails.matchType = 'suffix'; + findDetails.deinflect = false; + } else if (match[3]) { + findDetails.matchType = 'prefix'; + findDetails.deinflect = false; } + source = match[2]; } - - const {dictionaryEntries} = await this._application.api.termsFind(source, findDetails, optionsContext); - return dictionaryEntries; } + return {findDetails, source}; } /** -- cgit v1.2.3