From 34451ebf7122a2a5d140696215411abfb0dfe188 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 29 Nov 2020 13:09:02 -0500 Subject: JapaneseUtil normalization (#1076) * Use JapaneseUtil as a class which is manually instantiated * Use alias function for toKana --- ext/mixed/js/display-generator.js | 7 +++++-- ext/mixed/js/display.js | 12 ++++++++++-- ext/mixed/js/japanese.js | 21 +++++++-------------- 3 files changed, 22 insertions(+), 18 deletions(-) (limited to 'ext/mixed/js') diff --git a/ext/mixed/js/display-generator.js b/ext/mixed/js/display-generator.js index 63140330..82f6199b 100644 --- a/ext/mixed/js/display-generator.js +++ b/ext/mixed/js/display-generator.js @@ -19,11 +19,11 @@ * DictionaryDataUtil * HtmlTemplateCollection * api - * jp */ class DisplayGenerator { - constructor({mediaLoader}) { + constructor({japaneseUtil, mediaLoader}) { + this._japaneseUtil = japaneseUtil; this._mediaLoader = mediaLoader; this._templates = null; this._termPitchAccentStaticTemplateIsSetup = false; @@ -354,6 +354,7 @@ class DisplayGenerator { } _createPitch(details) { + const jp = this._japaneseUtil; const {reading, position, tags, exclusiveExpressions, exclusiveReadings} = details; const morae = jp.getKanaMorae(reading); @@ -417,6 +418,7 @@ class DisplayGenerator { } _populatePitchGraph(svg, position, morae) { + const jp = this._japaneseUtil; const svgns = svg.getAttribute('xmlns'); const ii = morae.length; svg.setAttribute('viewBox', `0 0 ${50 * (ii + 1)} 100`); @@ -475,6 +477,7 @@ class DisplayGenerator { } _appendKanjiLinks(container, text) { + const jp = this._japaneseUtil; let part = ''; for (const c of text) { if (jp.isCodePointKanji(c.codePointAt(0))) { diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index f3aead44..b9daa9a0 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -34,9 +34,10 @@ */ class Display extends EventDispatcher { - constructor(pageType) { + constructor(pageType, japaneseUtil) { super(); this._pageType = pageType; + this._japaneseUtil = japaneseUtil; this._container = document.querySelector('#definitions'); this._definitions = []; this._optionsContext = {depth: 0, url: window.location.href}; @@ -53,7 +54,10 @@ class Display extends EventDispatcher { this._autoPlayAudioTimer = null; this._autoPlayAudioDelay = 400; this._mediaLoader = new MediaLoader(); - this._displayGenerator = new DisplayGenerator({mediaLoader: this._mediaLoader}); + this._displayGenerator = new DisplayGenerator({ + japaneseUtil, + mediaLoader: this._mediaLoader + }); this._hotkeys = new Map(); this._actions = new Map(); this._messageHandlers = new Map(); @@ -177,6 +181,10 @@ class Display extends EventDispatcher { return this._mode; } + get japaneseUtil() { + return this._japaneseUtil; + } + async prepare() { // State setup const {documentElement} = document; diff --git a/ext/mixed/js/japanese.js b/ext/mixed/js/japanese.js index 2177ceee..4bd0dc65 100644 --- a/ext/mixed/js/japanese.js +++ b/ext/mixed/js/japanese.js @@ -15,7 +15,7 @@ * along with this program. If not, see . */ -const jp = (() => { +const JapaneseUtil = (() => { const ITERATION_MARK_CODE_POINT = 0x3005; const HIRAGANA_SMALL_TSU_CODE_POINT = 0x3063; const KATAKANA_SMALL_TSU_CODE_POINT = 0x30c3; @@ -179,19 +179,8 @@ const jp = (() => { } } - function getWanakana() { - try { - if (typeof wanakana !== 'undefined') { - // eslint-disable-next-line no-undef - return wanakana; - } - } catch (e) { - // NOP - } - return null; - } - + // eslint-disable-next-line no-shadow class JapaneseUtil { constructor(wanakana=null) { this._wanakana = wanakana; @@ -258,6 +247,10 @@ const jp = (() => { // Conversion functions + convertToKana(text) { + return this._getWanakana().toKana(text); + } + convertKatakanaToHiragana(text) { let result = ''; const offset = (HIRAGANA_CONVERSION_RANGE[0] - KATAKANA_CONVERSION_RANGE[0]); @@ -591,5 +584,5 @@ const jp = (() => { } - return new JapaneseUtil(getWanakana()); + return JapaneseUtil; })(); -- cgit v1.2.3