Add custom context object for note creation

This commit is contained in:
toasted-nutbread 2020-03-15 17:13:00 -04:00
parent 8b07a23de9
commit 059db280bb
5 changed files with 21 additions and 15 deletions

View File

@ -21,7 +21,7 @@ class AnkiNoteBuilder {
this._renderTemplate = renderTemplate;
}
async createNote(definition, mode, options, templates) {
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;
@ -35,7 +35,7 @@ class AnkiNoteBuilder {
};
for (const [fieldName, fieldValue] of modeOptionsFieldEntries) {
note.fields[fieldName] = await this.formatField(fieldValue, definition, mode, options, templates, null);
note.fields[fieldName] = await this.formatField(fieldValue, definition, mode, context, options, templates, null);
}
if (!isKanji && definition.audio) {
@ -60,7 +60,7 @@ class AnkiNoteBuilder {
return note;
}
async formatField(field, definition, mode, options, templates, errors=null) {
async formatField(field, definition, mode, context, options, templates, errors=null) {
const data = {
marker: null,
definition,
@ -69,7 +69,8 @@ class AnkiNoteBuilder {
modeTermKanji: mode === 'term-kanji',
modeTermKana: mode === 'term-kana',
modeKanji: mode === 'kanji',
compactGlossaries: options.general.compactGlossaries
compactGlossaries: options.general.compactGlossaries,
context
};
const pattern = /\{([\w-]+)\}/g;
return await AnkiNoteBuilder.stringReplaceAsync(field, pattern, async (g0, marker) => {

View File

@ -455,7 +455,7 @@ class Backend {
return results;
}
async _onApiDefinitionAdd({definition, mode, details, optionsContext}) {
async _onApiDefinitionAdd({definition, mode, context, details, optionsContext}) {
const options = this.getOptions(optionsContext);
const templates = this.defaultAnkiFieldTemplates;
@ -476,11 +476,11 @@ class Backend {
);
}
const note = await this.ankiNoteBuilder.createNote(definition, mode, options, templates);
const note = await this.ankiNoteBuilder.createNote(definition, mode, context, options, templates);
return this.anki.addNote(note);
}
async _onApiDefinitionsAddable({definitions, modes, optionsContext}) {
async _onApiDefinitionsAddable({definitions, modes, context, optionsContext}) {
const options = this.getOptions(optionsContext);
const templates = this.defaultAnkiFieldTemplates;
const states = [];
@ -489,7 +489,7 @@ class Backend {
const notes = [];
for (const definition of definitions) {
for (const mode of modes) {
const note = await this.ankiNoteBuilder.createNote(definition, mode, options, templates);
const note = await this.ankiNoteBuilder.createNote(definition, mode, context, options, templates);
notes.push(note);
}
}

View File

@ -99,10 +99,11 @@ async function ankiTemplatesValidate(infoNode, field, mode, showSuccessResult, i
const definition = await ankiTemplatesValidateGetDefinition(text, optionsContext);
if (definition !== null) {
const options = await apiOptionsGet(optionsContext);
const context = {};
let templates = options.anki.fieldTemplates;
if (typeof templates !== 'string') { templates = await apiGetDefaultAnkiFieldTemplates(); }
const ankiNoteBuilder = new AnkiNoteBuilder({renderTemplate: apiTemplateRender});
result = await ankiNoteBuilder.formatField(field, definition, mode, options, templates, exceptions);
result = await ankiNoteBuilder.formatField(field, definition, mode, context, options, templates, exceptions);
}
} catch (e) {
exceptions.push(e);

View File

@ -53,12 +53,12 @@ function apiKanjiFind(text, optionsContext) {
return _apiInvoke('kanjiFind', {text, optionsContext});
}
function apiDefinitionAdd(definition, mode, details, optionsContext) {
return _apiInvoke('definitionAdd', {definition, mode, details, optionsContext});
function apiDefinitionAdd(definition, mode, context, details, optionsContext) {
return _apiInvoke('definitionAdd', {definition, mode, context, details, optionsContext});
}
function apiDefinitionsAddable(definitions, modes, optionsContext) {
return _apiInvoke('definitionsAddable', {definitions, modes, optionsContext});
function apiDefinitionsAddable(definitions, modes, context, optionsContext) {
return _apiInvoke('definitionsAddable', {definitions, modes, context, optionsContext});
}
function apiNoteView(noteId) {

View File

@ -760,7 +760,7 @@ class Display {
}
}
const noteId = await apiDefinitionAdd(definition, mode, details, this.getOptionsContext());
const noteId = await apiDefinitionAdd(definition, mode, this._getNoteContext(), details, this.getOptionsContext());
if (noteId) {
const index = this.definitions.indexOf(definition);
const adderButton = this.adderButtonFind(index, mode);
@ -908,7 +908,7 @@ class Display {
async getDefinitionsAddable(definitions, modes) {
try {
return await apiDefinitionsAddable(definitions, modes, this.getOptionsContext());
return await apiDefinitionsAddable(definitions, modes, this._getNoteContext(), this.getOptionsContext());
} catch (e) {
return [];
}
@ -934,6 +934,10 @@ class Display {
return (typeof key === 'string' ? (key.length === 1 ? key.toUpperCase() : key) : '');
}
_getNoteContext() {
return {};
}
async _getAudioUri(definition, source) {
const optionsContext = this.getOptionsContext();
return await apiAudioGetUri(definition, source, optionsContext);