Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
133 lines (114 loc) · 4.67 KB
/
Copy pathscript.js
File metadata and controls
133 lines (114 loc) · 4.67 KB
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
const dropZone = document.getElementById('drop-zone');
const fileInput = document.getElementById('file-input');
const fileNameLabel = document.getElementById('file-name');
const actionBtn = document.getElementById('action-btn');
const statusMsg = document.getElementById('status');
const modeSlider = document.getElementById('mode-slider');
// UI Elements
const appLogo = document.getElementById('app-logo');
const heroTitle = document.getElementById('hero-title');
const heroSubtitle = document.getElementById('hero-subtitle');
const uploadIcon = document.getElementById('upload-icon');
let selectedFile = null;
let currentMode = 'convert';
// --- 1. SLIDER TOGGLE LOGIC ---
modeSlider.addEventListener('change', () => {
selectedFile = null;
actionBtn.disabled = true;
statusMsg.innerText = "";
fileInput.value = "";
if (modeSlider.checked) {
// Switch to Convertiny (Compress)
currentMode = 'compress';
appLogo.innerHTML = 'Convertiny<span>.</span>';
heroTitle.innerHTML = 'Compress your files <span class="text-gradient">Compactly.</span>';
heroSubtitle.innerText = 'Shrink PDF and DOCX files without losing quality.';
fileInput.accept = '.docx, .pdf';
fileNameLabel.innerHTML = 'Drag & drop your <b>.docx</b> or <b>.pdf</b> file here';
actionBtn.innerText = 'Compress File';
uploadIcon.innerText = '🗜️';
} else {
// Switch to Convertly (Convert)
currentMode = 'convert';
appLogo.innerHTML = 'Convertly<span>.</span>';
heroTitle.innerHTML = 'Convert your docx <span class="text-gradient">Swiftly.</span>';
heroSubtitle.innerText = 'Simple, Fast, and Secure. High-quality PDF conversion.';
fileInput.accept = '.docx';
fileNameLabel.innerHTML = 'Drag & drop your <b>.docx</b> file here';
actionBtn.innerText = 'Convert to PDF';
uploadIcon.innerText = '📄';
}
});
// --- 2. FILE SELECTION ---
dropZone.onclick = () => fileInput.click();
fileInput.onchange = (e) => {
handleFile(e.target.files[0]);
};
function handleFile(file) {
if (!file) return;
const ext = file.name.split('.').pop().toLowerCase();
let isValid = false;
// Focused only on DOCX for Convertly
if (currentMode === 'convert' && ext === 'docx') isValid = true;
if (currentMode === 'compress' && (ext === 'docx' || ext === 'pdf')) isValid = true;
if (isValid) {
selectedFile = file;
fileNameLabel.innerHTML = `Selected: <b>${file.name}</b>`;
actionBtn.disabled = false;
statusMsg.innerText = "";
} else {
const allowed = currentMode === 'convert' ? '.docx' : '.docx or .pdf';
alert(`Please upload a valid ${allowed} file.`);
fileInput.value = '';
}
}
// --- 3. ACTION LOGIC ---
actionBtn.onclick = async () => {
if (!selectedFile) return;
actionBtn.disabled = true;
actionBtn.innerText = "Processing...";
if (currentMode === 'convert') {
processConversion();
} else {
processCompression();
}
};
function processConversion() {
statusMsg.innerText = "Reading document...";
const reader = new FileReader();
reader.onload = async (event) => {
const arrayBuffer = event.target.result;
mammoth.convertToHtml({ arrayBuffer: arrayBuffer })
.then(function(result) {
const container = document.getElementById('preview-container');
container.innerHTML = result.value;
statusMsg.innerText = "Generating PDF...";
const opt = {
margin: 0.5,
filename: selectedFile.name.replace('.docx', '.pdf'),
image: { type: 'jpeg', quality: 0.98 },
html2canvas: { scale: 2 },
jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' }
};
html2pdf().set(opt).from(container).save().then(() => {
statusMsg.innerText = "✅ Conversion Successful!";
actionBtn.disabled = false;
actionBtn.innerText = "Convert to PDF";
});
})
.catch(err => {
console.error(err);
statusMsg.innerText = "❌ Error during conversion.";
actionBtn.disabled = false;
});
};
reader.readAsArrayBuffer(selectedFile);
}
function processCompression() {
statusMsg.innerText = "Compressing... (Mockup)";
setTimeout(() => {
statusMsg.innerText = "✅ Done! (Backend required for real compression)";
actionBtn.disabled = false;
actionBtn.innerText = "Compress File";
}, 1500);
}