mirror of
https://github.com/alam00000/bentopdf.git
synced 2026-11-07 00:28:39 +01:00
- 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.
61 lines
1.6 KiB
TypeScript
61 lines
1.6 KiB
TypeScript
import { WasmProvider } from './wasm-provider';
|
|
import type { WindowWithCoherentPdf, CpdfInstance } from '@/types';
|
|
|
|
let cpdfLoaded = false;
|
|
let cpdfLoadPromise: Promise<void> | null = null;
|
|
|
|
function getCpdfUrl(): string | undefined {
|
|
const userUrl = WasmProvider.getUrl('cpdf');
|
|
if (userUrl) {
|
|
const baseUrl = userUrl.endsWith('/') ? userUrl : `${userUrl}/`;
|
|
return `${baseUrl}coherentpdf.browser.min.js`;
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
export function isCpdfAvailable(): boolean {
|
|
return WasmProvider.isConfigured('cpdf');
|
|
}
|
|
|
|
export async function isCpdfLoaded(): Promise<void> {
|
|
if (cpdfLoaded) return;
|
|
|
|
if (cpdfLoadPromise) {
|
|
return cpdfLoadPromise;
|
|
}
|
|
|
|
const cpdfUrl = getCpdfUrl();
|
|
if (!cpdfUrl) {
|
|
throw new Error(
|
|
'CoherentPDF is not configured. Please configure it in WASM Settings.'
|
|
);
|
|
}
|
|
|
|
cpdfLoadPromise = new Promise((resolve, reject) => {
|
|
if (typeof (window as WindowWithCoherentPdf).coherentpdf !== 'undefined') {
|
|
cpdfLoaded = true;
|
|
resolve();
|
|
return;
|
|
}
|
|
|
|
const script = document.createElement('script');
|
|
script.src = cpdfUrl;
|
|
script.onload = () => {
|
|
cpdfLoaded = true;
|
|
console.log('[CPDF] Loaded from:', script.src);
|
|
resolve();
|
|
};
|
|
script.onerror = () => {
|
|
reject(new Error('Failed to load CoherentPDF library from: ' + cpdfUrl));
|
|
};
|
|
document.head.appendChild(script);
|
|
});
|
|
|
|
return cpdfLoadPromise;
|
|
}
|
|
|
|
export async function getCpdf(): Promise<CpdfInstance> {
|
|
await isCpdfLoaded();
|
|
return (window as WindowWithCoherentPdf).coherentpdf as CpdfInstance;
|
|
}
|