aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/backend.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-09-09 16:57:35 -0400
committerGitHub <noreply@github.com>2020-09-09 16:57:35 -0400
commitacb7ad32f39c40b879400c9daa4bc8cd25585ba7 (patch)
tree4b40ca7ccf64d2d685aafa8e0d637ffff604e64a /ext/bg/js/backend.js
parentc0a6849f98d433e8dbcaa8c2cb0e995174399d3c (diff)
Anki media injection move (#793)
* Update AnkiNoteBuilder to not store a reference to an AniConnect instance * Use more consistent details format * Organize options assignment * Move media injection * Inject images before injecting audio * Make functions private * Make static functions private
Diffstat (limited to 'ext/bg/js/backend.js')
-rw-r--r--ext/bg/js/backend.js49
1 files changed, 16 insertions, 33 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index 47e68072..df979c6f 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -58,7 +58,6 @@ class Backend {
useCache: false
});
this._ankiNoteBuilder = new AnkiNoteBuilder({
- anki: this._anki,
audioSystem: this._audioSystem,
renderTemplate: this._renderTemplate.bind(this),
getClipboardImage: this._onApiClipboardImageGet.bind(this),
@@ -446,33 +445,8 @@ class Backend {
async _onApiDefinitionAdd({definition, mode, context, ownerFrameId, optionsContext}, sender) {
const options = this.getOptions(optionsContext);
const templates = this._getTemplates(options);
- const fields = (
- mode === 'kanji' ?
- options.anki.kanji.fields :
- options.anki.terms.fields
- );
-
- if (mode !== 'kanji') {
- const {customSourceUrl} = options.audio;
- await this._ankiNoteBuilder.injectAudio(
- definition,
- fields,
- options.audio.sources,
- customSourceUrl
- );
- }
-
- await this._ankiNoteBuilder.injectClipboardImage(definition, fields);
-
const {id: tabId, windowId} = (sender && sender.tab ? sender.tab : {});
- const {format, quality} = options.anki.screenshot;
- await this._ankiNoteBuilder.injectScreenshot(
- definition,
- fields,
- {windowId, tabId, ownerFrameId, format, quality}
- );
-
- const note = await this._createNote(definition, mode, context, options, templates);
+ const note = await this._createNote(definition, mode, context, options, templates, true, {windowId, tabId, ownerFrameId});
return this._anki.addNote(note);
}
@@ -485,7 +459,7 @@ class Backend {
const notePromises = [];
for (const definition of definitions) {
for (const mode of modes) {
- const notePromise = this._createNote(definition, mode, context, options, templates);
+ const notePromise = this._createNote(definition, mode, context, options, templates, false, null);
notePromises.push(notePromise);
}
}
@@ -1611,11 +1585,17 @@ class Backend {
});
}
- 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;
+ async _createNote(definition, mode, context, options, templates, injectMedia, screenshotTarget) {
+ const {
+ general: {resultOutputMode, compactGlossaries},
+ anki: {tags, duplicateScope, kanji, terms, screenshot: {format, quality}},
+ audio: {sources, customSourceUrl}
+ } = options;
+ const modeOptions = (mode === 'kanji') ? kanji : terms;
+ const {windowId, tabId, ownerFrameId} = (isObject(screenshotTarget) ? screenshotTarget : {});
+
return await this._ankiNoteBuilder.createNote({
+ anki: injectMedia ? this._anki : null,
definition,
mode,
context,
@@ -1624,7 +1604,10 @@ class Backend {
duplicateScope,
resultOutputMode,
compactGlossaries,
- modeOptions
+ modeOptions,
+ audioDetails: {sources, customSourceUrl},
+ screenshotDetails: {windowId, tabId, ownerFrameId, format, quality},
+ clipboardImage: true
});
}