diff --git a/docs/superpowers/plans/2026-06-08-speaking-lab-study-practice-modes.md b/docs/superpowers/plans/2026-06-08-speaking-lab-study-practice-modes.md new file mode 100644 index 0000000..cf1e0c3 --- /dev/null +++ b/docs/superpowers/plans/2026-06-08-speaking-lab-study-practice-modes.md @@ -0,0 +1,911 @@ +# Speaking Lab Study Practice Modes Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** Add five study modes to the learning page, with `听写` and `中译英` input in the left current-sentence practice area and no `挖空` mode. + +**Architecture:** Extend the existing `subtitleMode` / `.tab[data-mode]` pattern instead of adding a second mode system. Add pure answer-checking helpers to `app.js` for Node tests, add a left-side practice panel in `study.html`, and style it in `styles.css` as part of the current video control surface. + +**Tech Stack:** Vanilla HTML, CSS, JavaScript, Node `assert` tests. + +--- + +## File Structure + +- `F:\ooo\speaking-practice-local\study.html`: move the mode bar into the left learning surface, add the current sentence practice panel, and keep the right transcript as a browsing/status panel. +- `F:\ooo\speaking-practice-local\app.js`: add practice helper functions, export them for tests, bind practice panel DOM events, render mode-specific prompts, and clear practice feedback on sentence or mode changes. +- `F:\ooo\speaking-practice-local\styles.css`: style the left mode bar, practice panel, answer row, feedback states, and responsive wrapping. +- `F:\ooo\speaking-practice-local\tests\practice-helpers.test.mjs`: cover answer normalization, answer comparison, and practice view-state derivation. + +## Task 1: Add Failing Practice Helper Tests + +**Files:** +- Create: `F:\ooo\speaking-practice-local\tests\practice-helpers.test.mjs` + +- [ ] **Step 1: Create the test file** + +Create `tests\practice-helpers.test.mjs` with: + +```javascript +import assert from "node:assert/strict"; +import { + normalizePracticeAnswer, + comparePracticeAnswer, + getPracticeViewState, + getSubtitleModeConfig, +} from "../app.js"; + +assert.equal(normalizePracticeAnswer(" Hello, WORLD! "), "hello world"); +assert.equal(normalizePracticeAnswer("I’m ready."), "i'm ready"); +assert.equal(normalizePracticeAnswer("Are you ok? Yes!"), "are you ok yes"); +assert.equal(normalizePracticeAnswer("Listen--then write"), "listen then write"); + +assert.deepEqual(comparePracticeAnswer("", "Good technology should help."), { + status: "empty", + isCorrect: false, + reference: "Good technology should help.", + message: "请输入答案后再核对。", +}); + +assert.deepEqual(comparePracticeAnswer("good technology should help", "Good technology should help."), { + status: "correct", + isCorrect: true, + reference: "Good technology should help.", + message: "完全匹配。", +}); + +assert.deepEqual(comparePracticeAnswer("Good technology should help!", "Good technology should help."), { + status: "correct", + isCorrect: true, + reference: "Good technology should help.", + message: "完全匹配。", +}); + +assert.deepEqual(comparePracticeAnswer("Good tools help.", "Good technology should help."), { + status: "incorrect", + isCorrect: false, + reference: "Good technology should help.", + message: "还不完全一致,参考答案如下。", +}); + +assert.deepEqual(comparePracticeAnswer("anything", ""), { + status: "missing-reference", + isCorrect: false, + reference: "", + message: "当前句没有可核对的英文参考。", +}); + +const cuePair = { + english: { + start: 0, + end: 3.5, + text: "Good technology should make people feel more capable.", + }, + chinese: { + start: 0, + end: 3.5, + text: "好的技术应该让人感觉自己更有能力。", + }, +}; + +assert.deepEqual(getPracticeViewState("bilingual", cuePair), { + mode: "bilingual", + label: "双语", + promptText: "Good technology should make people feel more capable.", + referenceText: "好的技术应该让人感觉自己更有能力。", + expectedText: "Good technology should make people feel more capable.", + inputVisible: false, + canCheck: false, + feedbackHint: "", +}); + +assert.deepEqual(getPracticeViewState("dictation", cuePair), { + mode: "dictation", + label: "听写", + promptText: "听音频,写出当前英文句子。", + referenceText: "", + expectedText: "Good technology should make people feel more capable.", + inputVisible: true, + canCheck: true, + feedbackHint: "", +}); + +assert.deepEqual(getPracticeViewState("translation", cuePair), { + mode: "translation", + label: "中译英", + promptText: "好的技术应该让人感觉自己更有能力。", + referenceText: "根据中文写出英文句子。", + expectedText: "Good technology should make people feel more capable.", + inputVisible: true, + canCheck: true, + feedbackHint: "", +}); + +assert.deepEqual(getPracticeViewState("translation", { english: cuePair.english, chinese: null }), { + mode: "translation", + label: "中译英", + promptText: "当前句没有中文字幕。", + referenceText: "根据中文写出英文句子。", + expectedText: "Good technology should make people feel more capable.", + inputVisible: true, + canCheck: true, + feedbackHint: "", +}); + +assert.deepEqual(getPracticeViewState("dictation", { english: null, chinese: cuePair.chinese }), { + mode: "dictation", + label: "听写", + promptText: "听音频,写出当前英文句子。", + referenceText: "", + expectedText: "", + inputVisible: true, + canCheck: false, + feedbackHint: "当前句没有可核对的英文参考。", +}); + +assert.deepEqual(getSubtitleModeConfig("dictation", { en: {}, zh: {} }), { + primaryCode: "en", + secondaryCode: "", + showSecondary: false, + message: "", +}); + +assert.deepEqual(getSubtitleModeConfig("translation", { en: {}, zh: {} }), { + primaryCode: "zh", + secondaryCode: "", + showSecondary: false, + message: "", +}); +``` + +- [ ] **Step 2: Run the new test and verify it fails** + +Run: + +```powershell +node tests\practice-helpers.test.mjs +``` + +Expected: FAIL with an import error for `normalizePracticeAnswer`, `comparePracticeAnswer`, or `getPracticeViewState`. + +- [ ] **Step 3: Commit the failing test** + +```powershell +git add tests\practice-helpers.test.mjs +git commit -m "test: add practice mode helper coverage" +``` + +## Task 2: Implement Practice Helper Functions + +**Files:** +- Modify: `F:\ooo\speaking-practice-local\app.js` +- Test: `F:\ooo\speaking-practice-local\tests\practice-helpers.test.mjs` + +- [ ] **Step 1: Add mode labels and helper functions near `getSubtitleModeConfig`** + +Add this code before `function getActiveSubtitleModeConfig()`: + +```javascript +const practiceModeLabels = { + bilingual: "双语", + english: "英文", + chinese: "中文", + dictation: "听写", + translation: "中译英", +}; + +function normalizePracticeAnswer(text) { + return String(text || "") + .toLowerCase() + .replace(/[’‘]/g, "'") + .replace(/[“”]/g, "\"") + .replace(/[-–—]+/g, " ") + .replace(/[.,!?;:"()[\]{},。!?;:“”‘’、]/g, " ") + .replace(/\s+/g, " ") + .trim(); +} + +function comparePracticeAnswer(input, expected) { + const reference = String(expected || "").trim(); + const normalizedInput = normalizePracticeAnswer(input); + const normalizedExpected = normalizePracticeAnswer(expected); + + if (!normalizedInput) { + return { + status: "empty", + isCorrect: false, + reference, + message: "请输入答案后再核对。", + }; + } + + if (!normalizedExpected) { + return { + status: "missing-reference", + isCorrect: false, + reference, + message: "当前句没有可核对的英文参考。", + }; + } + + if (normalizedInput === normalizedExpected) { + return { + status: "correct", + isCorrect: true, + reference, + message: "完全匹配。", + }; + } + + return { + status: "incorrect", + isCorrect: false, + reference, + message: "还不完全一致,参考答案如下。", + }; +} + +function getPracticeViewState(mode, cuePair = {}) { + const englishText = cuePair.english?.text?.trim() || ""; + const chineseText = cuePair.chinese?.text?.trim() || ""; + const normalizedMode = practiceModeLabels[mode] ? mode : "bilingual"; + + if (normalizedMode === "dictation") { + return { + mode: normalizedMode, + label: practiceModeLabels[normalizedMode], + promptText: "听音频,写出当前英文句子。", + referenceText: "", + expectedText: englishText, + inputVisible: true, + canCheck: Boolean(englishText), + feedbackHint: englishText ? "" : "当前句没有可核对的英文参考。", + }; + } + + if (normalizedMode === "translation") { + return { + mode: normalizedMode, + label: practiceModeLabels[normalizedMode], + promptText: chineseText || "当前句没有中文字幕。", + referenceText: "根据中文写出英文句子。", + expectedText: englishText, + inputVisible: true, + canCheck: Boolean(englishText), + feedbackHint: englishText ? "" : "当前句没有可核对的英文参考。", + }; + } + + if (normalizedMode === "english") { + return { + mode: normalizedMode, + label: practiceModeLabels[normalizedMode], + promptText: englishText || "当前句没有英文字幕。", + referenceText: "", + expectedText: englishText, + inputVisible: false, + canCheck: false, + feedbackHint: "", + }; + } + + if (normalizedMode === "chinese") { + return { + mode: normalizedMode, + label: practiceModeLabels[normalizedMode], + promptText: chineseText || "当前句没有中文字幕。", + referenceText: "", + expectedText: englishText, + inputVisible: false, + canCheck: false, + feedbackHint: "", + }; + } + + return { + mode: "bilingual", + label: practiceModeLabels.bilingual, + promptText: englishText || "当前句没有英文字幕。", + referenceText: chineseText, + expectedText: englishText, + inputVisible: false, + canCheck: false, + feedbackHint: "", + }; +} +``` + +- [ ] **Step 2: Extend `getSubtitleModeConfig` for input modes** + +In `getSubtitleModeConfig(mode, subtitles = {})`, add this branch after the `chinese` branch and before the final English fallback: + +```javascript + if (mode === "translation") { + return { + primaryCode: hasChinese ? "zh" : fallbackCode, + secondaryCode: "", + showSecondary: false, + message: hasChinese ? "" : "当前素材没有中文字幕。", + }; + } + + if (mode === "dictation") { + return { + primaryCode: hasEnglish ? "en" : fallbackCode, + secondaryCode: "", + showSecondary: false, + message: "", + }; + } +``` + +- [ ] **Step 3: Export the new helpers** + +At the bottom `module.exports` block, add: + +```javascript + normalizePracticeAnswer, + comparePracticeAnswer, + getPracticeViewState, +``` + +The export block should include the existing exports plus these three names. + +- [ ] **Step 4: Run the helper tests** + +Run: + +```powershell +node tests\practice-helpers.test.mjs +``` + +Expected: PASS with no output. + +- [ ] **Step 5: Run existing progress tests** + +Run: + +```powershell +node tests\progress-helpers.test.mjs +``` + +Expected: PASS with no output. + +- [ ] **Step 6: Commit helper implementation** + +```powershell +git add app.js tests\practice-helpers.test.mjs +git commit -m "feat: add practice answer helpers" +``` + +## Task 3: Add Left Practice Panel Markup and DOM Wiring + +**Files:** +- Modify: `F:\ooo\speaking-practice-local\study.html` +- Modify: `F:\ooo\speaking-practice-local\app.js` + +- [ ] **Step 1: Move mode buttons into the left video panel** + +In `study.html`, insert this block after the closing `` for `.tool-row` and before `
`: + +```html +
+
+ + + + + +
+ +
+
+ 双语 +

+

+
+ + + +

+
+
+``` + +- [ ] **Step 2: Replace the right transcript mode buttons with a transcript heading** + +In `study.html`, replace the existing `.transcript-toolbar` contents: + +```html +
+ + + +
+``` + +with: + +```html +
+ 字幕列表 + +
+``` + +This keeps `.transcript-toolbar` in place for layout and lets existing `els.subtitleMeta` become useful. + +- [ ] **Step 3: Add practice DOM references** + +In `app.js`, add these properties to the `els` object: + +```javascript + practiceModeLabel: IS_BROWSER ? document.querySelector("#practiceModeLabel") : null, + practicePromptText: IS_BROWSER ? document.querySelector("#practicePromptText") : null, + practiceReferenceText: IS_BROWSER ? document.querySelector("#practiceReferenceText") : null, + practiceAnswerRow: IS_BROWSER ? document.querySelector("#practiceAnswerRow") : null, + practiceAnswerInput: IS_BROWSER ? document.querySelector("#practiceAnswerInput") : null, + checkPracticeBtn: IS_BROWSER ? document.querySelector("#checkPracticeBtn") : null, + practiceFeedback: IS_BROWSER ? document.querySelector("#practiceFeedback") : null, +``` + +- [ ] **Step 4: Replace mode tab click handling** + +In `bindEvents()`, replace the existing `els.modeTabs.forEach` body with: + +```javascript + els.modeTabs.forEach((tab) => { + tab.addEventListener("click", () => { + setStudyMode(tab.dataset.mode || "bilingual"); + }); + }); +``` + +Then add practice input event listeners in `bindEvents()` after the mode tab listener: + +```javascript + els.practiceAnswerInput?.addEventListener("input", clearPracticeFeedback); + els.checkPracticeBtn?.addEventListener("click", checkPracticeAnswer); +``` + +- [ ] **Step 5: Add mode and practice render functions** + +Add these functions after `renderSubtitleModeTabs()`: + +```javascript +function setStudyMode(mode) { + subtitleMode = practiceModeLabels[mode] ? mode : "bilingual"; + state.subtitleMode = subtitleMode; + saveState(); + clearPracticeResponse(); + renderSubtitleModeTabs(); + renderTranscript(); + syncSubtitles(); + renderPracticePanel(); +} + +function getActiveCuePair() { + const video = getActiveVideo(); + const index = activeLineIndex >= 0 ? activeLineIndex : 0; + return { + english: video?.subtitles?.en?.cues?.[index] || null, + chinese: video?.subtitles?.zh?.cues?.[index] || null, + }; +} + +function clearPracticeFeedback() { + if (!els.practiceFeedback) return; + els.practiceFeedback.textContent = ""; + els.practiceFeedback.className = "practice-feedback"; +} + +function clearPracticeResponse() { + if (els.practiceAnswerInput) els.practiceAnswerInput.value = ""; + clearPracticeFeedback(); +} + +function renderPracticePanel() { + if (!els.practiceModeLabel || !els.practicePromptText || !els.practiceReferenceText) return; + + const viewState = getPracticeViewState(subtitleMode, getActiveCuePair()); + els.practiceModeLabel.textContent = viewState.label; + els.practicePromptText.textContent = viewState.promptText; + els.practiceReferenceText.textContent = viewState.referenceText; + + if (els.practiceAnswerRow) els.practiceAnswerRow.hidden = !viewState.inputVisible; + if (els.checkPracticeBtn) els.checkPracticeBtn.disabled = viewState.inputVisible && !viewState.canCheck; + + if (viewState.feedbackHint && els.practiceFeedback && !els.practiceFeedback.textContent) { + els.practiceFeedback.textContent = viewState.feedbackHint; + els.practiceFeedback.className = "practice-feedback is-muted"; + } +} + +function checkPracticeAnswer() { + if (!els.practiceAnswerInput || !els.practiceFeedback) return; + + const viewState = getPracticeViewState(subtitleMode, getActiveCuePair()); + const result = comparePracticeAnswer(els.practiceAnswerInput.value, viewState.expectedText); + els.practiceFeedback.textContent = result.isCorrect || !result.reference + ? result.message + : `${result.message} ${result.reference}`; + els.practiceFeedback.className = `practice-feedback is-${result.status}`; +} +``` + +- [ ] **Step 6: Render practice panel during page updates** + +In `selectVideo(id)`, after `syncSubtitles();`, add: + +```javascript + renderPracticePanel(); +``` + +In `setActiveLine(index, scrollIntoView)`, after `syncSubtitles();`, add: + +```javascript + clearPracticeResponse(); + renderPracticePanel(); +``` + +In `syncSubtitles()`, after the existing subtitle text updates, add: + +```javascript + renderPracticePanel(); +``` + +- [ ] **Step 7: Run syntax check** + +Run: + +```powershell +node --check app.js +``` + +Expected: PASS with no syntax errors. + +- [ ] **Step 8: Commit markup and wiring** + +```powershell +git add study.html app.js +git commit -m "feat: add study practice panel wiring" +``` + +## Task 4: Style Practice Modes and Practice Panel + +**Files:** +- Modify: `F:\ooo\speaking-practice-local\styles.css` + +- [ ] **Step 1: Update video panel row sizing** + +Replace: + +```css +.video-panel { + min-width: 0; + min-height: 0; + overflow: hidden; + display: grid; + grid-template-rows: minmax(520px, 1fr) auto auto; +} +``` + +with: + +```css +.video-panel { + min-width: 0; + min-height: 0; + overflow: hidden; + display: grid; + grid-template-rows: minmax(500px, 1fr) auto auto auto; +} +``` + +- [ ] **Step 2: Add practice panel styles after `.tool-row` button styles** + +Add: + +```css +.practice-panel { + display: grid; + gap: 12px; + padding: 13px 16px 14px; + border-top: 1px solid var(--line); + background: #fbfdfd; +} + +.practice-mode-bar { + flex-wrap: wrap; + gap: 5px; +} + +.practice-mode-bar .tab { + min-height: 32px; + border: 1px solid transparent; + border-radius: 999px; + padding: 0 13px; + color: #344054; + background: #eef4f6; + font-weight: 750; +} + +.practice-mode-bar .tab:hover { + border-color: #b9dcd5; + background: var(--brand-soft); +} + +.practice-mode-bar .tab.active { + color: #fff; + border-color: var(--accent); + background: var(--accent); +} + +.practice-body { + display: grid; + gap: 10px; +} + +.practice-prompt { + min-height: 70px; + display: grid; + align-content: center; + gap: 5px; + padding: 12px 14px; + border: 1px solid #dde7ef; + border-radius: 8px; + background: #ffffff; +} + +.practice-label { + color: var(--brand); + font-size: 12px; + font-weight: 850; +} + +.practice-primary, +.practice-secondary, +.practice-feedback { + margin: 0; +} + +.practice-primary { + color: #172033; + font-size: 16px; + line-height: 1.45; + font-weight: 750; +} + +.practice-secondary { + color: var(--muted); + font-size: 13px; + line-height: 1.45; +} + +.practice-answer-row { + display: grid; + grid-template-columns: minmax(0, 1fr) auto; + gap: 9px; + align-items: stretch; +} + +.practice-answer-row textarea { + min-height: 58px; + resize: vertical; +} + +.practice-answer-row button { + min-width: 76px; + min-height: 42px; + align-self: start; + color: #ffffff; + border-color: var(--brand); + background: var(--brand); + font-weight: 800; +} + +.practice-answer-row button:hover { + border-color: #0d625c; + background: #0d625c; +} + +.practice-answer-row button:disabled { + color: #98a2b3; + border-color: #d8e2ec; + background: #e8eef5; +} + +.practice-feedback { + min-height: 20px; + color: var(--muted); + font-size: 13px; + line-height: 1.45; +} + +.practice-feedback.is-correct { + color: #047857; + font-weight: 750; +} + +.practice-feedback.is-incorrect, +.practice-feedback.is-empty, +.practice-feedback.is-missing-reference { + color: var(--danger); + font-weight: 700; +} + +.practice-feedback.is-muted { + color: var(--muted); +} +``` + +- [ ] **Step 3: Add transcript title styles near `.transcript-toolbar`** + +Add: + +```css +.transcript-title { + display: grid; + gap: 3px; +} + +.transcript-title strong { + font-size: 14px; +} + +.transcript-title span { + color: var(--muted); + font-size: 12px; +} +``` + +- [ ] **Step 4: Update mobile video panel row sizing** + +In the `@media (max-width: 560px)` block, replace: + +```css + .video-panel { + grid-template-rows: minmax(360px, 1fr) auto auto; + } +``` + +with: + +```css + .video-panel { + grid-template-rows: minmax(340px, 1fr) auto auto auto; + } +``` + +- [ ] **Step 5: Add mobile stacking for the answer row** + +Inside the `@media (max-width: 560px)` block, add: + +```css + .practice-panel { + padding: 12px; + } + + .practice-answer-row { + grid-template-columns: 1fr; + } + + .practice-answer-row button { + width: 100%; + } +``` + +- [ ] **Step 6: Run CSS visual scan by searching new selectors** + +Run: + +```powershell +Select-String -Path styles.css -Pattern 'practice-panel|practice-mode-bar|practice-answer-row|transcript-title' +``` + +Expected: output includes all four selector groups. + +- [ ] **Step 7: Commit styles** + +```powershell +git add styles.css +git commit -m "style: polish study practice modes" +``` + +## Task 5: Verification + +**Files:** +- Verify: `F:\ooo\speaking-practice-local\study.html` +- Verify: `F:\ooo\speaking-practice-local\app.js` +- Verify: `F:\ooo\speaking-practice-local\styles.css` +- Verify: `F:\ooo\speaking-practice-local\tests\practice-helpers.test.mjs` + +- [ ] **Step 1: Run JavaScript syntax checks** + +```powershell +node --check app.js +node --check home.js +node --check admin.js +``` + +Expected: each command exits successfully with no syntax errors. + +- [ ] **Step 2: Run Node helper tests** + +```powershell +node tests\practice-helpers.test.mjs +node tests\progress-helpers.test.mjs +node tests\admin-file-input.test.mjs +``` + +Expected: each command exits successfully with no output. + +- [ ] **Step 3: Run backend syntax checks** + +```powershell +python -m py_compile backend\main.py backend\db.py +``` + +Expected: command exits successfully with no output. + +- [ ] **Step 4: Start the local server** + +Run: + +```powershell +python -m uvicorn backend.main:app --host 127.0.0.1 --port 8000 +``` + +Expected: server starts on `http://127.0.0.1:8000`. + +- [ ] **Step 5: Browser check the study page** + +Open: + +```text +http://127.0.0.1:8000/study.html +``` + +Confirm: + +- The mode bar appears on the left below the video controls. +- The mode bar shows exactly `双语`, `英文`, `中文`, `听写`, `中译英`. +- `挖空` does not appear. +- The right panel heading is `字幕列表`, not a mode input area. +- `听写` shows the answer input on the left. +- `中译英` shows the answer input on the left and uses the Chinese cue as the prompt. +- `双语`, `英文`, and `中文` hide the answer input. +- A correct answer with different case or punctuation shows `完全匹配。`. +- A wrong answer shows the reference English. +- Clicking another transcript sentence clears the input and feedback. +- Previous, replay, next, loop, favorite, practiced, difficult, and recording controls still respond. + +- [ ] **Step 6: Browser check narrow layout** + +Resize the viewport to about 390px wide. + +Confirm: + +- Mode buttons wrap without horizontal scrolling. +- The answer input and check button stack cleanly. +- No button text overflows. +- The transcript rows remain readable. + +- [ ] **Step 7: Final status check** + +Run: + +```powershell +git status --short +``` + +Expected: only intended feature files are modified or the working tree is clean after commits. Existing unrelated local files from earlier work may remain; do not revert them. + +## Self-Review Notes + +- Spec coverage: tasks cover the five modes, removal of `挖空`, left-side input, right transcript as navigation/status, simple checking, input reset, missing subtitle handling, styling, and verification. +- Placeholder scan: this plan uses explicit file paths, commands, expected results, function names, and code blocks. +- Type consistency: mode ids are `bilingual`, `english`, `chinese`, `dictation`, and `translation` across HTML, helper tests, helper implementation, and rendering.