aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/anki-note-builder.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-09-09 11:54:40 -0400
committerGitHub <noreply@github.com>2020-09-09 11:54:40 -0400
commit2aa86cc5f8cda022076f7fa047f17fdcca4a0f5e (patch)
treed89d3776d46e76fc818b3ac4ba420d38c024002e /ext/bg/js/anki-note-builder.js
parentb687870a55eae43a71ea3adc41be0ab341a8721f (diff)
Anki API refactor (#790)
* Add function guiBrowseNote * Assign default server as null * Use get/set properties for server/enabled * Refactor option values * Refactor createNote/Data functions to not use options format directly * Use createNote for testing * Add errors
Diffstat (limited to 'ext/bg/js/anki-note-builder.js')
-rw-r--r--ext/bg/js/anki-note-builder.js51
1 files changed, 28 insertions, 23 deletions
diff --git a/ext/bg/js/anki-note-builder.js b/ext/bg/js/anki-note-builder.js
index d69a4fea..4afb2d40 100644
--- a/ext/bg/js/anki-note-builder.js
+++ b/ext/bg/js/anki-note-builder.js
@@ -27,41 +27,46 @@ class AnkiNoteBuilder {
this._getClipboardImage = getClipboardImage;
}
- async createNote(definition, mode, context, options, templates) {
- const isKanji = (mode === 'kanji');
- const tags = options.anki.tags;
- const modeOptions = isKanji ? options.anki.kanji : options.anki.terms;
- const modeOptionsFieldEntries = Object.entries(modeOptions.fields);
-
- const fields = {};
+ async createNote({
+ definition,
+ mode,
+ context,
+ templates,
+ tags=[],
+ duplicateScope='collection',
+ resultOutputMode='split',
+ compactGlossaries=false,
+ modeOptions: {fields, deck, model},
+ errors=null
+ }) {
+ const fieldEntries = Object.entries(fields);
+ const noteFields = {};
const note = {
- fields,
+ fields: noteFields,
tags,
- deckName: modeOptions.deck,
- modelName: modeOptions.model,
- options: {
- duplicateScope: options.anki.duplicateScope
- }
+ deckName: deck,
+ modelName: model,
+ options: {duplicateScope}
};
- const data = this.createNoteData(definition, mode, context, options);
+ const data = this.createNoteData(definition, mode, context, resultOutputMode, compactGlossaries);
const formattedFieldValuePromises = [];
- for (const [, fieldValue] of modeOptionsFieldEntries) {
- const formattedFieldValuePromise = this.formatField(fieldValue, data, templates, null);
+ for (const [, fieldValue] of fieldEntries) {
+ const formattedFieldValuePromise = this.formatField(fieldValue, data, templates, errors);
formattedFieldValuePromises.push(formattedFieldValuePromise);
}
const formattedFieldValues = await Promise.all(formattedFieldValuePromises);
- for (let i = 0, ii = modeOptionsFieldEntries.length; i < ii; ++i) {
- const fieldName = modeOptionsFieldEntries[i][0];
+ for (let i = 0, ii = fieldEntries.length; i < ii; ++i) {
+ const fieldName = fieldEntries[i][0];
const formattedFieldValue = formattedFieldValues[i];
- fields[fieldName] = formattedFieldValue;
+ noteFields[fieldName] = formattedFieldValue;
}
return note;
}
- createNoteData(definition, mode, context, options) {
+ createNoteData(definition, mode, context, resultOutputMode, compactGlossaries) {
const pitches = DictionaryDataUtil.getPitchAccentInfos(definition);
const pitchCount = pitches.reduce((i, v) => i + v.pitches.length, 0);
return {
@@ -69,12 +74,12 @@ class AnkiNoteBuilder {
definition,
pitches,
pitchCount,
- group: options.general.resultOutputMode === 'group',
- merge: options.general.resultOutputMode === 'merge',
+ group: resultOutputMode === 'group',
+ merge: resultOutputMode === 'merge',
modeTermKanji: mode === 'term-kanji',
modeTermKana: mode === 'term-kana',
modeKanji: mode === 'kanji',
- compactGlossaries: options.general.compactGlossaries,
+ compactGlossaries,
context
};
}