diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-03-07 17:55:51 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-07 17:55:51 -0500 |
commit | 19f6bf5a3a669cda311e5a3f162131d8a9823aef (patch) | |
tree | b9e57be07764a782fa914a1faa877b2670d0e850 /ext/js/data | |
parent | 7793e14e57639426cb9ca64c82004151f9eaba05 (diff) |
Refactor furigana segment data (#1502)
* Remove redundant language assignment
* Segment furigana from DisplayGenerator
* Remove furiganaSegments from translator data
* Add backwards compatibility for furiganaSegments
* Update tests
Diffstat (limited to 'ext/js/data')
-rw-r--r-- | ext/js/data/anki-note-data.js | 48 |
1 files changed, 46 insertions, 2 deletions
diff --git a/ext/js/data/anki-note-data.js b/ext/js/data/anki-note-data.js index a7d0f9f6..529bad18 100644 --- a/ext/js/data/anki-note-data.js +++ b/ext/js/data/anki-note-data.js @@ -24,7 +24,7 @@ * The public properties and data should be backwards compatible. */ class AnkiNoteData { - constructor({ + constructor(japaneseUtil, marker, { definition, resultOutputMode, mode, @@ -32,7 +32,8 @@ class AnkiNoteData { compactTags, context, injectedMedia=null - }, marker) { + }) { + this._japaneseUtil = japaneseUtil; this._definition = definition; this._resultOutputMode = resultOutputMode; this._mode = mode; @@ -47,6 +48,7 @@ class AnkiNoteData { this._uniqueReadings = null; this._publicContext = null; this._cloze = null; + this._furiganaSegmentsCache = null; this._prepareDefinition(definition, injectedMedia, context); } @@ -236,5 +238,47 @@ class AnkiNoteData { enumerable: true, get: this._getClozeCached.bind(this) }); + + for (const definition2 of this._getAllDefinitions(definition)) { + if (definition2.type === 'term') { + this._defineFuriganaSegments(definition2); + } + for (const expression of definition2.expressions) { + this._defineFuriganaSegments(expression); + } + } + } + + _defineFuriganaSegments(object) { + Object.defineProperty(object, 'furiganaSegments', { + configurable: true, + enumerable: true, + get: this._getFuriganaSegments.bind(this, object) + }); + } + + _getFuriganaSegments(object) { + if (this._furiganaSegmentsCache !== null) { + const cachedResult = this._furiganaSegmentsCache.get(object); + if (typeof cachedResult !== 'undefined') { return cachedResult; } + } else { + this._furiganaSegmentsCache = new Map(); + } + + const {expression, reading} = object; + const result = this._japaneseUtil.distributeFurigana(expression, reading); + this._furiganaSegmentsCache.set(object, result); + return result; + } + + _getAllDefinitions(definition) { + const definitions = [definition]; + for (let i = 0; i < definitions.length; ++i) { + const childDefinitions = definitions[i].definitions; + if (Array.isArray(childDefinitions)) { + definitions.push(...childDefinitions); + } + } + return definitions; } } |