diff --git a/src/components/PagePreviewModal.tsx b/src/components/PagePreviewModal.tsx index 5430dc3..c87334a 100644 --- a/src/components/PagePreviewModal.tsx +++ b/src/components/PagePreviewModal.tsx @@ -1,11 +1,6 @@ import React, { useEffect, useRef } from 'react'; import type { PdfFile } from '../pdf/pdfTypes'; -import * as pdfjsLib from 'pdfjs-dist'; -import pdfjsWorker from 'pdfjs-dist/build/pdf.worker?worker&url'; - -// pdf.js worker setup -// eslint-disable-next-line @typescript-eslint/no-explicit-any -(pdfjsLib as any).GlobalWorkerOptions.workerSrc = pdfjsWorker; +import { pdfjsLib } from '../pdf/pdfJs'; interface PagePreviewModalProps { isOpen: boolean; diff --git a/src/pdf/pdfJs.ts b/src/pdf/pdfJs.ts new file mode 100644 index 0000000..efd355b --- /dev/null +++ b/src/pdf/pdfJs.ts @@ -0,0 +1,9 @@ +import './uint8ArrayToHexPolyfill'; +import * as pdfjsLib from 'pdfjs-dist/legacy/build/pdf.mjs'; +import pdfjsWorker from './pdfjsWorker?worker&url'; + +// pdf.js worker setup for Vite. +// eslint-disable-next-line @typescript-eslint/no-explicit-any +(pdfjsLib as any).GlobalWorkerOptions.workerSrc = pdfjsWorker; + +export { pdfjsLib }; diff --git a/src/pdf/pdfThumbnailService.ts b/src/pdf/pdfThumbnailService.ts index 948b21c..aeb9592 100644 --- a/src/pdf/pdfThumbnailService.ts +++ b/src/pdf/pdfThumbnailService.ts @@ -1,9 +1,4 @@ -import * as pdfjsLib from 'pdfjs-dist/legacy/build/pdf.mjs'; -import pdfjsWorker from 'pdfjs-dist/legacy/build/pdf.worker.mjs?worker&url'; - -// pdf.js worker setup for Vite -// eslint-disable-next-line @typescript-eslint/no-explicit-any -(pdfjsLib as any).GlobalWorkerOptions.workerSrc = pdfjsWorker; +import { pdfjsLib } from './pdfJs'; type RotationsMap = Record; // key: 0-based page index, value: degrees diff --git a/src/pdf/pdfjsWorker.ts b/src/pdf/pdfjsWorker.ts new file mode 100644 index 0000000..5c7d575 --- /dev/null +++ b/src/pdf/pdfjsWorker.ts @@ -0,0 +1,2 @@ +import './uint8ArrayToHexPolyfill'; +import 'pdfjs-dist/legacy/build/pdf.worker.mjs'; diff --git a/src/pdf/uint8ArrayToHexPolyfill.ts b/src/pdf/uint8ArrayToHexPolyfill.ts new file mode 100644 index 0000000..a662d0f --- /dev/null +++ b/src/pdf/uint8ArrayToHexPolyfill.ts @@ -0,0 +1,22 @@ +type Uint8ArrayWithToHex = Uint8Array & { + toHex?: () => string; +}; + +const uint8ArrayPrototype = Uint8Array.prototype as Uint8ArrayWithToHex; + +if (typeof uint8ArrayPrototype.toHex !== 'function') { + Object.defineProperty(Uint8Array.prototype, 'toHex', { + value: function toHex(this: Uint8Array): string { + const hex = new Array(this.length); + + for (let index = 0; index < this.length; index += 1) { + const value = this[index].toString(16); + hex[index] = value.length === 1 ? `0${value}` : value; + } + + return hex.join(''); + }, + writable: true, + configurable: true, + }); +}