2121 --button-disabled-text-color : # 6c757d ;
2222 --success-bg : # e7f7e7 ;
2323 --success-text : # 2d6a2d ;
24- --error-bg : # fde2e2 ;
24+ --error-bg : # fde2f2 ;
2525 --error-text : # b71c1c ;
2626 --shadow-color : rgba (0 , 0 , 0 , 0.1 );
2727 --accordion-hover-bg : # f1f1f1 ;
349349 margin-top : 10px ;
350350 margin-bottom : 10px ;
351351 }
352+ # instruction-container {
353+ display : none; /* Hide by default */
354+ }
352355 </ style >
353356</ head >
354357< body >
@@ -482,6 +485,7 @@ <h3>Asset Credits</h3>
482485
483486 let allVoices = { } ;
484487 let currentSettings = { } ;
488+ let modalProvider = null ; // New variable to hold provider selected in modal
485489 let sampleAudio = null ;
486490 let lastGeneratedFilename = null ;
487491 const demoAvailable = { { demo_available| tojson } } ;
@@ -500,7 +504,7 @@ <h3>Asset Credits</h3>
500504 }
501505
502506 function validateSpeakersInUI ( ) {
503- const provider = currentSettings . tts_provider || 'elevenlabs' ;
507+ const provider = currentSettings . tts_provider || 'elevenlabs' ; // Always use saved settings for main UI validation
504508
505509 if ( provider !== 'gemini' ) {
506510 speakerValidationErrorDiv . style . display = 'none' ;
@@ -578,7 +582,12 @@ <h3>Asset Credits</h3>
578582 }
579583
580584 // --- Modal Logic ---
581- settingsBtn . onclick = ( ) => { loadSettings ( ) ; settingsModal . style . display = 'block' ; }
585+ settingsBtn . onclick = ( ) => {
586+ loadSettings ( ) . then ( ( ) => {
587+ modalProvider = currentSettings . tts_provider ; // Initialize modalProvider AFTER currentSettings is loaded
588+ settingsModal . style . display = 'block' ;
589+ } ) ;
590+ }
582591 if ( demoAvailable && demoBtn ) { // Check if demoBtn exists before assigning onclick
583592 demoBtn . onclick = ( ) => { demoModal . style . display = 'block' ; }
584593 }
@@ -637,9 +646,9 @@ <h3>Asset Credits</h3>
637646 ] ) ;
638647 currentSettings = await settingsRes . json ( ) ;
639648 allVoices = await voicesRes . json ( ) ;
640- renderProviderSelection ( ) ;
641- renderVoiceSettings ( currentSettings . tts_provider || 'elevenlabs' ) ;
642- validateSpeakersInUI ( ) ; // Validate on settings load
649+ renderProviderSelection ( ) ; // This will use modalProvider, which is now correctly set
650+ renderVoiceSettings ( modalProvider ) ; // This will use modalProvider, which is now correctly set
651+ // validateSpeakersInUI(); // No need to call here, renderVoiceSettings calls updateFormattedScriptPreview which calls this.
643652 } catch ( error ) {
644653 console . error ( "Error loading settings:" , error ) ;
645654 providerContainer . innerHTML = '<p style="color: red;">Could not load settings.</p>' ;
@@ -656,10 +665,10 @@ <h3>Asset Credits</h3>
656665 const select = document . createElement ( 'select' ) ;
657666 select . id = 'tts-provider' ;
658667 select . innerHTML = `<option value="elevenlabs">ElevenLabs</option><option value="gemini">Gemini</option>` ;
659- select . value = currentSettings . tts_provider ;
668+ select . value = modalProvider ; // Use modalProvider for selection
660669 select . onchange = ( ) => {
661- currentSettings . tts_provider = select . value ; // Update state immediately
662- renderVoiceSettings ( select . value ) ;
670+ modalProvider = select . value ; // Update modalProvider
671+ renderVoiceSettings ( modalProvider ) ; // Render voices in modal based on modalProvider
663672 } ;
664673 providerContainer . appendChild ( select ) ;
665674 } else {
@@ -671,20 +680,14 @@ <h3>Asset Credits</h3>
671680 }
672681 }
673682
674- function renderVoiceSettings ( provider ) {
675- // Show/hide instruction box based on provider
676- if ( provider === 'elevenlabs' ) {
677- instructionContainer . style . display = 'none' ;
678- } else {
679- instructionContainer . style . display = 'block' ;
680- }
681- updateFormattedScriptPreview ( ) ; // Update preview when provider changes
682-
683+ function renderVoiceSettings ( providerToRender ) { // Accepts provider to render voices for
684+ // This function now ONLY renders the voice selection dropdowns within the modal.
685+ // It does NOT touch instructionContainer.style.display.
683686 voiceSettingsContainer . innerHTML = '' ;
684- const speakerVoices = ( provider === 'elevenlabs' ) ? currentSettings . speaker_voices_elevenlabs : currentSettings . speaker_voices ;
685- const voices = ( provider === 'elevenlabs' ) ? allVoices . elevenlabs : allVoices . gemini ;
687+ const speakerVoices = ( providerToRender === 'elevenlabs' ) ? currentSettings . speaker_voices_elevenlabs : currentSettings . speaker_voices ;
688+ const voices = ( providerToRender === 'elevenlabs' ) ? allVoices . elevenlabs : allVoices . gemini ;
686689
687- if ( ! voices || ( provider === 'elevenlabs' && voices . length === 0 ) ) {
690+ if ( ! voices || ( providerToRender === 'elevenlabs' && voices . length === 0 ) ) {
688691 voiceSettingsContainer . innerHTML = '<p>No voices available for this provider. Please check your API key and try again.</p>' ;
689692 return ;
690693 }
@@ -717,7 +720,7 @@ <h3>Asset Credits</h3>
717720 row . appendChild ( playBtn ) ;
718721 voiceSettingsContainer . appendChild ( row ) ;
719722
720- if ( provider === 'elevenlabs' ) {
723+ if ( providerToRender === 'elevenlabs' ) {
721724 voices . forEach ( voice => {
722725 const option = new Option ( `${ voice . name } (${ voice . labels . gender || 'N/A' } )` , voice . voice_id ) ;
723726 select . add ( option ) ;
@@ -731,7 +734,7 @@ <h3>Asset Credits</h3>
731734 }
732735 select . value = speakerVoices [ speaker ] ? speakerVoices [ speaker ] . split ( ' - ' ) [ 0 ] : '' ;
733736 }
734- playBtn . onclick = ( ) => playSample ( playBtn , select . value , provider ) ;
737+ playBtn . onclick = ( ) => playSample ( playBtn , select . value , providerToRender ) ;
735738 } ) ;
736739 }
737740
@@ -769,9 +772,18 @@ <h3>Asset Credits</h3>
769772 }
770773 }
771774
775+ // New function to update the main UI's instruction box visibility
776+ function updateMainUIProviderVisibility ( ) {
777+ if ( currentSettings . tts_provider === 'elevenlabs' ) {
778+ instructionContainer . style . display = 'none' ;
779+ } else {
780+ instructionContainer . style . display = 'block' ;
781+ }
782+ updateFormattedScriptPreview ( ) ; // Re-run preview and validation after visibility change
783+ }
784+
772785 saveSettingsBtn . onclick = async function ( ) {
773- const providerSelect = document . getElementById ( 'tts-provider' ) ;
774- const newProvider = providerSelect ? providerSelect . value : currentSettings . tts_provider ;
786+ const newProvider = modalProvider ; // Use the provider selected in the modal
775787 const newSpeakerVoices = { } ;
776788 const newSpeakerVoicesElevenlabs = { } ;
777789 const selects = voiceSettingsContainer . querySelectorAll ( 'select' ) ;
@@ -803,6 +815,8 @@ <h3>Asset Credits</h3>
803815 setTimeout ( ( ) => { statusDiv . style . display = 'none' ; } , 3000 ) ;
804816 closeModal ( settingsModal ) ;
805817 updateTtsStatusBar ( ) ; // Refresh status bar after saving settings
818+ currentSettings . tts_provider = newProvider ; // Update main settings object
819+ updateMainUIProviderVisibility ( ) ; // Update main UI based on new saved provider
806820 } else {
807821 const errorData = await response . json ( ) ;
808822 statusDiv . textContent = `Failed to save settings: ${ errorData . error || 'Unknown error' } ` ;
@@ -1037,14 +1051,9 @@ <h3>Asset Credits</h3>
10371051 // This will ensure the instruction box visibility is correct from the start
10381052 // and also trigger the first speaker validation.
10391053 loadSettings ( ) . then ( ( ) => {
1054+ modalProvider = currentSettings . tts_provider ; // Initialize modalProvider on page load
10401055 // After settings are loaded, ensure UI reflects the correct state
1041- const initialProvider = currentSettings . tts_provider || 'elevenlabs' ;
1042- if ( initialProvider === 'elevenlabs' ) {
1043- instructionContainer . style . display = 'none' ;
1044- } else {
1045- instructionContainer . style . display = 'block' ;
1046- }
1047- updateFormattedScriptPreview ( ) ; // Re-run preview update to ensure validation is correct
1056+ updateMainUIProviderVisibility ( ) ; // Call the new function here
10481057 } ) ;
10491058
10501059 // --- PWA Service Worker Registration ---
0 commit comments