aboutsummaryrefslogtreecommitdiff
path: root/ext/mixed/js/display.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-01-03 19:40:12 -0500
committerGitHub <noreply@github.com>2021-01-03 19:40:12 -0500
commit8c92c1cbc246001e27a1e31e53c2ce992bf72fbd (patch)
treeb92f99cccde03485f07669d172c85b8c78085d3e /ext/mixed/js/display.js
parent63971776a5db6ad0d3a68361f64a74aa9e78aff5 (diff)
Refactor anki note generation (#1202)
* Create _injectAnkiNoteMedia function * Remove unused code path * Simplify modeOptions
Diffstat (limited to 'ext/mixed/js/display.js')
-rw-r--r--ext/mixed/js/display.js64
1 files changed, 38 insertions, 26 deletions
diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js
index a3b70764..c36a0c1c 100644
--- a/ext/mixed/js/display.js
+++ b/ext/mixed/js/display.js
@@ -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') {