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
This commit is contained in:
toasted-nutbread 2020-09-09 11:54:40 -04:00 committed by GitHub
parent b687870a55
commit 2aa86cc5f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 80 additions and 40 deletions

View File

@ -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
};
}

View File

@ -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');

View File

@ -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
});
}
}

View File

@ -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);