aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/bg/js')
-rw-r--r--ext/bg/js/anki-note-builder.js51
-rw-r--r--ext/bg/js/anki.js24
-rw-r--r--ext/bg/js/backend.js27
-rw-r--r--ext/bg/js/settings/anki-templates.js18
4 files changed, 80 insertions, 40 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
};
}
diff --git a/ext/bg/js/anki.js b/ext/bg/js/anki.js
index b1e1f7b4..182b4ded 100644
--- a/ext/bg/js/anki.js
+++ b/ext/bg/js/anki.js
@@ -16,30 +16,30 @@
*/
class AnkiConnect {
- constructor(server) {
+ constructor() {
this._enabled = false;
- this._server = server;
+ this._server = null;
this._localVersion = 2;
this._remoteVersion = 0;
this._versionCheckPromise = null;
}
- setServer(server) {
- this._server = server;
- }
-
- getServer() {
+ get server() {
return this._server;
}
- setEnabled(enabled) {
- this._enabled = enabled;
+ set server(value) {
+ this._server = value;
}
- isEnabled() {
+ get enabled() {
return this._enabled;
}
+ set enabled(value) {
+ this._enabled = value;
+ }
+
async addNote(note) {
if (!this._enabled) { return null; }
await this._checkVersion();
@@ -76,6 +76,10 @@ class AnkiConnect {
return await this._invoke('guiBrowse', {query});
}
+ async guiBrowseNote(noteId) {
+ return await this.guiBrowse(`nid:${noteId}`);
+ }
+
async storeMediaFile(fileName, dataBase64) {
if (!this._enabled) {
throw new Error('AnkiConnect not enabled');
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index e9f3fbec..832dbc3a 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -471,7 +471,7 @@ class Backend {
);
}
- const note = await this._ankiNoteBuilder.createNote(definition, mode, context, options, templates);
+ const note = await this._createNote(definition, mode, context, options, templates);
return this._anki.addNote(note);
}
@@ -484,7 +484,7 @@ class Backend {
const notePromises = [];
for (const definition of definitions) {
for (const mode of modes) {
- const notePromise = this._ankiNoteBuilder.createNote(definition, mode, context, options, templates);
+ const notePromise = this._createNote(definition, mode, context, options, templates);
notePromises.push(notePromise);
}
}
@@ -524,7 +524,7 @@ class Backend {
}
async _onApiNoteView({noteId}) {
- return await this._anki.guiBrowse(`nid:${noteId}`);
+ return await this._anki.guiBrowseNote(noteId);
}
async _onApiTemplateRender({template, data, marker}) {
@@ -1023,8 +1023,8 @@ class Backend {
const options = this.getOptions({current: true});
this._updateBadge();
- this._anki.setServer(options.anki.server);
- this._anki.setEnabled(options.anki.enable);
+ this._anki.server = options.anki.server;
+ this._anki.enabled = options.anki.enable;
if (options.parsing.enableMecabParser) {
this._mecab.startListener();
@@ -1609,4 +1609,21 @@ class Backend {
reader.readAsDataURL(file);
});
}
+
+ async _createNote(definition, mode, context, options, templates) {
+ const {general: {resultOutputMode, compactGlossaries}, anki: ankiOptions} = options;
+ const {tags, duplicateScope} = ankiOptions;
+ const modeOptions = (mode === 'kanji') ? ankiOptions.kanji : ankiOptions.terms;
+ return await this._ankiNoteBuilder.createNote({
+ definition,
+ mode,
+ context,
+ templates,
+ tags,
+ duplicateScope,
+ resultOutputMode,
+ compactGlossaries,
+ modeOptions
+ });
+ }
}
diff --git a/ext/bg/js/settings/anki-templates.js b/ext/bg/js/settings/anki-templates.js
index fb03ef14..87f13100 100644
--- a/ext/bg/js/settings/anki-templates.js
+++ b/ext/bg/js/settings/anki-templates.js
@@ -147,8 +147,22 @@ class AnkiTemplatesController {
renderTemplate: api.templateRender.bind(api),
getClipboardImage: api.clipboardGetImage.bind(api)
});
- const data = ankiNoteBuilder.createNoteData(definition, mode, context, options);
- result = await ankiNoteBuilder.formatField(field, data, templates, exceptions);
+ const {general: {resultOutputMode, compactGlossaries}} = options;
+ const note = await ankiNoteBuilder.createNote({
+ definition,
+ mode,
+ context,
+ templates,
+ resultOutputMode,
+ compactGlossaries,
+ modeOptions: {
+ fields: {field},
+ deck: '',
+ model: ''
+ },
+ errors: exceptions
+ });
+ result = note.fields.field;
}
} catch (e) {
exceptions.push(e);