diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2024-01-28 07:22:47 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-28 12:22:47 +0000 |
commit | acc013a1a8051d34322f0f5f91d7bdecc0a18843 (patch) | |
tree | 601344b2f047f395548ddfb16a83319af10464f9 /ext/js/display/query-parser.js | |
parent | a51f1ab2dc675a49bfeeb08cc24b97eb8d888e4a (diff) |
JapaneseUtil refactor (#555)
* Copy functions from JapaneseUtil
* Remove JapaneseUtil
* Update usages of JapaneseUtil functions
Diffstat (limited to 'ext/js/display/query-parser.js')
-rw-r--r-- | ext/js/display/query-parser.js | 40 |
1 files changed, 30 insertions, 10 deletions
diff --git a/ext/js/display/query-parser.js b/ext/js/display/query-parser.js index e129e1be..eb053f38 100644 --- a/ext/js/display/query-parser.js +++ b/ext/js/display/query-parser.js @@ -19,6 +19,7 @@ import {EventDispatcher} from '../core/event-dispatcher.js'; import {log} from '../core/logger.js'; import {querySelectorNotNull} from '../dom/query-selector.js'; +import {convertHiraganaToKatakana, convertKatakanaToHiragana, isStringEntirelyKana} from '../language/japanese.js'; import {TextScanner} from '../language/text-scanner.js'; import {yomitan} from '../yomitan.js'; @@ -29,12 +30,10 @@ export class QueryParser extends EventDispatcher { /** * @param {import('display').QueryParserConstructorDetails} details */ - constructor({getSearchContext, japaneseUtil}) { + constructor({getSearchContext}) { super(); /** @type {import('display').GetSearchContextCallback} */ this._getSearchContext = getSearchContext; - /** @type {import('../language/sandbox/japanese-util.js').JapaneseUtil} */ - this._japaneseUtil = japaneseUtil; /** @type {string} */ this._text = ''; /** @type {?import('core').TokenObject} */ @@ -65,6 +64,10 @@ export class QueryParser extends EventDispatcher { searchKanji: false, searchOnClick: true }); + /** @type {?(import('../language/japanese-wanakana.js'))} */ + this._japaneseWanakanaModule = null; + /** @type {?Promise<import('../language/japanese-wanakana.js')>} */ + this._japaneseWanakanaModuleImport = null; } /** @type {string} */ @@ -93,7 +96,7 @@ export class QueryParser extends EventDispatcher { this._queryParser.dataset.termSpacing = `${termSpacing}`; } if (typeof readingMode === 'string') { - this._readingMode = readingMode; + this._setReadingMode(readingMode); } if (typeof useInternalParser === 'boolean') { this._useInternalParser = useInternalParser; @@ -346,15 +349,15 @@ export class QueryParser extends EventDispatcher { _convertReading(term, reading) { switch (this._readingMode) { case 'hiragana': - return this._japaneseUtil.convertKatakanaToHiragana(reading); + return convertKatakanaToHiragana(reading); case 'katakana': - return this._japaneseUtil.convertHiraganaToKatakana(reading); + return convertHiraganaToKatakana(reading); case 'romaji': - if (this._japaneseUtil.convertToRomajiSupported()) { + if (this._japaneseWanakanaModule !== null) { if (reading.length > 0) { - return this._japaneseUtil.convertToRomaji(reading); - } else if (this._japaneseUtil.isStringEntirelyKana(term)) { - return this._japaneseUtil.convertToRomaji(term); + return this._japaneseWanakanaModule.convertToRomaji(reading); + } else if (isStringEntirelyKana(term)) { + return this._japaneseWanakanaModule.convertToRomaji(term); } } return reading; @@ -398,4 +401,21 @@ export class QueryParser extends EventDispatcher { node = node.parentNode; } } + + /** + * @param {import('settings').ParsingReadingMode} value + */ + _setReadingMode(value) { + this._readingMode = value; + if (value === 'romaji') { + this._loadJapaneseWanakanaModule(); + } + } + + /** */ + _loadJapaneseWanakanaModule() { + if (this._japaneseWanakanaModuleImport !== null) { return; } + this._japaneseWanakanaModuleImport = import('../language/japanese-wanakana.js'); + this._japaneseWanakanaModuleImport.then((value) => { this._japaneseWanakanaModule = value; }); + } } |