Make PDF Viewer a native feature

- Move the PDF Viewer plugin into src/lib/features/pdf/ with pdfjs-dist 6.4.299 (minified builds).
- A static pdf document type marks PDF files and opens them as tabs. The first PDF tab loads the module.
- The worker, the character maps, the fonts, and the decoders come from the app. One worker for each tab.
- The toolbar uses IconButton and the shared tooltip, with the new zoom-in, zoom-out, fit-width, and rotate icons.
- Add THIRD_PARTY_NOTICES.md for PDF.js and xterm.
- Update the tests, the menu inventory, the docs, and UI-059.
This commit is contained in:
Knotty Wyvern
2026-10-29 19:41:17 -07:00
parent 833f793244
commit e6deecc092
35 changed files with 2101 additions and 49 deletions
+69
View File
@@ -0,0 +1,69 @@
/// <reference types="vite/client" />
// PDF.js for the PDF Viewer feature. The worker, the character maps, the standard fonts, and the WebAssembly image
// decoders come from the app (`'self'`) as Vite assets, so a PDF never causes a network request.
// Each open document has its own worker. `close` stops it.
// The reference to the Vite types serves the unit test type check, which reaches this file through `loader.ts`.
import { getDocument, PDFWorker, PasswordResponses, type PDFDocumentLoadingTask, type PDFDocumentProxy } from "pdfjs-dist/build/pdf.min.mjs";
import workerUrl from "pdfjs-dist/build/pdf.worker.min.mjs?url";
export { TextLayer, type PDFDocumentProxy, type RenderTask } from "pdfjs-dist/build/pdf.min.mjs";
// `build.assetsInlineLimit` in vite.config.js keeps these files out of the code, so each one is a file of the app.
// The JPEG 2000 and JBIG2 decoders are the only WebAssembly files that PDF.js asks for with `useWorkerFetch: false`.
// The viewer runs no PDF scripts (quickjs) and uses no ICC color profiles (qcms), as the PDF Viewer plugin did.
const assets = import.meta.glob<string>([
"/node_modules/pdfjs-dist/cmaps/*.bcmap",
"/node_modules/pdfjs-dist/standard_fonts/*.{pfb,ttf}",
"/node_modules/pdfjs-dist/wasm/{jbig2,openjpeg}.wasm",
], { query: "?url", import: "default", eager: true });
const folders: Record<string, string> = { cMapUrl: "cmaps", standardFontDataUrl: "standard_fonts", wasmUrl: "wasm" };
/** The app URL of a PDF.js data file, or undefined for an unknown kind or file. */
export function assetUrl(kind: string, filename: string): string | undefined {
const folder = folders[kind];
if (!folder || !/^[\w.-]+$/.test(filename)) return undefined;
return assets[`/node_modules/pdfjs-dist/${folder}/${filename}`];
}
/** PDF.js asks the window for its data files (`useWorkerFetch: false`). This class reads them from the app. */
class AppBinaryDataFactory {
async fetch({ kind, filename }: { kind: string; filename: string }): Promise<Uint8Array> {
const url = assetUrl(kind, filename);
if (!url) throw new Error(`PDF.js has no data file ${filename}.`);
const response = await fetch(url);
if (!response.ok) throw new Error(`PDF.js could not read ${filename}: ${response.status}.`);
return new Uint8Array(await response.arrayBuffer());
}
}
export interface OpenPdf {
/** Resolves with the document after the password, if the file needs one. */
promise: Promise<PDFDocumentProxy>;
/** Stop the load or release the document, and stop the worker. Later calls return the same promise. */
close(): Promise<void>;
}
/**
* Open the bytes of a PDF in a new worker. PDF.js calls `onPassword` for a protected file, with "incorrect" after a
* wrong password. The viewer calls `answer` with the next password.
*/
export function openPdf(data: Uint8Array, onPassword: (reason: "needed" | "incorrect", answer: (password: string) => void) => void): OpenPdf {
const port = new Worker(workerUrl, { type: "module", name: "PDF.js" });
const worker = PDFWorker.create({ port });
const task: PDFDocumentLoadingTask = getDocument({
data, worker, useWorkerFetch: false, BinaryDataFactory: AppBinaryDataFactory,
useSystemFonts: false, isImageDecoderSupported: false, enableXfa: false,
});
task.onPassword = (answer: (password: string) => void, reason: number) => {
onPassword(reason === PasswordResponses.INCORRECT_PASSWORD ? "incorrect" : "needed", answer);
};
let closing: Promise<void> | undefined;
return {
promise: task.promise,
close: () => closing ??= (async () => {
try { await task.destroy(); }
finally { worker.destroy(); port.terminate(); }
})(),
};
}