Pass optionsContext to audioBuildUrl handlers

This commit is contained in:
toasted-nutbread 2019-10-09 22:03:56 -04:00
parent 8be0ddeb26
commit 22b218d172
5 changed files with 12 additions and 11 deletions

View File

@ -68,7 +68,8 @@ async function apiDefinitionAdd(definition, mode, context, optionsContext) {
await audioInject( await audioInject(
definition, definition,
options.anki.terms.fields, options.anki.terms.fields,
options.general.audioSource options.general.audioSource,
optionsContext
); );
} }
@ -174,8 +175,8 @@ apiCommandExec.handlers = {
} }
}; };
async function apiAudioGetUrl(definition, source) { async function apiAudioGetUrl(definition, source, optionsContext) {
return audioBuildUrl(definition, source); return audioBuildUrl(definition, source, optionsContext);
} }
async function apiInjectScreenshot(definition, fields, screenshot) { async function apiInjectScreenshot(definition, fields, screenshot) {

View File

@ -88,7 +88,7 @@ const audioUrlBuilders = {
} }
}; };
async function audioBuildUrl(definition, mode, cache={}) { async function audioBuildUrl(definition, mode, optionsContext, cache={}) {
const cacheKey = `${mode}:${definition.expression}`; const cacheKey = `${mode}:${definition.expression}`;
if (cache.hasOwnProperty(cacheKey)) { if (cache.hasOwnProperty(cacheKey)) {
return Promise.resolve(cache[cacheKey]); return Promise.resolve(cache[cacheKey]);
@ -96,7 +96,7 @@ async function audioBuildUrl(definition, mode, cache={}) {
if (audioUrlBuilders.hasOwnProperty(mode)) { if (audioUrlBuilders.hasOwnProperty(mode)) {
const handler = audioUrlBuilders[mode]; const handler = audioUrlBuilders[mode];
return handler(definition).then( return handler(definition, optionsContext).then(
(url) => { (url) => {
cache[cacheKey] = url; cache[cacheKey] = url;
return url; return url;
@ -138,7 +138,7 @@ function audioBuildFilename(definition) {
} }
} }
async function audioInject(definition, fields, mode) { async function audioInject(definition, fields, mode, optionsContext) {
let usesAudio = false; let usesAudio = false;
for (const name in fields) { for (const name in fields) {
if (fields[name].includes('{audio}')) { if (fields[name].includes('{audio}')) {
@ -157,7 +157,7 @@ async function audioInject(definition, fields, mode) {
audioSourceDefinition = definition.expressions[0]; audioSourceDefinition = definition.expressions[0];
} }
const url = await audioBuildUrl(audioSourceDefinition, mode); const url = await audioBuildUrl(audioSourceDefinition, mode, optionsContext);
const filename = audioBuildFilename(audioSourceDefinition); const filename = audioBuildFilename(audioSourceDefinition);
if (url && filename) { if (url && filename) {

View File

@ -181,7 +181,7 @@ Backend.messageHandlers = {
noteView: ({noteId}) => apiNoteView(noteId), noteView: ({noteId}) => apiNoteView(noteId),
templateRender: ({template, data, dynamic}) => apiTemplateRender(template, data, dynamic), templateRender: ({template, data, dynamic}) => apiTemplateRender(template, data, dynamic),
commandExec: ({command}) => apiCommandExec(command), commandExec: ({command}) => apiCommandExec(command),
audioGetUrl: ({definition, source}) => apiAudioGetUrl(definition, source), audioGetUrl: ({definition, source, optionsContext}) => apiAudioGetUrl(definition, source, optionsContext),
screenshotGet: ({options}, sender) => apiScreenshotGet(options, sender), screenshotGet: ({options}, sender) => apiScreenshotGet(options, sender),
forward: ({action, params}, sender) => apiForward(action, params, sender), forward: ({action, params}, sender) => apiForward(action, params, sender),
frameInformationGet: (params, sender) => apiFrameInformationGet(sender), frameInformationGet: (params, sender) => apiFrameInformationGet(sender),

View File

@ -45,8 +45,8 @@ function apiTemplateRender(template, data, dynamic) {
return utilInvoke('templateRender', {data, template, dynamic}); return utilInvoke('templateRender', {data, template, dynamic});
} }
function apiAudioGetUrl(definition, source) { function apiAudioGetUrl(definition, source, optionsContext) {
return utilInvoke('audioGetUrl', {definition, source}); return utilInvoke('audioGetUrl', {definition, source, optionsContext});
} }
function apiCommandExec(command) { function apiCommandExec(command) {

View File

@ -404,7 +404,7 @@ class Display {
this.setSpinnerVisible(true); this.setSpinnerVisible(true);
const expression = expressionIndex === -1 ? definition : definition.expressions[expressionIndex]; const expression = expressionIndex === -1 ? definition : definition.expressions[expressionIndex];
let url = await apiAudioGetUrl(expression, this.options.general.audioSource); let url = await apiAudioGetUrl(expression, this.options.general.audioSource, this.optionsContext);
if (!url) { if (!url) {
url = '/mixed/mp3/button.mp3'; url = '/mixed/mp3/button.mp3';
} }