Week A1 — Sport Science & Performance Foundations
Energy systems, testing, safety
Ready
`; const w = window.open('', '_blank', 'width=900,height=700'); w.document.write(html); w.document.close(); setTimeout(()=> w.print(), 350); }; document.getElementById('downloadChapter').onclick = ()=>{ const id = readerState.current; const data = CURRICULUM[id]; if(!data) return alert('No chapter loaded.'); const md = `# ${data.title}\n\n${stripHtml(data.body)}`; const blob = new Blob([md], {type:'text/markdown'}); const a = document.createElement('a'); a.href = URL.createObjectURL(blob); a.download = `${id}_chapter.md`; a.click(); }; /* ========== TTS ========== */ let ttsUtter = null; document.getElementById('playTTS').onclick = ()=>{ if(!('speechSynthesis' in window)) { alert('Text-to-speech is not supported in this browser.'); return; } const text = chapterBodyEl.innerText || chapterBodyEl.textContent || ''; if(!text) return alert('No text in chapter.'); // cancel existing if(window.speechSynthesis.speaking) window.speechSynthesis.cancel(); const u = new SpeechSynthesisUtterance(text); u.lang = document.documentElement.lang || 'en-US'; u.rate = 1; u.pitch = 1; u.onend = ()=> { ttsUtter = null; statusBadge.textContent = 'TTS ended'; }; u.onerror = (e)=> { ttsUtter = null; statusBadge.textContent = 'TTS error'; }; window.speechSynthesis.speak(u); ttsUtter = u; statusBadge.textContent = 'Reading aloud...'; }; document.getElementById('stopTTS').onclick = ()=> { if('speechSynthesis' in window && window.speechSynthesis.speaking) window.speechSynthesis.cancel(); ttsUtter = null; statusBadge.textContent = 'TTS stopped'; }; /* ========== Export / Import reader state ========== */ document.getElementById('exportReaderJSON').onclick = ()=> { const payload = { exportedAt: new Date().toISOString(), state: readerState }; const blob = new Blob([JSON.stringify(payload, null, 2)], {type:'application/json'}); const a = document.createElement('a'); a.href = URL.createObjectURL(blob); a.download = 'pe10_textbook_export.json'; a.click(); }; document.getElementById('importReaderJSON').onclick = ()=> document.getElementById('importReaderFile').click(); document.getElementById('importReaderFile').addEventListener('change', async (e)=>{ const f = e.target.files[0]; if(!f) return; try{ const txt = await f.text(); const j = JSON.parse(txt); if(j && j.state){ readerState = j.state; saveState(); renderCurrentChapter(); renderTOC(sidebarSearch.value || ''); alert('Reader state imported.'); } else alert('Invalid file format.'); }catch(err){ alert('Import failed: ' + err); } e.target.value = ''; }); /* Also provide the two top small export/import buttons */ document.getElementById('exportReaderBtn').onclick = ()=> document.getElementById('exportReaderJSON').click(); document.getElementById('importReaderBtn').onclick = ()=> document.getElementById('importReaderFile').click(); /* ========== TOC search ========== */ sidebarSearch.addEventListener('input', (e)=> renderTOC(e.target.value || '')); /* ========== Initialisation ========== */ function init(){ loadState(); applyTextSize(); renderTOC(); renderCurrentChapter(); updateControlStates(!!CURRICULUM[readerState.current]); // attach click handler on TOC entries to support alt-click open in new tab behavior tocList.addEventListener('click', (ev)=> { const it = ev.target.closest('.chapter-item'); if(!it) return; const id = it.dataset.id; if(!id) return; openChapter(id); }); } init(); /* ========== Utility ========= */