From 0d167095479822adf1ed8918e3d1a349b3a53377 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 9 Jul 2021 17:31:16 -0400 Subject: JapaneseUtil updates (#1813) * Rename test file * Use shorthand * Add support checking functions * Remove convertReading from JapaneseUtil --- ext/js/background/backend.js | 2 +- ext/js/display/query-parser.js | 26 +++++++++++++++++++++++--- ext/js/language/japanese-util.js | 29 ++++++++++------------------- 3 files changed, 34 insertions(+), 23 deletions(-) (limited to 'ext/js') diff --git a/ext/js/background/backend.js b/ext/js/background/backend.js index c797bbf5..af847610 100644 --- a/ext/js/background/backend.js +++ b/ext/js/background/backend.js @@ -1066,7 +1066,7 @@ class Backend { if ( dictionaryEntries.length > 0 && originalTextLength > 0 && - (originalTextLength !== character.length || this._japaneseUtil.isCodePointJapanese(codePoint)) + (originalTextLength !== character.length || jp.isCodePointJapanese(codePoint)) ) { previousUngroupedSegment = null; const {headwords: [{term, reading}]} = dictionaryEntries[0]; diff --git a/ext/js/display/query-parser.js b/ext/js/display/query-parser.js index 0acf6ec8..cbcf7cff 100644 --- a/ext/js/display/query-parser.js +++ b/ext/js/display/query-parser.js @@ -208,8 +208,6 @@ class QueryParser extends EventDispatcher { } _createParseResult(data) { - const jp = this._japaneseUtil; - const readingMode = this._readingMode; const fragment = document.createDocumentFragment(); for (const term of data) { const termNode = document.createElement('span'); @@ -218,7 +216,7 @@ class QueryParser extends EventDispatcher { if (reading.length === 0) { termNode.appendChild(document.createTextNode(text)); } else { - const reading2 = jp.convertReading(text, reading, readingMode); + const reading2 = this._convertReading(text, reading); termNode.appendChild(this._createSegment(text, reading2)); } } @@ -245,4 +243,26 @@ class QueryParser extends EventDispatcher { return segmentNode; } + + _convertReading(term, reading) { + switch (this._readingMode) { + case 'hiragana': + return this._japaneseUtil.convertKatakanaToHiragana(reading); + case 'katakana': + return this._japaneseUtil.convertHiraganaToKatakana(reading); + case 'romaji': + if (this._japaneseUtil.convertToRomajiSupported()) { + if (reading.length > 0) { + return this._japaneseUtil.convertToRomaji(reading); + } else if (this._japaneseUtil.isStringEntirelyKana(term)) { + return this._japaneseUtil.convertToRomaji(term); + } + } + return reading; + case 'none': + return ''; + default: + return reading; + } + } } diff --git a/ext/js/language/japanese-util.js b/ext/js/language/japanese-util.js index 7ee726b2..9d7ad0d8 100644 --- a/ext/js/language/japanese-util.js +++ b/ext/js/language/japanese-util.js @@ -277,6 +277,10 @@ const JapaneseUtil = (() => { return this._getWanakana().toKana(text); } + convertToKanaSupported() { + return this._wanakana !== null; + } + convertKatakanaToHiragana(text) { let result = ''; const offset = (HIRAGANA_CONVERSION_RANGE[0] - KATAKANA_CONVERSION_RANGE[0]); @@ -315,25 +319,8 @@ const JapaneseUtil = (() => { return wanakana.toRomaji(text); } - convertReading(term, reading, readingMode) { - switch (readingMode) { - case 'hiragana': - return this.convertKatakanaToHiragana(reading); - case 'katakana': - return this.convertHiraganaToKatakana(reading); - case 'romaji': - if (reading.length > 0) { - return this.convertToRomaji(reading); - } else if (this.isStringEntirelyKana(term)) { - return this.convertToRomaji(term); - } else { - return reading; - } - case 'none': - return ''; - default: - return reading; - } + convertToRomajiSupported() { + return this._wanakana !== null; } convertNumericToFullWidth(text) { @@ -426,6 +413,10 @@ const JapaneseUtil = (() => { return result; } + convertAlphabeticToKanaSupported() { + return this._wanakana !== null; + } + // Furigana distribution distributeFurigana(term, reading) { -- cgit v1.2.3