mirror of
https://github.com/alam00000/bentopdf.git
synced 2026-11-09 00:57:15 +01:00
- Update README with comprehensive subdirectory hosting instructions and BASE_URL configuration guide - Convert absolute asset paths to relative paths in index.html for proper subdirectory deployment - Update all worker script imports to use relative paths instead of absolute paths - Fix favicon and image references to work correctly when hosted in nested directories - Normalize whitespace and formatting across worker files for consistency - Update vite.config.ts to properly handle BASE_URL configuration for subdirectory deployments - Ensure all tool pages and logic files maintain compatibility with subdirectory hosting - Enable BentoPDF to be deployed at any URL path (e.g., example.com/tools/bentopdf/) without breaking asset loading
128 lines
3.9 KiB
TypeScript
128 lines
3.9 KiB
TypeScript
// Logic for PDF Editor Page
|
|
import { createIcons, icons } from 'lucide';
|
|
import { showAlert, showLoader, hideLoader } from '../ui.js';
|
|
|
|
let currentPdfUrl: string | null = null;
|
|
|
|
if (document.readyState === 'loading') {
|
|
document.addEventListener('DOMContentLoaded', initializePage);
|
|
} else {
|
|
initializePage();
|
|
}
|
|
|
|
function initializePage() {
|
|
createIcons({ icons });
|
|
|
|
const fileInput = document.getElementById('file-input') as HTMLInputElement;
|
|
const dropZone = document.getElementById('drop-zone');
|
|
|
|
if (fileInput) {
|
|
fileInput.addEventListener('change', handleFileUpload);
|
|
}
|
|
|
|
if (dropZone) {
|
|
dropZone.addEventListener('dragover', (e) => {
|
|
e.preventDefault();
|
|
dropZone.classList.add('border-indigo-500');
|
|
});
|
|
|
|
dropZone.addEventListener('dragleave', () => {
|
|
dropZone.classList.remove('border-indigo-500');
|
|
});
|
|
|
|
dropZone.addEventListener('drop', (e) => {
|
|
e.preventDefault();
|
|
dropZone.classList.remove('border-indigo-500');
|
|
const files = e.dataTransfer?.files;
|
|
if (files && files.length > 0) {
|
|
handleFiles(files);
|
|
}
|
|
});
|
|
|
|
dropZone.addEventListener('click', () => {
|
|
fileInput?.click();
|
|
});
|
|
}
|
|
|
|
document.getElementById('back-to-tools')?.addEventListener('click', () => {
|
|
window.location.href = import.meta.env.BASE_URL;
|
|
});
|
|
}
|
|
|
|
async function handleFileUpload(e: Event) {
|
|
const input = e.target as HTMLInputElement;
|
|
if (input.files && input.files.length > 0) {
|
|
await handleFiles(input.files);
|
|
}
|
|
input.value = '';
|
|
}
|
|
|
|
async function handleFiles(files: FileList) {
|
|
const file = files[0];
|
|
if (!file || file.type !== 'application/pdf') {
|
|
showAlert('Invalid File', 'Please upload a valid PDF file.');
|
|
return;
|
|
}
|
|
|
|
showLoader('Loading PDF Editor...');
|
|
|
|
try {
|
|
const pdfWrapper = document.getElementById('embed-pdf-wrapper');
|
|
const pdfContainer = document.getElementById('embed-pdf-container');
|
|
const uploader = document.getElementById('tool-uploader');
|
|
const dropZone = document.getElementById('drop-zone');
|
|
|
|
if (!pdfWrapper || !pdfContainer || !uploader || !dropZone) return;
|
|
|
|
// Hide uploader elements but keep the container
|
|
dropZone.classList.add('hidden');
|
|
|
|
// Clear previous content
|
|
pdfContainer.textContent = '';
|
|
if (currentPdfUrl) {
|
|
URL.revokeObjectURL(currentPdfUrl);
|
|
}
|
|
|
|
// Show editor container
|
|
pdfWrapper.classList.remove('hidden');
|
|
|
|
const fileURL = URL.createObjectURL(file);
|
|
currentPdfUrl = fileURL;
|
|
|
|
// Dynamically load EmbedPDF script
|
|
const script = document.createElement('script');
|
|
script.type = 'module';
|
|
script.textContent = `
|
|
import EmbedPDF from 'https://snippet.embedpdf.com/embedpdf.js';
|
|
EmbedPDF.init({
|
|
type: 'container',
|
|
target: document.getElementById('embed-pdf-container'),
|
|
src: '${fileURL}',
|
|
});
|
|
`;
|
|
document.head.appendChild(script);
|
|
|
|
// Update back button to reset state
|
|
const backBtn = document.getElementById('back-to-tools');
|
|
if (backBtn) {
|
|
// Clone to remove old listeners
|
|
const newBackBtn = backBtn.cloneNode(true);
|
|
backBtn.parentNode?.replaceChild(newBackBtn, backBtn);
|
|
|
|
newBackBtn.addEventListener('click', () => {
|
|
if (currentPdfUrl) {
|
|
URL.revokeObjectURL(currentPdfUrl);
|
|
currentPdfUrl = null;
|
|
}
|
|
window.location.href = import.meta.env.BASE_URL;
|
|
});
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('Error loading PDF Editor:', error);
|
|
showAlert('Error', 'Failed to load the PDF Editor.');
|
|
} finally {
|
|
hideLoader();
|
|
}
|
|
}
|