File size: 5,702 Bytes
dea2c47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
(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);