Refactor anki note generation (#1202)
* Create _injectAnkiNoteMedia function * Remove unused code path * Simplify modeOptions
This commit is contained in:
parent
63971776a5
commit
8c92c1cbc2
@ -26,42 +26,35 @@ class AnkiNoteBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async createNote({
|
async createNote({
|
||||||
anki=null,
|
|
||||||
definition,
|
definition,
|
||||||
mode,
|
mode,
|
||||||
context,
|
context,
|
||||||
templates,
|
templates,
|
||||||
|
deckName,
|
||||||
|
modelName,
|
||||||
|
fields,
|
||||||
tags=[],
|
tags=[],
|
||||||
checkForDuplicates=true,
|
checkForDuplicates=true,
|
||||||
duplicateScope='collection',
|
duplicateScope='collection',
|
||||||
resultOutputMode='split',
|
resultOutputMode='split',
|
||||||
glossaryLayoutMode='default',
|
glossaryLayoutMode='default',
|
||||||
compactTags=false,
|
compactTags=false,
|
||||||
modeOptions: {fields, deck, model},
|
|
||||||
audioDetails=null,
|
|
||||||
screenshotDetails=null,
|
|
||||||
clipboardDetails=null,
|
|
||||||
errors=null
|
errors=null
|
||||||
}) {
|
}) {
|
||||||
if (anki !== null) {
|
|
||||||
await this._injectMedia(anki, definition, fields, mode, audioDetails, screenshotDetails, clipboardDetails);
|
|
||||||
}
|
|
||||||
|
|
||||||
let duplicateScopeDeckName = null;
|
let duplicateScopeDeckName = null;
|
||||||
let duplicateScopeCheckChildren = false;
|
let duplicateScopeCheckChildren = false;
|
||||||
if (duplicateScope === 'deck-root') {
|
if (duplicateScope === 'deck-root') {
|
||||||
duplicateScope = 'deck';
|
duplicateScope = 'deck';
|
||||||
duplicateScopeDeckName = this.getRootDeckName(deck);
|
duplicateScopeDeckName = this.getRootDeckName(deckName);
|
||||||
duplicateScopeCheckChildren = true;
|
duplicateScopeCheckChildren = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const fieldEntries = Object.entries(fields);
|
|
||||||
const noteFields = {};
|
const noteFields = {};
|
||||||
const note = {
|
const note = {
|
||||||
fields: noteFields,
|
fields: noteFields,
|
||||||
tags,
|
tags,
|
||||||
deckName: deck,
|
deckName,
|
||||||
modelName: model,
|
modelName,
|
||||||
options: {
|
options: {
|
||||||
allowDuplicate: !checkForDuplicates,
|
allowDuplicate: !checkForDuplicates,
|
||||||
duplicateScope,
|
duplicateScope,
|
||||||
@ -74,14 +67,14 @@ class AnkiNoteBuilder {
|
|||||||
|
|
||||||
const data = this._createNoteData(definition, mode, context, resultOutputMode, glossaryLayoutMode, compactTags);
|
const data = this._createNoteData(definition, mode, context, resultOutputMode, glossaryLayoutMode, compactTags);
|
||||||
const formattedFieldValuePromises = [];
|
const formattedFieldValuePromises = [];
|
||||||
for (const [, fieldValue] of fieldEntries) {
|
for (const [, fieldValue] of fields) {
|
||||||
const formattedFieldValuePromise = this._formatField(fieldValue, data, templates, errors);
|
const formattedFieldValuePromise = this._formatField(fieldValue, data, templates, errors);
|
||||||
formattedFieldValuePromises.push(formattedFieldValuePromise);
|
formattedFieldValuePromises.push(formattedFieldValuePromise);
|
||||||
}
|
}
|
||||||
|
|
||||||
const formattedFieldValues = await Promise.all(formattedFieldValuePromises);
|
const formattedFieldValues = await Promise.all(formattedFieldValuePromises);
|
||||||
for (let i = 0, ii = fieldEntries.length; i < ii; ++i) {
|
for (let i = 0, ii = fields.length; i < ii; ++i) {
|
||||||
const fieldName = fieldEntries[i][0];
|
const fieldName = fields[i][0];
|
||||||
const formattedFieldValue = formattedFieldValues[i];
|
const formattedFieldValue = formattedFieldValues[i];
|
||||||
noteFields[fieldName] = formattedFieldValue;
|
noteFields[fieldName] = formattedFieldValue;
|
||||||
}
|
}
|
||||||
@ -91,7 +84,7 @@ class AnkiNoteBuilder {
|
|||||||
|
|
||||||
containsMarker(fields, marker) {
|
containsMarker(fields, marker) {
|
||||||
marker = `{${marker}}`;
|
marker = `{${marker}}`;
|
||||||
for (const fieldValue of Object.values(fields)) {
|
for (const [, fieldValue] of fields) {
|
||||||
if (fieldValue.includes(marker)) {
|
if (fieldValue.includes(marker)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -189,14 +189,14 @@ class AnkiTemplatesController {
|
|||||||
mode,
|
mode,
|
||||||
context,
|
context,
|
||||||
templates,
|
templates,
|
||||||
|
deckName: '',
|
||||||
|
modelName: '',
|
||||||
|
fields: [
|
||||||
|
['field', field]
|
||||||
|
],
|
||||||
resultOutputMode,
|
resultOutputMode,
|
||||||
glossaryLayoutMode,
|
glossaryLayoutMode,
|
||||||
compactTags,
|
compactTags,
|
||||||
modeOptions: {
|
|
||||||
fields: {field},
|
|
||||||
deck: '',
|
|
||||||
model: ''
|
|
||||||
},
|
|
||||||
errors: exceptions
|
errors: exceptions
|
||||||
});
|
});
|
||||||
result = note.fields.field;
|
result = note.fields.field;
|
||||||
|
@ -1545,15 +1545,42 @@ class Display extends EventDispatcher {
|
|||||||
async _createNote(definition, mode, context, options, templates, injectMedia) {
|
async _createNote(definition, mode, context, options, templates, injectMedia) {
|
||||||
const {
|
const {
|
||||||
general: {resultOutputMode, glossaryLayoutMode, compactTags},
|
general: {resultOutputMode, glossaryLayoutMode, compactTags},
|
||||||
anki: {tags, checkForDuplicates, duplicateScope, kanji, terms, screenshot: {format, quality}},
|
anki: ankiOptions
|
||||||
audio: {sources, customSourceUrl}
|
|
||||||
} = options;
|
} = 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) {
|
if (injectMedia) {
|
||||||
|
await this._injectAnkiNoteMedia(definition, mode, options, fields);
|
||||||
|
}
|
||||||
|
|
||||||
|
return await this._ankiNoteBuilder.createNote({
|
||||||
|
definition,
|
||||||
|
mode,
|
||||||
|
context,
|
||||||
|
templates,
|
||||||
|
deckName,
|
||||||
|
modelName,
|
||||||
|
fields,
|
||||||
|
tags,
|
||||||
|
checkForDuplicates,
|
||||||
|
duplicateScope,
|
||||||
|
resultOutputMode,
|
||||||
|
glossaryLayoutMode,
|
||||||
|
compactTags
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async _injectAnkiNoteMedia(definition, mode, options, fields) {
|
||||||
|
const {
|
||||||
|
anki: {screenshot: {format, quality}},
|
||||||
|
audio: {sources, customSourceUrl}
|
||||||
|
} = options;
|
||||||
|
|
||||||
const timestamp = Date.now();
|
const timestamp = Date.now();
|
||||||
const ownerFrameId = this._ownerFrameId;
|
const ownerFrameId = this._ownerFrameId;
|
||||||
const {fields} = modeOptions;
|
|
||||||
const definitionDetails = this._getDefinitionDetailsForNote(definition);
|
const definitionDetails = this._getDefinitionDetailsForNote(definition);
|
||||||
const audioDetails = (mode !== 'kanji' && this._ankiNoteBuilder.containsMarker(fields, 'audio') ? {sources, customSourceUrl} : null);
|
const audioDetails = (mode !== 'kanji' && this._ankiNoteBuilder.containsMarker(fields, 'audio') ? {sources, customSourceUrl} : null);
|
||||||
const screenshotDetails = (this._ankiNoteBuilder.containsMarker(fields, 'screenshot') ? {ownerFrameId, format, quality} : null);
|
const screenshotDetails = (this._ankiNoteBuilder.containsMarker(fields, 'screenshot') ? {ownerFrameId, format, quality} : null);
|
||||||
@ -1574,21 +1601,6 @@ class Display extends EventDispatcher {
|
|||||||
if (clipboardText !== null) { definition.clipboardText = clipboardText; }
|
if (clipboardText !== null) { definition.clipboardText = clipboardText; }
|
||||||
}
|
}
|
||||||
|
|
||||||
return await this._ankiNoteBuilder.createNote({
|
|
||||||
definition,
|
|
||||||
mode,
|
|
||||||
context,
|
|
||||||
templates,
|
|
||||||
tags,
|
|
||||||
checkForDuplicates,
|
|
||||||
duplicateScope,
|
|
||||||
resultOutputMode,
|
|
||||||
glossaryLayoutMode,
|
|
||||||
compactTags,
|
|
||||||
modeOptions
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
_getDefinitionDetailsForNote(definition) {
|
_getDefinitionDetailsForNote(definition) {
|
||||||
const {type} = definition;
|
const {type} = definition;
|
||||||
if (type === 'kanji') {
|
if (type === 'kanji') {
|
||||||
|
Loading…
Reference in New Issue
Block a user