Spaces:
Running
Running
File size: 3,659 Bytes
4bd2f4a 7a4266f 4bd2f4a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | <!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<title>單字卡模式 - 英文單字小學堂</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<div class="container">
<div class="card">
<a href="{{ url_for('index') }}">← 返回主頁</a>
<h2 id="unit-title">正在載入學習單元...</h2>
<div id="progress" style="margin-bottom: 20px;"></div>
<div class="card word-card">
<div id="word" class="word-main">---</div>
<div id="pos" class="pos">---</div>
<div id="definition" class="definition">---</div>
<div style="margin-top: 20px;">
<button onclick="playAudio('word')" class="btn">🔊 唸單字</button>
</div>
<hr style="margin: 30px 0; border: none; border-top: 1px solid var(--border-color);">
<p id="sentence" style="font-weight: 500; min-height: 50px; font-size: 1.15rem; line-height: 1.8;">---</p>
<button onclick="playAudio('sentence')" class="btn">🗣️ 唸例句</button>
</div>
<div style="display: flex; justify-content: space-between; margin-top: 20px;">
<button onclick="prevWord()" class="btn">上一個</button>
<button onclick="nextWord()" class="btn btn-primary">下一個</button>
</div>
</div>
</div>
<script>
const unit = "{{ unit }}";
let words = [];
let currentIndex = 0;
async function init() {
const res = await fetch(`/api/words/${unit}`);
words = await res.json();
renderWord();
}
function renderWord() {
if (words.length === 0) return;
const w = words[currentIndex];
document.getElementById('unit-title').innerText = `學習單元:${unit}`;
document.getElementById('progress').innerText = `進度:${currentIndex + 1} / ${words.length}`;
document.getElementById('word').innerText = w.word;
document.getElementById('pos').innerText = w.pos;
document.getElementById('definition').innerText = w.definition;
// 將中文部分換行顯示
const sentenceEl = document.getElementById('sentence');
const raw = w.sentence || '';
// 找到中文括號或第一個中文字的位置,插入換行
const match = raw.match(/[((][\u4e00-\u9fff]/);
if (match) {
const idx = raw.indexOf(match[0]);
sentenceEl.innerHTML = raw.substring(0, idx) + '<br>' + raw.substring(idx);
} else {
sentenceEl.innerText = raw;
}
}
function nextWord() {
if (currentIndex < words.length - 1) {
currentIndex++;
renderWord();
}
}
function prevWord() {
if (currentIndex > 0) {
currentIndex--;
renderWord();
}
}
async function playAudio(type) {
const text = type === 'word' ? words[currentIndex].word : words[currentIndex].sentence;
try {
const res = await fetch(`/api/tts?text=${encodeURIComponent(text)}`);
const data = await res.json();
if (data.url) new Audio(data.url).play();
} catch (e) {
console.error('TTS error:', e);
}
}
init();
</script>
</body>
</html> |