-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
623 lines (556 loc) · 32.9 KB
/
index.html
File metadata and controls
623 lines (556 loc) · 32.9 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>自然拼读 Pro - V33</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mammoth/1.4.21/mammoth.browser.min.js"></script>
<style>
:root {
--primary: #7b61ff;
--success: #52c41a;
--success-bg: #d9f7be;
--danger: #ff4d4f;
--bg: #f5f7fa;
--font-main: "Segoe UI", "Microsoft YaHei", sans-serif;
--font-mono: "Consolas", "Monaco", monospace;
}
body {
font-family: var(--font-main);
background-color: var(--bg);
margin: 0; height: 100vh;
display: flex; flex-direction: column; overflow: hidden;
transition: background-color 0.5s ease;
}
.view-section { display: none; height: 100%; width: 100%; flex-direction: column; }
.view-section.active { display: flex; }
.dashboard-container { padding: 40px; max-width: 1000px; margin: 0 auto; width: 100%; overflow-y: auto; }
.key-hint { display: inline-block; padding: 2px 8px; border: 1px solid #ccc; border-radius: 5px; font-size: 12px; color: #999; margin-left: 5px; background: #fff; }
.stats-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 20px; margin: 30px 0; }
.stat-card { background: white; padding: 25px; border-radius: 15px; text-align: center; box-shadow: 0 4px 15px rgba(0,0,0,0.05); }
.stat-num { font-size: 36px; font-weight: 800; color: var(--primary); }
.menu-btn { font-size: 18px; padding: 15px 40px; border-radius: 50px; border: none; cursor: pointer; font-weight: bold; margin: 0 10px; transition: transform 0.2s; position: relative; }
.menu-btn:hover { transform: translateY(-3px); }
.btn-primary { background: var(--primary); color: white; }
.header { background: white; padding: 15px 40px; display: flex; justify-content: space-between; align-items: center; height: 60px; box-shadow: 0 2px 5px rgba(0,0,0,0.05); }
.step-indicators { display: flex; gap: 40px; }
.step-ind { font-size: 20px; color: #ddd; font-weight: bold; transition: 0.3s; }
.step-ind.active { color: var(--primary); transform: scale(1.1); }
.stage { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; position: relative; gap: 20px; }
.stage-title { font-size: 32px; color: #333; font-weight: bold; margin-bottom: 20px; }
.translation-text { font-size: 42px; color: #555; font-weight: bold; margin-top: 30px; letter-spacing: 2px; }
/* 声音设置面板 */
.sound-settings { background: white; padding: 20px; border-radius: 15px; margin-bottom: 30px; display: flex; justify-content: center; gap: 30px; box-shadow: 0 4px 10px rgba(0,0,0,0.05); flex-wrap: wrap; }
.sound-col { display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.sound-label { font-size: 14px; font-weight: bold; color: #555; }
.sound-select { padding: 8px; border-radius: 8px; border: 1px solid #ccc; width: 250px; font-family: var(--font-main); font-size: 14px; }
/* 换肤控件 (修复补回) */
.theme-selector { display: flex; gap: 15px; justify-content: center; margin-bottom: 20px; background: rgba(255,255,255,0.6); padding: 10px; border-radius: 30px; width: fit-content; margin-left: auto; margin-right: auto; }
.color-dot { width: 30px; height: 30px; border-radius: 50%; cursor: pointer; border: 3px solid transparent; transition: transform 0.2s; }
.color-dot:hover { transform: scale(1.2); }
.color-dot.active { border-color: #333; transform: scale(1.1); box-shadow: 0 2px 5px rgba(0,0,0,0.2); }
/* 输入框 */
.rich-input-wrapper {
position: relative; display: inline-block; margin: 0 10px;
background: white;
border-radius: 20px; border: 5px solid #ccc;
box-shadow: 0 8px 20px rgba(0,0,0,0.08); cursor: text;
transition: all 0.2s;
}
.visual-layer {
font-family: var(--font-mono); font-size: 80px; font-weight: 800;
text-align: center; width: 100%; height: 100%; line-height: 160px;
position: absolute; top: 0; left: 0; z-index: 1;
pointer-events: none; white-space: pre;
background: transparent !important;
}
.ghost-input {
position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: 10;
opacity: 0 !important; cursor: text; padding: 0; margin: 0; border: none;
}
.cursor {
display: inline-block; width: 4px; height: 80px; background-color: #333;
vertical-align: middle; margin-bottom: 12px; animation: blink 1s step-end infinite;
}
.ghost-input:not(:focus) + .visual-layer .cursor { display: none; }
@keyframes blink { 0%, 100% { opacity: 1; } 50% { opacity: 0; } }
.rich-input-wrapper:focus-within { border-color: var(--primary); box-shadow: 0 0 0 6px rgba(123,97,255,0.2); }
.rich-input-wrapper.correct {
border-color: var(--success) !important;
background-color: var(--success-bg) !important;
box-shadow: 0 0 30px rgba(82, 196, 26, 0.5) !important;
transform: scale(1.05);
}
.char-normal { color: #333; }
.c { color: #0050b3; }
.v { color: #cf1322; }
.rich-input-wrapper.error { border-color: var(--danger); animation: shake 0.4s; }
@keyframes shake { 0%,100%{transform:translateX(0);} 25%{transform:translateX(-10px);} 75%{transform:translateX(10px);} }
.enter-tip { position: fixed; bottom: 30px; left: 50%; transform: translateX(-50%); color: #aaa; font-size: 14px; opacity: 0; transition: 0.3s; }
.enter-tip.show { opacity: 1; }
.summary-card { background: white; padding: 40px; border-radius: 20px; text-align: center; box-shadow: 0 10px 30px rgba(0,0,0,0.1); }
.drop-zone { border: 3px dashed #ccc; padding: 40px; background: white; margin: 20px auto; max-width: 600px; cursor: pointer; text-align: center; }
.data-management { margin-top: 50px; padding-top: 30px; border-top: 2px dashed #e0e0e0; text-align: center; color: #666; }
.data-btn { background: white; border: 1px solid #ccc; padding: 10px 20px; border-radius: 20px; cursor: pointer; margin: 5px 10px; font-weight: 600; color: #555; transition: all 0.2s; }
.data-btn:hover { border-color: var(--primary); color: var(--primary); transform: translateY(-2px); }
.btn-reset { border-color: #ffccc7; color: var(--danger); }
.btn-reset:hover { background: var(--danger); color: white; border-color: var(--danger); }
</style>
</head>
<body>
<div id="view-dashboard" class="view-section active">
<div class="dashboard-container">
<h1 style="text-align:center; color:#333;">📚 自然拼读 Pro</h1>
<p style="text-align:center; color:#666;">今日待学:<b id="daily-target" style="color:var(--primary)">0</b> 词</p>
<!-- 🎨 换肤控件 -->
<div class="theme-selector">
<div class="color-dot active" style="background:#f5f7fa" onclick="App.setTheme('#f5f7fa', this)" title="护眼蓝"></div>
<div class="color-dot" style="background:#f3e8ff" onclick="App.setTheme('#f3e8ff', this)" title="淡雅紫"></div>
<div class="color-dot" style="background:#fff0f6" onclick="App.setTheme('#fff0f6', this)" title="樱花粉"></div>
<div class="color-dot" style="background:#e6fffb" onclick="App.setTheme('#e6fffb', this)" title="薄荷绿"></div>
<div class="color-dot" style="background:#fff7e6" onclick="App.setTheme('#fff7e6', this)" title="暖米色"></div>
</div>
<!-- 🔊 声音设置 -->
<div class="sound-settings">
<div class="sound-col">
<span class="sound-label">🗣️ 读单词</span>
<select id="cfg-word-voice" class="sound-select" onchange="App.previewVoice(this.value)">
<option value="">加载中...</option>
</select>
</div>
<div class="sound-col">
<span class="sound-label">🔔 提示音</span>
<select id="cfg-prompt-voice" class="sound-select" onchange="App.previewVoice(this.value)">
<option value="">加载中...</option>
</select>
</div>
</div>
<div class="stats-grid">
<div class="stat-card"><span class="stat-num" id="stat-total">0</span><br>总词库</div>
<div class="stat-card"><span class="stat-num" id="stat-learned" style="color:var(--success)">0</span><br>已掌握</div>
<div class="stat-card"><span class="stat-num" id="stat-remain" style="color:#faad14">0</span><br>未学习</div>
<div class="stat-card"><span class="stat-num" id="stat-mistakes" style="color:var(--danger)">0</span><br>错题本</div>
</div>
<div style="text-align:center; margin-bottom:20px;">
<label>计划:<input type="number" id="plan-days" value="21" style="width:50px;text-align:center" onchange="App.saveConfig()"> 天</label>
<select id="learn-order"><option value="random">🎲 随机乱序</option><option value="sequence">🔢 列表顺序</option></select>
</div>
<div style="text-align:center;">
<button class="menu-btn btn-primary" onclick="App.start('daily')">🚀 开始练习 <span class="key-hint">Enter</span></button>
<button class="menu-btn" style="color:var(--danger); border:2px solid var(--danger); background:white;" onclick="App.start('review')">💊 复习错题</button>
<button class="menu-btn" style="border:2px solid #ddd; background:white;" onclick="App.switchView('import')">📂 导入新词</button>
</div>
<div class="data-management">
<button class="data-btn" onclick="DataManager.exportData()">⬇️ 备份</button>
<button class="data-btn" onclick="document.getElementById('backup-input').click()">⬆️ 恢复</button>
<button class="data-btn" style="border-color:var(--primary); color:var(--primary)" onclick="DataManager.exportMistakesTxt()">📄 导出错题本</button>
<button class="data-btn" style="border-color:#ffccc7; color:var(--danger)" onclick="DataManager.resetData()">🗑️ 重置</button>
<input type="file" id="backup-input" hidden accept=".json" onchange="DataManager.importData(this.files[0])">
</div>
</div>
</div>
<div id="view-import" class="view-section">
<div class="dashboard-container">
<h2 style="text-align:center">导入新单词</h2>
<div class="drop-zone" onclick="document.getElementById('file-input').click()">
<div style="font-size:30px">📄</div>
<div>点击上传 Word / Txt</div>
<div id="file-name" style="color:var(--primary); margin-top:10px; font-weight:bold;"></div>
</div>
<input type="file" id="file-input" hidden accept=".docx,.txt" onchange="Importer.handleFile(this.files[0])">
<textarea id="manual-text" placeholder="格式: apple 苹果 (系统会自动识别单词和释义)" style="width:100%; height:150px; margin:20px 0; padding:10px; font-family: monospace; border:2px solid #eee; border-radius:10px;"></textarea>
<div style="text-align:center;">
<button class="menu-btn btn-primary" onclick="Importer.parseManual()">确定导入</button>
<button class="menu-btn" onclick="App.switchView('dashboard')">返回主页</button>
</div>
</div>
</div>
<div id="view-practice" class="view-section">
<div class="header">
<button onclick="App.endSession()" style="padding:5px 15px; cursor:pointer;">Esc 退出</button>
<div class="step-indicators">
<div id="ind-1" class="step-ind">1. 读</div>
<div id="ind-2" class="step-ind">2. 拼(色)</div>
<div id="ind-3" class="step-ind">3. 默(黑)</div>
</div>
<div>剩余: <b id="remain-count">0</b></div>
</div>
<div class="stage" id="stage"></div>
<div id="enter-tip" class="enter-tip">按 Enter 下一步</div>
</div>
<script>
// ================== 发音引擎 V32 ==================
const SpeechEngine = {
voices: [],
config: { word: null, prompt: null },
init(savedCfg) {
if(savedCfg) this.config = savedCfg;
const load = () => {
this.voices = window.speechSynthesis.getVoices();
this.populateSelect('cfg-word-voice', this.config.word);
this.populateSelect('cfg-prompt-voice', this.config.prompt);
};
if (window.speechSynthesis.onvoiceschanged !== undefined) {
window.speechSynthesis.onvoiceschanged = load;
}
load();
setTimeout(load, 1000);
},
populateSelect(elemId, currentVal) {
const select = document.getElementById(elemId);
select.innerHTML = '';
const validLangs = ['en-us', 'en-gb'];
let filtered = this.voices.filter(v => {
const lang = v.lang.toLowerCase().replace('_', '-');
const name = v.name.toLowerCase();
const isTargetLang = validLangs.includes(lang);
const isClean = !name.includes('multilingual') && !name.includes('india') && !name.includes('australia') && !name.includes('canada') && !name.includes('ireland');
return isTargetLang && isClean;
});
let formatted = filtered.map(v => {
let label = v.name;
label = label.replace(/^Microsoft\s/, '')
.replace(/\sOnline\s\(Natural\).*/, '')
.replace(/\s-\sEnglish.*/, '')
.replace(' English (United States)', '')
.replace(' English (United Kingdom)', '');
const isUK = v.lang.toLowerCase().includes('gb') || label.includes('UK');
const prefix = isUK ? 'UK' : 'US';
return { originalObj: v, label: `${prefix} ${label}`, region: prefix };
});
formatted.sort((a, b) => (a.region === b.region) ? a.label.localeCompare(b.label) : (a.region === 'UK' ? -1 : 1));
if(formatted.length === 0) {
const opt = document.createElement('option');
opt.text = "系统未检测到英语语音";
select.appendChild(opt);
return;
}
formatted.forEach(item => {
const opt = document.createElement('option');
opt.value = item.originalObj.voiceURI;
opt.text = item.label;
select.appendChild(opt);
});
if(currentVal && formatted.some(item => item.originalObj.voiceURI === currentVal)) {
select.value = currentVal;
} else if (formatted.length > 0) {
select.value = formatted[0].originalObj.voiceURI;
if(elemId === 'cfg-word-voice') this.config.word = select.value;
if(elemId === 'cfg-prompt-voice') this.config.prompt = select.value;
}
},
testVoice(voiceID) { this.speakTTS("Hello", voiceID); },
speak(text, usePromptVoice = false) {
const voiceID = usePromptVoice ? this.config.prompt : this.config.word;
this.speakTTS(text, voiceID);
},
speakTTS(text, voiceID) {
window.speechSynthesis.cancel();
const u = new SpeechSynthesisUtterance(text);
u.lang = 'en-US';
u.rate = 1.0;
if(voiceID) {
const v = this.voices.find(v => v.voiceURI === voiceID);
if(v) {
u.voice = v;
if(v.name.includes("Online")) u.rate = 0.9;
}
}
window.speechSynthesis.speak(u);
},
playPrompt(type) {
const text = type === 'correct' ? 'Excellent' : (type==='good' ? 'Good' : 'Try again');
this.speak(text, true);
}
};
// ================== 数据库 ==================
const DB = {
key: 'phonics_pro_v33_final_skin',
data: { lib: [], config: { planDays: 21, bgColor: '#f5f7fa', voiceCfg: null } },
load() {
const r = localStorage.getItem(this.key);
if(r) {
this.data = JSON.parse(r);
if(!this.data.config.voiceCfg) this.data.config.voiceCfg = { word: null, prompt: null };
if(!this.data.config.bgColor) this.data.config.bgColor = '#f5f7fa';
}
},
save() { localStorage.setItem(this.key, JSON.stringify(this.data)); App.updateUI(); },
add(list) {
let c = 0;
list.forEach(i => {
if(!this.data.lib.find(w=>w.word===i.word)) {
this.data.lib.push({ word: i.word, trans: i.trans, status: 0, err: 0, allTimeErr: 0 });
c++;
}
});
alert(`导入 ${c} 词`); this.save();
}
};
// ================== 解析器 ==================
const Importer = {
handleFile(f) {
if(!f) return; document.getElementById('file-name').innerText = f.name;
const r = new FileReader();
if(f.name.endsWith('.docx')) r.onload = e => mammoth.extractRawText({arrayBuffer:e.target.result}).then(x=>this.parse(x.value));
else r.onload = e => this.parse(e.target.result);
f.name.endsWith('.docx') ? r.readAsArrayBuffer(f) : r.readAsText(f);
},
parseManual() { this.parse(document.getElementById('manual-text').value); },
parse(txt) {
const list = [];
const lines = txt.split(/[\r\n]+/);
lines.forEach(line => {
line = line.trim();
if(!line) return;
const chineseCharMatch = line.match(/[\u4e00-\u9fa5]/);
let wordPart = "", transPart = "";
if (chineseCharMatch) {
const index = chineseCharMatch.index;
wordPart = line.substring(0, index).trim();
transPart = line.substring(index).trim();
} else {
wordPart = line.trim();
transPart = "暂无释义";
}
const cleanWord = wordPart.replace(/\//g, '').toLowerCase();
if(/[a-z]/.test(cleanWord)) {
list.push({ word: cleanWord, trans: transPart });
}
});
if(list.length > 0) { DB.add(list); App.switchView('dashboard'); }
else { alert("未识别到单词。"); }
}
};
// ================== 练习逻辑 ==================
const Practice = {
queue: [], idx: 0, step: 1, canNext: false, mode: 'learning', currentWordHasError: false,
start(list, mode) {
this.queue = list; this.idx = 0; this.mode = mode;
this.loadWord(0); document.addEventListener('keydown', this.handleKey);
},
stop() { document.removeEventListener('keydown', this.handleKey); App.switchView('dashboard'); },
handleKey(e) {
if(e.key === 'Escape') { App.endSession(); return; }
if(Practice.mode === 'summary' && e.key === 'Enter') { Practice.stop(); return; }
if(Practice.mode !== 'summary' && e.key === 'Enter') {
e.stopPropagation();
if(Practice.canNext) Practice.next();
}
},
loadWord(i) {
this.idx = i; this.step = 1; this.canNext = false; this.currentWordHasError = false;
document.getElementById('remain-count').innerText = this.queue.length - i;
this.render();
},
setCanNext(b) { this.canNext = b; document.getElementById('enter-tip').classList.toggle('show', b); },
render() {
const data = this.queue[this.idx];
const stage = document.getElementById('stage');
stage.innerHTML = '';
[1,2,3].forEach(n => document.getElementById(`ind-${n}`).className = `step-ind ${n===this.step?'active':''}`);
// Step 1: 认读
if(this.step === 1) {
this.setCanNext(true);
const title = document.createElement('div');
title.className = 'stage-title'; title.innerText = "认读记忆";
stage.appendChild(title);
const wordBox = document.createElement('div');
wordBox.innerHTML = `
<div class="rich-input-wrapper" style="width:${Math.max(300, data.word.length*70)}px; height:160px; border-color:var(--primary);">
<div class="visual-layer" style="line-height:160px;">${this.color(data.word, true)}</div>
</div>`;
stage.appendChild(wordBox);
const transBox = document.createElement('div');
transBox.className = 'translation-text'; transBox.innerText = data.trans;
stage.appendChild(transBox);
setTimeout(() => SpeechEngine.speak(data.word), 100);
}
// Step 2: 看词拼写
else if(this.step === 2) {
this.setCanNext(false);
const title = document.createElement('div');
title.className = 'stage-title'; title.innerText = "看词拼写";
stage.appendChild(title);
const {wrapper, input} = this.createGhostInput(data.word.length, 'spell-color', true);
wrapper.style.width = Math.max(300, data.word.length*70) + 'px';
input.addEventListener('keydown', e => {
if(e.key === 'Enter') {
e.stopPropagation();
if(this.canNext) { Practice.next(); return; }
if(input.value.toLowerCase() === data.word) {
wrapper.classList.add('correct'); input.disabled=true;
SpeechEngine.playPrompt('good');
this.setCanNext(true);
} else { this.triggerError(wrapper); }
}
});
stage.appendChild(wrapper);
const transBox = document.createElement('div');
transBox.className = 'translation-text'; transBox.innerText = data.trans;
stage.appendChild(transBox);
setTimeout(() => { input.focus(); SpeechEngine.speak(data.word); }, 100);
}
// Step 3: 听音默写
else if(this.step === 3) {
this.setCanNext(false);
const title = document.createElement('div');
title.className = 'stage-title'; title.innerText = "听音默写";
stage.appendChild(title);
const {wrapper, input} = this.createGhostInput(data.word.length, 'spell-blind', false);
wrapper.style.width = Math.max(300, data.word.length*70) + 'px';
input.addEventListener('keydown', e => {
if(e.key === 'Enter') {
e.stopPropagation();
if(this.canNext) { Practice.next(); return; }
if(input.value.toLowerCase() === data.word) {
wrapper.classList.add('correct'); input.disabled=true;
this.saveResult(data);
SpeechEngine.playPrompt('correct');
this.setCanNext(true);
} else { this.triggerError(wrapper); }
}
});
stage.appendChild(wrapper);
const transBox = document.createElement('div');
transBox.className = 'translation-text'; transBox.innerText = data.trans;
stage.appendChild(transBox);
setTimeout(() => { input.focus(); SpeechEngine.speak(data.word); }, 100);
}
},
createGhostInput(len, id, useColor) {
const w = document.createElement('div'); w.className = 'rich-input-wrapper';
w.style.height = '160px';
const inp = document.createElement('input'); inp.className = 'ghost-input';
inp.maxLength = len; inp.id = id; inp.autocomplete = "off";
const vis = document.createElement('div'); vis.className = 'visual-layer';
vis.innerHTML = `<span class="cursor"></span>`;
inp.addEventListener('input', () => {
let html = this.color(inp.value, useColor);
if (inp.value.length < len) html += `<span class="cursor"></span>`;
vis.innerHTML = html;
});
w.append(inp, vis);
return {wrapper:w, input:inp};
},
triggerError(wrapper) {
wrapper.classList.add('error');
SpeechEngine.playPrompt('wrong');
this.currentWordHasError = true;
const dbItem = DB.data.lib.find(w => w.word === this.queue[this.idx].word);
if(dbItem) { dbItem.err++; dbItem.allTimeErr++; }
DB.save();
setTimeout(() => { this.step = 1; this.render(); }, 1500);
},
saveResult(data) {
const dbItem = DB.data.lib.find(w => w.word === data.word);
if(!dbItem) return;
if (this.mode === 'review' && !this.currentWordHasError) {
if(dbItem.err > 0) dbItem.err--;
}
dbItem.status = 1;
DB.save();
},
next() {
if(this.step < 3) { this.step++; this.render(); }
else { if(this.idx < this.queue.length-1) this.loadWord(this.idx+1); else this.showSummary(); }
},
showSummary() {
this.mode = 'summary';
document.getElementById('stage').innerHTML = `<div class="summary-card"><h1>🎉 完成</h1><p style="color:#999">按 Enter 返回</p></div>`;
this.setCanNext(false);
},
color(t, useColor) {
if(!t) return '';
if(!useColor) return t.split('').map(c => `<span class="char-normal">${c}</span>`).join('');
return t.split('').map(c=>`<span class="${/[aeiouy]/i.test(c)?'v':'c'}">${c}</span>`).join('');
}
};
const DataManager = {
exportData() {
const blob = new Blob([JSON.stringify(DB.data)], {type: "application/json"});
const url = URL.createObjectURL(blob);
const a = document.createElement('a'); a.href = url; a.download = `phonics_backup.json`; a.click();
},
exportMistakesTxt() {
const mistakes = DB.data.lib.filter(w => (w.allTimeErr || 0) > 0);
if(mistakes.length === 0) return alert("👍 没有历史错题");
const txtContent = mistakes.map(item => `${item.word} ${item.trans}`).join('\n');
const blob = new Blob([txtContent], {type: "text/plain"});
const url = URL.createObjectURL(blob);
const a = document.createElement('a'); a.href = url; a.download = `mistakes.txt`; a.click();
},
importData(file) {
if(!file) return;
const r = new FileReader();
r.onload = (e) => {
try {
const d = JSON.parse(e.target.result);
if(d.lib) { DB.data = d; DB.save(); alert("✅ 恢复成功"); location.reload(); }
} catch(e) { alert("❌ 文件错误"); }
};
r.readAsText(file);
},
resetData() { if(confirm("⚠️ 清空所有数据?")) { localStorage.removeItem(DB.key); location.reload(); } }
};
const App = {
init() {
DB.load();
this.applyTheme(DB.data.config.bgColor || '#f5f7fa');
this.updateUI();
SpeechEngine.init(DB.data.config.voiceCfg);
document.addEventListener('keydown', (e) => {
if(document.getElementById('view-dashboard').classList.contains('active') && e.key === 'Enter') { App.start('daily'); }
});
},
setTheme(color, el) {
this.applyTheme(color);
document.querySelectorAll('.color-dot').forEach(d => d.classList.remove('active'));
el.classList.add('active');
DB.data.config.bgColor = color;
DB.save();
},
applyTheme(color) { document.body.style.backgroundColor = color; },
previewVoice(voiceVal) { this.saveVoiceConfig(); SpeechEngine.testVoice(voiceVal); },
saveVoiceConfig() {
const cfg = {
word: document.getElementById('cfg-word-voice').value,
prompt: document.getElementById('cfg-prompt-voice').value
};
DB.data.config.voiceCfg = cfg;
SpeechEngine.config = cfg;
DB.save();
},
switchView(id) { document.querySelectorAll('.view-section').forEach(e=>e.classList.remove('active')); document.getElementById('view-'+id).classList.add('active'); this.updateUI(); },
updateUI() {
const l = DB.data.lib;
document.getElementById('stat-total').innerText = l.length;
document.getElementById('stat-learned').innerText = l.filter(w=>w.status==1).length;
document.getElementById('stat-remain').innerText = l.filter(w=>w.status==0).length;
document.getElementById('stat-mistakes').innerText = l.filter(w=>w.err>0).length;
document.getElementById('daily-target').innerText = Math.ceil(l.filter(w=>w.status==0).length / DB.data.config.planDays) || 0;
},
saveConfig() { DB.data.config.planDays = document.getElementById('plan-days').value; DB.save(); },
start(mode) {
const l = DB.data.lib;
let list = [];
if(mode==='daily') {
let pool = l.filter(w=>w.status==0);
if(!pool.length) pool = l.filter(w=>w.status==1);
if(!pool.length) return alert("没有词汇");
const c = parseInt(document.getElementById('daily-target').innerText) || 10;
const rnd = document.getElementById('learn-order').value==='random';
list = rnd ? pool.sort(()=>0.5-Math.random()).slice(0,c) : pool.slice(0,c);
} else {
list = l.filter(w=>w.err>0);
if(!list.length) return alert("没有错题");
}
App.switchView('practice'); Practice.start(list, mode);
},
endSession() { if(confirm("退出?")) { Practice.stop(); App.switchView('dashboard'); } }
};
App.init();
</script>
</body>
</html>