@softure-ai/theme-notion 0.16.2 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,64 @@
1
+ // The PDF engine of `DocumentViewer`: pdf.js (Apache-2.0) loaded on first use, its worker code run on the page's main
2
+ // thread (no worker URL to configure, and the chunks are ordinary build files a service worker keeps). Behind a small
3
+ // interface, so a test or an app can pass another engine.
4
+
5
+ /** An opened PDF: its page count, and each page drawn into a canvas at a CSS width. */
6
+ export interface PdfDocumentHandle {
7
+ readonly pageCount: number;
8
+ /** Draws page `pageNumber` (from 1) into `canvas`, `width` CSS pixels wide, sharp on the device's pixel ratio. */
9
+ renderPage(pageNumber: number, canvas: HTMLCanvasElement, width: number): Promise<void>;
10
+ /** Frees the document; a render after it fails. */
11
+ destroy(): void;
12
+ }
13
+
14
+ export interface PdfEngine {
15
+ /** Opens a PDF from its bytes; rejects for bytes that are not a PDF it can read. */
16
+ open(data: ArrayBuffer): Promise<PdfDocumentHandle>;
17
+ }
18
+
19
+ interface PdfjsGlobal {
20
+ pdfjsWorker?: unknown;
21
+ }
22
+
23
+ let loading: Promise<PdfEngine> | null = null;
24
+
25
+ /**
26
+ * pdf.js as a `PdfEngine`, loaded once. The legacy build, so an older phone browser reads it too. Call it ahead (e.g.
27
+ * when a page with documents opens) to fetch its chunks while online, so an installed app opens a PDF offline later.
28
+ */
29
+ export function loadPdfjsEngine(): Promise<PdfEngine> {
30
+ loading ??= importPdfjs().catch((error: unknown) => {
31
+ loading = null;
32
+ throw error;
33
+ });
34
+ return loading;
35
+ }
36
+
37
+ async function importPdfjs(): Promise<PdfEngine> {
38
+ const [pdfjs, worker] = await Promise.all([import("pdfjs-dist/legacy/build/pdf.mjs"), import("pdfjs-dist/legacy/build/pdf.worker.min.mjs")]);
39
+ const scope = globalThis as PdfjsGlobal;
40
+ scope.pdfjsWorker ??= worker;
41
+ return {
42
+ async open(data) {
43
+ const task = pdfjs.getDocument({ data: new Uint8Array(data.slice(0)), useSystemFonts: true });
44
+ const document = await task.promise;
45
+ return {
46
+ pageCount: document.numPages,
47
+ async renderPage(pageNumber, canvas, width) {
48
+ const page = await document.getPage(pageNumber);
49
+ const base = page.getViewport({ scale: 1 });
50
+ const ratio = globalThis.devicePixelRatio > 0 ? globalThis.devicePixelRatio : 1;
51
+ const viewport = page.getViewport({ scale: (width / base.width) * ratio });
52
+ canvas.width = Math.floor(viewport.width);
53
+ canvas.height = Math.floor(viewport.height);
54
+ canvas.style.width = `${String(width)}px`;
55
+ canvas.style.height = `${String(Math.floor(viewport.height / ratio))}px`;
56
+ await page.render({ canvas, viewport }).promise;
57
+ },
58
+ destroy() {
59
+ void task.destroy();
60
+ },
61
+ };
62
+ },
63
+ };
64
+ }