2026-01-05 14:57:30 +05:30
|
|
|
import { DividePagesState } from '@/types';
|
2025-12-11 19:34:14 +05:30
|
|
|
import { showLoader, hideLoader, showAlert } from '../ui.js';
|
2026-03-26 12:11:12 +05:30
|
|
|
import {
|
|
|
|
|
downloadFile,
|
|
|
|
|
formatBytes,
|
|
|
|
|
parsePageRanges,
|
|
|
|
|
} from '../utils/helpers.js';
|
2025-12-11 19:34:14 +05:30
|
|
|
import { createIcons, icons } from 'lucide';
|
|
|
|
|
import { PDFDocument as PDFLibDocument } from 'pdf-lib';
|
2026-03-26 12:11:12 +05:30
|
|
|
import { loadPdfWithPasswordPrompt } from '../utils/password-prompt.js';
|
2026-03-26 13:40:21 +05:30
|
|
|
import { loadPdfDocument } from '../utils/load-pdf-document.js';
|
2025-12-11 19:34:14 +05:30
|
|
|
|
|
|
|
|
const pageState: DividePagesState = {
|
2026-03-26 12:11:12 +05:30
|
|
|
file: null,
|
|
|
|
|
pdfDoc: null,
|
|
|
|
|
totalPages: 0,
|
2025-12-11 19:34:14 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function resetState() {
|
2026-03-26 12:11:12 +05:30
|
|
|
pageState.file = null;
|
|
|
|
|
pageState.pdfDoc = null;
|
|
|
|
|
pageState.totalPages = 0;
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
const fileDisplayArea = document.getElementById('file-display-area');
|
|
|
|
|
if (fileDisplayArea) fileDisplayArea.innerHTML = '';
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
const toolOptions = document.getElementById('tool-options');
|
|
|
|
|
if (toolOptions) toolOptions.classList.add('hidden');
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
const fileInput = document.getElementById('file-input') as HTMLInputElement;
|
|
|
|
|
if (fileInput) fileInput.value = '';
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
const splitTypeSelect = document.getElementById(
|
|
|
|
|
'split-type'
|
|
|
|
|
) as HTMLSelectElement;
|
|
|
|
|
if (splitTypeSelect) splitTypeSelect.value = 'vertical';
|
feat: Add VitePress docs, EPUB to PDF tool, Phosphor icons, and licensing updates
- Set up VitePress documentation site (docs:dev, docs:build, docs:preview)
- Added Getting Started, Tools Reference, Contributing, and Commercial License pages
- Created self-hosting guides for Docker, Vercel, Netlify, Cloudflare, AWS, Hostinger, Nginx, Apache
- Updated README with documentation link, sponsors section, and docs contribution guide
- Added EPUB to PDF converter using LibreOffice WASM
- Migrated to Phosphor Icons for consistent iconography
- Added donation ribbon banner on landing page
- Removed 'Like My Work?' section (replaced by ribbon)
- Updated licensing.html with delivery model, AGPL notice, invoicing, and no-refund policy
- Added Commercial License documentation page
- Updated translations table (Chinese added, marked non-English as In Progress)
- Added sponsors.yml workflow for auto-generating sponsor avatars
2025-12-27 19:30:31 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
const pageRangeInput = document.getElementById(
|
|
|
|
|
'page-range'
|
|
|
|
|
) as HTMLInputElement;
|
|
|
|
|
if (pageRangeInput) pageRangeInput.value = '';
|
2025-12-11 19:34:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function updateUI() {
|
2026-03-26 12:11:12 +05:30
|
|
|
const fileDisplayArea = document.getElementById('file-display-area');
|
|
|
|
|
const toolOptions = document.getElementById('tool-options');
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
if (!fileDisplayArea) return;
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
fileDisplayArea.innerHTML = '';
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
if (pageState.file) {
|
|
|
|
|
const fileDiv = document.createElement('div');
|
|
|
|
|
fileDiv.className =
|
|
|
|
|
'flex items-center justify-between bg-gray-700 p-3 rounded-lg text-sm';
|
feat: Add VitePress docs, EPUB to PDF tool, Phosphor icons, and licensing updates
- Set up VitePress documentation site (docs:dev, docs:build, docs:preview)
- Added Getting Started, Tools Reference, Contributing, and Commercial License pages
- Created self-hosting guides for Docker, Vercel, Netlify, Cloudflare, AWS, Hostinger, Nginx, Apache
- Updated README with documentation link, sponsors section, and docs contribution guide
- Added EPUB to PDF converter using LibreOffice WASM
- Migrated to Phosphor Icons for consistent iconography
- Added donation ribbon banner on landing page
- Removed 'Like My Work?' section (replaced by ribbon)
- Updated licensing.html with delivery model, AGPL notice, invoicing, and no-refund policy
- Added Commercial License documentation page
- Updated translations table (Chinese added, marked non-English as In Progress)
- Added sponsors.yml workflow for auto-generating sponsor avatars
2025-12-27 19:30:31 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
const infoContainer = document.createElement('div');
|
|
|
|
|
infoContainer.className = 'flex flex-col overflow-hidden';
|
feat: Add VitePress docs, EPUB to PDF tool, Phosphor icons, and licensing updates
- Set up VitePress documentation site (docs:dev, docs:build, docs:preview)
- Added Getting Started, Tools Reference, Contributing, and Commercial License pages
- Created self-hosting guides for Docker, Vercel, Netlify, Cloudflare, AWS, Hostinger, Nginx, Apache
- Updated README with documentation link, sponsors section, and docs contribution guide
- Added EPUB to PDF converter using LibreOffice WASM
- Migrated to Phosphor Icons for consistent iconography
- Added donation ribbon banner on landing page
- Removed 'Like My Work?' section (replaced by ribbon)
- Updated licensing.html with delivery model, AGPL notice, invoicing, and no-refund policy
- Added Commercial License documentation page
- Updated translations table (Chinese added, marked non-English as In Progress)
- Added sponsors.yml workflow for auto-generating sponsor avatars
2025-12-27 19:30:31 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
const nameSpan = document.createElement('div');
|
|
|
|
|
nameSpan.className = 'truncate font-medium text-gray-200 text-sm mb-1';
|
|
|
|
|
nameSpan.textContent = pageState.file.name;
|
feat: Add VitePress docs, EPUB to PDF tool, Phosphor icons, and licensing updates
- Set up VitePress documentation site (docs:dev, docs:build, docs:preview)
- Added Getting Started, Tools Reference, Contributing, and Commercial License pages
- Created self-hosting guides for Docker, Vercel, Netlify, Cloudflare, AWS, Hostinger, Nginx, Apache
- Updated README with documentation link, sponsors section, and docs contribution guide
- Added EPUB to PDF converter using LibreOffice WASM
- Migrated to Phosphor Icons for consistent iconography
- Added donation ribbon banner on landing page
- Removed 'Like My Work?' section (replaced by ribbon)
- Updated licensing.html with delivery model, AGPL notice, invoicing, and no-refund policy
- Added Commercial License documentation page
- Updated translations table (Chinese added, marked non-English as In Progress)
- Added sponsors.yml workflow for auto-generating sponsor avatars
2025-12-27 19:30:31 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
const metaSpan = document.createElement('div');
|
|
|
|
|
metaSpan.className = 'text-xs text-gray-400';
|
|
|
|
|
metaSpan.textContent = `${formatBytes(pageState.file.size)} • Loading...`;
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
infoContainer.append(nameSpan, metaSpan);
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
const removeBtn = document.createElement('button');
|
|
|
|
|
removeBtn.className = 'ml-4 text-red-400 hover:text-red-300 flex-shrink-0';
|
|
|
|
|
removeBtn.innerHTML = '<i data-lucide="trash-2" class="w-4 h-4"></i>';
|
|
|
|
|
removeBtn.onclick = function () {
|
|
|
|
|
resetState();
|
|
|
|
|
};
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
fileDiv.append(infoContainer, removeBtn);
|
|
|
|
|
fileDisplayArea.appendChild(fileDiv);
|
|
|
|
|
createIcons({ icons });
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
try {
|
|
|
|
|
const result = await loadPdfWithPasswordPrompt(pageState.file);
|
|
|
|
|
if (!result) {
|
|
|
|
|
resetState();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
result.pdf.destroy();
|
|
|
|
|
pageState.file = result.file;
|
|
|
|
|
showLoader('Loading PDF...');
|
|
|
|
|
|
2026-03-26 13:40:21 +05:30
|
|
|
pageState.pdfDoc = await loadPdfDocument(result.bytes);
|
2026-03-26 12:11:12 +05:30
|
|
|
pageState.totalPages = pageState.pdfDoc.getPageCount();
|
|
|
|
|
hideLoader();
|
|
|
|
|
|
|
|
|
|
metaSpan.textContent = `${formatBytes(pageState.file.size)} • ${pageState.totalPages} pages`;
|
|
|
|
|
|
|
|
|
|
if (toolOptions) toolOptions.classList.remove('hidden');
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error loading PDF:', error);
|
|
|
|
|
hideLoader();
|
|
|
|
|
showAlert('Error', 'Failed to load PDF file.');
|
|
|
|
|
resetState();
|
2025-12-11 19:34:14 +05:30
|
|
|
}
|
2026-03-26 12:11:12 +05:30
|
|
|
} else {
|
|
|
|
|
if (toolOptions) toolOptions.classList.add('hidden');
|
|
|
|
|
}
|
2025-12-11 19:34:14 +05:30
|
|
|
}
|
|
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
async function dividePages() {
|
|
|
|
|
if (!pageState.pdfDoc || !pageState.file) {
|
|
|
|
|
showAlert('Error', 'Please upload a PDF first.');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const pageRangeInput = document.getElementById(
|
|
|
|
|
'page-range'
|
|
|
|
|
) as HTMLInputElement;
|
|
|
|
|
const pageRangeValue = pageRangeInput?.value.trim().toLowerCase() || '';
|
|
|
|
|
const splitTypeSelect = document.getElementById(
|
|
|
|
|
'split-type'
|
|
|
|
|
) as HTMLSelectElement;
|
|
|
|
|
const splitType = splitTypeSelect.value;
|
|
|
|
|
|
|
|
|
|
let pagesToDivide: Set<number>;
|
|
|
|
|
|
|
|
|
|
if (pageRangeValue === '' || pageRangeValue === 'all') {
|
|
|
|
|
pagesToDivide = new Set(
|
|
|
|
|
Array.from({ length: pageState.totalPages }, (_, i) => i + 1)
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
const parsedIndices = parsePageRanges(pageRangeValue, pageState.totalPages);
|
|
|
|
|
pagesToDivide = new Set(parsedIndices.map((i) => i + 1));
|
|
|
|
|
|
|
|
|
|
if (pagesToDivide.size === 0) {
|
|
|
|
|
showAlert(
|
|
|
|
|
'Invalid Range',
|
|
|
|
|
'Please enter a valid page range (e.g., 1-5, 8, 11-13).'
|
|
|
|
|
);
|
|
|
|
|
return;
|
2025-12-11 19:34:14 +05:30
|
|
|
}
|
2026-03-26 12:11:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
showLoader('Splitting PDF pages...');
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const newPdfDoc = await PDFLibDocument.create();
|
|
|
|
|
const pages = pageState.pdfDoc.getPages();
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < pages.length; i++) {
|
|
|
|
|
const pageNum = i + 1;
|
|
|
|
|
const originalPage = pages[i];
|
|
|
|
|
const { width, height } = originalPage.getSize();
|
|
|
|
|
|
|
|
|
|
showLoader(`Processing page ${pageNum} of ${pages.length}...`);
|
|
|
|
|
|
|
|
|
|
if (pagesToDivide.has(pageNum)) {
|
|
|
|
|
const [page1] = await newPdfDoc.copyPages(pageState.pdfDoc, [i]);
|
|
|
|
|
const [page2] = await newPdfDoc.copyPages(pageState.pdfDoc, [i]);
|
|
|
|
|
|
|
|
|
|
switch (splitType) {
|
|
|
|
|
case 'vertical':
|
|
|
|
|
page1.setCropBox(0, 0, width / 2, height);
|
|
|
|
|
page2.setCropBox(width / 2, 0, width / 2, height);
|
|
|
|
|
break;
|
|
|
|
|
case 'horizontal':
|
|
|
|
|
page1.setCropBox(0, height / 2, width, height / 2);
|
|
|
|
|
page2.setCropBox(0, 0, width, height / 2);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
newPdfDoc.addPage(page1);
|
|
|
|
|
newPdfDoc.addPage(page2);
|
|
|
|
|
} else {
|
|
|
|
|
const [copiedPage] = await newPdfDoc.copyPages(pageState.pdfDoc, [i]);
|
|
|
|
|
newPdfDoc.addPage(copiedPage);
|
|
|
|
|
}
|
2025-12-11 19:34:14 +05:30
|
|
|
}
|
|
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
const newPdfBytes = await newPdfDoc.save();
|
|
|
|
|
downloadFile(
|
|
|
|
|
new Blob([new Uint8Array(newPdfBytes)], { type: 'application/pdf' }),
|
2026-04-05 13:44:16 +05:30
|
|
|
pageState.file.name
|
2026-03-26 12:11:12 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
showAlert(
|
|
|
|
|
'Success',
|
|
|
|
|
'Pages have been divided successfully!',
|
|
|
|
|
'success',
|
|
|
|
|
function () {
|
|
|
|
|
resetState();
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
showAlert('Error', 'An error occurred while dividing the PDF.');
|
|
|
|
|
} finally {
|
|
|
|
|
hideLoader();
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
function handleFileSelect(files: FileList | null) {
|
|
|
|
|
if (files && files.length > 0) {
|
|
|
|
|
const file = files[0];
|
|
|
|
|
if (
|
|
|
|
|
file.type === 'application/pdf' ||
|
|
|
|
|
file.name.toLowerCase().endsWith('.pdf')
|
|
|
|
|
) {
|
|
|
|
|
pageState.file = file;
|
|
|
|
|
updateUI();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
document.addEventListener('DOMContentLoaded', function () {
|
|
|
|
|
const fileInput = document.getElementById('file-input') as HTMLInputElement;
|
|
|
|
|
const dropZone = document.getElementById('drop-zone');
|
|
|
|
|
const processBtn = document.getElementById('process-btn');
|
|
|
|
|
const backBtn = document.getElementById('back-to-tools');
|
|
|
|
|
|
|
|
|
|
if (backBtn) {
|
|
|
|
|
backBtn.addEventListener('click', function () {
|
|
|
|
|
window.location.href = import.meta.env.BASE_URL;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fileInput && dropZone) {
|
|
|
|
|
fileInput.addEventListener('change', function (e) {
|
|
|
|
|
handleFileSelect((e.target as HTMLInputElement).files);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
dropZone.addEventListener('dragover', function (e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
dropZone.classList.add('bg-gray-700');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
dropZone.addEventListener('dragleave', function (e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
dropZone.classList.remove('bg-gray-700');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
dropZone.addEventListener('drop', function (e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
dropZone.classList.remove('bg-gray-700');
|
|
|
|
|
const files = e.dataTransfer?.files;
|
|
|
|
|
if (files && files.length > 0) {
|
|
|
|
|
const pdfFiles = Array.from(files).filter(function (f) {
|
|
|
|
|
return (
|
|
|
|
|
f.type === 'application/pdf' ||
|
|
|
|
|
f.name.toLowerCase().endsWith('.pdf')
|
|
|
|
|
);
|
2025-12-11 19:34:14 +05:30
|
|
|
});
|
2026-03-26 12:11:12 +05:30
|
|
|
if (pdfFiles.length > 0) {
|
|
|
|
|
const dataTransfer = new DataTransfer();
|
|
|
|
|
dataTransfer.items.add(pdfFiles[0]);
|
|
|
|
|
handleFileSelect(dataTransfer.files);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
fileInput.addEventListener('click', function () {
|
|
|
|
|
fileInput.value = '';
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
if (processBtn) {
|
|
|
|
|
processBtn.addEventListener('click', dividePages);
|
|
|
|
|
}
|
2025-12-11 19:34:14 +05:30
|
|
|
});
|