From 20d062329bfcdc0d7c5fd07f48071ae6524f9566 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 25 Mar 2017 10:46:59 -0700 Subject: more audio handling improvements --- ext/bg/background.html | 1 + ext/bg/js/yomichan.js | 35 +++++++++++++---------------------- ext/mixed/js/display.js | 23 ++++++----------------- ext/mixed/js/util.js | 31 ++++++++++++++++++++++++++++++- 4 files changed, 50 insertions(+), 40 deletions(-) (limited to 'ext') diff --git a/ext/bg/background.html b/ext/bg/background.html index 8fb41a38..b5ae147b 100644 --- a/ext/bg/background.html +++ b/ext/bg/background.html @@ -7,6 +7,7 @@ + diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index 3a42c594..50a8a636 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -56,7 +56,10 @@ window.yomichan = new class { } noteFormat(definition, mode) { - const note = {fields: {}, tags: this.options.anki.tags}; + const note = { + fields: {}, + tags: this.options.anki.tags + }; let fields = []; if (mode === 'kanji') { @@ -67,25 +70,6 @@ window.yomichan = new class { fields = this.options.anki.terms.fields; note.deckName = this.options.anki.terms.deck; note.modelName = this.options.anki.terms.model; - - if (definition.audio) { - const audio = { - url: definition.audio.url, - filename: definition.audio.filename, - skipHash: '7e2c2f954ef6051373ba916f000168dc', - fields: [] - }; - - for (const name in fields) { - if (fields[name].includes('{audio}')) { - audio.fields.push(name); - } - } - - if (audio.fields.length > 0) { - note.audio = audio; - } - } } for (const name in fields) { @@ -117,8 +101,15 @@ window.yomichan = new class { } definitionAdd(definition, mode) { - const note = this.noteFormat(definition, mode); - return this.anki.addNote(note); + let promise = Promise.resolve(); + if (this.options.general.audioPlayback && mode !== 'kanji') { + promise = audioInject(definition, this.options.anki.terms.fields); + } + + return promise.then(() => { + const note = this.noteFormat(definition, mode); + return this.anki.addNote(note); + }); } definitionsAddable(definitions, modes) { diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 29a292c1..050caf4e 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -251,24 +251,13 @@ class Display { noteAdd(definition, mode) { this.spinner.show(); - - let promise = Promise.resolve(); - if (mode !== 'kanji') { - const filename = Display.audioBuildFilename(definition); - if (filename) { - promise = audioBuildUrl(definition, this.responseCache).then(url => definition.audio = {url, filename}).catch(() => {}); + return this.definitionAdd(definition, mode).then(success => { + if (success) { + const index = this.definitions.indexOf(definition); + Display.adderButtonFind(index, mode).addClass('disabled'); + } else { + this.handleError('note could not be added'); } - } - - promise.then(() => { - return this.definitionAdd(definition, mode).then(success => { - if (success) { - const index = this.definitions.indexOf(definition); - Display.adderButtonFind(index, mode).addClass('disabled'); - } else { - this.handleError('note could not be added'); - } - }); }).catch(this.handleError.bind(this)).then(() => this.spinner.hide()); } diff --git a/ext/mixed/js/util.js b/ext/mixed/js/util.js index 13f124a0..1289455c 100644 --- a/ext/mixed/js/util.js +++ b/ext/mixed/js/util.js @@ -21,7 +21,7 @@ * Audio */ -function audioBuildUrl(definition, cache) { +function audioBuildUrl(definition, cache={}) { return new Promise((resolve, reject) => { const response = cache[definition.expression]; if (response) { @@ -78,3 +78,32 @@ function audioBuildFilename(definition) { return filename += '.mp3'; } } + +function audioInject(definition, fields) { + const filename = audioBuildFilename(definition); + if (!filename) { + return Promise.resolve(true); + } + + const audio = { + filename, + skipHash: '7e2c2f954ef6051373ba916f000168dc', + fields: [] + }; + + for (const name in fields) { + if (fields[name].includes('{audio}')) { + audio.fields.push(name); + } + } + + if (audio.fields.length === 0) { + return Promise.resolve(true); + } + + return audioBuildUrl(definition).then(url => { + audio.url = url; + note.audio = audio; + return true; + }).catch(() => false); +} -- cgit v1.2.3