Files
RuneStone/src/lib/features/pdf/index.ts
T
Knotty Wyvern e6deecc092 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.
2026-10-29 19:41:17 -07:00

33 lines
1.6 KiB
TypeScript

// The PDF Viewer feature. `loader.ts` imports this module when the first PDF tab opens. The file tree marks PDF files
// before that, through the document type of the catalog (`../pdfDocument.ts`).
// Each tab reads its file with `workspace_read_bytes` (64 MiB at most) and has its own PDF.js worker.
import { flushSync, mount, unmount } from "svelte";
import { tr } from "../../i18n/index.ts";
import { workspaceReadBytes } from "../../workspace/api.ts";
import type { FeatureContext, FeatureInstance } from "../types.ts";
import PdfViewer from "./PdfViewer.svelte";
export function start(_context: FeatureContext): FeatureInstance {
const tabs = new Set<() => Promise<void>>();
let stopped = false;
return {
mount() { throw new Error(tr("This view is unavailable.")); },
mountDocument(type, element, file) {
if (type !== "pdf") throw new Error(tr("This view is unavailable."));
if (stopped) throw new Error(tr("This feature is off."));
const viewer = mount(PdfViewer, { target: element, props: { file, readBytes: workspaceReadBytes } });
flushSync();
let closing: Promise<void> | undefined;
// Release the document and stop its worker first. Then remove the component.
const dispose = () => closing ??= viewer.close().then(() => unmount(viewer)).finally(() => { tabs.delete(dispose); });
tabs.add(dispose);
return dispose;
},
/** The host closes the PDF tabs first. Close what is left, so no worker stays. */
async dispose() {
stopped = true;
await Promise.allSettled([...tabs].map(dispose => dispose()));
},
};
}