Pass AudioSystem instance to AnkiNoteBuilder constructor

This commit is contained in:
toasted-nutbread 2020-04-05 19:34:31 -04:00
parent 716ab99fc0
commit 8a419dfa67
2 changed files with 9 additions and 5 deletions

View File

@ -17,7 +17,8 @@
*/ */
class AnkiNoteBuilder { class AnkiNoteBuilder {
constructor({renderTemplate}) { constructor({audioSystem, renderTemplate}) {
this._audioSystem = audioSystem;
this._renderTemplate = renderTemplate; this._renderTemplate = renderTemplate;
} }
@ -84,14 +85,14 @@ class AnkiNoteBuilder {
}); });
} }
async injectAudio(definition, fields, sources, audioSystem, optionsContext) { async injectAudio(definition, fields, sources, optionsContext) {
if (!this._containsMarker(fields, 'audio')) { return; } if (!this._containsMarker(fields, 'audio')) { return; }
try { try {
const expressions = definition.expressions; const expressions = definition.expressions;
const audioSourceDefinition = Array.isArray(expressions) ? expressions[0] : definition; const audioSourceDefinition = Array.isArray(expressions) ? expressions[0] : definition;
const {uri} = await audioSystem.getDefinitionAudio(audioSourceDefinition, sources, {tts: false, optionsContext}); const {uri} = await this.audioSystem.getDefinitionAudio(audioSourceDefinition, sources, {tts: false, optionsContext});
const filename = this._createInjectedAudioFileName(audioSourceDefinition); const filename = this._createInjectedAudioFileName(audioSourceDefinition);
if (filename !== null) { if (filename !== null) {
definition.audio = {url: uri, filename}; definition.audio = {url: uri, filename};

View File

@ -51,12 +51,16 @@ class Backend {
this.anki = new AnkiNull(); this.anki = new AnkiNull();
this.mecab = new Mecab(); this.mecab = new Mecab();
this.clipboardMonitor = new ClipboardMonitor({getClipboard: this._onApiClipboardGet.bind(this)}); this.clipboardMonitor = new ClipboardMonitor({getClipboard: this._onApiClipboardGet.bind(this)});
this.ankiNoteBuilder = new AnkiNoteBuilder({renderTemplate: this._renderTemplate.bind(this)});
this.options = null; this.options = null;
this.optionsSchema = null; this.optionsSchema = null;
this.defaultAnkiFieldTemplates = null; this.defaultAnkiFieldTemplates = null;
this.audioSystem = new AudioSystem({getAudioUri: this._getAudioUri.bind(this)}); this.audioSystem = new AudioSystem({getAudioUri: this._getAudioUri.bind(this)});
this.audioUriBuilder = new AudioUriBuilder(); this.audioUriBuilder = new AudioUriBuilder();
this.ankiNoteBuilder = new AnkiNoteBuilder({
audioSystem: this.audioSystem,
renderTemplate: this._renderTemplate.bind(this)
});
this.optionsContext = { this.optionsContext = {
depth: 0, depth: 0,
url: window.location.href url: window.location.href
@ -464,7 +468,6 @@ class Backend {
definition, definition,
options.anki.terms.fields, options.anki.terms.fields,
options.audio.sources, options.audio.sources,
this.audioSystem,
optionsContext optionsContext
); );
} }