aboutsummaryrefslogtreecommitdiff
path: root/ext/js/data/anki-note-data.js
blob: 529bad184a607182e0bb51706de9ae66f6211e81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * Copyright (C) 2021  Yomichan Authors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

/* global
 * DictionaryDataUtil
 */

/**
 * This class represents the data that is exposed to the Anki template renderer.
 * The public properties and data should be backwards compatible.
 */
class AnkiNoteData {
    constructor(japaneseUtil, marker, {
        definition,
        resultOutputMode,
        mode,
        glossaryLayoutMode,
        compactTags,
        context,
        injectedMedia=null
    }) {
        this._japaneseUtil = japaneseUtil;
        this._definition = definition;
        this._resultOutputMode = resultOutputMode;
        this._mode = mode;
        this._glossaryLayoutMode = glossaryLayoutMode;
        this._compactTags = compactTags;
        this._context = context;
        this._marker = marker;
        this._injectedMedia = injectedMedia;
        this._pitches = null;
        this._pitchCount = null;
        this._uniqueExpressions = null;
        this._uniqueReadings = null;
        this._publicContext = null;
        this._cloze = null;
        this._furiganaSegmentsCache = null;

        this._prepareDefinition(definition, injectedMedia, context);
    }

    get marker() {
        return this._marker;
    }

    set marker(value) {
        this._marker = value;
    }

    get definition() {
        return this._definition;
    }

    get uniqueExpressions() {
        if (this._uniqueExpressions === null) {
            this._uniqueExpressions = this._getUniqueExpressions();
        }
        return this._uniqueExpressions;
    }

    get uniqueReadings() {
        if (this._uniqueReadings === null) {
            this._uniqueReadings = this._getUniqueReadings();
        }
        return this._uniqueReadings;
    }

    get pitches() {
        if (this._pitches === null) {
            this._pitches = DictionaryDataUtil.getPitchAccentInfos(this._definition);
        }
        return this._pitches;
    }

    get pitchCount() {
        if (this._pitchCount === null) {
            this._pitchCount = this.pitches.reduce((i, v) => i + v.pitches.length, 0);
        }
        return this._pitchCount;
    }

    get group() {
        return this._resultOutputMode === 'group';
    }

    get merge() {
        return this._resultOutputMode === 'merge';
    }

    get modeTermKanji() {
        return this._mode === 'term-kanji';
    }

    get modeTermKana() {
        return this._mode === 'term-kana';
    }

    get modeKanji() {
        return this._mode === 'kanji';
    }

    get compactGlossaries() {
        return this._glossaryLayoutMode === 'compact';
    }

    get glossaryLayoutMode() {
        return this._glossaryLayoutMode;
    }

    get compactTags() {
        return this._compactTags;
    }

    get context() {
        if (this._publicContext === null) {
            this._publicContext = this._getPublicContext();
        }
        return this._publicContext;
    }

    createPublic() {
        const self = this;
        return {
            get marker() { return self.marker; },
            set marker(value) { self.marker = value; },
            get definition() { return self.definition; },
            get glossaryLayoutMode() { return self.glossaryLayoutMode; },
            get compactTags() { return self.compactTags; },
            get group() { return self.group; },
            get merge() { return self.merge; },
            get modeTermKanji() { return self.modeTermKanji; },
            get modeTermKana() { return self.modeTermKana; },
            get modeKanji() { return self.modeKanji; },
            get compactGlossaries() { return self.compactGlossaries; },
            get uniqueExpressions() { return self.uniqueExpressions; },
            get uniqueReadings() { return self.uniqueReadings; },
            get pitches() { return self.pitches; },
            get pitchCount() { return self.pitchCount; },
            get context() { return self.context; }
        };
    }

    // Private

    _asObject(value) {
        return (typeof value === 'object' && value !== null ? value : {});
    }

    _getUniqueExpressions() {
        const results = new Set();
        const definition = this._definition;
        if (definition.type !== 'kanji') {
            for (const {expression} of definition.expressions) {
                results.add(expression);
            }
        }
        return [...results];
    }

    _getUniqueReadings() {
        const results = new Set();
        const definition = this._definition;
        if (definition.type !== 'kanji') {
            for (const {reading} of definition.expressions) {
                results.add(reading);
            }
        }
        return [...results];
    }

    _getPublicContext() {
        let {documentTitle} = this._asObject(this._context);
        if (typeof documentTitle !== 'string') { documentTitle = ''; }

        return {
            document: {
                title: documentTitle
            }
        };
    }

    _getCloze() {
        const {sentence} = this._asObject(this._context);
        let {text, offset} = this._asObject(sentence);
        if (typeof text !== 'string') { text = ''; }
        if (typeof offset !== 'number') { offset = 0; }

        const definition = this._definition;
        const source = definition.type === 'kanji' ? definition.character : definition.rawSource;

        return {
            sentence: text,
            prefix: text.substring(0, offset),
            body: text.substring(offset, offset + source.length),
            suffix: text.substring(offset + source.length)
        };
    }

    _getClozeCached() {
        if (this._cloze === null) {
            this._cloze = this._getCloze();
        }
        return this._cloze;
    }

    _prepareDefinition(definition, injectedMedia, context) {
        const {
            screenshotFileName=null,
            clipboardImageFileName=null,
            clipboardText=null,
            audioFileName=null
        } = this._asObject(injectedMedia);

        let {url} = this._asObject(context);
        if (typeof url !== 'string') { url = ''; }

        definition.screenshotFileName = screenshotFileName;
        definition.clipboardImageFileName = clipboardImageFileName;
        definition.clipboardText = clipboardText;
        definition.audioFileName = audioFileName;
        definition.url = url;
        Object.defineProperty(definition, 'cloze', {
            configurable: true,
            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;
    }
}