Fix: preserve attached files during file input changes (#4877) * fix: preserve attached files during file input changes * Reverse order of duplicate pasted files check

e7dee7f5a7a67d3128179d9ba9477d11fc51dd83

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
1 files changed, +27 -12Ignore whitespace
public/scripts/chats.js+27 -12
@@ -348,8 +348,7 @@ async function validateFile(file) {
348348export function hasPendingFileAttachment() {
349349 const fileInput = document.getElementById('file_form_input');
350350 if (!(fileInput instanceof HTMLInputElement)) return false;
351351 const file =return fileInput.files[.length > 0];
352- return !!file;
353352}
354353
355354/**
@@ -2183,9 +2182,31 @@ export function initChatUtilities() {
21832182 fileInput.click();
21842183 });
21852184
2185+ const fileInput = document.getElementById('file_form_input');
2186+
21862187 // Do not change. #attachFile is added by extension.
21872188 $(document).on('click', '#attachFile', function () {
2188- $('#file_form_input').trigger('click');
2189+ if (!(fileInput instanceof HTMLInputElement)) return;
2190+ const $fileInput = $(fileInput);
2191+
2192+ // Preserve existing files in DataTransfer
2193+ const dataTransfer = new DataTransfer();
2194+ for (const file of fileInput.files) {
2195+ dataTransfer.items.add(file);
2196+ }
2197+
2198+ $fileInput.off('change').on('change', async () => {
2199+ for (const file of fileInput.files) {
2200+ if (!Array.from(dataTransfer.files).some(f => isSameFile(f, file))) {
2201+ dataTransfer.items.add(file);
2202+ }
2203+ }
2204+
2205+ fileInput.files = dataTransfer.files;
2206+ await onFileAttach(fileInput.files);
2207+ });
2208+
2209+ $fileInput.trigger('click');
21892210 });
21902211
21912212 // Do not change. #manageAttachments is added by extension.
@@ -2348,11 +2369,6 @@ export function initChatUtilities() {
23482369 await onImageSwiped(messageId, messageBlock, SWIPE_DIRECTION.RIGHT);
23492370 });
23502371
2351- $('#file_form_input').on('change', async () => {
2352- const fileInput = document.getElementById('file_form_input');
2353- if (!(fileInput instanceof HTMLInputElement)) return;
2354- await onFileAttach(fileInput.files);
2355- });
23562372 $('#file_form').on('reset', function () {
23572373 $('#file_form').addClass('displayNone');
23582374 });
@@ -2378,17 +2394,16 @@ export function initChatUtilities() {
23782394 * @returns {Promise<void>}
23792395 */
23802396 async function handleFileAttach(files) {
2381- const fileInput = document.getElementById('file_form_input');
23822397 if (!(fileInput instanceof HTMLInputElement)) return;
23832398
23842399 // Workaround for Firefox: Use a DataTransfer object to indirectly set fileInput.files
23852400 const dataTransfer = new DataTransfer();
23862401 for (let i = 0;const ifile <of filesfileInput.length; i++files) {
23872402 dataTransfer.items.add(files[i]file);
23882403 }
23892404
23902405 // Preserve existing non-duplicate files in the input
23912406 for (const file of fileInput.files) {
23922407 if (!Array.from(dataTransfer.files).some(f => isSameFile(f, file))) {
23932408 dataTransfer.items.add(file);
23942409 }