-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
490 lines (415 loc) · 22.5 KB
/
index.html
File metadata and controls
490 lines (415 loc) · 22.5 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>InvoiceMerger A4</title>
<!-- Libraries -->
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://unpkg.com/lucide@latest"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.min.js"></script>
<style>
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
body {
font-family: 'Inter', sans-serif;
background-color: #f3f4f6;
}
/* Loading Spinner Animation */
.spinner {
animation: spin 1s linear infinite;
}
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
/* Print Styles */
@media print {
@page {
margin: 0;
size: auto; /* Orientation is handled by dynamic styles */
}
html, body, #app, main {
width: 100% !important;
height: 100% !important;
overflow: visible !important;
margin: 0 !important;
padding: 0 !important;
background: white !important;
}
.no-print {
display: none !important;
}
* {
-webkit-print-color-adjust: exact !important;
print-color-adjust: exact !important;
}
#canvas-container {
display: block !important;
position: absolute;
top: 0;
left: 0;
margin: 0 !important;
padding: 0 !important;
width: 100% !important;
height: 100% !important;
}
#a4-page {
margin: 0 !important;
box-shadow: none !important;
border: none !important;
transform: none !important;
page-break-after: always;
width: 100% !important;
height: 100% !important;
}
/* Hide remove buttons and file names when printing */
.invoice-actions, .invoice-label {
display: none !important;
}
.invoice-item {
border: none !important;
background: transparent !important;
}
}
</style>
<script type="importmap">
{
"imports": {
"vite": "https://esm.sh/vite@^7.3.1"
}
}
</script>
<link rel="stylesheet" href="/index.css">
</head>
<body>
<div id="app" class="min-h-screen flex flex-col">
<!-- Header -->
<header class="bg-white border-b border-slate-200 py-4 px-6 no-print sticky top-0 z-50">
<div class="max-w-7xl mx-auto flex items-center justify-between">
<div class="flex items-center gap-3">
<div class="bg-blue-600 p-2 rounded-lg">
<i data-lucide="layout" class="w-5 h-5 text-white"></i>
</div>
<div>
<h1 class="text-xl font-bold text-slate-800 leading-tight">InvoiceMerger</h1>
<p class="text-xs text-slate-500 font-medium">Auto-Layout Print Tool</p>
</div>
</div>
<div class="flex items-center gap-3">
<div id="action-buttons" class="flex items-center gap-3 hidden">
<button onclick="app.clearAll()" class="p-2 text-slate-500 hover:text-red-600 hover:bg-red-50 rounded-lg transition-colors flex items-center gap-2 text-sm font-medium">
<i data-lucide="trash-2" class="w-4 h-4"></i>
<span class="hidden sm:inline">Clear</span>
</button>
<div class="h-6 w-px bg-slate-200 mx-1"></div>
<button onclick="window.print()" class="flex items-center gap-2 px-5 py-2.5 rounded-lg font-semibold shadow-sm transition-all bg-blue-600 text-white hover:bg-blue-700 hover:shadow-md">
<i data-lucide="printer" class="w-4 h-4"></i>
Print A4
</button>
</div>
<div id="action-buttons-placeholder">
<button disabled class="flex items-center gap-2 px-5 py-2.5 rounded-lg font-semibold shadow-sm bg-slate-100 text-slate-400 cursor-not-allowed">
<i data-lucide="printer" class="w-4 h-4"></i>
Print A4
</button>
</div>
</div>
</div>
</header>
<!-- Main Content -->
<main class="flex-1 bg-slate-100 relative overflow-x-hidden">
<div class="max-w-7xl mx-auto p-4 md:p-8 flex flex-col lg:flex-row gap-8">
<!-- Sidebar (Left) -->
<div class="w-full lg:w-80 flex-shrink-0 flex flex-col gap-6 no-print">
<!-- Upload Box -->
<div class="bg-white rounded-xl shadow-sm border border-slate-200 p-5">
<h2 class="text-sm font-bold text-slate-900 uppercase tracking-wide mb-4">Upload Invoices</h2>
<div id="dropzone"
class="relative border-2 border-dashed border-slate-300 rounded-xl p-8 transition-all duration-200 ease-in-out text-center cursor-pointer hover:border-slate-400 bg-white group"
onclick="document.getElementById('file-upload').click()">
<input id="file-upload" type="file" multiple accept="application/pdf" class="hidden" onchange="app.handleFileSelect(event)">
<!-- Idle State -->
<div id="dropzone-idle" class="flex flex-col items-center justify-center gap-3">
<div class="p-3 rounded-full bg-slate-100 group-hover:bg-slate-200 transition-colors">
<i data-lucide="upload" class="w-6 h-6 text-slate-500"></i>
</div>
<div>
<p class="text-base font-semibold text-slate-700">Click to upload or drag and drop</p>
<p class="text-sm text-slate-500 mt-1">PDF files only (Invoices)</p>
</div>
</div>
<!-- Processing State -->
<div id="dropzone-processing" class="hidden flex flex-col items-center justify-center gap-3">
<i data-lucide="loader-2" class="w-10 h-10 text-blue-500 spinner"></i>
<p class="text-sm font-medium text-slate-600">Processing invoices...</p>
</div>
</div>
<div class="mt-4 flex items-start gap-2 p-3 bg-blue-50 text-blue-700 rounded-lg text-xs leading-relaxed">
<i data-lucide="info" class="w-4 h-4 flex-shrink-0 mt-0.5"></i>
<p>
<strong>Layout Logic:</strong><br/>
• 1-2 Invoices: Portrait A4<br/>
• 3+ Invoices: Landscape A4 (6 slots)
</p>
</div>
</div>
<!-- Status Box -->
<div class="bg-white rounded-xl shadow-sm border border-slate-200 p-5 hidden lg:block">
<h2 class="text-sm font-bold text-slate-900 uppercase tracking-wide mb-3">Current Setup</h2>
<div class="space-y-3" id="status-panel">
<div class="flex justify-between text-sm">
<span class="text-slate-500">Files</span>
<span class="font-semibold text-slate-900" id="status-count">0</span>
</div>
<div class="flex justify-between text-sm">
<span class="text-slate-500">Orientation</span>
<span class="font-semibold text-slate-900" id="status-orientation">Portrait</span>
</div>
<div class="flex justify-between text-sm">
<span class="text-slate-500">Paper Size</span>
<span class="font-semibold text-slate-900" id="status-size">210 x 297mm</span>
</div>
<div class="flex justify-between text-sm">
<span class="text-slate-500">Layout</span>
<span class="font-semibold text-slate-900" id="status-layout">1 x 1</span>
</div>
</div>
</div>
</div>
<!-- Preview Area (Right) -->
<div class="flex-1 flex flex-col items-center min-h-[500px]">
<!-- Zoom Controls -->
<div class="w-full flex justify-center mb-4 no-print">
<div class="bg-white/80 backdrop-blur rounded-full shadow border border-slate-200 p-1 flex items-center gap-1">
<button onclick="app.updateScale(-0.1)" class="w-8 h-8 flex items-center justify-center rounded-full hover:bg-slate-100 text-slate-600 font-bold">-</button>
<span id="zoom-level" class="text-xs font-mono w-12 text-center text-slate-500">80%</span>
<button onclick="app.updateScale(0.1)" class="w-8 h-8 flex items-center justify-center rounded-full hover:bg-slate-100 text-slate-600 font-bold">+</button>
</div>
</div>
<!-- Canvas Container -->
<div id="canvas-container" class="w-full overflow-auto flex justify-center pb-20">
<!-- Dynamic A4 Page -->
<div id="a4-page" class="bg-white shadow-2xl relative overflow-hidden flex flex-wrap content-start transition-all duration-300 ease-in-out">
<!-- Empty State -->
<div id="empty-state" class="w-full h-full flex flex-col items-center justify-center text-slate-300 border-2 border-dashed border-slate-200 rounded-lg m-10 box-border">
<span class="text-4xl font-bold opacity-20">A4 Preview</span>
<span class="mt-2 text-sm opacity-40">Upload files to populate</span>
</div>
<!-- Grid Container -->
<div id="grid-container" class="w-full h-full grid gap-4 hidden p-[10mm]">
<!-- Items will be injected here -->
</div>
</div>
</div>
<!-- Helper Text -->
<div id="helper-text" class="text-xs text-slate-400 no-print flex gap-2 mt-[-3rem]">
A4 Portrait • 0 items
</div>
</div>
</div>
</main>
</div>
<!-- Dynamic Styles for Print -->
<style id="dynamic-print-style"></style>
<script>
// --- Application Logic ---
const app = {
files: [], // Array of { id, name, previewUrl }
scale: 0.8,
isProcessing: false,
init() {
// PDF.js Worker Configuration
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.worker.min.js';
this.setupDragAndDrop();
this.updateScale(0); // Initialize UI
lucide.createIcons();
// Responsive scale init
if (window.innerWidth < 640) this.scale = 0.4;
else if (window.innerWidth < 1024) this.scale = 0.6;
this.updateUI();
},
// File Processing
async processFiles(fileList) {
if (fileList.length === 0) return;
this.setProcessing(true);
const pdfFiles = Array.from(fileList).filter(f => f.type === 'application/pdf');
if (pdfFiles.length === 0) {
alert("Please upload valid PDF files.");
this.setProcessing(false);
return;
}
try {
const newFiles = await Promise.all(pdfFiles.map(file => this.convertPdfToImage(file)));
this.files = [...this.files, ...newFiles];
this.render();
} catch (error) {
console.error("Error processing files:", error);
alert("Failed to process some files.");
} finally {
this.setProcessing(false);
}
},
async convertPdfToImage(file) {
const arrayBuffer = await file.arrayBuffer();
const pdf = await pdfjsLib.getDocument({ data: arrayBuffer }).promise;
const page = await pdf.getPage(1);
const viewport = page.getViewport({ scale: 5.0 }); // High quality for print
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
canvas.height = viewport.height;
canvas.width = viewport.width;
await page.render({ canvasContext: context, viewport }).promise;
return {
id: Math.random().toString(36).substring(7),
name: file.name,
previewUrl: canvas.toDataURL('image/png')
};
},
// UI Actions
handleFileSelect(event) {
this.processFiles(event.target.files);
event.target.value = ''; // Reset input
},
removeFile(id) {
this.files = this.files.filter(f => f.id !== id);
this.render();
},
clearAll() {
if(confirm('Are you sure you want to clear all invoices?')) {
this.files = [];
this.render();
}
},
updateScale(delta) {
this.scale = Math.min(1.5, Math.max(0.3, this.scale + delta));
this.updateUI();
},
setProcessing(loading) {
this.isProcessing = loading;
const idleEl = document.getElementById('dropzone-idle');
const processingEl = document.getElementById('dropzone-processing');
if (loading) {
idleEl.classList.add('hidden');
processingEl.classList.remove('hidden');
} else {
idleEl.classList.remove('hidden');
processingEl.classList.add('hidden');
}
// Re-render icons if state changed
lucide.createIcons();
},
// Rendering Logic
render() {
const count = this.files.length;
// 1. Determine Layout Configuration
let config = { rows: 1, cols: 1, orientation: 'portrait' };
if (count === 2) {
config = { rows: 2, cols: 1, orientation: 'portrait' };
} else if (count >= 3 && count < 5) {
config = { rows: 2, cols: 2, orientation: 'landscape' };
} else if (count > 4) {
config = { rows: 2, cols: 3, orientation: 'landscape' };
alert("请注意, 最多只能放置6张发票")
}
const isLandscape = config.orientation === 'landscape';
const widthMm = isLandscape ? 297 : 210;
const heightMm = isLandscape ? 210 : 297;
// 2. Update Status Panel
document.getElementById('status-count').textContent = count;
document.getElementById('status-orientation').textContent = config.orientation.charAt(0).toUpperCase() + config.orientation.slice(1);
document.getElementById('status-orientation').className = `font-semibold ${isLandscape ? 'text-blue-600' : 'text-slate-900'}`;
document.getElementById('status-size').textContent = `${widthMm} x ${heightMm}mm`;
document.getElementById('status-layout').textContent = `${config.rows} x ${config.cols}`;
// 3. Update Header Actions
const actionBtns = document.getElementById('action-buttons');
const placeholder = document.getElementById('action-buttons-placeholder');
if (count > 0) {
actionBtns.classList.remove('hidden');
placeholder.classList.add('hidden');
} else {
actionBtns.classList.add('hidden');
placeholder.classList.remove('hidden');
}
// 4. Render A4 Canvas
const page = document.getElementById('a4-page');
const emptyState = document.getElementById('empty-state');
const gridContainer = document.getElementById('grid-container');
const helperText = document.getElementById('helper-text');
// Dimensions
page.style.width = `${widthMm}mm`;
page.style.height = `${heightMm}mm`;
if (count === 0) {
emptyState.classList.remove('hidden');
gridContainer.classList.add('hidden');
} else {
emptyState.classList.add('hidden');
gridContainer.classList.remove('hidden');
// Grid Styles
gridContainer.style.gridTemplateColumns = `repeat(${config.cols}, minmax(0, 1fr))`;
gridContainer.style.gridTemplateRows = `repeat(${config.rows}, minmax(0, 1fr))`;
// Generate HTML for items
gridContainer.innerHTML = this.files.map(file => `
<div class="invoice-item relative group border border-slate-100 hover:border-blue-300 transition-colors rounded p-2 flex items-center justify-center bg-slate-50/50 overflow-hidden">
<img src="${file.previewUrl}" class="max-w-full max-h-full object-contain shadow-sm" alt="${file.name}">
<div class="invoice-actions absolute top-1 right-1 opacity-0 group-hover:opacity-100 transition-opacity flex gap-1 z-10">
<button onclick="app.removeFile('${file.id}')" class="p-1.5 bg-red-500 text-white rounded-full hover:bg-red-600 shadow-md" title="Remove invoice">
<i data-lucide="x" width="12" height="12"></i>
</button>
</div>
<div class="invoice-label absolute bottom-1 left-1 opacity-0 group-hover:opacity-100 bg-black/75 text-white text-[10px] px-2 py-0.5 rounded pointer-events-none truncate max-w-[90%]">
${file.name}
</div>
</div>
`).join('');
}
// Helper text
helperText.innerHTML = `A4 ${isLandscape ? 'Landscape' : 'Portrait'} (${widthMm}mm x ${heightMm}mm) • ${count} items`;
// 5. Update Print Styles
const printStyle = document.getElementById('dynamic-print-style');
printStyle.innerHTML = `
@media print {
@page { size: A4 ${config.orientation}; }
.a4-page { width: ${widthMm}mm !important; height: ${heightMm}mm !important; }
}
`;
// 6. Refresh Icons and Scale
lucide.createIcons();
this.updateUI();
},
updateUI() {
// Apply visual scaling to preview
const page = document.getElementById('a4-page');
page.style.transform = `scale(${this.scale})`;
page.style.transformOrigin = 'top center';
document.getElementById('zoom-level').textContent = Math.round(this.scale * 100) + '%';
},
setupDragAndDrop() {
const dropzone = document.getElementById('dropzone');
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
dropzone.addEventListener(eventName, preventDefaults, false);
});
function preventDefaults(e) {
e.preventDefault();
e.stopPropagation();
}
['dragenter', 'dragover'].forEach(eventName => {
dropzone.addEventListener(eventName, () => {
dropzone.classList.add('border-blue-500', 'bg-blue-50');
}, false);
});
['dragleave', 'drop'].forEach(eventName => {
dropzone.addEventListener(eventName, () => {
dropzone.classList.remove('border-blue-500', 'bg-blue-50');
}, false);
});
dropzone.addEventListener('drop', (e) => {
this.processFiles(e.dataTransfer.files);
}, false);
}
};
// Initialize App
window.addEventListener('DOMContentLoaded', () => app.init());
</script>
<script type="module" src="/index.tsx"></script>
</body>
</html>