Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
fix(ui): let phones scroll every page of a previewed PDF (#3014)
Call Whitecb436300d40d
diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.ymlindex 4a79cd22e..95451cf39 100644--- a/.github/workflows/lint.yml+++ b/.github/workflows/lint.yml@@ -207,6 +207,7 @@ jobs:npm run test:voice-shortcutnpm run test:chat-interactionsnpm run test:chat-selection+ npm run test:pdf-previewshow-router-integration:needs: changesdiff --git a/ui/e2e/pdf-preview/fixture.html b/ui/e2e/pdf-preview/fixture.htmlnew file mode 100644index 000000000..2d755dd3c--- /dev/null+++ b/ui/e2e/pdf-preview/fixture.html@@ -0,0 +1,12 @@+<!doctype html>+<html lang="en">+ <head>+ <meta charset="UTF-8" />+ <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />+ <title>PDF Preview</title>+ </head>+ <body>+ <div id="root"></div>+ <script type="module" src="./fixture.tsx"></script>+ </body>+</html>diff --git a/ui/e2e/pdf-preview/fixture.tsx b/ui/e2e/pdf-preview/fixture.tsxnew file mode 100644index 000000000..feef8e5e9--- /dev/null+++ b/ui/e2e/pdf-preview/fixture.tsx@@ -0,0 +1,23 @@+import { createRoot } from 'react-dom/client';++import { FileCard } from '../../src/components/ui/file-card';+import { FileViewerProvider } from '../../src/components/ui/file-viewer';+import { ThemeProvider } from '../../src/context/ThemeProvider';+import '../../src/i18n';+import '../../src/index.css';++// A chat attachment card for the PDF named by ``?file=`` (a media-proxy id), inside the same+// providers ChatPage mounts. Its preview eye opens the real chat file viewer; every+// `/api/media/...` request is answered by the spec, so nothing reaches an Avibe service.++const file = new URLSearchParams(location.search).get('file') ?? 'doc_five';++createRoot(document.getElementById('root')!).render(+ <ThemeProvider>+ <FileViewerProvider>+ <div className="flex h-dvh flex-col bg-background p-4 text-foreground" data-testid="pdf-fixture">+ <FileCard href={`/api/media/${file}`}>{`${file}.pdf`}</FileCard>+ </div>+ </FileViewerProvider>+ </ThemeProvider>,+);diff --git a/ui/e2e/pdf-preview/pdf.ts b/ui/e2e/pdf-preview/pdf.tsnew file mode 100644index 000000000..940977c1a--- /dev/null+++ b/ui/e2e/pdf-preview/pdf.ts@@ -0,0 +1,81 @@+// A minimal PDF writer for the preview suite, so every fixture document is generated from a+// readable description instead of checked in as an opaque binary.+//+// Each page is US Letter with a large filled band in its own colour (what the spec samples from the+// rendered canvas to prove *which* page was drawn) and a "Page N" label. ``cjk`` adds a line set in+// a non-embedded Adobe-GB1 font through the predefined ``UniGB-UCS2-H`` CMap — the shape of PDFs+// many generators emit for Chinese text, and one pdf.js can only decode with its bundled CMaps.++export type Rgb = readonly [number, number, number];++export const PAGE_WIDTH = 612;+export const PAGE_HEIGHT = 792;+// The coloured band, in PDF user space (origin bottom-left).+export const BAND = { x: 56, y: 240, width: 500, height: 320 } as const;++export function pageColor(index: number): Rgb {+ const palette: Rgb[] = [+ [220, 40, 40],+ [40, 170, 60],+ [40, 80, 220],+ [230, 160, 20],+ [150, 50, 200],+ [20, 170, 190],+ ];+ return palette[index % palette.length];+}++const unit = (v: number) => (v / 255).toFixed(3);++export function buildPdf({ pages, cjk = false }: { pages: number; cjk?: boolean }): Buffer {+ const objects: string[] = [];+ const add = (body: string) => {+ objects.push(body);+ return objects.length;+ };++ const catalog = add('');+ const pagesNode = add('');+ const helvetica = add('<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>');+ let song = 0;+ if (cjk) {+ const descriptor = add(+ '<< /Type /FontDescriptor /FontName /STSong-Light /Flags 6 /FontBBox [-25 -254 1000 880] /ItalicAngle 0 /Ascent 880 /Descent -120 /CapHeight 880 /StemV 93 >>',+ );+ const cidFont = add(+ `<< /Type /Font /Subtype /CIDFontType0 /BaseFont /STSong-Light /CIDSystemInfo << /Registry (Adobe) /Ordering (GB1) /Supplement 4 >> /FontDescriptor ${descriptor} 0 R /DW 1000 >>`,+ );+ song = add(`<< /Type /Font /Subtype /Type0 /BaseFont /STSong-Light-UniGB-UCS2-H /Encoding /UniGB-UCS2-H /DescendantFonts [${cidFont} 0 R] >>`);+ }++ const kids: number[] = [];+ for (let i = 0; i < pages; i += 1) {+ const [r, g, b] = pageColor(i);+ const ops = [+ `${unit(r)} ${unit(g)} ${unit(b)} rg ${BAND.x} ${BAND.y} ${BAND.width} ${BAND.height} re f`,+ `0 0 0 rg BT /F1 48 Tf 72 680 Td (Page ${i + 1}) Tj ET`,+ ];+ // "中文预览" as UCS-2 code units.+ if (cjk) ops.push('0 0 0 rg BT /F2 40 Tf 72 620 Td <4E2D6587987889C8> Tj ET');+ const stream = ops.join('\n');+ const content = add(`<< /Length ${Buffer.byteLength(stream)} >>\nstream\n${stream}\nendstream`);+ const fonts = `/F1 ${helvetica} 0 R${cjk ? ` /F2 ${song} 0 R` : ''}`;+ kids.push(+ add(`<< /Type /Page /Parent ${pagesNode} 0 R /MediaBox [0 0 ${PAGE_WIDTH} ${PAGE_HEIGHT}] /Contents ${content} 0 R /Resources << /Font << ${fonts} >> >> >>`),+ );+ }+ objects[catalog - 1] = `<< /Type /Catalog /Pages ${pagesNode} 0 R >>`;+ objects[pagesNode - 1] = `<< /Type /Pages /Kids [${kids.map((k) => `${k} 0 R`).join(' ')}] /Count ${kids.length} >>`;++ let out = '%PDF-1.4\n';+ const offsets: number[] = [];+ objects.forEach((body, i) => {+ offsets.push(Buffer.byteLength(out));+ out += `${i + 1} 0 obj\n${body}\nendobj\n`;+ });+ const xref = Buffer.byteLength(out);+ out += `xref\n0 ${objects.length + 1}\n0000000000 65535 f \n`;+ for (const offset of offsets) out += `${String(offset).padStart(10, '0')} 00000 n \n`;+ out += `trailer\n<< /Size ${objects.length + 1} /Root ${catalog} 0 R >>\nstartxref\n${xref}\n%%EOF\n`;+ return Buffer.from(out, 'latin1');+}diff --git a/ui/e2e/pdf-preview/preview.spec.ts b/ui/e2e/pdf-preview/preview.spec.tsnew file mode 100644index 000000000..f666ed549--- /dev/null+++ b/ui/e2e/pdf-preview/preview.spec.ts@@ -0,0 +1,214 @@+import { expect, test, type Locator, type Page } from '@playwright/test';++import { BAND, PAGE_HEIGHT, PAGE_WIDTH, buildPdf, pageColor, type Rgb } from './pdf';++/**+ * The chat file viewer's PDF preview, in real engines.+ *+ * Framing a PDF in an <iframe> hands it to the browser's own viewer. On iOS that+ * viewer paints only page one, as an image that does not scroll; on Android+ * there is no viewer at all. Phones and tablets therefore get a page reader+ * drawn with pdf.js, and these tests hold it to what the frame could not do:+ * every page is reachable and drawn, and it is the right page. Pixels are read+ * back from the canvases, so "drawn" means the page's own colour is on screen,+ * not that an element exists.+ */++const DOCS: Record<string, Buffer> = {+ doc_five: buildPdf({ pages: 5 }),+ doc_long: buildPdf({ pages: 14 }),+ doc_cjk: buildPdf({ pages: 2, cjk: true }),+ doc_broken: Buffer.from('%PDF-1.4\nthis is not a PDF document\n'),+};++test.beforeEach(async ({ page }) => {+ await page.route('**/api/media/**', (route) => {+ const parts = new URL(route.request().url()).pathname.split('/');+ const isMeta = parts.at(-1) === 'meta';+ const id = (isMeta ? parts.at(-2) : parts.at(-1)) ?? '';+ const body = DOCS[id];+ if (!body) return route.fulfill({ status: 404, body: 'not found' });+ if (isMeta) {+ return route.fulfill({ json: { name: `${id}.pdf`, size: body.length, content_type: 'application/pdf', ext: 'pdf' } });+ }+ return route.fulfill({ status: 200, contentType: 'application/pdf', body });+ });+});++const openPreview = async (page: Page, file: string) => {+ await page.goto(`/e2e/pdf-preview/fixture.html?file=${file}`);+ await page.getByRole('button', { name: 'Preview' }).click();+ const dialog = page.getByRole('dialog');+ await expect(dialog).toBeVisible();+ return dialog;+};++const pageBox = (dialog: Locator, n: number) => dialog.locator(`[data-pdf-page="${n}"]`);+const scroller = (dialog: Locator) => dialog.locator('[data-pdf-scroller]');+const position = (dialog: Locator) => dialog.locator('[data-pdf-position]');++// The colour at the centre of the page's band, read from its canvas.+const bandColor = (dialog: Locator, n: number): Promise<Rgb> =>+ pageBox(dialog, n)+ .locator('canvas')+ .evaluate(+ (canvas: HTMLCanvasElement, geometry) => {+ const x = Math.floor((canvas.width * (geometry.x + geometry.width / 2)) / geometry.pageWidth);+ const y = Math.floor(canvas.height * (1 - (geometry.y + geometry.height / 2) / geometry.pageHeight));+ const [r, g, b] = canvas.getContext('2d')!.getImageData(x, y, 1, 1).data;+ return [r, g, b] as const;+ },+ { ...BAND, pageWidth: PAGE_WIDTH, pageHeight: PAGE_HEIGHT },+ );++const expectDrawn = async (dialog: Locator, n: number) => {+ await expect(pageBox(dialog, n)).toHaveAttribute('data-pdf-rendered', 'true');+ const [r, g, b] = await bandColor(dialog, n);+ const [er, eg, eb] = pageColor(n - 1);+ const close = (a: number, e: number) => Math.abs(a - e) <= 16;+ expect(close(r, er) && close(g, eg) && close(b, eb), `page ${n} band is rgb(${r}, ${g}, ${b}), expected rgb(${er}, ${eg}, ${eb})`).toBe(true);+};++const scrollToPage = async (dialog: Locator, n: number) => {+ await scroller(dialog).evaluate((el, top) => el.scrollTo({ top }), await pageBox(dialog, n).evaluate((p) => (p as HTMLElement).offsetTop));+};++test.describe('phones and tablets', () => {+ test.skip(({ isMobile }) => !isMobile, 'the page reader is the touch-device path');++ test('every page of a PDF can be scrolled to, and each one is drawn', async ({ page }) => {+ const dialog = await openPreview(page, 'doc_five');+ await expect(dialog.locator('iframe')).toHaveCount(0);+ await expect(dialog.locator('[data-pdf-page]')).toHaveCount(5);+ await expectDrawn(dialog, 1);+ await expect(position(dialog)).toHaveText('1 / 5');++ // The pages stack past the viewer's height: page one alone does not fill the document.+ const { scrollHeight, clientHeight } = await scroller(dialog).evaluate((el) => ({ scrollHeight: el.scrollHeight, clientHeight: el.clientHeight }));+ expect(scrollHeight).toBeGreaterThan(clientHeight * 2);++ await scroller(dialog).evaluate((el) => el.scrollTo({ top: el.scrollHeight }));+ await expectDrawn(dialog, 5);+ await expect(position(dialog)).toHaveText('5 / 5');++ await scrollToPage(dialog, 3);+ await expectDrawn(dialog, 3);+ await expect(position(dialog)).toHaveText('3 / 5');+ });++ test('pages are fit to the viewer width', async ({ page }) => {+ const dialog = await openPreview(page, 'doc_five');+ await expectDrawn(dialog, 1);+ const view = (await scroller(dialog).boundingBox())!;+ const first = (await pageBox(dialog, 1).boundingBox())!;+ expect(first.width).toBeGreaterThan(view.width - 40);+ expect(first.width).toBeLessThanOrEqual(view.width);+ expect(first.height / first.width).toBeCloseTo(PAGE_HEIGHT / PAGE_WIDTH, 1);+ });++ test('a long document only keeps canvases near what is on screen', async ({ page }) => {+ const dialog = await openPreview(page, 'doc_long');+ await expect(dialog.locator('[data-pdf-page]')).toHaveCount(14);+ await expectDrawn(dialog, 1);+ // Far below the fold at the top of the document.+ await expect(pageBox(dialog, 14).locator('canvas')).toHaveCount(0);++ await scroller(dialog).evaluate((el) => el.scrollTo({ top: el.scrollHeight }));+ await expectDrawn(dialog, 14);+ await expect(pageBox(dialog, 1).locator('canvas')).toHaveCount(0);+ expect(await dialog.locator('[data-pdf-page] canvas').count()).toBeLessThan(14);+ });++ test('zooming keeps the page being read in view, and fit restores it', async ({ page }) => {+ const dialog = await openPreview(page, 'doc_five');+ await expectDrawn(dialog, 1);+ const fitWidth = (await pageBox(dialog, 1).boundingBox())!.width;++ await scrollToPage(dialog, 3);+ await expect(position(dialog)).toHaveText('3 / 5');+ await dialog.getByRole('button', { name: 'Zoom in' }).click();+ await dialog.getByRole('button', { name: 'Zoom in' }).click();+ await expect(dialog.getByText('150%')).toBeVisible();+ await expect.poll(async () => Math.round((await pageBox(dialog, 3).boundingBox())!.width)).toBe(Math.round(fitWidth * 1.5));++ // The viewport's centre is still on page three, and page three is redrawn at the new size.+ const view = (await scroller(dialog).boundingBox())!;+ const third = (await pageBox(dialog, 3).boundingBox())!;+ const centreY = view.y + view.height / 2;+ expect(centreY).toBeGreaterThan(third.y);+ expect(centreY).toBeLessThan(third.y + third.height);+ await expect(position(dialog)).toHaveText('3 / 5');+ await expectDrawn(dialog, 3);++ await dialog.getByRole('button', { name: 'Reset zoom' }).click();+ await expect(dialog.getByText('100%')).toBeVisible();+ await expect.poll(async () => Math.round((await pageBox(dialog, 3).boundingBox())!.width)).toBe(Math.round(fitWidth));+ });++ test('a two-finger pinch zooms the document', async ({ page, browserName }) => {+ test.skip(browserName !== 'chromium', 'multi-touch input is driven through the Chromium DevTools protocol');+ const dialog = await openPreview(page, 'doc_five');+ await expectDrawn(dialog, 1);+ const fitWidth = (await pageBox(dialog, 1).boundingBox())!.width;+ const view = (await scroller(dialog).boundingBox())!;+ const cx = view.x + view.width / 2;+ const cy = view.y + view.height / 3;++ const cdp = await page.context().newCDPSession(page);+ const fingers = (spread: number) => [+ { x: cx - spread, y: cy, id: 1 },+ { x: cx + spread, y: cy, id: 2 },+ ];+ await cdp.send('Input.dispatchTouchEvent', { type: 'touchStart', touchPoints: fingers(30) });+ for (let spread = 40; spread <= 90; spread += 10) {+ await cdp.send('Input.dispatchTouchEvent', { type: 'touchMove', touchPoints: fingers(spread) });+ }+ await cdp.send('Input.dispatchTouchEvent', { type: 'touchEnd', touchPoints: [] });++ // Spread 60px → 180px: three times the fit width.+ await expect(dialog.getByText('300%')).toBeVisible();+ await expect.poll(async () => Math.round((await pageBox(dialog, 1).boundingBox())!.width)).toBe(Math.round(fitWidth * 3));+ // The browser's own page zoom stayed out of it.+ expect(await page.evaluate(() => window.visualViewport?.scale ?? 1)).toBe(1);+ });++ test('Chinese text set through a predefined CMap is decoded with the bundled CMaps', async ({ page }) => {+ const cmapRequests: string[] = [];+ page.on('request', (request) => {+ if (/UniGB-UCS2-H[^/]*\.bcmap/.test(request.url())) cmapRequests.push(request.url());+ });+ const dialog = await openPreview(page, 'doc_cjk');+ await expectDrawn(dialog, 1);+ expect(cmapRequests.length).toBeGreaterThan(0);+ });++ test('a file that is not a valid PDF says so instead of drawing blank pages', async ({ page }) => {+ const dialog = await openPreview(page, 'doc_broken');+ await expect(dialog.getByText("Couldn't load this file for preview.")).toBeVisible();+ await expect(dialog.locator('[data-pdf-page]')).toHaveCount(0);+ });+});++test.describe('desktop browsers', () => {+ test.skip(({ isMobile }) => isMobile, 'desktop reader selection');++ const reportViewer = (page: Page, enabled: boolean) =>+ page.addInitScript((value) => {+ Object.defineProperty(Navigator.prototype, 'pdfViewerEnabled', { configurable: true, get: () => value });+ }, enabled);++ test('a browser with a built-in PDF viewer keeps it, with its search and printing', async ({ page }) => {+ await reportViewer(page, true);+ const dialog = await openPreview(page, 'doc_five');+ await expect(dialog.locator('iframe')).toHaveAttribute('src', '/api/media/doc_five');+ await expect(dialog.locator('[data-pdf-scroller]')).toHaveCount(0);+ });++ test('a browser without one gets the page reader', async ({ page }) => {+ await reportViewer(page, false);+ const dialog = await openPreview(page, 'doc_five');+ await expect(dialog.locator('iframe')).toHaveCount(0);+ await expect(dialog.locator('[data-pdf-page]')).toHaveCount(5);+ await expectDrawn(dialog, 1);+ });+});diff --git a/ui/e2e/pdf-preview/tsconfig.json b/ui/e2e/pdf-preview/tsconfig.jsonnew file mode 100644index 000000000..c787f3629--- /dev/null+++ b/ui/e2e/pdf-preview/tsconfig.json@@ -0,0 +1,8 @@+{+ "extends": "../../tsconfig.app.json",+ "compilerOptions": {+ "types": ["vite/client", "node"]+ },+ "include": ["*.ts", "*.tsx", "../../playwright.pdf-preview.config.ts", "../../agentation.d.ts"],+ "exclude": []+}diff --git a/ui/package-lock.json b/ui/package-lock.jsonindex 9494962ec..57da3dfe7 100644--- a/ui/package-lock.json+++ b/ui/package-lock.json@@ -46,6 +46,7 @@"lucide-react": "^0.562.0","monaco-editor": "^0.57.0","papaparse": "^5.7.0",+ "pdfjs-dist": "^6.4.299","pptxviewjs": "^1.1.9","qrcode.react": "^4.2.0","react": "^19.3.1",@@ -1537,6 +1538,271 @@"react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"}},+ "node_modules/@napi-rs/canvas": {+ "version": "1.0.13",+ "resolved": "https://registry.npmjs.org/@napi-rs/canvas/-/canvas-1.0.13.tgz",+ "integrity": "sha512-b87ugZpQ5ViwqVBkjervxpwa0GmJPsKHLBNoZN6GpjWeHzTbDUGryriq+2DYW2XbB+xCqSIINQysC0/I87nPbw==",+ "license": "MIT",+ "optional": true,+ "workspaces": [+ "e2e/*"+ ],+ "engines": {+ "node": ">= 10"+ },+ "funding": {+ "type": "github",+ "url": "https://github.com/sponsors/Brooooooklyn"+ },+ "optionalDependencies": {+ "@napi-rs/canvas-android-arm64": "1.0.13",+ "@napi-rs/canvas-darwin-arm64": "1.0.13",+ "@napi-rs/canvas-darwin-x64": "1.0.13",+ "@napi-rs/canvas-linux-arm-gnueabihf": "1.0.13",+ "@napi-rs/canvas-linux-arm64-gnu": "1.0.13",+ "@napi-rs/canvas-linux-arm64-musl": "1.0.13",+ "@napi-rs/canvas-linux-riscv64-gnu": "1.0.13",+ "@napi-rs/canvas-linux-x64-gnu": "1.0.13",+ "@napi-rs/canvas-linux-x64-musl": "1.0.13",+ "@napi-rs/canvas-win32-arm64-msvc": "1.0.13",+ "@napi-rs/canvas-win32-x64-msvc": "1.0.13"+ }+ },+ "node_modules/@napi-rs/canvas-android-arm64": {+ "version": "1.0.13",+ "resolved": "https://registry.npmjs.org/@napi-rs/canvas-android-arm64/-/canvas-android-arm64-1.0.13.tgz",+ "integrity": "sha512-trn7AaDzQ+yeeA0ezhkC7cB1ZDPtp4UYk0EvB5F8qtYL9jEVWtVrU30WOpI52DszoTxaTsCgf5PP4ZVaOlOIHQ==",+ "cpu": [+ "arm64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "android"+ ],+ "engines": {+ "node": ">= 10"+ },+ "funding": {+ "type": "github",+ "url": "https://github.com/sponsors/Brooooooklyn"+ }+ },+ "node_modules/@napi-rs/canvas-darwin-arm64": {+ "version": "1.0.13",+ "resolved": "https://registry.npmjs.org/@napi-rs/canvas-darwin-arm64/-/canvas-darwin-arm64-1.0.13.tgz",+ "integrity": "sha512-6Y2djEetL8copxfNK6hg7oig/xqTwOchv19hBOm+zBNdKc/02Z3kjZN5wrLzv/l5O04/bAQS6I/AlQpwBlXgPg==",+ "cpu": [+ "arm64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "darwin"+ ],+ "engines": {+ "node": ">= 10"+ },+ "funding": {+ "type": "github",+ "url": "https://github.com/sponsors/Brooooooklyn"+ }+ },+ "node_modules/@napi-rs/canvas-darwin-x64": {+ "version": "1.0.13",+ "resolved": "https://registry.npmjs.org/@napi-rs/canvas-darwin-x64/-/canvas-darwin-x64-1.0.13.tgz",+ "integrity": "sha512-OZoPwwu4TMMg8GTCFBrg8UFExwQTU/55i3uTC/izDat6PHE+ro9dimyQoOboFf91+NY7Pnn70ScR8f8I8+eZXw==",+ "cpu": [+ "x64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "darwin"+ ],+ "engines": {+ "node": ">= 10"+ },+ "funding": {+ "type": "github",+ "url": "https://github.com/sponsors/Brooooooklyn"+ }+ },+ "node_modules/@napi-rs/canvas-linux-arm-gnueabihf": {+ "version": "1.0.13",+ "resolved": "https://registry.npmjs.org/@napi-rs/canvas-linux-arm-gnueabihf/-/canvas-linux-arm-gnueabihf-1.0.13.tgz",+ "integrity": "sha512-dZSYaKROuswU2eUWG5aBKndNVe68elo1YX93xtRax7q658tj8rYE+HwduovWJgs6qwv1r1/CLR60nDeBbyjvyg==",+ "cpu": [+ "arm"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">= 10"+ },+ "funding": {+ "type": "github",+ "url": "https://github.com/sponsors/Brooooooklyn"+ }+ },+ "node_modules/@napi-rs/canvas-linux-arm64-gnu": {+ "version": "1.0.13",+ "resolved": "https://registry.npmjs.org/@napi-rs/canvas-linux-arm64-gnu/-/canvas-linux-arm64-gnu-1.0.13.tgz",+ "integrity": "sha512-W6FzYE8ADHI93In1vi80KBL6btDtAMuj50Ch5ptkWS8xuntyzbR4h5JDUmXoakxZn2yMInksudTEqVSWoFqFGg==",+ "cpu": [+ "arm64"+ ],+ "libc": [+ "glibc"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">= 10"+ },+ "funding": {+ "type": "github",+ "url": "https://github.com/sponsors/Brooooooklyn"+ }+ },+ "node_modules/@napi-rs/canvas-linux-arm64-musl": {+ "version": "1.0.13",+ "resolved": "https://registry.npmjs.org/@napi-rs/canvas-linux-arm64-musl/-/canvas-linux-arm64-musl-1.0.13.tgz",+ "integrity": "sha512-+6fQ+rR49hkAa4voh2stLIwQ+4t+gmoseM8Bc53/DlMXb73IKymDPWK9A/NtWeqTW4ijWppYuyuxS2hPI8tzNA==",+ "cpu": [+ "arm64"+ ],+ "libc": [+ "musl"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">= 10"+ },+ "funding": {+ "type": "github",+ "url": "https://github.com/sponsors/Brooooooklyn"+ }+ },+ "node_modules/@napi-rs/canvas-linux-riscv64-gnu": {+ "version": "1.0.13",+ "resolved": "https://registry.npmjs.org/@napi-rs/canvas-linux-riscv64-gnu/-/canvas-linux-riscv64-gnu-1.0.13.tgz",+ "integrity": "sha512-17XEMO3t9RW/TtHtSFf2AkaSWuCWdzbPeidz7zrOFawHxoLSJCnpTI1hN5aUvrZNnkNNmt/vZVJCh0ryUaik3A==",+ "cpu": [+ "riscv64"+ ],+ "libc": [+ "glibc"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">= 10"+ },+ "funding": {+ "type": "github",+ "url": "https://github.com/sponsors/Brooooooklyn"+ }+ },+ "node_modules/@napi-rs/canvas-linux-x64-gnu": {+ "version": "1.0.13",+ "resolved": "https://registry.npmjs.org/@napi-rs/canvas-linux-x64-gnu/-/canvas-linux-x64-gnu-1.0.13.tgz",+ "integrity": "sha512-lc48IwknVHBsNzXn9Xs5OkUsRX38Ml3Z1aOybQ51mMEzZckm8+4A0qROlyYHEoBPzr1kL3IU8ZUAqDb7wtdX8w==",+ "cpu": [+ "x64"+ ],+ "libc": [+ "glibc"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">= 10"+ },+ "funding": {+ "type": "github",+ "url": "https://github.com/sponsors/Brooooooklyn"+ }+ },+ "node_modules/@napi-rs/canvas-linux-x64-musl": {+ "version": "1.0.13",+ "resolved": "https://registry.npmjs.org/@napi-rs/canvas-linux-x64-musl/-/canvas-linux-x64-musl-1.0.13.tgz",+ "integrity": "sha512-bGY1r26S2bs8UNFJcPWcCSNdCFbKep5PATsvtgtC9A5u6SRgr2/jJE/RlhHL5m9ZpixNDhVkLY/5h16Kx5V9YA==",+ "cpu": [+ "x64"+ ],+ "libc": [+ "musl"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">= 10"+ },+ "funding": {+ "type": "github",+ "url": "https://github.com/sponsors/Brooooooklyn"+ }+ },+ "node_modules/@napi-rs/canvas-win32-arm64-msvc": {+ "version": "1.0.13",+ "resolved": "https://registry.npmjs.org/@napi-rs/canvas-win32-arm64-msvc/-/canvas-win32-arm64-msvc-1.0.13.tgz",+ "integrity": "sha512-2vRPBZoaIvJrUkDhZTE2Hn0YrnpJRCWVM3EJACoBKjuWI6WgPxGtUoRMsxRviCsyks8bKTSFdyw14CellnG2qA==",+ "cpu": [+ "arm64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "win32"+ ],+ "engines": {+ "node": ">= 10"+ },+ "funding": {+ "type": "github",+ "url": "https://github.com/sponsors/Brooooooklyn"+ }+ },+ "node_modules/@napi-rs/canvas-win32-x64-msvc": {+ "version": "1.0.13",+ "resolved": "https://registry.npmjs.org/@napi-rs/canvas-win32-x64-msvc/-/canvas-win32-x64-msvc-1.0.13.tgz",+ "integrity": "sha512-DUlW3lZPz63kYsf8mxgyyZuevhlH0T3/BGP9l+NazhB0F1+gL6D4hqZdnhZpw24DlWmTpATDcyYSZlJOi+DdfQ==",+ "cpu": [+ "x64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "win32"+ ],+ "engines": {+ "node": ">= 10"+ },+ "funding": {+ "type": "github",+ "url": "https://github.com/sponsors/Brooooooklyn"+ }+ },"node_modules/@noble/curves": {"version": "2.4.0","resolved": "https://registry.npmjs.org/@noble/curves/-/curves-2.4.0.tgz",@@ -6823,6 +7089,18 @@"dev": true,"license": "MIT"},+ "node_modules/pdfjs-dist": {+ "version": "6.4.299",+ "resolved": "https://registry.npmjs.org/pdfjs-dist/-/pdfjs-dist-6.4.299.tgz",+ "integrity": "sha512-AVl138zALtfaAPvADulE0PZThbYzCBS79nL4pOSL/6Sm/4AH5A21BD9VHt97OlCuzJuCpmeZtAtkinisF4Vb1g==",+ "license": "Apache-2.0",+ "engines": {+ "node": ">=22.13.0 || >=24"+ },+ "optionalDependencies": {+ "@napi-rs/canvas": "^1.0.10"+ }+ },"node_modules/picocolors": {"version": "1.1.1","resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz",diff --git a/ui/package.json b/ui/package.jsonindex a2180fa1b..fef4f5aff 100644--- a/ui/package.json+++ b/ui/package.json@@ -29,6 +29,7 @@"test:project-order": "tsc -p e2e/project-order/tsconfig.json --noEmit && playwright test --config playwright.project-order.config.ts","test:session-dot": "tsc -p e2e/session-dot/tsconfig.json --noEmit && playwright test --config playwright.session-dot.config.ts","test:queue-attachments": "tsc -p e2e/queue-attachments/tsconfig.json --noEmit && playwright test --config playwright.queue-attachments.config.ts",+ "test:pdf-preview": "tsc -p e2e/pdf-preview/tsconfig.json --noEmit && playwright test --config playwright.pdf-preview.config.ts","test:model-catalog": "tsc -p e2e/model-catalog/tsconfig.json --noEmit && playwright test --config playwright.model-catalog.config.ts","test:model-provider": "tsc -p e2e/model-provider/tsconfig.json --noEmit && playwright test --config playwright.model-provider.config.ts","test:onboarding-fidelity": "tsc -p e2e/onboarding-fidelity/tsconfig.json --noEmit && playwright test --config playwright.onboarding-fidelity.config.ts",@@ -78,6 +79,7 @@"lucide-react": "^0.562.0","monaco-editor": "^0.57.0","papaparse": "^5.7.0",+ "pdfjs-dist": "^6.4.299","pptxviewjs": "^1.1.9","qrcode.react": "^4.2.0","react": "^19.3.1",diff --git a/ui/playwright.pdf-preview.config.ts b/ui/playwright.pdf-preview.config.tsnew file mode 100644index 000000000..066022c5f--- /dev/null+++ b/ui/playwright.pdf-preview.config.ts@@ -0,0 +1,27 @@+import { defineConfig, devices } from '@playwright/test';++// Hermetic: the dev server points at a dead backend port and every+// `/api/media/...` request is answered by the spec with a generated PDF.+//+// The page reader is what phones and tablets get, so it runs on an Android+// device in Chromium and on an iPhone in WebKit (the engine whose framed viewer+// showed only page one). The desktop project checks which reader a desktop+// browser is given.+export default defineConfig({+ testDir: './e2e/pdf-preview',+ outputDir: './e2e/.artifacts/pdf-preview',+ workers: 1,+ retries: 0,+ use: { baseURL: 'http://127.0.0.1:5231', locale: 'en-US', trace: 'retain-on-failure' },+ webServer: {+ command: 'npm run dev -- --host 127.0.0.1 --port 5231 --strictPort',+ env: { VIBE_UI_BACKEND: 'http://127.0.0.1:9' },+ url: 'http://127.0.0.1:5231/e2e/pdf-preview/fixture.html',+ reuseExistingServer: true,+ },+ projects: [+ { name: 'desktop', use: { ...devices['Desktop Chrome'] } },+ { name: 'android', use: { ...devices['Pixel 7'] } },+ { name: 'iphone-webkit', use: { ...devices['iPhone 13'] } },+ ],+});diff --git a/ui/src/components/ui/file-preview.test.tsx b/ui/src/components/ui/file-preview.test.tsxindex fe14ffeb4..a94de5ddb 100644--- a/ui/src/components/ui/file-preview.test.tsx+++ b/ui/src/components/ui/file-preview.test.tsx@@ -1,7 +1,7 @@/* @vitest-environment jsdom */import { act, cleanup, render } from '@testing-library/react';-import { afterEach, beforeEach, describe, expect, it } from 'vitest';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import {EDITOR_FONT_DEFAULT,@@ -10,6 +10,11 @@ import {} from '@/lib/editorFontSize';import { FilePreview } from './file-preview';+// The page reader pulls in pdf.js; these tests only decide which reader a PDF gets.+vi.mock('@/components/ui/pdf-pages', () => ({+ default: ({ url }: { url: string }) => <div data-testid="pdf-pages" data-url={url} />,+}));+beforeEach(resetEditorFontSize);afterEach(() => {@@ -52,3 +57,25 @@ describe('FilePreview font size', () => {expect(preview?.style.getPropertyValue('--vr-fileview-font-size')).toBe(`${EDITOR_FONT_DEFAULT + 3}px`);});});++describe('FilePreview PDF', () => {+ const setViewer = (value: boolean | undefined) =>+ Object.defineProperty(window.navigator, 'pdfViewerEnabled', { configurable: true, get: () => value });++ afterEach(() => {+ Reflect.deleteProperty(window.navigator, 'pdfViewerEnabled');+ });++ it('frames the browser viewer where it is a real reader', () => {+ setViewer(true);+ const { container } = render(<FilePreview source={{ name: 'report.pdf', url: '/api/media/tok' }} />);+ expect(container.querySelector('iframe')?.getAttribute('src')).toBe('/api/media/tok');+ });++ it('draws the pages itself where the browser has no viewer', async () => {+ setViewer(false);+ const { container, findByTestId } = render(<FilePreview source={{ name: 'report.pdf', url: '/api/media/tok' }} />);+ expect((await findByTestId('pdf-pages')).getAttribute('data-url')).toBe('/api/media/tok');+ expect(container.querySelector('iframe')).toBeNull();+ });+});diff --git a/ui/src/components/ui/file-preview.tsx b/ui/src/components/ui/file-preview.tsxindex 462cfb911..420745578 100644--- a/ui/src/components/ui/file-preview.tsx+++ b/ui/src/components/ui/file-preview.tsx@@ -24,6 +24,7 @@ import {previewRenderKind,} from '@/lib/filePreview';import { FIT_VIEW, type ImageView, clampPan, maxScale, oneToOneScale, toggleScale, zoomPercent, zoomToPoint } from '@/lib/imageZoom';+import { shouldRenderPdfPages } from '@/lib/pdfPreview';import { useLatestRef } from '@/lib/useLatestRef';// ── Shared file-preview kernel ("Quick Look") ───────────────────────────────@@ -51,6 +52,8 @@ export type PreviewSource = {// JSON tree is the one renderer whose dep can't be dynamic-imported inline (it's a component), so it// lives in its own module and loads lazily here.const PreviewJson = React.lazy(() => import('@/components/ui/preview-json'));+// pdf.js and its worker, for the PDF reader used on phones and tablets (see PdfView).+const PdfPages = React.lazy(() => import('@/components/ui/pdf-pages'));// ``onDownload`` — optional. When set, the image renderer's control cluster shows a download button// that invokes it. Only a shell that does NOT already expose its own download affordance should pass@@ -306,10 +309,20 @@ const ImageBody: React.FC<{ src: string; name?: string; className?: string; onDo};// ── PDF ───────────────────────────────────────────────────────────────────────-// The browser's built-in viewer via an <iframe>. The backend serves PDF inline (same-origin), so no-// JS engine is shipped and it frames without CSP trouble.+// The browser's built-in viewer via an <iframe> where it is a real reader (desktop browsers and the+// desktop shells): the backend serves PDF inline (same-origin), so it frames without CSP trouble and+// brings search, selection and printing. Phones and tablets get our own page renderer instead+// (``shouldRenderPdfPages``): iOS paints only a framed PDF's first page and Android frames nothing.const PdfView: React.FC<{ url: string; className?: string }> = ({ url, className }) => {const { t } = useTranslation();+ const [renderPages] = React.useState(() => shouldRenderPdfPages());+ if (renderPages) {+ return (+ <React.Suspense fallback={<Spinner className={clsx('!bg-[#0c0c0f]', className)} />}>+ <PdfPages key={url} url={url} className={className} />+ </React.Suspense>+ );+ }return <iframe title={t('preview.title')} src={url} className={clsx('h-full w-full border-0 bg-white', className)} />;};diff --git a/ui/src/components/ui/pdf-pages.tsx b/ui/src/components/ui/pdf-pages.tsxnew file mode 100644index 000000000..da326e29e--- /dev/null+++ b/ui/src/components/ui/pdf-pages.tsx@@ -0,0 +1,419 @@+import * as React from 'react';+import { useTranslation } from 'react-i18next';+import { Loader2, Maximize2, ZoomIn, ZoomOut } from 'lucide-react';+import clsx from 'clsx';+import type { PDFDocumentLoadingTask, PDFDocumentProxy, PDFWorker, RenderTask } from 'pdfjs-dist/legacy/build/pdf.mjs';+import PdfjsWorker from 'pdfjs-dist/legacy/build/pdf.worker.mjs?worker';++import { Button } from '@/components/ui/button';+import { PDF_MAX_ZOOM, PDF_MIN_ZOOM, clampPdfZoom, pageIndexAt, pdfOutputScale, stepPdfZoom } from '@/lib/pdfPreview';++// ── PDF pages ────────────────────────────────────────────────────────────────+// Every page of a PDF drawn to its own canvas by pdf.js, stacked in one scroller: the reader used+// where framing the browser's viewer does not work (see ``shouldRenderPdfPages``). Default export so+// the file-preview kernel can ``React.lazy`` it; pdf.js, its worker and its data files load only when+// a PDF is opened here.+//+// - Pages are fit to the viewer's width; zoom (buttons, pinch, ctrl-wheel) scales that width and+// keeps the point under the fingers / centre in place.+// - Only pages within a screen and a half of the viewport hold a canvas. Others are released, which+// is what keeps a long document inside iOS's canvas memory budget.+// - The legacy build is used on purpose: the modern one relies on JavaScript features newer than the+// mobile Safari versions people still run.++// pdf.js asks for its CMaps (needed by many CJK PDFs), standard fonts and image-decoder wasm by file+// name. They are bundled as hashed assets and served from this table, so no unhashed directory has+// to exist beside the build and a stale tab never mixes files from two pdf.js versions.+const byFileName = (modules: Record<string, string>): Record<string, string> =>+ Object.fromEntries(Object.entries(modules).map(([path, url]) => [path.slice(path.lastIndexOf('/') + 1), url]));++const BUNDLED_DATA: Record<string, Record<string, string>> = {+ cMapUrl: byFileName(import.meta.glob<string>('/node_modules/pdfjs-dist/cmaps/*.bcmap', { query: '?url', import: 'default', eager: true })),+ standardFontDataUrl: byFileName(+ import.meta.glob<string>(['/node_modules/pdfjs-dist/standard_fonts/*.pfb', '/node_modules/pdfjs-dist/standard_fonts/*.ttf'], { query: '?url', import: 'default', eager: true }),+ ),+ wasmUrl: byFileName(import.meta.glob<string>(['/node_modules/pdfjs-dist/wasm/*.wasm', '!**/quickjs-*'], { query: '?url', import: 'default', eager: true })),+};++// pdf.js's ``BinaryDataFactory`` contract: constructed with its base URLs (unused here) and asked to+// ``fetch({ kind, filename })`` the bytes of one data file.+class BundledDataFactory {+ async fetch({ kind, filename }: { kind: string; filename: string }): Promise<Uint8Array> {+ const url = BUNDLED_DATA[kind]?.[filename];+ if (!url) throw new Error(`pdf.js data file is not bundled: ${filename}`);+ const res = await fetch(url);+ if (!res.ok) throw new Error(`pdf.js data file ${filename}: HTTP ${res.status}`);+ return new Uint8Array(await res.arrayBuffer());+ }+}++// Pages this far (as a fraction of the viewport) above or below the visible area keep a canvas.+const NEAR_MARGIN = '150% 0px';+// Horizontal padding around the page column (px-3 on each side), taken out of the fit width.+const PAGE_GUTTER = 24;+const WHEEL_ZOOM_SENSITIVITY = 0.01;+// Overlay buttons on the dark mat; matches the image preview's control cluster in file-preview.tsx.+const ZOOM_BTN = 'h-8 w-8 rounded-full text-white hover:bg-white/15 hover:text-white';++// One IntersectionObserver for every page, rooted at the scroller. Pages register before the+// scroller's effect attaches it (child effects run first), so ``attach`` observes what is waiting.+class PageWatch {+ private io: IntersectionObserver | null = null;+ private readonly callbacks = new Map<Element, (near: boolean) => void>();++ attach(root: HTMLElement): () => void {+ const io = new IntersectionObserver(+ (entries) => {+ for (const entry of entries) this.callbacks.get(entry.target)?.(entry.isIntersecting);+ },+ { root, rootMargin: NEAR_MARGIN },+ );+ this.io = io;+ for (const el of this.callbacks.keys()) io.observe(el);+ return () => {+ io.disconnect();+ if (this.io === io) this.io = null;+ };+ }++ observe(el: Element, onChange: (near: boolean) => void): () => void {+ this.callbacks.set(el, onChange);+ this.io?.observe(el);+ return () => {+ this.callbacks.delete(el);+ this.io?.unobserve(el);+ };+ }+}++type DocState = { status: 'loading' } | { status: 'error' } | { status: 'ready'; doc: PDFDocumentProxy; ratio: number };++type Anchor = { page: number; fx: number; fy: number; ax: number; ay: number };++const pageElements = (content: HTMLElement | null): HTMLElement[] =>+ content ? Array.from(content.querySelectorAll<HTMLElement>('[data-pdf-page]')) : [];++export default function PdfPages({ url, className }: { url: string; className?: string }) {+ const { t } = useTranslation();+ const [state, setState] = React.useState<DocState>({ status: 'loading' });++ // Callers key this component by URL, so a new file always starts from 'loading'.+ React.useEffect(() => {+ let alive = true;+ let port: Worker | null = null;+ let worker: PDFWorker | null = null;+ let task: PDFDocumentLoadingTask | null = null;+ (async () => {+ try {+ const pdfjs = await import('pdfjs-dist/legacy/build/pdf.mjs');+ if (!alive) return;+ // A worker per document, terminated with it, so closing the preview gives its memory back.+ // (pdf.js's generated typings declare ``port`` as null-only; it takes a Worker at runtime.)+ port = new PdfjsWorker();+ worker = new pdfjs.PDFWorker({ port } as unknown as ConstructorParameters<typeof pdfjs.PDFWorker>[0]);+ task = pdfjs.getDocument({+ // Same-origin content URL: pdf.js fetches it with the session cookie, by ranges when the+ // server allows, so the first page can draw before a large file has fully arrived.+ url: new URL(url, window.location.href).href,+ worker,+ useWorkerFetch: false,+ BinaryDataFactory: BundledDataFactory,+ });+ const doc = await task.promise;+ const first = await doc.getPage(1);+ const viewport = first.getViewport({ scale: 1 });+ if (alive) setState({ status: 'ready', doc, ratio: viewport.height / viewport.width });+ } catch {+ if (alive) setState({ status: 'error' });+ }+ })();+ return () => {+ alive = false;+ const pending = task ? task.destroy() : Promise.resolve();+ void pending+ .catch(() => undefined)+ .finally(() => {+ worker?.destroy();+ port?.terminate();+ });+ };+ }, [url]);++ if (state.status === 'error') {+ return <div className={clsx('grid h-full min-h-0 place-items-center bg-surface p-6 text-center text-[12.5px] text-muted', className)}>{t('preview.failed')}</div>;+ }+ if (state.status === 'loading') {+ return (+ <div className={clsx('grid h-full min-h-0 place-items-center bg-[#0c0c0f] text-muted', className)} data-pdf-loading>+ <Loader2 className="size-5 animate-spin" />+ </div>+ );+ }+ return <PdfReader doc={state.doc} ratio={state.ratio} className={className} />;+}++function PdfReader({ doc, ratio, className }: { doc: PDFDocumentProxy; ratio: number; className?: string }) {+ const { t } = useTranslation();+ const [scroller, setScroller] = React.useState<HTMLDivElement | null>(null);+ const contentRef = React.useRef<HTMLDivElement>(null);+ const [watch] = React.useState(() => new PageWatch());+ const [fitWidth, setFitWidth] = React.useState(0);+ const [zoom, setZoom] = React.useState(1);+ const zoomRef = React.useRef(1);+ const anchorRef = React.useRef<Anchor | null>(null);+ const [current, setCurrent] = React.useState(1);+ const total = doc.numPages;+ const pageWidth = Math.max(0, Math.round(fitWidth * zoom));++ React.useEffect(() => {+ if (!scroller) return;+ return watch.attach(scroller);+ }, [scroller, watch]);++ // Fit width follows the scroller (sheet resize, rotation). The observer reports once on observe.+ React.useEffect(() => {+ if (!scroller || typeof ResizeObserver === 'undefined') return;+ const ro = new ResizeObserver(() => setFitWidth(Math.max(0, Math.floor(scroller.clientWidth - PAGE_GUTTER))));+ ro.observe(scroller);+ return () => ro.disconnect();+ }, [scroller]);++ // The page indicator: the page crossing 30% of the viewport's height.+ React.useEffect(() => {+ if (!scroller) return;+ let frame = 0;+ const update = () => {+ frame = 0;+ const pages = pageElements(contentRef.current);+ if (!pages.length) return;+ const tops = pages.map((p) => p.offsetTop);+ setCurrent(pageIndexAt(tops, scroller.scrollTop + scroller.clientHeight * 0.3) + 1);+ };+ const onScroll = () => {+ if (!frame) frame = requestAnimationFrame(update);+ };+ scroller.addEventListener('scroll', onScroll, { passive: true });+ return () => {+ scroller.removeEventListener('scroll', onScroll);+ if (frame) cancelAnimationFrame(frame);+ };+ }, [scroller]);++ // Zoom about a point of the scroller's viewport (default: its centre). The point is remembered as+ // a position within the page under it, then restored after layout, so it stays put on screen+ // regardless of the fixed gaps between pages.+ const applyZoom = React.useCallback(+ (next: number, ax?: number, ay?: number) => {+ const target = clampPdfZoom(next);+ if (!scroller || Math.abs(target - zoomRef.current) < 0.001) return;+ const x = ax ?? scroller.clientWidth / 2;+ const y = ay ?? scroller.clientHeight / 2;+ const pages = pageElements(contentRef.current);+ if (pages.length) {+ const cx = scroller.scrollLeft + x;+ const cy = scroller.scrollTop + y;+ const index = pageIndexAt(pages.map((p) => p.offsetTop), cy);+ const page = pages[index];+ anchorRef.current = {+ page: index,+ fx: (cx - page.offsetLeft) / Math.max(1, page.offsetWidth),+ fy: (cy - page.offsetTop) / Math.max(1, page.offsetHeight),+ ax: x,+ ay: y,+ };+ }+ zoomRef.current = target;+ setZoom(target);+ },+ [scroller],+ );++ React.useLayoutEffect(() => {+ const anchor = anchorRef.current;+ if (!anchor || !scroller) return;+ anchorRef.current = null;+ const page = pageElements(contentRef.current)[anchor.page];+ if (!page) return;+ scroller.scrollTo({+ left: page.offsetLeft + anchor.fx * page.offsetWidth - anchor.ax,+ top: page.offsetTop + anchor.fy * page.offsetHeight - anchor.ay,+ });+ }, [zoom, scroller]);++ // Pinch zoom on touch screens and ctrl-wheel (trackpad pinch) elsewhere. While the fingers move the+ // column is only scaled with a transform; the zoom, and the re-render it costs, lands on release.+ // Single-finger scrolling stays native. The app's viewport disables page zoom, so without this a+ // phone could not zoom the document at all.+ React.useEffect(() => {+ const el = scroller;+ const content = contentRef.current;+ if (!el || !content) return;+ let pinch: { dist: number; ax: number; ay: number } | null = null;+ let ratio = 1;+ const spread = (touches: TouchList) => Math.hypot(touches[0].clientX - touches[1].clientX, touches[0].clientY - touches[1].clientY);+ const reset = () => {+ content.style.transform = '';+ content.style.transformOrigin = '';+ };+ const onStart = (e: TouchEvent) => {+ if (e.touches.length !== 2) return;+ const rect = el.getBoundingClientRect();+ const ax = (e.touches[0].clientX + e.touches[1].clientX) / 2 - rect.left;+ const ay = (e.touches[0].clientY + e.touches[1].clientY) / 2 - rect.top;+ pinch = { dist: Math.max(1, spread(e.touches)), ax, ay };+ ratio = 1;+ content.style.transformOrigin = `${el.scrollLeft + ax}px ${el.scrollTop + ay}px`;+ };+ const onMove = (e: TouchEvent) => {+ if (!pinch || e.touches.length !== 2) return;+ e.preventDefault();+ const z = zoomRef.current;+ ratio = clampPdfZoom((z * spread(e.touches)) / pinch.dist) / z;+ content.style.transform = `scale(${ratio})`;+ };+ const onEnd = (e: TouchEvent) => {+ if (!pinch || e.touches.length >= 2) return;+ const { ax, ay } = pinch;+ pinch = null;+ reset();+ if (Math.abs(ratio - 1) > 0.01) applyZoom(zoomRef.current * ratio, ax, ay);+ ratio = 1;+ };+ const onWheel = (e: WheelEvent) => {+ if (!e.ctrlKey) return;+ e.preventDefault();+ const rect = el.getBoundingClientRect();+ applyZoom(zoomRef.current * Math.exp(-e.deltaY * WHEEL_ZOOM_SENSITIVITY), e.clientX - rect.left, e.clientY - rect.top);+ };+ // iOS Safari starts its own page zoom from these unless told not to.+ const onGesture = (e: Event) => e.preventDefault();+ el.addEventListener('touchstart', onStart, { passive: true });+ el.addEventListener('touchmove', onMove, { passive: false });+ el.addEventListener('touchend', onEnd);+ el.addEventListener('touchcancel', onEnd);+ el.addEventListener('wheel', onWheel, { passive: false });+ el.addEventListener('gesturestart', onGesture);+ return () => {+ el.removeEventListener('touchstart', onStart);+ el.removeEventListener('touchmove', onMove);+ el.removeEventListener('touchend', onEnd);+ el.removeEventListener('touchcancel', onEnd);+ el.removeEventListener('wheel', onWheel);+ el.removeEventListener('gesturestart', onGesture);+ reset();+ };+ }, [scroller, applyZoom]);++ const pages = React.useMemo(() => Array.from({ length: total }, (_, i) => i + 1), [total]);+ const atFit = zoom <= PDF_MIN_ZOOM + 0.001;+ const atMax = zoom >= PDF_MAX_ZOOM - 0.001;++ return (+ <div className={clsx('relative h-full min-h-0 overflow-hidden bg-[#0c0c0f]', className)}>+ <div ref={setScroller} className="h-full overflow-auto overscroll-contain" style={{ touchAction: 'pan-x pan-y' }} data-pdf-scroller>+ {/* ``relative``: the pages' offsets are measured against this column, the scroller's content. */}+ <div ref={contentRef} className="relative flex w-max min-w-full flex-col items-center gap-3 px-3 pb-16 pt-3">+ {pageWidth > 0 &&+ pages.map((n) => <PdfPage key={n} doc={doc} pageNumber={n} width={pageWidth} fallbackRatio={ratio} watch={watch} label={t('preview.pdfPage', { page: n })} />)}+ </div>+ </div>+ {/* Control cluster, as in the image preview; the bottom padding of the column keeps the last+ page scrollable clear of it, and the safe-area inset keeps it off the home indicator. */}+ <div className="pointer-events-none absolute inset-x-0 bottom-0 flex justify-center p-3" style={{ paddingBottom: 'max(0.75rem, env(safe-area-inset-bottom))' }}>+ <div className="pointer-events-auto flex items-center gap-0.5 rounded-full bg-black/55 px-1 py-1 text-white shadow-lg backdrop-blur-sm">+ <Button variant="ghost" size="icon" className={ZOOM_BTN} onClick={() => applyZoom(stepPdfZoom(zoomRef.current, -1))} disabled={atFit} aria-label={t('preview.zoomOut')}>+ <ZoomOut className="size-4" />+ </Button>+ <span className="min-w-[3.25rem] text-center text-[11px] font-medium tabular-nums" aria-live="polite">+ {t('preview.zoomPercent', { percent: Math.round(zoom * 100) })}+ </span>+ <Button variant="ghost" size="icon" className={ZOOM_BTN} onClick={() => applyZoom(stepPdfZoom(zoomRef.current, 1))} disabled={atMax} aria-label={t('preview.zoomIn')}>+ <ZoomIn className="size-4" />+ </Button>+ <span className="mx-0.5 h-4 w-px bg-white/20" aria-hidden />+ <Button variant="ghost" size="icon" className={ZOOM_BTN} onClick={() => applyZoom(PDF_MIN_ZOOM)} disabled={atFit} aria-label={t('preview.resetZoom')}>+ <Maximize2 className="size-4" />+ </Button>+ <span className="mx-0.5 h-4 w-px bg-white/20" aria-hidden />+ <span className="px-2 text-[11px] font-medium tabular-nums" role="status" aria-label={t('preview.pdfPosition', { page: current, total })} data-pdf-position>+ {t('preview.pdfCounter', { page: current, total })}+ </span>+ </div>+ </div>+ </div>+ );+}++const PdfPage = React.memo(function PdfPage({+ doc,+ pageNumber,+ width,+ fallbackRatio,+ watch,+ label,+}: {+ doc: PDFDocumentProxy;+ pageNumber: number;+ width: number;+ fallbackRatio: number;+ watch: PageWatch;+ label: string;+}) {+ const boxRef = React.useRef<HTMLDivElement>(null);+ const [near, setNear] = React.useState(false);+ // The page's own proportions once loaded; until then the first page's stand in for its placeholder.+ const [ratio, setRatio] = React.useState<number | null>(null);++ React.useEffect(() => {+ const box = boxRef.current;+ if (!box) return;+ return watch.observe(box, setNear);+ }, [watch]);++ // The canvas lives outside React's tree (the box has no React children), so a re-render at a new+ // width keeps showing the previous bitmap, stretched, until the sharper one replaces it.+ React.useEffect(() => {+ const box = boxRef.current;+ if (!box) return;+ if (!near) {+ box.replaceChildren();+ delete box.dataset.pdfRendered;+ return;+ }+ let cancelled = false;+ let task: RenderTask | null = null;+ (async () => {+ const page = await doc.getPage(pageNumber);+ if (cancelled) return;+ const base = page.getViewport({ scale: 1 });+ const pageRatio = base.height / base.width;+ setRatio(pageRatio);+ const outputScale = pdfOutputScale(width, width * pageRatio, window.devicePixelRatio);+ const viewport = page.getViewport({ scale: (width / base.width) * outputScale });+ const canvas = document.createElement('canvas');+ canvas.width = Math.max(1, Math.floor(viewport.width));+ canvas.height = Math.max(1, Math.floor(viewport.height));+ canvas.className = 'absolute inset-0 h-full w-full';+ task = page.render({ canvas, viewport });+ await task.promise;+ if (cancelled) return;+ box.replaceChildren(canvas);+ box.dataset.pdfRendered = 'true';+ })().catch(() => undefined); // cancelled renders reject; a page that fails to draw stays blank+ return () => {+ cancelled = true;+ task?.cancel();+ };+ }, [doc, pageNumber, width, near]);++ return (+ <div+ ref={boxRef}+ role="img"+ aria-label={label}+ data-pdf-page={pageNumber}+ className="relative shrink-0 bg-white shadow-[0_1px_4px_rgba(0,0,0,0.5)]"+ style={{ width, height: Math.round(width * (ratio ?? fallbackRatio)) }}+ />+ );+});diff --git a/ui/src/i18n/en.json b/ui/src/i18n/en.jsonindex 838661c4d..9ce48563b 100644--- a/ui/src/i18n/en.json+++ b/ui/src/i18n/en.json@@ -189,6 +189,10 @@"zoomIn": "Zoom in","zoomOut": "Zoom out","resetZoom": "Reset zoom",+ "pdfPage": "Page {{page}}",+ "pdfPosition": "Page {{page}} of {{total}}",+ "pdfCounter": "{{page}} / {{total}}",+ "zoomPercent": "{{percent}}%","download": "Download"},"common": {diff --git a/ui/src/i18n/zh.json b/ui/src/i18n/zh.jsonindex 90610b163..57d85a07d 100644--- a/ui/src/i18n/zh.json+++ b/ui/src/i18n/zh.json@@ -189,6 +189,10 @@"zoomIn": "放大","zoomOut": "缩小","resetZoom": "重置缩放",+ "pdfPage": "第 {{page}} 页",+ "pdfPosition": "第 {{page}} 页,共 {{total}} 页",+ "pdfCounter": "{{page}} / {{total}}",+ "zoomPercent": "{{percent}}%","download": "下载"},"common": {diff --git a/ui/src/lib/pdfPreview.test.ts b/ui/src/lib/pdfPreview.test.tsnew file mode 100644index 000000000..f3b5c7b96--- /dev/null+++ b/ui/src/lib/pdfPreview.test.ts@@ -0,0 +1,98 @@+import { describe, expect, it } from 'vitest';++import {+ PDF_MAX_CANVAS_PIXELS,+ PDF_MAX_ZOOM,+ PDF_MIN_ZOOM,+ PDF_ZOOM_STEPS,+ clampPdfZoom,+ pageIndexAt,+ pdfOutputScale,+ shouldRenderPdfPages,+ stepPdfZoom,+} from './pdfPreview';++describe('shouldRenderPdfPages', () => {+ const desktop = { pdfViewerEnabled: true, ios: false, coarsePointer: false };++ it('keeps the built-in viewer for a desktop browser that has one', () => {+ expect(shouldRenderPdfPages(desktop)).toBe(false);+ // Older engines that do not report the flag are assumed to frame PDFs as before.+ expect(shouldRenderPdfPages({ ...desktop, pdfViewerEnabled: undefined })).toBe(false);+ });++ it('draws the pages itself where the browser has no viewer (Android Chrome)', () => {+ expect(shouldRenderPdfPages({ ...desktop, pdfViewerEnabled: false })).toBe(true);+ expect(shouldRenderPdfPages({ pdfViewerEnabled: false, ios: false, coarsePointer: true })).toBe(true);+ });++ it('draws the pages itself on iOS and iPadOS, which report a viewer that shows only page one', () => {+ expect(shouldRenderPdfPages({ ...desktop, ios: true })).toBe(true);+ // iPadOS with a trackpad still reports a fine pointer.+ expect(shouldRenderPdfPages({ pdfViewerEnabled: true, ios: true, coarsePointer: false })).toBe(true);+ });++ it('draws the pages itself on other touch-first devices', () => {+ expect(shouldRenderPdfPages({ ...desktop, coarsePointer: true })).toBe(true);+ });+});++describe('PDF zoom', () => {+ it('clamps to the fit-width range', () => {+ expect(clampPdfZoom(0.5)).toBe(PDF_MIN_ZOOM);+ expect(clampPdfZoom(1.8)).toBe(1.8);+ expect(clampPdfZoom(9)).toBe(PDF_MAX_ZOOM);+ expect(clampPdfZoom(Number.NaN)).toBe(PDF_MIN_ZOOM);+ });++ it('steps along the ladder from any zoom, including one reached by pinching', () => {+ expect(stepPdfZoom(1, 1)).toBe(1.25);+ expect(stepPdfZoom(1.25, 1)).toBe(1.5);+ expect(stepPdfZoom(1.7, 1)).toBe(2);+ expect(stepPdfZoom(1.7, -1)).toBe(1.5);+ expect(stepPdfZoom(2, -1)).toBe(1.5);+ expect(stepPdfZoom(PDF_MAX_ZOOM, 1)).toBe(PDF_MAX_ZOOM);+ expect(stepPdfZoom(PDF_MIN_ZOOM, -1)).toBe(PDF_MIN_ZOOM);+ });++ it('walks every step up and back down', () => {+ const up: number[] = [];+ for (let z: number = PDF_MIN_ZOOM; z < PDF_MAX_ZOOM; z = stepPdfZoom(z, 1)) up.push(z);+ expect([...up, PDF_MAX_ZOOM]).toEqual([...PDF_ZOOM_STEPS]);+ });+});++describe('pdfOutputScale', () => {+ it('uses the display pixel ratio while the bitmap fits the budget', () => {+ expect(pdfOutputScale(390, 505, 3)).toBe(3);+ expect(pdfOutputScale(390, 505, 1)).toBe(1);+ });++ it('caps the bitmap area for zoomed-in pages', () => {+ const scale = pdfOutputScale(1560, 2020, 3);+ expect(scale).toBeLessThan(3);+ expect(1560 * scale * 2020 * scale).toBeLessThanOrEqual(PDF_MAX_CANVAS_PIXELS * 1.0001);+ });++ it('treats a missing or invalid pixel ratio as 1', () => {+ expect(pdfOutputScale(390, 505, 0)).toBe(1);+ expect(pdfOutputScale(390, 505, Number.NaN)).toBe(1);+ });+});++describe('pageIndexAt', () => {+ const tops = [12, 520, 1028, 1536];++ it('finds the page whose top is the last one at or above the point', () => {+ expect(pageIndexAt(tops, 0)).toBe(0);+ expect(pageIndexAt(tops, 12)).toBe(0);+ expect(pageIndexAt(tops, 519)).toBe(0);+ expect(pageIndexAt(tops, 520)).toBe(1);+ expect(pageIndexAt(tops, 1200)).toBe(2);+ expect(pageIndexAt(tops, 99999)).toBe(3);+ });++ it('is 0 for an empty document', () => {+ expect(pageIndexAt([], 100)).toBe(0);+ });+});diff --git a/ui/src/lib/pdfPreview.ts b/ui/src/lib/pdfPreview.tsnew file mode 100644index 000000000..b3f970a5d--- /dev/null+++ b/ui/src/lib/pdfPreview.ts@@ -0,0 +1,83 @@+import { isIosDevice } from '@/lib/platform';++// Pure decisions behind the PDF preview (components/ui/pdf-pages.tsx), kept free of pdf.js so they+// can be unit-tested and shared without pulling the engine into any bundle.++export type PdfViewerEnv = {+ /** ``navigator.pdfViewerEnabled``; undefined where the browser does not report it. */+ pdfViewerEnabled: boolean | undefined;+ ios: boolean;+ coarsePointer: boolean;+};++export function currentPdfViewerEnv(): PdfViewerEnv {+ if (typeof navigator === 'undefined') return { pdfViewerEnabled: undefined, ios: false, coarsePointer: false };+ return {+ pdfViewerEnabled: typeof navigator.pdfViewerEnabled === 'boolean' ? navigator.pdfViewerEnabled : undefined,+ ios: isIosDevice(),+ coarsePointer: typeof window !== 'undefined' && !!window.matchMedia?.('(pointer: coarse)').matches,+ };+}++// Whether to draw the pages ourselves instead of framing the browser's built-in viewer. The built-in+// viewer is kept where it is a real, scrollable reader (desktop Chrome / Edge / Firefox / Safari, and+// the macOS / Windows desktop shells), because it brings search, selection and printing for free.+// It is replaced where framing it fails:+// - iOS / iPadOS WebKit paints only the first page of a framed PDF, as a static image that cannot be+// scrolled, while still reporting a viewer;+// - Android Chrome (and other engines without one) report ``pdfViewerEnabled === false`` and show+// nothing or a download prompt in the frame;+// - any other touch-first device, so phones and tablets all get the one reader that is built for+// touch here (pinch zoom inside the sheet) and covered by the mobile browser tests.+export function shouldRenderPdfPages(env: PdfViewerEnv = currentPdfViewerEnv()): boolean {+ if (env.pdfViewerEnabled === false) return true;+ return env.ios || env.coarsePointer;+}++// Zoom is relative to "fit width" (1 = each page as wide as the viewer). Buttons walk this ladder;+// pinch and ctrl-wheel are continuous and clamped to the same range.+export const PDF_ZOOM_STEPS = [1, 1.25, 1.5, 2, 3, 4] as const;+export const PDF_MIN_ZOOM = PDF_ZOOM_STEPS[0];+export const PDF_MAX_ZOOM = PDF_ZOOM_STEPS[PDF_ZOOM_STEPS.length - 1];++export function clampPdfZoom(zoom: number): number {+ if (!Number.isFinite(zoom)) return PDF_MIN_ZOOM;+ return Math.min(PDF_MAX_ZOOM, Math.max(PDF_MIN_ZOOM, zoom));+}++/** The next ladder step strictly above (dir 1) or below (dir -1) ``zoom``, clamped to the range. */+export function stepPdfZoom(zoom: number, dir: 1 | -1): number {+ const eps = 0.001;+ if (dir > 0) return PDF_ZOOM_STEPS.find((s) => s > zoom + eps) ?? PDF_MAX_ZOOM;+ for (let i = PDF_ZOOM_STEPS.length - 1; i >= 0; i -= 1) if (PDF_ZOOM_STEPS[i] < zoom - eps) return PDF_ZOOM_STEPS[i];+ return PDF_MIN_ZOOM;+}++// Device pixels per CSS pixel for one page's canvas: the display's pixel ratio, reduced when the+// bitmap would exceed ``maxPixels``. iOS refuses canvases much past 16.7M pixels and keeps a small+// budget for all canvases together, so a zoomed-in page is drawn a little softer rather than blank.+export const PDF_MAX_CANVAS_PIXELS = 2 ** 23;++export function pdfOutputScale(cssWidth: number, cssHeight: number, devicePixelRatio: number, maxPixels = PDF_MAX_CANVAS_PIXELS): number {+ const ratio = Number.isFinite(devicePixelRatio) && devicePixelRatio > 0 ? devicePixelRatio : 1;+ const area = cssWidth * cssHeight;+ if (!(area > 0)) return ratio;+ return Math.min(ratio, Math.sqrt(maxPixels / area));+}++/** Index of the page containing ``y`` given each page's top offset in ascending order (0 when empty). */+export function pageIndexAt(tops: readonly number[], y: number): number {+ let lo = 0;+ let hi = tops.length - 1;+ let found = 0;+ while (lo <= hi) {+ const mid = (lo + hi) >> 1;+ if (tops[mid] <= y) {+ found = mid;+ lo = mid + 1;+ } else {+ hi = mid - 1;+ }+ }+ return found;+}