fix: TTS sincrono + pulsante Rileggi nella modalità cucina

This commit is contained in:
dadaloop82
2026-03-29 15:46:43 +00:00
parent b4ee70862e
commit 227c31d9f9
3 changed files with 40 additions and 19 deletions
+19
View File
@@ -3048,6 +3048,25 @@ body {
width: 100%; width: 100%;
} }
.cooking-replay-btn {
display: inline-flex;
align-items: center;
gap: 6px;
background: rgba(255,255,255,0.10);
border: 1px solid rgba(255,255,255,0.20);
color: #fff;
border-radius: 20px;
padding: 7px 18px;
font-size: 0.95rem;
cursor: pointer;
transition: background 0.2s;
flex-shrink: 0;
}
.cooking-replay-btn:active {
background: rgba(255,255,255,0.22);
}
.cooking-step-ings { .cooking-step-ings {
width: 100%; width: 100%;
max-width: 480px; max-width: 480px;
+20 -19
View File
@@ -6063,6 +6063,10 @@ function renderCookingStep() {
ingsEl.style.display = 'none'; ingsEl.style.display = 'none';
} }
// Replay button
const replayBtn = document.getElementById('cooking-replay');
if (replayBtn) replayBtn.style.display = 'speechSynthesis' in window ? 'inline-flex' : 'none';
// Navigation button states // Navigation button states
const prevBtn = document.getElementById('cooking-prev'); const prevBtn = document.getElementById('cooking-prev');
const nextBtn = document.getElementById('cooking-next'); const nextBtn = document.getElementById('cooking-next');
@@ -6076,26 +6080,23 @@ function renderCookingStep() {
function speakCookingStep(text) { function speakCookingStep(text) {
if (!('speechSynthesis' in window)) return; if (!('speechSynthesis' in window)) return;
window.speechSynthesis.cancel(); window.speechSynthesis.cancel();
const utt = new SpeechSynthesisUtterance(text);
function _doSpeak() { utt.lang = 'it-IT';
const utt = new SpeechSynthesisUtterance(text); utt.rate = 0.9;
utt.lang = 'it-IT'; utt.pitch = 1.0;
utt.rate = 0.9; // Pick Italian voice if available; fall back to browser default
utt.pitch = 1.0;
const voices = window.speechSynthesis.getVoices();
const itVoice = voices.find(v => v.lang.startsWith('it'));
if (itVoice) utt.voice = itVoice;
window.speechSynthesis.speak(utt);
}
// Voices may not be ready yet on first load — wait for voiceschanged if empty
const voices = window.speechSynthesis.getVoices(); const voices = window.speechSynthesis.getVoices();
if (voices.length > 0) { const itVoice = voices.find(v => v.lang.startsWith('it'));
// Small delay after cancel() to avoid the speak being silently dropped if (itVoice) utt.voice = itVoice;
setTimeout(_doSpeak, 50); // Must stay synchronous to preserve the user-gesture chain on iOS/Android
} else { window.speechSynthesis.speak(utt);
window.speechSynthesis.addEventListener('voiceschanged', () => setTimeout(_doSpeak, 50), { once: true }); }
}
function replayCookingTTS() {
if (!_cookingRecipe) return;
const steps = _cookingRecipe.steps || [];
const text = (steps[_cookingStep] || '').replace(/^Passo\s*\d+\s*[:.]\s*/i, '');
if (text) speakCookingStep(text);
} }
function toggleCookingTTS() { function toggleCookingTTS() {
+1
View File
@@ -981,6 +981,7 @@
<div class="cooking-body"> <div class="cooking-body">
<div class="cooking-step-num" id="cooking-step-num">1 / 1</div> <div class="cooking-step-num" id="cooking-step-num">1 / 1</div>
<div class="cooking-step-text" id="cooking-step-text"></div> <div class="cooking-step-text" id="cooking-step-text"></div>
<button class="cooking-replay-btn" id="cooking-replay" onclick="replayCookingTTS()" title="Rileggi questo passo" style="display:none">🔊 Rileggi</button>
<div class="cooking-step-ings" id="cooking-step-ings" style="display:none"></div> <div class="cooking-step-ings" id="cooking-step-ings" style="display:none"></div>
</div> </div>
<div class="cooking-nav"> <div class="cooking-nav">