Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ npm run test:e2e
- 若 PDF 本机副本保存失败,可继续阅读并「重试保存」;系统会先补存原 PDF,再保存最新现场,不会悄悄丢下无法重新打开的书籍
- 若上次现场读取失败,切换书籍和返回书库不会覆盖它。可重试恢复;选择「保存现场」时需明确确认是否用当前现场替换
- 关闭错误提示后,顶部「查看问题」可重新打开原有说明和重试入口;查看说明不会自动重试或覆盖现场
- 若单页绘制失败,阅读区会显示「重试此页」;明确重试只重新绘制这一页,保留书籍、缩放、夹页与窗口现场,也可以直接翻到其他页继续阅读
- 最近列表展示 3 本书,但不会删除更早书籍的 PDF 或阅读现场;重新导入同一本 PDF 可恢复
- 文件在当前浏览器中处理,阅读现场保存在 IndexedDB。清除浏览器数据会清除本机副本;请保留原 PDF
- 常规自动保存只写阅读现场和轻量书籍信息,不会反复重写整份 PDF;旧书的信息按需补建
Expand Down
6 changes: 3 additions & 3 deletions playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ export default defineConfig({
},
{ name: 'firefox', testIgnore: '**/native-touch.test.ts', use: { ...devices['Desktop Firefox'], viewport: { width: 1440, height: 900 } } },
{ name: 'webkit', testIgnore: '**/native-touch.test.ts', use: { ...devices['Desktop Safari'], viewport: { width: 1440, height: 900 } } },
{ name: 'mobile-webkit', testMatch: ['**/gesture-contract.test.ts', '**/site-icons.test.ts', '**/storage-recovery.test.ts', '**/held-read-intent.test.ts', '**/thumbnail-actions.test.ts', '**/fit-width.test.ts', '**/password-recovery.test.ts', '**/hidden-save.test.ts', '**/recovery-access.test.ts', '**/reader-activation.test.ts', '**/mixed-pages.test.ts', '**/pdf-decoders.test.ts'], use: { ...devices['iPhone 13'] } },
{ name: 'mobile-webkit', testMatch: ['**/gesture-contract.test.ts', '**/site-icons.test.ts', '**/storage-recovery.test.ts', '**/held-read-intent.test.ts', '**/thumbnail-actions.test.ts', '**/fit-width.test.ts', '**/password-recovery.test.ts', '**/hidden-save.test.ts', '**/recovery-access.test.ts', '**/reader-activation.test.ts', '**/mixed-pages.test.ts', '**/pdf-decoders.test.ts', '**/render-recovery.test.ts'], use: { ...devices['iPhone 13'] } },
{
name: 'tablet',
testMatch: ['**/responsive.test.ts', '**/native-touch.test.ts', '**/gesture-contract.test.ts', '**/site-icons.test.ts', '**/storage-recovery.test.ts', '**/held-read-intent.test.ts', '**/thumbnail-actions.test.ts', '**/fit-width.test.ts', '**/password-recovery.test.ts', '**/hidden-save.test.ts', '**/recovery-access.test.ts', '**/reader-activation.test.ts', '**/mixed-pages.test.ts', '**/pdf-decoders.test.ts'],
testMatch: ['**/responsive.test.ts', '**/native-touch.test.ts', '**/gesture-contract.test.ts', '**/site-icons.test.ts', '**/storage-recovery.test.ts', '**/held-read-intent.test.ts', '**/thumbnail-actions.test.ts', '**/fit-width.test.ts', '**/password-recovery.test.ts', '**/hidden-save.test.ts', '**/recovery-access.test.ts', '**/reader-activation.test.ts', '**/mixed-pages.test.ts', '**/pdf-decoders.test.ts', '**/render-recovery.test.ts'],
use: { ...devices['Desktop Chrome'], viewport: { width: 768, height: 1024 }, hasTouch: true, launchOptions: executablePath ? { executablePath } : {} },
},
{
name: 'mobile',
testMatch: ['**/responsive.test.ts', '**/native-touch.test.ts', '**/gesture-contract.test.ts', '**/site-icons.test.ts', '**/storage-recovery.test.ts', '**/held-read-intent.test.ts', '**/thumbnail-actions.test.ts', '**/fit-width.test.ts', '**/password-recovery.test.ts', '**/hidden-save.test.ts', '**/recovery-access.test.ts', '**/reader-activation.test.ts', '**/mixed-pages.test.ts', '**/pdf-decoders.test.ts'],
testMatch: ['**/responsive.test.ts', '**/native-touch.test.ts', '**/gesture-contract.test.ts', '**/site-icons.test.ts', '**/storage-recovery.test.ts', '**/held-read-intent.test.ts', '**/thumbnail-actions.test.ts', '**/fit-width.test.ts', '**/password-recovery.test.ts', '**/hidden-save.test.ts', '**/recovery-access.test.ts', '**/reader-activation.test.ts', '**/mixed-pages.test.ts', '**/pdf-decoders.test.ts', '**/render-recovery.test.ts'],
use: { ...devices['Pixel 7'], viewport: { width: 390, height: 844 }, defaultBrowserType: 'chromium', launchOptions: executablePath ? { executablePath } : {} },
},
],
Expand Down
175 changes: 125 additions & 50 deletions src/components/reader/ReaderViewport.tsx

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/tests/e2e/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Playwright builds the deployable bundle, then starts Vite preview on `127.0.0.1:
- **Rendered viewport:** independent main/reference scale and two-axis scroll restoration, repeated fit-width reset, Ctrl-wheel paper-point anchoring
- **Mixed-page geometry:** a ten-page image-only fixture covers rotation, offset/rotated crop boxes, portrait/landscape/square and 6:1 tall/wide pages. Repeated toolbar zoom to 400% and a fresh zoom after scrolling deep into a tall page retain the same actual-canvas point within 2 CSS pixels, including fractional DPR. The maximum-size canvas is screen-captured before readback and its visible pixel is compared with the decoded canvas; cropped/rotated thumbnails, reference zoom/scroll, saved reopen and PDF-byte hashes are checked separately
- **Local PDF decoders:** a genuine small lossless JPEG2000 scan and identical RGB control require decoded red/green pixels in main/reference readers and Quick Flip/held thumbnails. Worker and main-thread thumbnail routes run separately, as do native WASM and forced local-JavaScript decoder fallback. Exact binary/license hashes, WASM magic, JavaScript MIME, app-base URL prefixes, saved reopen and original PDF hashes are checked
- **Page raster recovery:** controlled native canvas drawing failures require a visible, keyboard-reachable page-local Retry above real PDF text/link layers. Grab and text-selection modes, repeated explicit attempts, 10%/400% scale, a 320px short viewport, navigation away from failure and desktop background-reference ownership are covered. Recovery replaces only the failed Page canvas, retains its Document node and worker count, and checks saved reading state and original PDF-byte hashes
- **Large raster PDF:** generated 106 MB image-only document, first-render timing evidence, final-page navigation, held thumbnail and IndexedDB reopen
- **Import recovery:** real opening-password rejection gives actionable local-copy guidance; owner-permission-only encryption still opens. Repeated failure resets the file input/import lock. Protected and corrupt replacements preserve PDF-byte hashes, held pages, independent scale/viewport and reference windows; recent-card recovery needs no original-file reimport
- **Activation identity:** repeated motionless reader clicks, stationary touch where supported, and toolbar focus do not rewrite the saved snapshot or its timestamp. Controlled page-clock checks retain a genuine page change’s original 500 ms save deadline. Real reference activation/Enter still save, while cancelled preview focus does not. These are operation counts and input contracts, not latency or battery benchmarks
Expand Down Expand Up @@ -68,3 +69,7 @@ Decoder tests install an off-origin HTTP(S) route blocker and verify actual deco
For a non-root decoder check, build and preview with `npx vite build --base=/leafspace-test/` and `npx vite preview --base=/leafspace-test/ --port 4173 --strictPort`, then run `PLAYWRIGHT_BASE_URL=http://localhost:4173/leafspace-test/ npx playwright test pdf-decoders.test.ts --project=chromium --retries=0`. Tests require every observed decoder URL to remain below the mounted app prefix, so availability of root assets cannot mask an incorrect worker-relative URL. A deployed host must mount the whole build at its configured prefix.

Held decoder checks wait for the actual 60px bitmap before sampling colors, so a cached larger preview cannot conceal a failed small render. Custom-worker results must contain successes and zero errors. Firefox can omit dynamic worker imports from Playwright network events; worker Resource Timing supplements those events while retaining the same required local fallback URL and mounted-prefix assertions. No decoder or image-rendering errors are suppressed, and no blanket blank-page heuristic is used.

Raster-recovery tests throw once or twice from native `CanvasRenderingContext2D.drawImage` only for the selected reader/page canvas. PDF.js and React-PDF still execute their real rejection and retry paths; temporary canvases and thumbnail workers are untouched. A generated raster PDF with an internal full-page link makes native PDF layer stacking observable. Recovered screenshots precede the first pixel readback of the replacement canvas. This verifies explicit failed-raster recovery, not automatic detection of silently blank PDFs, memory-pressure diagnosis, physical-device canvas limits or the separate pending-worker teardown issue. Mobile projects establish responsive emulation; native-touch and assistive-technology hardware validation remain separate.

Deterministic UI cases additionally cover stale page/scale/size/session callbacks, repeated clicks, unmount and StrictMode, a focused Retry removed by resize, background completion that must not steal focus, and interruption of an older pinch preview. Retry does not reload Document, clear durable state, change PDF.js versions or suppress render errors.
4 changes: 2 additions & 2 deletions src/tests/e2e/fit-width.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ test('every grid pane fits without horizontal scrolling and still zooms independ
await info.attach('fit-grid-geometry', { body: JSON.stringify(measurements), contentType: 'application/json' });
for (const region of await regions.all()) expectPixels(await decodedPixels(region));
const reference = regions.nth(1);
await reference.locator('..').getByRole('button', { name: '放大', exact: true }).click();
await reference.locator('xpath=ancestor::*[@data-reader-shell][1]').getByRole('button', { name: '放大', exact: true }).click();
await expect.poll(() => reference.evaluate(element => element.scrollWidth - element.clientWidth)).toBeGreaterThan(20);
await expect(reader(page).locator('..').getByRole('button', { name: '恢复适合宽度', exact: true })).toHaveText('100%');
await expect(reader(page).locator('xpath=ancestor::*[@data-reader-shell][1]').getByRole('button', { name: '恢复适合宽度', exact: true })).toHaveText('100%');
});
9 changes: 8 additions & 1 deletion src/tests/e2e/gesture-contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ import type { Locator, Page } from '@playwright/test';
async function dragStart(page: Page, slider: Locator, fraction: number) {
const box = await slider.boundingBox();
if (!box) throw new Error('Missing slider');
const destinationX = Math.round(box.x + 8 + (box.width - 16) * fraction);
const total = Number(await slider.getAttribute('max'));
const destinationPage = Math.round(1 + (destinationX - box.x - 8) / (box.width - 16) * (total - 1));
await page.mouse.move(box.x + 8, box.y + box.height / 2);
await page.mouse.down();
await page.mouse.move(box.x + 8 + (box.width - 16) * fraction, box.y + box.height / 2, { steps: 6 });
await page.mouse.move(destinationX, box.y + box.height / 2, { steps: 6 });
// WebKit may acknowledge native movement before its last pointermove reaches
// React. Observe that intended preview before sampling or cancelling it.
await expect(slider).toHaveValue(String(destinationPage));
await expect(slider).toHaveAttribute('aria-valuetext', /^预览:/);
}

test('timeline pointer drag previews without saving and commits on release; keyboard remains immediate', async ({ page }, info) => {
Expand Down
2 changes: 1 addition & 1 deletion src/tests/e2e/held-read-intent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ for (const origin of ['main', 'reference'] as const) {
const region = page.locator(`[data-window-id="${id}"] [role="region"]`);
for (let index = 0; index < 5; index++) {
const width = await region.locator('canvas').evaluate(element => element.getBoundingClientRect().width);
await region.locator('..').getByRole('button', { name: '放大', exact: true }).click();
await region.locator('xpath=ancestor::*[@data-reader-shell][1]').getByRole('button', { name: '放大', exact: true }).click();
await expect.poll(() => region.locator('canvas').evaluate(element => element.getBoundingClientRect().width)).toBeGreaterThan(width * 1.15);
}
await region.evaluate(element => element.scrollTo(120, 350));
Expand Down
20 changes: 10 additions & 10 deletions src/tests/e2e/mixed-pages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,19 @@ test('mixed raster pages retain their effective rotation, crop ratio and fit-wid
test.setTimeout(90_000); await page.goto('/'); await importBook(page, fixture);
const evidence = [];
for (const number of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1]) {
await goTo(page, number); await reader(page).locator('..').getByRole('button', { name: '恢复适合宽度', exact: true }).click();
await goTo(page, number); await reader(page).locator('xpath=ancestor::*[@data-reader-shell][1]').getByRole('button', { name: '恢复适合宽度', exact: true }).click();
await expect.poll(() => reader(page).evaluate(element => element.scrollWidth - element.clientWidth)).toBe(0);
if ([2, 7, 9, 10].includes(number)) await info.attach(`mixed-page-${number}`, { body: await page.screenshot(), contentType: 'image/png' });
const dimensions = await decoded(reader(page), number);
evidence.push({ page: number, ...dimensions });
if (number === 2) {
const anchor = await paperPoint(reader(page)), baseWidth = dimensions.frameWidth - 2;
for (let step = 1; step <= 5; step++) {
await reader(page).locator('..').getByRole('button', { name: '放大', exact: true }).click();
await reader(page).locator('xpath=ancestor::*[@data-reader-shell][1]').getByRole('button', { name: '放大', exact: true }).click();
await expect.poll(() => reader(page).locator('canvas').evaluate((canvas, width) => Math.abs(canvas.getBoundingClientRect().width - width), Math.floor(baseWidth * 1.2 ** step))).toBeLessThanOrEqual(1);
await decoded(reader(page), number); await expectAnchor(reader(page), anchor);
}
await reader(page).locator('..').getByRole('button', { name: '恢复适合宽度', exact: true }).click();
await reader(page).locator('xpath=ancestor::*[@data-reader-shell][1]').getByRole('button', { name: '恢复适合宽度', exact: true }).click();
await decoded(reader(page), number);
}
await expect.poll(() => reader(page).evaluate(element => element.scrollWidth - element.clientWidth)).toBe(0);
Expand All @@ -142,14 +142,14 @@ test('cropped rotated thumbnails and a zoomed reference survive reopening withou
const referenceFitWidth = (await decoded(reference, 7)).frameWidth - 2;
const id = await reference.locator('xpath=ancestor::*[@data-window-id][1]').getAttribute('data-window-id');
for (let index = 0; index < 5; index++) {
await reference.locator('..').getByRole('button', { name: '放大', exact: true }).click();
await reference.locator('xpath=ancestor::*[@data-reader-shell][1]').getByRole('button', { name: '放大', exact: true }).click();
await expect.poll(() => reference.locator('canvas').evaluate((canvas, width) => Math.abs(canvas.getBoundingClientRect().width - width), Math.floor(referenceFitWidth * 1.2 ** (index + 1)))).toBeLessThanOrEqual(1);
await decoded(reference, 7);
}
await reference.evaluate(element => element.scrollTo(90, 90));
await expect.poll(() => reference.evaluate(element => [element.scrollLeft, element.scrollTop])).toEqual([90, 90]);
const anchor = await paperPoint(reference);
await reference.locator('..').getByRole('button', { name: '放大', exact: true }).click();
await reference.locator('xpath=ancestor::*[@data-reader-shell][1]').getByRole('button', { name: '放大', exact: true }).click();
await expect.poll(() => reference.locator('canvas').evaluate((canvas, width) => Math.abs(canvas.getBoundingClientRect().width - width), Math.floor(referenceFitWidth * 1.2 ** 6))).toBeLessThanOrEqual(1);
await decoded(reference, 7);
await expect.poll(async () => { const after = await paperPoint(reference); return Math.max(Math.abs(after.x - anchor.x) * after.width, Math.abs(after.y - anchor.y) * after.height); }).toBeLessThanOrEqual(2);
Expand All @@ -172,15 +172,15 @@ test('a tall raster page remains decoded at maximum zoom and returns to fit', as
const fitWidth = (await decoded(reader(page), 9)).frameWidth - 2;
const anchor = await paperPoint(reader(page));
for (let index = 0; index < 8; index++) {
await reader(page).locator('..').getByRole('button', { name: '放大', exact: true }).click();
await reader(page).locator('xpath=ancestor::*[@data-reader-shell][1]').getByRole('button', { name: '放大', exact: true }).click();
await expect.poll(() => reader(page).locator('canvas').evaluate((canvas, width) => Math.abs(canvas.getBoundingClientRect().width - width), Math.floor(fitWidth * Math.min(4, 1.2 ** (index + 1))))).toBeLessThanOrEqual(1);
if (index < 7) {
await decoded(reader(page), 9);
await expectAnchor(reader(page), anchor);
await info.attach(`tall-anchor-step-${index + 1}`, { body: JSON.stringify({ origin: anchor, current: await paperPoint(reader(page)) }, null, 2), contentType: 'application/json' });
}
}
await expect(reader(page).locator('..').getByRole('button', { name: '放大', exact: true })).toBeDisabled();
await expect(reader(page).locator('xpath=ancestor::*[@data-reader-shell][1]').getByRole('button', { name: '放大', exact: true })).toBeDisabled();
await expect.poll(() => reader(page).locator('canvas').evaluate((canvas, width) => Math.abs(canvas.getBoundingClientRect().width - width * 4), fitWidth)).toBeLessThanOrEqual(1);
await expect(reader(page).locator('canvas')).toBeVisible();
const point = await reader(page).evaluate(element => {
Expand All @@ -202,7 +202,7 @@ test('a tall raster page remains decoded at maximum zoom and returns to fit', as
expect(screenColor).toEqual(canvasColor);
const evidence = await decoded(reader(page), 9); await expectAnchor(reader(page), anchor);
await info.attach('tall-page-canvas-dimensions', { body: JSON.stringify(evidence, null, 2), contentType: 'application/json' });
await reader(page).locator('..').getByRole('button', { name: '恢复适合宽度', exact: true }).click(); await decoded(reader(page), 9);
await reader(page).locator('xpath=ancestor::*[@data-reader-shell][1]').getByRole('button', { name: '恢复适合宽度', exact: true }).click(); await decoded(reader(page), 9);
await expect.poll(() => reader(page).evaluate(element => element.scrollWidth - element.clientWidth)).toBe(0);
await goTo(page, 1); await decoded(reader(page), 1);
});
Expand All @@ -212,7 +212,7 @@ test('one zoom after scrolling deep into a tall page preserves its actual displa
test.setTimeout(90_000); await page.goto('/'); await importBook(page, fixture); await goTo(page, 9);
const baseWidth = (await decoded(reader(page), 9)).frameWidth - 2;
for (let step = 1; step <= 4; step++) {
await reader(page).locator('..').getByRole('button', { name: '放大', exact: true }).click();
await reader(page).locator('xpath=ancestor::*[@data-reader-shell][1]').getByRole('button', { name: '放大', exact: true }).click();
await expect.poll(() => reader(page).locator('canvas').evaluate((canvas, width) => Math.abs(canvas.getBoundingClientRect().width - width), Math.floor(baseWidth * 1.2 ** step))).toBeLessThanOrEqual(1);
await decoded(reader(page), 9);
}
Expand All @@ -223,7 +223,7 @@ test('one zoom after scrolling deep into a tall page preserves its actual displa
await expect.poll(async () => Math.abs((await paperPoint(reader(page))).y - .85)).toBeLessThan(.001);
const anchor = await paperPoint(reader(page));
expect(await reader(page).evaluate(element => element.scrollTop > 0 && element.scrollTop < element.scrollHeight - element.clientHeight - 10)).toBe(true);
await reader(page).locator('..').getByRole('button', { name: '放大', exact: true }).click();
await reader(page).locator('xpath=ancestor::*[@data-reader-shell][1]').getByRole('button', { name: '放大', exact: true }).click();
await expect.poll(() => reader(page).locator('canvas').evaluate((canvas, width) => Math.abs(canvas.getBoundingClientRect().width - width), Math.floor(baseWidth * 1.2 ** 5))).toBeLessThanOrEqual(1);
await decoded(reader(page), 9);
await expectAnchor(reader(page), anchor);
Expand Down
Loading
Loading