Refactor anki note generation (#1202)

* Create _injectAnkiNoteMedia function

* Remove unused code path

* Simplify modeOptions
This commit is contained in:
toasted-nutbread 2021-01-03 19:40:12 -05:00 committed by GitHub
parent 63971776a5
commit 8c92c1cbc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 48 deletions

View File

@ -26,42 +26,35 @@ class AnkiNoteBuilder {
}
async createNote({
anki=null,
definition,
mode,
context,
templates,
deckName,
modelName,
fields,
tags=[],
checkForDuplicates=true,
duplicateScope='collection',
resultOutputMode='split',
glossaryLayoutMode='default',
compactTags=false,
modeOptions: {fields, deck, model},
audioDetails=null,
screenshotDetails=null,
clipboardDetails=null,
errors=null
}) {
if (anki !== null) {
await this._injectMedia(anki, definition, fields, mode, audioDetails, screenshotDetails, clipboardDetails);
}
let duplicateScopeDeckName = null;
let duplicateScopeCheckChildren = false;
if (duplicateScope === 'deck-root') {
duplicateScope = 'deck';
duplicateScopeDeckName = this.getRootDeckName(deck);
duplicateScopeDeckName = this.getRootDeckName(deckName);
duplicateScopeCheckChildren = true;
}
const fieldEntries = Object.entries(fields);
const noteFields = {};
const note = {
fields: noteFields,
tags,
deckName: deck,
modelName: model,
deckName,
modelName,
options: {
allowDuplicate: !checkForDuplicates,
duplicateScope,
@ -74,14 +67,14 @@ class AnkiNoteBuilder {
const data = this._createNoteData(definition, mode, context, resultOutputMode, glossaryLayoutMode, compactTags);
const formattedFieldValuePromises = [];
for (const [, fieldValue] of fieldEntries) {
for (const [, fieldValue] of fields) {
const formattedFieldValuePromise = this._formatField(fieldValue, data, templates, errors);
formattedFieldValuePromises.push(formattedFieldValuePromise);
}
const formattedFieldValues = await Promise.all(formattedFieldValuePromises);
for (let i = 0, ii = fieldEntries.length; i < ii; ++i) {
const fieldName = fieldEntries[i][0];
for (let i = 0, ii = fields.length; i < ii; ++i) {
const fieldName = fields[i][0];
const formattedFieldValue = formattedFieldValues[i];
noteFields[fieldName] = formattedFieldValue;
}
@ -91,7 +84,7 @@ class AnkiNoteBuilder {
containsMarker(fields, marker) {
marker = `{${marker}}`;
for (const fieldValue of Object.values(fields)) {
for (const [, fieldValue] of fields) {
if (fieldValue.includes(marker)) {
return true;
}

View File

@ -189,14 +189,14 @@ class AnkiTemplatesController {
mode,
context,
templates,
deckName: '',
modelName: '',
fields: [
['field', field]
],
resultOutputMode,
glossaryLayoutMode,
compactTags,
modeOptions: {
fields: {field},
deck: '',
model: ''
},
errors: exceptions
});
result = note.fields.field;

View File

@ -1545,33 +1545,15 @@ class Display extends EventDispatcher {
async _createNote(definition, mode, context, options, templates, injectMedia) {
const {
general: {resultOutputMode, glossaryLayoutMode, compactTags},
anki: {tags, checkForDuplicates, duplicateScope, kanji, terms, screenshot: {format, quality}},
audio: {sources, customSourceUrl}
anki: ankiOptions
} = options;
const modeOptions = (mode === 'kanji') ? kanji : terms;
const {tags, checkForDuplicates, duplicateScope} = ankiOptions;
const modeOptions = (mode === 'kanji') ? ankiOptions.kanji : ankiOptions.terms;
const {deck: deckName, model: modelName} = modeOptions;
const fields = Object.entries(modeOptions.fields);
if (injectMedia) {
const timestamp = Date.now();
const ownerFrameId = this._ownerFrameId;
const {fields} = modeOptions;
const definitionDetails = this._getDefinitionDetailsForNote(definition);
const audioDetails = (mode !== 'kanji' && this._ankiNoteBuilder.containsMarker(fields, 'audio') ? {sources, customSourceUrl} : null);
const screenshotDetails = (this._ankiNoteBuilder.containsMarker(fields, 'screenshot') ? {ownerFrameId, format, quality} : null);
const clipboardDetails = {
image: this._ankiNoteBuilder.containsMarker(fields, 'clipboard-image'),
text: this._ankiNoteBuilder.containsMarker(fields, 'clipboard-text')
};
const {screenshotFileName, clipboardImageFileName, clipboardText, audioFileName} = await api.injectAnkiNoteMedia(
timestamp,
definitionDetails,
audioDetails,
screenshotDetails,
clipboardDetails
);
if (screenshotFileName !== null) { definition.screenshotFileName = screenshotFileName; }
if (clipboardImageFileName !== null) { definition.clipboardImageFileName = clipboardImageFileName; }
if (audioFileName !== null) { definition.audioFileName = audioFileName; }
if (clipboardText !== null) { definition.clipboardText = clipboardText; }
await this._injectAnkiNoteMedia(definition, mode, options, fields);
}
return await this._ankiNoteBuilder.createNote({
@ -1579,16 +1561,46 @@ class Display extends EventDispatcher {
mode,
context,
templates,
deckName,
modelName,
fields,
tags,
checkForDuplicates,
duplicateScope,
resultOutputMode,
glossaryLayoutMode,
compactTags,
modeOptions
compactTags
});
}
async _injectAnkiNoteMedia(definition, mode, options, fields) {
const {
anki: {screenshot: {format, quality}},
audio: {sources, customSourceUrl}
} = options;
const timestamp = Date.now();
const ownerFrameId = this._ownerFrameId;
const definitionDetails = this._getDefinitionDetailsForNote(definition);
const audioDetails = (mode !== 'kanji' && this._ankiNoteBuilder.containsMarker(fields, 'audio') ? {sources, customSourceUrl} : null);
const screenshotDetails = (this._ankiNoteBuilder.containsMarker(fields, 'screenshot') ? {ownerFrameId, format, quality} : null);
const clipboardDetails = {
image: this._ankiNoteBuilder.containsMarker(fields, 'clipboard-image'),
text: this._ankiNoteBuilder.containsMarker(fields, 'clipboard-text')
};
const {screenshotFileName, clipboardImageFileName, clipboardText, audioFileName} = await api.injectAnkiNoteMedia(
timestamp,
definitionDetails,
audioDetails,
screenshotDetails,
clipboardDetails
);
if (screenshotFileName !== null) { definition.screenshotFileName = screenshotFileName; }
if (clipboardImageFileName !== null) { definition.clipboardImageFileName = clipboardImageFileName; }
if (audioFileName !== null) { definition.audioFileName = audioFileName; }
if (clipboardText !== null) { definition.clipboardText = clipboardText; }
}
_getDefinitionDetailsForNote(definition) {
const {type} = definition;
if (type === 'kanji') {