feat: add WASM module configuration with pre-configured CDN URLs and update settings UI
This commit is contained in:
@@ -78,16 +78,34 @@ function initializePage() {
|
||||
|
||||
if (config.pymupdf) {
|
||||
pymupdfUrl.value = config.pymupdf;
|
||||
if (
|
||||
!WasmProvider.isUserConfigured('pymupdf') &&
|
||||
WasmProvider.hasEnvDefault('pymupdf')
|
||||
) {
|
||||
pymupdfUrl.placeholder = config.pymupdf;
|
||||
}
|
||||
updateStatus('pymupdf', true);
|
||||
}
|
||||
|
||||
if (config.ghostscript) {
|
||||
ghostscriptUrl.value = config.ghostscript;
|
||||
if (
|
||||
!WasmProvider.isUserConfigured('ghostscript') &&
|
||||
WasmProvider.hasEnvDefault('ghostscript')
|
||||
) {
|
||||
ghostscriptUrl.placeholder = config.ghostscript;
|
||||
}
|
||||
updateStatus('ghostscript', true);
|
||||
}
|
||||
|
||||
if (config.cpdf) {
|
||||
cpdfUrl.value = config.cpdf;
|
||||
if (
|
||||
!WasmProvider.isUserConfigured('cpdf') &&
|
||||
WasmProvider.hasEnvDefault('cpdf')
|
||||
) {
|
||||
cpdfUrl.placeholder = config.cpdf;
|
||||
}
|
||||
updateStatus('cpdf', true);
|
||||
}
|
||||
}
|
||||
@@ -110,8 +128,12 @@ function initializePage() {
|
||||
statusEl.textContent = 'Testing...';
|
||||
statusEl.className =
|
||||
'text-xs px-2 py-1 rounded-full bg-yellow-600/30 text-yellow-300';
|
||||
} else if (configured) {
|
||||
statusEl.textContent = 'Configured';
|
||||
} else if (configured && WasmProvider.isUserConfigured(packageName)) {
|
||||
statusEl.textContent = 'Custom Override';
|
||||
statusEl.className =
|
||||
'text-xs px-2 py-1 rounded-full bg-blue-600/30 text-blue-300';
|
||||
} else if (configured || WasmProvider.hasEnvDefault(packageName)) {
|
||||
statusEl.textContent = 'Pre-configured';
|
||||
statusEl.className =
|
||||
'text-xs px-2 py-1 rounded-full bg-green-600/30 text-green-300';
|
||||
} else {
|
||||
@@ -202,17 +224,25 @@ function initializePage() {
|
||||
clearPyMuPDFCache();
|
||||
clearGhostscriptCache();
|
||||
|
||||
pymupdfUrl.value = '';
|
||||
ghostscriptUrl.value = '';
|
||||
cpdfUrl.value = '';
|
||||
const defaults = WasmProvider.getAllProviders();
|
||||
pymupdfUrl.value = defaults.pymupdf || '';
|
||||
ghostscriptUrl.value = defaults.ghostscript || '';
|
||||
cpdfUrl.value = defaults.cpdf || '';
|
||||
|
||||
updateStatus('pymupdf', false);
|
||||
updateStatus('ghostscript', false);
|
||||
updateStatus('cpdf', false);
|
||||
updateStatus('pymupdf', WasmProvider.isConfigured('pymupdf'));
|
||||
updateStatus('ghostscript', WasmProvider.isConfigured('ghostscript'));
|
||||
updateStatus('cpdf', WasmProvider.isConfigured('cpdf'));
|
||||
|
||||
const hasDefaults =
|
||||
WasmProvider.hasEnvDefault('pymupdf') ||
|
||||
WasmProvider.hasEnvDefault('ghostscript') ||
|
||||
WasmProvider.hasEnvDefault('cpdf');
|
||||
|
||||
showAlert(
|
||||
'Cleared',
|
||||
'All configurations and cached modules have been cleared.',
|
||||
'Reset',
|
||||
hasDefaults
|
||||
? 'Custom overrides cleared. Pre-configured defaults are active.'
|
||||
: 'All configurations and cached modules have been cleared.',
|
||||
'success'
|
||||
);
|
||||
});
|
||||
|
||||
@@ -8,6 +8,12 @@ interface WasmProviderConfig {
|
||||
|
||||
const STORAGE_KEY = 'bentopdf:wasm-providers';
|
||||
|
||||
const ENV_DEFAULTS: Record<WasmPackage, string | undefined> = {
|
||||
pymupdf: import.meta.env.VITE_WASM_PYMUPDF_URL || undefined,
|
||||
ghostscript: import.meta.env.VITE_WASM_GS_URL || undefined,
|
||||
cpdf: import.meta.env.VITE_WASM_CPDF_URL || undefined,
|
||||
};
|
||||
|
||||
class WasmProviderManager {
|
||||
private config: WasmProviderConfig;
|
||||
private validationCache: Map<WasmPackage, boolean> = new Map();
|
||||
@@ -31,6 +37,10 @@ class WasmProviderManager {
|
||||
return {};
|
||||
}
|
||||
|
||||
private getEnvDefault(packageName: WasmPackage): string | undefined {
|
||||
return ENV_DEFAULTS[packageName];
|
||||
}
|
||||
|
||||
private saveConfig(): void {
|
||||
try {
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(this.config));
|
||||
@@ -40,7 +50,7 @@ class WasmProviderManager {
|
||||
}
|
||||
|
||||
getUrl(packageName: WasmPackage): string | undefined {
|
||||
return this.config[packageName];
|
||||
return this.config[packageName] || this.getEnvDefault(packageName);
|
||||
}
|
||||
|
||||
setUrl(packageName: WasmPackage, url: string): void {
|
||||
@@ -57,11 +67,22 @@ class WasmProviderManager {
|
||||
}
|
||||
|
||||
isConfigured(packageName: WasmPackage): boolean {
|
||||
return !!(this.config[packageName] || this.getEnvDefault(packageName));
|
||||
}
|
||||
|
||||
isUserConfigured(packageName: WasmPackage): boolean {
|
||||
return !!this.config[packageName];
|
||||
}
|
||||
|
||||
hasEnvDefault(packageName: WasmPackage): boolean {
|
||||
return !!this.getEnvDefault(packageName);
|
||||
}
|
||||
|
||||
hasAnyProvider(): boolean {
|
||||
return Object.keys(this.config).length > 0;
|
||||
return (
|
||||
Object.keys(this.config).length > 0 ||
|
||||
Object.values(ENV_DEFAULTS).some(Boolean)
|
||||
);
|
||||
}
|
||||
|
||||
async validateUrl(
|
||||
@@ -172,13 +193,25 @@ class WasmProviderManager {
|
||||
}
|
||||
|
||||
getAllProviders(): WasmProviderConfig {
|
||||
return { ...this.config };
|
||||
return {
|
||||
pymupdf: this.config.pymupdf || ENV_DEFAULTS.pymupdf,
|
||||
ghostscript: this.config.ghostscript || ENV_DEFAULTS.ghostscript,
|
||||
cpdf: this.config.cpdf || ENV_DEFAULTS.cpdf,
|
||||
};
|
||||
}
|
||||
|
||||
clearAll(): void {
|
||||
this.config = {};
|
||||
this.validationCache.clear();
|
||||
this.saveConfig();
|
||||
try {
|
||||
localStorage.removeItem(STORAGE_KEY);
|
||||
} catch (e) {
|
||||
console.error('[WasmProvider] Failed to clear localStorage:', e);
|
||||
}
|
||||
}
|
||||
|
||||
resetToDefaults(): void {
|
||||
this.clearAll();
|
||||
}
|
||||
|
||||
getPackageDisplayName(packageName: WasmPackage): string {
|
||||
|
||||
Reference in New Issue
Block a user