|
566 | 566 | } |
567 | 567 | evt.preventDefault(); |
568 | 568 | }; |
| 569 | + |
| 570 | + document.addEventListener('paste', function (evt) { |
| 571 | + const items = evt.clipboardData?.items; |
| 572 | + if (!items) return; |
| 573 | + |
| 574 | + const pastedFiles = []; |
| 575 | + for (let i = 0; i < items.length; i++) { |
| 576 | + if (items[i].kind === 'file') { |
| 577 | + const file = items[i].getAsFile(); |
| 578 | + if (file) pastedFiles.push(file); |
| 579 | + } |
| 580 | + } |
| 581 | + |
| 582 | + if (pastedFiles.length === 0) return; |
| 583 | + |
| 584 | + evt.preventDefault(); |
| 585 | + |
| 586 | + const dataTransfer = new DataTransfer(); |
| 587 | + |
| 588 | + const existingFiles = Array.from(hiddenFileButton.files); |
| 589 | + for (const file of existingFiles) { |
| 590 | + dataTransfer.items.add(file); |
| 591 | + } |
| 592 | + |
| 593 | + // Add pasted files, skipping duplicates (same size + type already attached) |
| 594 | + let added = 0; |
| 595 | + for (const newFile of pastedFiles) { |
| 596 | + // Check if this exact file (size + name + type) is already there |
| 597 | + const isDuplicate = existingFiles.some(oldFile => |
| 598 | + oldFile.size === newFile.size && |
| 599 | + oldFile.name === newFile.name && |
| 600 | + oldFile.type === newFile.type |
| 601 | + ); |
| 602 | + |
| 603 | + if (!isDuplicate) { |
| 604 | + // We keep the original 'newFile' object to preserve the filename |
| 605 | + dataTransfer.items.add(newFile); |
| 606 | + added++; |
| 607 | + } |
| 608 | + } |
| 609 | + |
| 610 | + if (added === 0) return; |
| 611 | + |
| 612 | + hiddenFileButton.files = dataTransfer.files; |
| 613 | + |
| 614 | + if (hiddenFileButton.files.length > 1) { |
| 615 | + attachFileButton.textContent = "Attached: " + hiddenFileButton.files.length + " files"; |
| 616 | + } else if (hiddenFileButton.files.length === 1) { |
| 617 | + attachFileButton.textContent = "Attached: " + hiddenFileButton.files[0].name; |
| 618 | + } |
| 619 | + |
| 620 | + fileOversized(); |
| 621 | + }); |
569 | 622 | // {%- endif %} |
570 | 623 |
|
571 | 624 | </script> |
|
0 commit comments