From 4d6b10950d6aa0b951bc045f02934461b4189103 Mon Sep 17 00:00:00 2001 From: HugoFara Date: Fri, 5 Jan 2024 16:26:27 +0100 Subject: [PATCH] Cleans up code, closes #182. --- js/pgm.js | 3 +- src/js/user_interactions.js | 71 +++++++++++++++++++------------------ 2 files changed, 38 insertions(+), 36 deletions(-) diff --git a/js/pgm.js b/js/pgm.js index 7061a817..ad6ad194 100644 --- a/js/pgm.js +++ b/js/pgm.js @@ -485,4 +485,5 @@ if(rate){msg.rate=rate}else if(tts_settings.rate){msg.rate=tts_settings.rate} if(pitch){msg.pitch=pitch}else if(tts_settings.pitch){msg.pitch=tts_settings.pitch} window.speechSynthesis.speak(msg);return msg} function readTextAloud(text,lang,rate,pitch,voice,convert_to_phonetic){if(convert_to_phonetic){getPhoneticTextAsync(text,lang).then(function(data){readRawTextAloud(data.phonetic_reading,lang,rate,pitch,voice)})}else{readRawTextAloud(text,lang,rate,pitch,voice)}} -function speechDispatcher(term,lang_id){return $.getJSON('api.php/v1/languages/'+lang_id+'/reading-configuration',{lgid:lang_id},function(data){if(data.reading_mode=="direct"||data.reading_mode=="internal"){const lang_settings=cookieTTSSettings(data.language);if(data.reading_mode=="direct"){readRawTextAloud(term,data.abbreviation,lang_settings.rate,lang_settings.pitch,lang_settings.voice)}else if(data.reading_mode=="internal"){getPhoneticTextAsync(term,parseInt(lang_id,10)).then(function(reparsed_text){readRawTextAloud(reparsed_text.phonetic_reading,data.abbreviation,lang_settings.rate,lang_settings.pitch,lang_settings.voice)})}}else if(data.reading_mode=="external"){readTextWithExternal(term,data.voiceApi,data.language)}})} \ No newline at end of file +function handleReadingConfiguration(language,term,lang_id){if(language.reading_mode=="direct"||language.reading_mode=="internal"){const lang_settings=cookieTTSSettings(language.name);if(language.reading_mode=="direct"){readRawTextAloud(term,language.abbreviation,lang_settings.rate,lang_settings.pitch,lang_settings.voice)}else{getPhoneticTextAsync(term,parseInt(lang_id,10)).then(function(reparsed_text){readRawTextAloud(reparsed_text.phonetic_reading,language.abbreviation,lang_settings.rate,lang_settings.pitch,lang_settings.voice)})}}else if(language.reading_mode=="external"){readTextWithExternal(term,language.voiceapi,language.name)}} +function speechDispatcher(term,lang_id){return $.getJSON('api.php/v1/languages/'+lang_id+'/reading-configuration',{lang_id},(data)=>handleReadingConfiguration(data,term,parseInt(lang_id,10)))} \ No newline at end of file diff --git a/src/js/user_interactions.js b/src/js/user_interactions.js index 5513af38..4986adf3 100644 --- a/src/js/user_interactions.js +++ b/src/js/user_interactions.js @@ -341,43 +341,44 @@ function readTextAloud (text, lang, rate, pitch, voice, convert_to_phonetic) { } } +function handleReadingConfiguration(language, term, lang_id) { + if (language.reading_mode == "direct" || language.reading_mode == "internal") { + const lang_settings = cookieTTSSettings(language.name); + if (language.reading_mode == "direct") { + // No reparsing needed + readRawTextAloud( + term, + language.abbreviation, + lang_settings.rate, + lang_settings.pitch, + lang_settings.voice + ); + } else { + // Server handled reparsing + getPhoneticTextAsync(term, parseInt(lang_id, 10)) + .then( + function (reparsed_text) { + readRawTextAloud( + reparsed_text.phonetic_reading, + language.abbreviation, + lang_settings.rate, + lang_settings.pitch, + lang_settings.voice + ); + } + ); + } + } else if (language.reading_mode == "external") { + // Use external API + readTextWithExternal(term, language.voiceapi, language.name); + } + +} + function speechDispatcher (term, lang_id) { return $.getJSON( 'api.php/v1/languages/' + lang_id + '/reading-configuration', - { - lgid: lang_id - }, - function (data) { - if (data.reading_mode == "direct" || data.reading_mode == "internal") { - const lang_settings = cookieTTSSettings(data.language); - if (data.reading_mode == "direct") { - // No reparsing needed - readRawTextAloud( - term, - data.abbreviation, - lang_settings.rate, - lang_settings.pitch, - lang_settings.voice - ); - } else if (data.reading_mode == "internal") { - // Server handled reparsing - getPhoneticTextAsync(term, parseInt(lang_id, 10)) - .then( - function (reparsed_text) { - readRawTextAloud( - reparsed_text.phonetic_reading, - data.abbreviation, - lang_settings.rate, - lang_settings.pitch, - lang_settings.voice - ); - } - ); - } - } else if (data.reading_mode == "external") { - // Use external API - readTextWithExternal(term, data.voiceApi, data.language); - } - } + { lang_id }, + (data) => handleReadingConfiguration(data, term, parseInt(lang_id, 10)) ); }