feat: 练习页支持回车提交答案、查看原题与移动端标题换行

- 答案输入框在非输入法组合态按 Enter 直接提交
- 顶部增加查看原题入口;直达链接无站内历史时返回原错题详情
- 练习题卡片标题改为可换行,窄屏不挤压右侧控件
- 补充回车提交、查看原题、直达返回与手机换行 E2E

Refs: https://github.com/margrop/wrong-notebook/issues/93
This commit is contained in:
margrop
2026-12-04 15:43:58 +08:00
parent 271cebfa03
commit 4c4cdfd778
4 changed files with 52 additions and 5 deletions

View File

@@ -18,7 +18,7 @@ for (const profile of viewports) {
expect(records[0]).toMatchObject({ errorItemId: item.id, source: 'practice', result: 'correct' });
expect(records[0].questionText).toContain(`${scenario.marker}变式`);
await expect.poll(async () => (await scenario.db.errorItem.findUniqueOrThrow({ where: { id: item.id } })).masteryProbability).toBe(0.7);
await page.locator(`a[href="/error-items/${item.id}"]`).click();
await page.locator(`a[href="/error-items/${item.id}"]`).first().click();
await page.getByRole('button', { name: /做对.*单题练习/ }).click();
await expect(page.getByText(`${scenario.marker}变式`, { exact: false })).toBeVisible();
await page.goto(`/practice?id=${item.id}`);
@@ -50,3 +50,29 @@ test('[E2E-M06] 判题失败不自动判错,用户手工确认后保存', asyn
await expect(page.getByText(`${scenario.marker}变式`, { exact: false })).toBeVisible();
expect(await scenario.records()).toHaveLength(1);
});
test('[E2E-M07] 练习页回车提交答案、查看原题入口与手机标题换行', async ({ page, scenario }) => {
test.setTimeout(90000);
const item = await generatePractice(page, scenario);
await page.setViewportSize({ width: 390, height: 844 });
const title = page.getByTestId('practice-question-title');
await expect(title).toBeVisible();
expect(await title.evaluate((element) => element.scrollWidth <= element.clientWidth)).toBe(true);
await page.getByPlaceholder('输入你的答案...').fill('令连续整数为 k 与 k+1,因此和为 2k+1,按奇数定义可得结论。');
await page.getByPlaceholder('输入你的答案...').press('Enter');
await expect(page.getByText('测试夹具判定:结论及论证完整', { exact: false })).toBeVisible({ timeout: 20000 });
await page.getByRole('link', { name: '查看原题', exact: true }).click();
await expect(page).toHaveURL(`/error-items/${item.id}`);
});
test('[E2E-M08] 直达练习页时返回按钮回到原错题详情', async ({ page, scenario }) => {
const item = await scenario.item();
await scenario.login(page);
const fresh = await page.context().newPage();
await scenario.guardPage(fresh);
await fresh.goto(`/practice?id=${item.id}`);
await fresh.getByRole('button', { name: '返回', exact: true }).click();
await expect(fresh).toHaveURL(`/error-items/${item.id}`);
});

View File

@@ -1,8 +1,9 @@
"use client";
import { Suspense } from "react";
import Link from "next/link";
import { useSearchParams, useRouter } from "next/navigation";
import { Loader2, ArrowLeft } from "lucide-react";
import { Loader2, ArrowLeft, Eye } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Textarea } from "@/components/ui/textarea";
@@ -37,13 +38,25 @@ function PracticeContent() {
return <div className="p-8 text-center">{t.practice.invalidRequest || "Invalid Request"}</div>;
}
// 有站内来源时返回上一页;从书签或新标签直达时回到原错题详情
const goBack = () => {
if (typeof window !== "undefined" && document.referrer.startsWith(window.location.origin)) router.back();
else router.push(`/error-items/${errorItemId}`);
};
return (
<div className="max-w-3xl mx-auto space-y-8">
<div className="flex justify-between items-center mb-4">
<Button variant="ghost" onClick={() => router.back()}>
<div className="flex flex-wrap justify-between items-center gap-2 mb-4">
<Button variant="ghost" onClick={goBack}>
<ArrowLeft className="mr-2 h-4 w-4" />
{t.common?.back || "Back"}
</Button>
<Button variant="ghost" size="sm" asChild>
<Link href={`/error-items/${errorItemId}`}>
<Eye className="mr-2 h-4 w-4" />
{t.practice.viewOriginal || t.practice.history?.viewOriginal || "View original"}
</Link>
</Button>
</div>
<div className="text-center space-y-4">
<h1 className="text-3xl font-bold">{t.practice.title}</h1>
@@ -77,7 +90,7 @@ function PracticeContent() {
<div className="space-y-6 animate-in fade-in slide-in-from-bottom-4 duration-500">
<Card className="border-primary/50 shadow-lg">
<CardHeader>
<CardTitle className="flex justify-between items-center">
<CardTitle className="flex flex-wrap justify-between items-center gap-2" data-testid="practice-question-title">
<span>{t.app.practiceProblem}</span>
<div className="flex flex-wrap items-center gap-2">
<PracticeSourcePicker value={session.source} onChange={session.setSource} disabled={loading} />
@@ -108,6 +121,12 @@ function PracticeContent() {
placeholder={t.app.answerPlaceholder || "Enter your answer..."}
value={session.userAnswer}
onChange={(e) => session.setUserAnswer(e.target.value)}
onKeyDown={(event) => {
if (event.key !== "Enter" || event.shiftKey || event.nativeEvent.isComposing) return;
if (session.isSubmitted || session.grading || !session.userAnswer.trim()) return;
event.preventDefault();
void session.submitAnswer();
}}
disabled={session.isSubmitted}
className="text-lg"
/>

View File

@@ -760,6 +760,7 @@ export const en = {
sourceBank: "Pick from question bank",
sourceBankVariant: "Variant from bank",
lastGeneratedAt: "Generated at {time}. Click Regenerate for a new question.",
viewOriginal: "View original",
history: {
title: "Smart Practice",
subtitle: "Browse generated variants by notebook, knowledge point, difficulty and attempt status; repeat a question without generating it again.",

View File

@@ -760,6 +760,7 @@ export const zh = {
sourceBank: "从真题库选题",
sourceBankVariant: "真题库变式",
lastGeneratedAt: "生成于 {time},点击「重新生成」可换一道新题。",
viewOriginal: "查看原题",
history: {
title: "智能练习",
subtitle: "查看已生成的变式题,按错题本、知识点、难度与作答状态筛选;同一道题可重复作答,无需重新出题。",