Refactor and enhance type safety across various modules
- Updated function parameters and return types in `page-preview.ts`, `pdf-decrypt.ts`, and `pymupdf-loader.ts` for improved type safety. - Introduced type definitions for `CpdfInstance`, `PyMuPDFInstance`, and other related types to ensure better type checking. - Enhanced error handling in `sanitize.ts` by creating a utility function for error messages. - Removed unnecessary type assertions and improved type inference in `editor.ts`, `serialization.ts`, and `tools.test.ts`. - Added type definitions for markdown-it plugins to improve compatibility and type safety. - Enforced stricter TypeScript settings by enabling `noImplicitAny` in `tsconfig.json`. - Cleaned up test files by refining type assertions and ensuring consistency in type usage.
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import createModule from '@neslinesli93/qpdf-wasm';
|
||||
import type { QpdfInstanceExtended } from '@/types';
|
||||
import { showLoader, hideLoader, showAlert } from '../ui.js';
|
||||
import { createIcons } from 'lucide';
|
||||
import { state, resetState } from '../state.js';
|
||||
import * as pdfjsLib from 'pdfjs-dist';
|
||||
import type { DocumentInitParameters } from 'pdfjs-dist/types/src/display/api';
|
||||
|
||||
const STANDARD_SIZES = {
|
||||
A4: { width: 595.28, height: 841.89 },
|
||||
@@ -13,7 +15,7 @@ const STANDARD_SIZES = {
|
||||
A5: { width: 419.53, height: 595.28 },
|
||||
};
|
||||
|
||||
export function getStandardPageName(width: any, height: any) {
|
||||
export function getStandardPageName(width: number, height: number) {
|
||||
const tolerance = 1; // Allow for minor floating point variations
|
||||
for (const [name, size] of Object.entries(STANDARD_SIZES)) {
|
||||
if (
|
||||
@@ -28,7 +30,7 @@ export function getStandardPageName(width: any, height: any) {
|
||||
return 'Custom';
|
||||
}
|
||||
|
||||
export function convertPoints(points: any, unit: any) {
|
||||
export function convertPoints(points: number, unit: string) {
|
||||
let result: number;
|
||||
switch (unit) {
|
||||
case 'in':
|
||||
@@ -59,7 +61,7 @@ export function hexToRgb(hex: string): { r: number; g: number; b: number } {
|
||||
: { r: 0, g: 0, b: 0 };
|
||||
}
|
||||
|
||||
export const formatBytes = (bytes: any, decimals = 1) => {
|
||||
export const formatBytes = (bytes: number, decimals = 1) => {
|
||||
if (bytes === 0) return '0 Bytes';
|
||||
const k = 1024;
|
||||
const dm = decimals < 0 ? 0 : decimals;
|
||||
@@ -79,10 +81,12 @@ export const downloadFile = (blob: Blob, filename: string): void => {
|
||||
URL.revokeObjectURL(url);
|
||||
};
|
||||
|
||||
export const readFileAsArrayBuffer = (file: any) => {
|
||||
export const readFileAsArrayBuffer = (
|
||||
file: Blob
|
||||
): Promise<ArrayBuffer | null> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => resolve(reader.result);
|
||||
reader.onload = () => resolve(reader.result as ArrayBuffer | null);
|
||||
reader.onerror = (error) => reject(error);
|
||||
reader.readAsArrayBuffer(file);
|
||||
});
|
||||
@@ -139,7 +143,7 @@ export function parsePageRanges(
|
||||
* @param {string} isoDateString - The ISO 8601 date string.
|
||||
* @returns {string} A localized date and time string, or the original string if parsing fails.
|
||||
*/
|
||||
export function formatIsoDate(isoDateString) {
|
||||
export function formatIsoDate(isoDateString: string) {
|
||||
if (!isoDateString || typeof isoDateString !== 'string') {
|
||||
return isoDateString; // Return original value if it's not a valid string
|
||||
}
|
||||
@@ -156,20 +160,20 @@ export function formatIsoDate(isoDateString) {
|
||||
}
|
||||
}
|
||||
|
||||
let qpdfInstance: any = null;
|
||||
let qpdfInstance: QpdfInstanceExtended | null = null;
|
||||
|
||||
/**
|
||||
* Initialize qpdf-wasm singleton.
|
||||
* Subsequent calls return the same instance.
|
||||
*/
|
||||
export async function initializeQpdf() {
|
||||
export async function initializeQpdf(): Promise<QpdfInstanceExtended> {
|
||||
if (qpdfInstance) return qpdfInstance;
|
||||
|
||||
showLoader('Initializing PDF engine...');
|
||||
try {
|
||||
qpdfInstance = await createModule({
|
||||
qpdfInstance = (await createModule({
|
||||
locateFile: () => import.meta.env.BASE_URL + 'qpdf.wasm',
|
||||
});
|
||||
})) as unknown as QpdfInstanceExtended;
|
||||
} catch (error) {
|
||||
console.error('Failed to initialize qpdf-wasm:', error);
|
||||
showAlert(
|
||||
@@ -267,24 +271,19 @@ export function resetAndReloadTool(preResetCallback?: () => void) {
|
||||
* @param src The source to load (url string, typed array, or parameters object)
|
||||
* @returns The PDF loading task
|
||||
*/
|
||||
export function getPDFDocument(src: any) {
|
||||
let params = src;
|
||||
export function getPDFDocument(
|
||||
src: string | Uint8Array | ArrayBuffer | DocumentInitParameters
|
||||
) {
|
||||
let params: DocumentInitParameters;
|
||||
|
||||
// Handle different input types similar to how getDocument handles them,
|
||||
// but we ensure we have an object to attach wasmUrl to.
|
||||
if (typeof src === 'string') {
|
||||
params = { url: src };
|
||||
} else if (src instanceof Uint8Array || src instanceof ArrayBuffer) {
|
||||
params = { data: src };
|
||||
} else {
|
||||
params = src;
|
||||
}
|
||||
|
||||
// Ensure params is an object
|
||||
if (typeof params !== 'object' || params === null) {
|
||||
params = {};
|
||||
}
|
||||
|
||||
// Add wasmUrl pointing to our public/wasm directory
|
||||
// This is required for PDF.js v5+ to load OpenJPEG for certain images
|
||||
return pdfjsLib.getDocument({
|
||||
...params,
|
||||
wasmUrl: import.meta.env.BASE_URL + 'pdfjs-viewer/wasm/',
|
||||
@@ -413,7 +412,7 @@ export function formatRawDate(raw: string): string {
|
||||
year,
|
||||
hoursStr,
|
||||
minsStr,
|
||||
secsStr,
|
||||
_secsStr,
|
||||
timezone,
|
||||
] = match;
|
||||
|
||||
@@ -455,8 +454,8 @@ export function formatRawDate(raw: string): string {
|
||||
|
||||
return `${fullDay}, ${fullMonth} ${dom}, ${year} at ${hours}:${minsStr} ${ampm} (${formattedTz})`;
|
||||
}
|
||||
} catch (e) {
|
||||
// Fallback to raw string if parsing fails
|
||||
} catch {
|
||||
console.error('Error parsing date string:', raw);
|
||||
}
|
||||
return raw;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user