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:
alam00000
2026-03-31 17:59:49 +05:30
parent a1fc2fc3c6
commit 9d0b68e18c
114 changed files with 2577 additions and 1868 deletions

View File

@@ -1,6 +1,5 @@
import { resetState } from './state.js';
import { formatBytes, getPDFDocument } from './utils/helpers.js';
import { tesseractLanguages } from './config/tesseract-languages.js';
import {
renderPagesProgressively,
cleanupLazyRendering,
@@ -14,6 +13,7 @@ import {
} from './utils/rotation-state.js';
import * as pdfjsLib from 'pdfjs-dist';
import { t } from './i18n/i18n';
import type { FileInputOptions } from '@/types';
pdfjsLib.GlobalWorkerOptions.workerSrc = new URL(
'pdfjs-dist/build/pdf.worker.min.mjs',
@@ -112,8 +112,8 @@ export const hideLoader = () => {
};
export const showAlert = (
title: any,
message: any,
title: string,
message: string,
type: string = 'error',
callback?: () => void
) => {
@@ -137,7 +137,7 @@ export const hideAlert = () => {
if (dom.alertModal) dom.alertModal.classList.add('hidden');
};
export const switchView = (view: any) => {
export const switchView = (view: string) => {
if (view === 'grid') {
dom.gridView.classList.remove('hidden');
dom.toolInterface.classList.add('hidden');
@@ -170,11 +170,13 @@ export const switchView = (view: any) => {
}
};
const thumbnailState = {
const thumbnailState: {
sortableInstances: Record<string, Sortable>;
} = {
sortableInstances: {},
};
function initializeOrganizeSortable(containerId: any) {
function initializeOrganizeSortable(containerId: string) {
const container = document.getElementById(containerId);
if (!container) return;
@@ -189,10 +191,10 @@ function initializeOrganizeSortable(containerId: any) {
dragClass: 'sortable-drag',
filter: '.delete-page-btn',
preventOnFilter: true,
onStart: function (evt: any) {
onStart: function (evt: Sortable.SortableEvent) {
evt.item.style.opacity = '0.5';
},
onEnd: function (evt: any) {
onEnd: function (evt: Sortable.SortableEvent) {
evt.item.style.opacity = '1';
},
});
@@ -203,7 +205,10 @@ function initializeOrganizeSortable(containerId: any) {
* @param {string} toolId The ID of the active tool.
* @param {object} pdfDoc The loaded pdf-lib document instance.
*/
export const renderPageThumbnails = async (toolId: any, pdfDoc: any) => {
export const renderPageThumbnails = async (
toolId: string,
pdfDoc: { save: () => Promise<Uint8Array> }
) => {
const containerId =
toolId === 'organize'
? 'page-organizer'
@@ -229,8 +234,7 @@ export const renderPageThumbnails = async (toolId: any, pdfDoc: any) => {
// Function to create wrapper element for each page
const createWrapper = (canvas: HTMLCanvasElement, pageNumber: number) => {
const wrapper = document.createElement('div');
// @ts-expect-error TS(2322) FIXME: Type 'number' is not assignable to type 'string'.
wrapper.dataset.pageIndex = pageNumber - 1;
wrapper.dataset.pageIndex = String(pageNumber - 1);
const imgContainer = document.createElement('div');
imgContainer.className = 'relative';
@@ -452,10 +456,10 @@ export const renderPageThumbnails = async (toolId: any, pdfDoc: any) => {
* @param {HTMLElement} container The DOM element to render the list into.
* @param {File[]} files The array of file objects.
*/
export const renderFileDisplay = (container: any, files: any) => {
export const renderFileDisplay = (container: HTMLElement, files: File[]) => {
container.textContent = '';
if (files.length > 0) {
files.forEach((file: any) => {
files.forEach((file: File) => {
const fileDiv = document.createElement('div');
fileDiv.className =
'flex items-center justify-between bg-gray-700 p-3 rounded-lg text-sm';
@@ -474,13 +478,10 @@ export const renderFileDisplay = (container: any, files: any) => {
}
};
const createFileInputHTML = (options = {}) => {
// @ts-expect-error TS(2339) FIXME: Property 'multiple' does not exist on type '{}'.
const createFileInputHTML = (options: FileInputOptions = {}) => {
const multiple = options.multiple ? 'multiple' : '';
// @ts-expect-error TS(2339) FIXME: Property 'accept' does not exist on type '{}'.
const acceptedFiles = options.accept || 'application/pdf';
// @ts-expect-error TS(2339) FIXME: Property 'showControls' does not exist on type '{}... Remove this comment to see the full error message
const showControls = options.showControls || false; // NEW: Add this parameter
const showControls = options.showControls || false;
return `
<div id="drop-zone" class="relative flex flex-col items-center justify-center w-full h-48 border-2 border-dashed border-gray-600 rounded-xl cursor-pointer bg-gray-900 hover:bg-gray-700 transition-colors duration-300">