pdf / static /js /features /split.js
github-actions[bot]
Deploy from GitHub Actions
dea2c47
(function(window) {
const PdfApp = window.PdfApp = window.PdfApp || {};
const features = PdfApp.features = PdfApp.features || {};
features.split = function initSplitFeature() {
const dropArea = document.getElementById('splitDropArea');
const fileInput = document.getElementById('split_pdf_file');
const fileList = document.getElementById('splitFileList');
const form = document.getElementById('splitPdfForm');
const submitButton = document.getElementById('splitPdfBtn');
const loading = document.getElementById('splitLoading');
if (!dropArea || !fileInput || !fileList || !form || !submitButton || !loading) {
return;
}
let selectedFile = null;
function displayPdfPageInfo(file, targetElement) {
const infoDiv = document.createElement('div');
infoDiv.className = 'pdf-info mt-2';
const infoText = document.createElement('small');
infoText.className = 'text-muted';
const infoIcon = document.createElement('i');
infoIcon.className = 'fas fa-info-circle';
infoText.appendChild(infoIcon);
infoText.appendChild(document.createTextNode(` 文件大小:${PdfApp.formatFileSize(file.size)}`));
infoDiv.appendChild(infoText);
const oldInfo = targetElement.querySelector('.pdf-info');
if (oldInfo) {
oldInfo.remove();
}
targetElement.appendChild(infoDiv);
}
function displaySelectedFile() {
fileList.innerHTML = '';
if (!selectedFile) {
submitButton.disabled = true;
return;
}
const fileItem = PdfApp.createFileItem(selectedFile, removeSelectedFile);
fileList.appendChild(fileItem);
submitButton.disabled = false;
}
function removeSelectedFile() {
selectedFile = null;
fileInput.value = '';
displaySelectedFile();
}
fileInput.addEventListener('change', function(event) {
selectedFile = event.target.files[0] || null;
displaySelectedFile();
if (selectedFile) {
displayPdfPageInfo(selectedFile, fileList);
}
});
PdfApp.bindDragAndDrop(dropArea, function(event) {
const files = Array.from(event.dataTransfer.files).filter(function(file) {
return file.type === 'application/pdf';
});
if (!files.length) {
alert('请拖拽PDF文件');
return;
}
selectedFile = files[0];
fileInput.files = PdfApp.createFileList([selectedFile]);
displaySelectedFile();
displayPdfPageInfo(selectedFile, fileList);
});
PdfApp.bindRadioToggle('split_type', {
range: 'rangeOptions',
group: 'groupOptions',
extract: 'extractOptions'
});
form.addEventListener('submit', function(event) {
event.preventDefault();
if (!selectedFile) {
alert('请选择一个PDF文件');
return;
}
loading.style.display = 'block';
submitButton.disabled = true;
const formData = new FormData();
formData.append('pdf_file', selectedFile);
const splitType = document.querySelector('input[name="split_type"]:checked').value;
formData.append('split_type', splitType);
if (splitType === 'range') {
const ranges = document.getElementById('ranges').value;
if (!ranges) {
alert('请输入页面范围');
loading.style.display = 'none';
submitButton.disabled = false;
return;
}
formData.append('ranges', ranges);
} else if (splitType === 'group') {
formData.append('pages_per_group', document.getElementById('pages_per_group').value);
} else if (splitType === 'extract') {
const pageNumbers = document.getElementById('page_numbers').value;
if (!pageNumbers) {
alert('请输入要提取的页码');
loading.style.display = 'none';
submitButton.disabled = false;
return;
}
formData.append('page_numbers', pageNumbers);
}
fetch('/split-pdf', {
method: 'POST',
body: formData
})
.then(function(response) {
if (response.ok) {
return response.blob();
}
return response.json().then(function(err) {
throw new Error(err.error || '分割失败');
});
})
.then(function(blob) {
const filename = blob.type === 'application/zip' ? 'split_pdf_files.zip' : 'extracted_page.pdf';
PdfApp.downloadBlob(blob, filename);
removeSelectedFile();
})
.catch(function(error) {
console.error('Error:', error);
alert('分割失败: ' + error.message);
})
.finally(function() {
loading.style.display = 'none';
submitButton.disabled = false;
});
});
};
})(window);