Pop-up dictionary browser extension for language learning. Successor to Yomichan. (PERSONAL FORK)
1/*
2 * Copyright (C) 2023-2025 Yomitan Authors
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18import type * as Dictionary from './dictionary';
19import type * as DictionaryData from './dictionary-data';
20import type * as DictionaryImporter from './dictionary-importer';
21
22export type DatabaseId = {
23 id: number; // Automatic database primary key
24};
25
26export type MediaDataBase<TContentType = unknown> = {
27 dictionary: string;
28 path: string;
29 mediaType: string;
30 width: number;
31 height: number;
32 content: TContentType;
33};
34
35export type MediaDataArrayBufferContent = MediaDataBase<ArrayBuffer>;
36
37export type MediaDataStringContent = MediaDataBase<string>;
38
39type MediaType = ArrayBuffer | string | null;
40
41export type Media<T extends MediaType = ArrayBuffer> = {index: number} & MediaDataBase<T>;
42
43export type DrawMedia<T extends MediaType = ArrayBuffer> = {index: number} & MediaDataBase<T> & {canvasWidth: number, canvasHeight: number, canvasIndexes: number[], generation: number};
44
45export type DatabaseTermEntry = {
46 expression: string;
47 reading: string;
48 expressionReverse?: string;
49 readingReverse?: string;
50 definitionTags: string | null;
51 /** Legacy alias for the `definitionTags` field. */
52 tags?: string;
53 rules: string;
54 score: number;
55 glossary: DictionaryData.TermGlossary[];
56 sequence?: number;
57 termTags?: string;
58 dictionary: string;
59};
60
61export type DatabaseTermEntryWithId = DatabaseTermEntry & DatabaseId;
62
63export type TermEntry = {
64 index: number;
65 matchType: MatchType;
66 matchSource: MatchSource;
67 term: string;
68 reading: string;
69 definitionTags: string[];
70 termTags: string[];
71 rules: string[];
72 definitions: DictionaryData.TermGlossary[];
73 score: number;
74 dictionary: string;
75 id: number;
76 sequence: number;
77};
78
79export type DatabaseKanjiEntry = {
80 character: string;
81 onyomi: string;
82 kunyomi: string;
83 tags: string;
84 meanings: string[];
85 dictionary: string;
86 stats?: {[name: string]: string};
87};
88
89export type KanjiEntry = {
90 index: number;
91 character: string;
92 onyomi: string[];
93 kunyomi: string[];
94 tags: string[];
95 definitions: string[];
96 stats: {[name: string]: string};
97 dictionary: string;
98};
99
100export type Tag = {
101 name: string;
102 category: string;
103 order: number;
104 notes: string;
105 score: number;
106 dictionary: string;
107};
108
109export type DatabaseTermMeta = DatabaseTermMetaFrequency | DatabaseTermMetaPitch | DatabaseTermMetaPhoneticData;
110
111export type DatabaseTermMetaFrequency = {
112 expression: string;
113 mode: 'freq';
114 data: DictionaryData.GenericFrequencyData | DictionaryData.TermMetaFrequencyDataWithReading;
115 dictionary: string;
116};
117
118export type DatabaseTermMetaPitch = {
119 expression: string;
120 mode: 'pitch';
121 data: DictionaryData.TermMetaPitchData;
122 dictionary: string;
123};
124
125export type DatabaseTermMetaPhoneticData = {
126 expression: string;
127 mode: 'ipa';
128 data: DictionaryData.TermMetaPhoneticData;
129 dictionary: string;
130};
131
132export type TermMetaFrequencyDataWithReading = {
133 reading: string;
134 frequency: DictionaryData.GenericFrequencyData;
135};
136
137export type TermMeta = TermMetaFrequency | TermMetaPitch | TermMetaPhoneticData;
138
139export type TermMetaType = TermMeta['mode'];
140
141export type TermMetaFrequency = {
142 index: number;
143 term: string;
144 mode: 'freq';
145 data: DictionaryData.GenericFrequencyData | DictionaryData.TermMetaFrequencyDataWithReading;
146 dictionary: string;
147};
148
149export type TermMetaPitch = {
150 mode: 'pitch';
151 index: number;
152 term: string;
153 data: DictionaryData.TermMetaPitchData;
154 dictionary: string;
155};
156
157export type TermMetaPhoneticData = {
158 mode: 'ipa';
159 index: number;
160 term: string;
161 data: DictionaryData.TermMetaPhoneticData;
162 dictionary: string;
163};
164
165export type DatabaseKanjiMeta = DatabaseKanjiMetaFrequency;
166
167export type DatabaseKanjiMetaFrequency = {
168 character: string;
169 mode: 'freq';
170 data: DictionaryData.GenericFrequencyData;
171 dictionary: string;
172};
173
174export type KanjiMeta = KanjiMetaFrequency;
175
176export type KanjiMetaType = KanjiMeta['mode'];
177
178export type KanjiMetaFrequency = {
179 index: number;
180 character: string;
181 mode: 'freq';
182 data: DictionaryData.GenericFrequencyData;
183 dictionary: string;
184};
185
186export type DictionaryCounts = {
187 total: DictionaryCountGroup | null;
188 counts: DictionaryCountGroup[];
189};
190
191export type DictionaryCountGroup = {
192 [key: string]: number;
193};
194
195export type ObjectStoreName = (
196 'dictionaries' |
197 'terms' |
198 'termMeta' |
199 'kanji' |
200 'kanjiMeta' |
201 'tagMeta' |
202 'media'
203);
204
205export type ObjectStoreData<T extends ObjectStoreName> = (
206 T extends 'dictionaries' ? DictionaryImporter.Summary :
207 T extends 'terms' ? DatabaseTermEntry :
208 T extends 'termMeta' ? DatabaseTermMeta :
209 T extends 'kanji' ? DatabaseKanjiEntry :
210 T extends 'kanjiMeta' ? DatabaseKanjiMeta :
211 T extends 'tagMeta' ? Tag :
212 T extends 'media' ? MediaDataArrayBufferContent :
213 never
214);
215
216export type DatabaseUpdateItem = {
217 primaryKey: IDBValidKey;
218 data: ObjectStoreData<T>;
219};
220
221export type DeleteDictionaryProgressData = {
222 count: number;
223 processed: number;
224 storeCount: number;
225 storesProcesed: number;
226};
227
228export type DeleteDictionaryProgressCallback = (data: DeleteDictionaryProgressData) => void;
229
230export type MatchType = Dictionary.TermSourceMatchType;
231
232export type MatchSource = Dictionary.TermSourceMatchSource;
233
234export type DictionaryAndQueryRequest = {
235 query: string | number;
236 dictionary: string;
237};
238
239export type TermExactRequest = {
240 term: string;
241 reading: string;
242};
243
244export type MediaRequest = {
245 path: string;
246 dictionary: string;
247};
248
249export type DrawMediaRequest = {
250 path: string;
251 dictionary: string;
252 canvasIndex: number;
253 canvasWidth: number;
254 canvasHeight: number;
255 generation: number;
256};
257
258export type DrawMediaGroupedRequest = {
259 path: string;
260 dictionary: string;
261 canvasIndexes: number[];
262 canvasWidth: number;
263 canvasHeight: number;
264 generation: number;
265};
266
267export type FindMultiBulkData<TItem = unknown> = {
268 item: TItem;
269 itemIndex: number;
270 indexIndex: number;
271};
272
273export type CreateQuery<TItem = unknown> = (item: TItem) => (IDBValidKey | IDBKeyRange | null);
274
275export type FindPredicate<TItem = unknown, TRow = unknown> = (row: TRow, item: TItem) => boolean;
276
277export type CreateResult<TItem = unknown, TRow = unknown, TResult = unknown> = (row: TRow, data: FindMultiBulkData<TItem>) => TResult;
278
279export type DictionarySet = {
280 has(value: string): boolean;
281};
282
283/** API for communicating with its own worker */
284
285import type {
286 ApiMap as BaseApiMap,
287 ApiMapInit as BaseApiMapInit,
288 ApiHandler as BaseApiHandler,
289 ApiParams as BaseApiParams,
290 ApiReturn as BaseApiReturn,
291 ApiNames as BaseApiNames,
292 ApiParam as BaseApiParam,
293 ApiParamNames as BaseApiParamNames,
294 ApiParamsAny as BaseApiParamsAny,
295} from './api-map';
296
297type ApiSurface = {
298 drawMedia: {
299 params: {
300 requests: DrawMediaRequest[];
301 };
302 return: void;
303 };
304 dummy: {
305 params: void;
306 return: void;
307 };
308};
309
310type ApiExtraArgs = [port: MessagePort];
311
312export type ApiNames = BaseApiNames<ApiSurface>;
313
314export type ApiMap = BaseApiMap<ApiSurface, ApiExtraArgs>;
315
316export type ApiMapInit = BaseApiMapInit<ApiSurface, ApiExtraArgs>;
317
318export type ApiHandler<TName extends ApiNames> = BaseApiHandler<ApiSurface[TName], ApiExtraArgs>;
319
320export type ApiHandlerNoExtraArgs<TName extends ApiNames> = BaseApiHandler<ApiSurface[TName], []>;
321
322export type ApiParams<TName extends ApiNames> = BaseApiParams<ApiSurface[TName]>;
323
324export type ApiParam<TName extends ApiNames, TParamName extends BaseApiParamNames<ApiSurface[TName]>> = BaseApiParam<ApiSurface[TName], TParamName>;
325
326export type ApiReturn<TName extends ApiNames> = BaseApiReturn<ApiSurface[TName]>;
327
328export type ApiParamsAny = BaseApiParamsAny<ApiSurface>;
329
330export type ApiMessageAny = {[name in ApiNames]: ApiMessage<name>}[ApiNames];
331
332type ApiMessage<TName extends ApiNames> = {
333 action: TName;
334 params: ApiParams<TName>;
335};