const host = document.getElementById('voice-options'); // Gradio HTML props are the component state. The public /generate API retains // its original 13 inputs; generate.js takes an atomic snapshot of this state. let savedAutoplay=true;try{savedAutoplay=localStorage.getItem('zonos.autoplay')!=='false';}catch{} const defaults = {autoplay:savedAutoplay,source:'Preset',consent:false,seed:0,randomize:true,normalize:true,rate:0,temperature:.75,top_p:.69,top_k:80,repetition:2.4}; let state = {...defaults, ...JSON.parse(props.value || '{}')}; let open = false; const exampleEvents={en:'example_en',fr:'example_fr',de:'example_de',es:'example_es',it:'example_it',pt:'example_pt',ja:'example_ja',zh:'example_zh',ko:'example_ko'}; const listeners = new AbortController(); function publish() { props.value = JSON.stringify(state); host.dataset.settings = props.value; } function sync() { const expanded = element.querySelector('.settings-toggle'); expanded.setAttribute('aria-expanded', String(open)); element.querySelectorAll('.settings-body').forEach(node => node.hidden = !open); const clip = state.source === 'Voice clip'; host.dataset.open = String(open); host.dataset.clip = String(clip); element.querySelector('[data-consent-row]').hidden = !clip; element.querySelectorAll('[data-source]').forEach(node => node.setAttribute('aria-pressed', String(node.dataset.source === state.source))); element.querySelectorAll('[data-setting]').forEach(node => { const value = state[node.dataset.setting]; if (node.type === 'checkbox') node.checked = value; else if (document.activeElement !== node) node.value = value ?? ''; if (node.type === 'range') node.style.setProperty('--range-fill', `${100*(Number(node.value)-Number(node.min))/(Number(node.max)-Number(node.min))}%`); if (node.dataset.setting === 'seed') node.disabled = state.randomize; }); publish(); } function sourceChanged() { document.getElementById('reference-clip')?.dispatchEvent(new CustomEvent('zonos:source', {detail:state.source})); document.getElementById('voice-picker')?.dispatchEvent(new CustomEvent('zonos:source', {detail:state.source})); } element.addEventListener('input', event => { const node = event.target; if (!node.matches('[data-setting]')) return; state[node.dataset.setting] = node.type === 'checkbox' ? node.checked : (node.value === '' ? null : Number(node.value)); if(node.dataset.setting==='autoplay'){try{localStorage.setItem('zonos.autoplay',String(state.autoplay));}catch{}} sync(); }, {signal:listeners.signal}); element.addEventListener('click', event => { const button = event.target.closest('button'); if (!button) return; if (button.matches('.settings-toggle')) { open = !open; sync(); } if (button.dataset.source) { state.source = button.dataset.source; props.source = state.source; sync(); sourceChanged(); } if (button.hasAttribute('data-reset')) { for (const key of ['temperature','top_p','top_k','repetition']) state[key] = defaults[key]; sync(); } if (exampleEvents[button.dataset.example]) trigger(exampleEvents[button.dataset.example]); }, {signal:listeners.signal}); host.addEventListener('zonos:seed', event => { state.seed = event.detail; sync(); }, {signal:listeners.signal}); watch('source', () => { state.source = props.source; sync(); sourceChanged(); }); watch('value', () => { state = {...defaults, ...JSON.parse(props.value || '{}')}; sync(); }); sync(); host.dataset.ready='true'; const observer = new MutationObserver(() => { if (!element.isConnected) { listeners.abort(); observer.disconnect(); } }); observer.observe(document.documentElement, {childList:true, subtree:true});