more audio handling improvements

This commit is contained in:
Alex Yatskov 2017-03-25 10:46:59 -07:00
parent 0e16787692
commit 20d062329b
4 changed files with 50 additions and 40 deletions

View File

@ -7,6 +7,7 @@
<script src="/mixed/lib/handlebars.min.js"></script> <script src="/mixed/lib/handlebars.min.js"></script>
<script src="/mixed/lib/dexie.min.js"></script> <script src="/mixed/lib/dexie.min.js"></script>
<script src="/mixed/lib/wanakana.min.js"></script> <script src="/mixed/lib/wanakana.min.js"></script>
<script src="/mixed/js/util.js"></script>
<script src="/bg/js/templates.js"></script> <script src="/bg/js/templates.js"></script>
<script src="/bg/js/util.js"></script> <script src="/bg/js/util.js"></script>
<script src="/bg/js/anki-connect.js"></script> <script src="/bg/js/anki-connect.js"></script>

View File

@ -56,7 +56,10 @@ window.yomichan = new class {
} }
noteFormat(definition, mode) { noteFormat(definition, mode) {
const note = {fields: {}, tags: this.options.anki.tags}; const note = {
fields: {},
tags: this.options.anki.tags
};
let fields = []; let fields = [];
if (mode === 'kanji') { if (mode === 'kanji') {
@ -67,25 +70,6 @@ window.yomichan = new class {
fields = this.options.anki.terms.fields; fields = this.options.anki.terms.fields;
note.deckName = this.options.anki.terms.deck; note.deckName = this.options.anki.terms.deck;
note.modelName = this.options.anki.terms.model; 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) { for (const name in fields) {
@ -117,8 +101,15 @@ window.yomichan = new class {
} }
definitionAdd(definition, mode) { definitionAdd(definition, mode) {
const note = this.noteFormat(definition, mode); let promise = Promise.resolve();
return this.anki.addNote(note); 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) { definitionsAddable(definitions, modes) {

View File

@ -251,24 +251,13 @@ class Display {
noteAdd(definition, mode) { noteAdd(definition, mode) {
this.spinner.show(); this.spinner.show();
return this.definitionAdd(definition, mode).then(success => {
let promise = Promise.resolve(); if (success) {
if (mode !== 'kanji') { const index = this.definitions.indexOf(definition);
const filename = Display.audioBuildFilename(definition); Display.adderButtonFind(index, mode).addClass('disabled');
if (filename) { } else {
promise = audioBuildUrl(definition, this.responseCache).then(url => definition.audio = {url, filename}).catch(() => {}); 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()); }).catch(this.handleError.bind(this)).then(() => this.spinner.hide());
} }

View File

@ -21,7 +21,7 @@
* Audio * Audio
*/ */
function audioBuildUrl(definition, cache) { function audioBuildUrl(definition, cache={}) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const response = cache[definition.expression]; const response = cache[definition.expression];
if (response) { if (response) {
@ -78,3 +78,32 @@ function audioBuildFilename(definition) {
return filename += '.mp3'; 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);
}