feat: 优化手机端 SVG 播放器进度条与全屏安全区

- 手机竖屏进度条独占一行,播放/重播/语音/全屏保留第二行
- 浮层全屏补齐四向安全区 padding,原生全屏仍保留 p-4
- 补充进度行单测与 375×667 全屏 E2E
This commit is contained in:
margrop
2026-12-04 23:47:38 +08:00
parent 6fa6a5b8d8
commit e1f200c6ce
3 changed files with 138 additions and 43 deletions

View File

@@ -336,6 +336,65 @@ test('移动端大图查看器关闭按钮在安全区内、锁定滚动、Esc
expect((await page.request.delete(`/api/notebooks/${notebook.id}`)).ok()).toBeTruthy();
});
test('手机竖屏动画进度条独占一行,浮层全屏预留安全区', async ({ page, scenario }) => {
test.setTimeout(90000);
await scenario.db.user.update({ where: { id: scenario.user.id }, data: { enableSvgAnimation: true } });
const item = await scenario.db.errorItem.create({
data: {
id: `item-${scenario.id}-svg`,
shortId: `${scenario.id.slice(0, 4)}SVG`,
userId: scenario.user.id,
subjectId: scenario.book.id,
originalImageUrl: '',
questionText: `动画讲解进度测试 ${scenario.marker}`,
answerText: '圆的半径随时间变化。',
analysis: '动画展示了半径的变化过程。',
svgAnimation: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 800 500"><circle cx="400" cy="250" r="100" fill="blue"><animate attributeName="r" values="100;150;100" dur="10s" repeatCount="1"/></circle></svg>',
},
});
await scenario.login(page);
await page.setViewportSize({ width: 375, height: 667 });
await page.goto(`/error-items/${item.id}`);
await expect(page.locator('iframe[title="动画讲解"]')).toBeVisible({ timeout: 15000 });
// 375px 竖屏下进度条独占一行,宽度不再被 7 个控件挤到几十像素
const ranges = page.locator('input[type="range"]');
await expect(ranges).toHaveCount(2);
const mobileRange = ranges.first();
await expect(mobileRange).toBeVisible();
const rangeBox = await mobileRange.boundingBox();
const playBox = await page.getByRole('button', { name: '播放', exact: true }).boundingBox();
expect(rangeBox).not.toBeNull();
expect(playBox).not.toBeNull();
expect(rangeBox!.width).toBeGreaterThan(250);
expect(rangeBox!.y).toBeLessThan(playBox!.y);
// 模拟不支持原生 Fullscreen 的手机浏览器,浮层全屏需落在视口内并预留安全区
await page.evaluate(() => {
Object.defineProperty(Element.prototype, 'requestFullscreen', { value: undefined, configurable: true });
});
await page.getByRole('button', { name: '全屏', exact: true }).click();
const container = page.getByTestId('svg-animation-player');
await expect(container).toBeVisible();
const containerBox = await container.boundingBox();
expect(containerBox).not.toBeNull();
expect(containerBox!.x).toBe(0);
expect(containerBox!.y).toBe(0);
expect(containerBox!.width).toBe(375);
expect(containerBox!.height).toBe(667);
const padding = await container.evaluate((element) => {
const style = getComputedStyle(element);
return { top: parseFloat(style.paddingTop), bottom: parseFloat(style.paddingBottom), left: parseFloat(style.paddingLeft), right: parseFloat(style.paddingRight) };
});
expect(padding.top).toBeGreaterThanOrEqual(16);
expect(padding.bottom).toBeGreaterThanOrEqual(16);
expect(padding.left).toBeGreaterThanOrEqual(16);
expect(padding.right).toBeGreaterThanOrEqual(16);
await expect(page.getByRole('button', { name: '退出全屏', exact: true })).toBeInViewport({ ratio: 1 });
await page.keyboard.press('Escape');
await expect(page.getByRole('button', { name: '退出全屏', exact: true })).toHaveCount(0);
});
test('学生查看错题详情时答案与解析默认折叠,管理员视角展开', async ({ page }) => {
test.setTimeout(60000);
await login(page);

View File

@@ -103,4 +103,18 @@ describe('SVG 动画必须手工播放', () => {
expect(timeline.unpauseAnimations).toHaveBeenCalledOnce();
expect(cssAnimation.play).toHaveBeenCalledOnce();
});
it('窄屏进度行与桌面进度条共用同一时间轴', async () => {
await render();
await load();
const ranges = container.querySelectorAll<HTMLInputElement>('input[type="range"]');
expect(ranges).toHaveLength(2);
expect(ranges[0].value).toBe('0');
const setValue = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')!.set!;
await act(async () => {
setValue.call(ranges[0], '4');
ranges[0].dispatchEvent(new Event('input', { bubbles: true }));
});
expect(container.textContent).toContain('0:04 / 0:10');
});
});

View File

@@ -173,10 +173,12 @@ export function SvgAnimationPlayer({ svg, errorItemId }: SvgAnimationPlayerProps
return (
<div
ref={containerRef}
data-testid="svg-animation-player"
className={cn(
"space-y-2",
fullscreen && "flex flex-col items-center justify-center bg-black p-4",
overlayFullscreen && "fixed inset-0 z-50"
fullscreen && "flex flex-col items-center justify-center bg-black",
nativeFullscreen && "p-4",
overlayFullscreen && "fixed inset-0 z-50 pt-[max(1rem,env(safe-area-inset-top))] pb-[max(1rem,env(safe-area-inset-bottom))] pl-[max(1rem,env(safe-area-inset-left))] pr-[max(1rem,env(safe-area-inset-right))]"
)}
>
<iframe
@@ -195,32 +197,10 @@ export function SvgAnimationPlayer({ svg, errorItemId }: SvgAnimationPlayerProps
? { aspectRatio, width: `min(100%, calc((100dvh - 96px) * ${aspectRatio}))` }
: { aspectRatio }) }}
/>
<div className={cn("mx-auto flex w-full items-center gap-2", fullscreen ? "max-w-5xl text-white" : "max-w-3xl")}>
<Button
variant="outline"
size="sm"
// 全屏时控制栏文字为白色,播放按钮是白底,图标需保持深色
className="h-8 w-8 shrink-0 p-0 text-foreground"
onClick={playing ? pause : play}
disabled={loadedSvg !== svg}
aria-label={playing ? t.svgAnimation.pause : t.svgAnimation.play}
title={playing ? t.svgAnimation.pause : t.svgAnimation.play}
>
{playing ? <Pause className="h-4 w-4" /> : <Play className="h-4 w-4" />}
</Button>
<Button
variant="ghost"
size="sm"
className={cn("h-8 w-8 shrink-0 p-0", fullscreen && "text-white hover:bg-white/20 hover:text-white")}
onClick={() => { seek(0); play(); }}
disabled={loadedSvg !== svg}
aria-label={t.svgAnimation.replay}
title={t.svgAnimation.replay}
>
<RotateCcw className="h-4 w-4" />
</Button>
<div className={cn("mx-auto w-full", fullscreen ? "max-w-5xl text-white" : "max-w-3xl")}>
{/* 手机竖屏:进度条独占一行,播放/重播/语音/全屏在下一行,拖动空间不再被按钮挤压 */}
{duration > 0 && (
<>
<div className="flex items-center gap-2 sm:hidden">
<input
type="range"
min={0}
@@ -234,29 +214,71 @@ export function SvgAnimationPlayer({ svg, errorItemId }: SvgAnimationPlayerProps
<span className={cn("shrink-0 text-xs tabular-nums", fullscreen ? "text-white/80" : "text-muted-foreground")}>
{formatTime(current)} / {formatTime(duration)}
</span>
</>
</div>
)}
<div className="ml-auto flex shrink-0 items-center gap-1">
{!!narrationSegments?.length && (
<NarrationControls
supported={narration.supported}
enabled={narration.enabled}
rate={narration.rate}
fullscreen={fullscreen}
onToggle={() => narration.setEnabled(!narration.enabled, svgRef.current?.getCurrentTime() ?? 0)}
onCycleRate={narration.cycleRate}
/>
)}
<div className="flex items-center gap-2">
<Button
variant="outline"
size="sm"
// 全屏时控制栏文字为白色,播放按钮是白底,图标需保持深色
className="h-8 w-8 shrink-0 p-0 text-foreground"
onClick={playing ? pause : play}
disabled={loadedSvg !== svg}
aria-label={playing ? t.svgAnimation.pause : t.svgAnimation.play}
title={playing ? t.svgAnimation.pause : t.svgAnimation.play}
>
{playing ? <Pause className="h-4 w-4" /> : <Play className="h-4 w-4" />}
</Button>
<Button
variant="ghost"
size="sm"
className={cn("h-8 w-8 shrink-0 p-0", fullscreen && "text-white hover:bg-white/20 hover:text-white")}
onClick={toggleFullscreen}
aria-label={fullscreen ? t.svgAnimation.exitFullscreen : t.svgAnimation.fullscreen}
title={fullscreen ? t.svgAnimation.exitFullscreen : t.svgAnimation.fullscreen}
onClick={() => { seek(0); play(); }}
disabled={loadedSvg !== svg}
aria-label={t.svgAnimation.replay}
title={t.svgAnimation.replay}
>
{fullscreen ? <Minimize className="h-4 w-4" /> : <Maximize className="h-4 w-4" />}
<RotateCcw className="h-4 w-4" />
</Button>
{duration > 0 && (
<>
<input
type="range"
min={0}
max={duration}
step={0.1}
value={Math.min(current, duration)}
onChange={(e) => seek(Number(e.target.value))}
aria-label={t.svgAnimation.progress}
className="hidden h-2 min-w-0 flex-1 cursor-pointer accent-primary sm:block"
/>
<span className={cn("hidden shrink-0 text-xs tabular-nums sm:inline", fullscreen ? "text-white/80" : "text-muted-foreground")}>
{formatTime(current)} / {formatTime(duration)}
</span>
</>
)}
<div className="ml-auto flex shrink-0 items-center gap-1">
{!!narrationSegments?.length && (
<NarrationControls
supported={narration.supported}
enabled={narration.enabled}
rate={narration.rate}
fullscreen={fullscreen}
onToggle={() => narration.setEnabled(!narration.enabled, svgRef.current?.getCurrentTime() ?? 0)}
onCycleRate={narration.cycleRate}
/>
)}
<Button
variant="ghost"
size="sm"
className={cn("h-8 w-8 shrink-0 p-0", fullscreen && "text-white hover:bg-white/20 hover:text-white")}
onClick={toggleFullscreen}
aria-label={fullscreen ? t.svgAnimation.exitFullscreen : t.svgAnimation.fullscreen}
title={fullscreen ? t.svgAnimation.exitFullscreen : t.svgAnimation.fullscreen}
>
{fullscreen ? <Minimize className="h-4 w-4" /> : <Maximize className="h-4 w-4" />}
</Button>
</div>
</div>
</div>
</div>