diff options
author | Kuuuube <61125188+Kuuuube@users.noreply.github.com> | 2024-06-26 17:49:24 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-26 21:49:24 +0000 |
commit | 4e3f23e942252dacb31780de30f0233eccf1d9f8 (patch) | |
tree | 7afeffd98758141d46eeb2f7cbffb96750a3f55f /ext | |
parent | 1343c59475661a3cf94662090b55019a78ff12b8 (diff) |
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 <stefanvukovic44@gmail.com>
Diffstat (limited to 'ext')
-rw-r--r-- | ext/js/display/display.js | 51 |
1 files changed, 34 insertions, 17 deletions
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<import('dictionary').DictionaryEntry[]>} */ 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}; } /** |