From 267d119f52406484a3f562723cf7366ec0b521a9 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Thu, 26 Jun 2025 20:29:14 -0700 Subject: [PATCH] Preview images before sharing on chat --- src/App.vue | 118 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 95 insertions(+), 23 deletions(-) diff --git a/src/App.vue b/src/App.vue index 4f1e2f6..fe1da70 100644 --- a/src/App.vue +++ b/src/App.vue @@ -261,6 +261,13 @@ export default { username: "", }, + photoShareModal: { + visible: false, + file: null, // File handle from upload + previewImg: null, // Image src for preview modal + bytes: null, // Bytes to send to server + }, + messageHistoryModal: { visible: false, }, @@ -1997,12 +2004,52 @@ export default { let dt = e.dataTransfer; let file = dt.files[0]; - this.onFileUpload(file); + this.onFileSelected(file); }); }, // Common file selection handler for drag/drop or manual upload. - onFileUpload(file) { + onFileSelected(file) { + // Show the share modal so the user previews before posting it. + + // Validate they can upload it here. + if (!this.canUploadFile) { + this.ChatClient("Photo sharing in this channel is not available."); + return; + } + + // Get image file data. + let reader = new FileReader(); + let rawData = new ArrayBuffer(); + reader.addEventListener("load", () => { + rawData = reader.result; + + let fileByteArray = [], + u8array = new Uint8Array(rawData); + for (let i = 0; i < u8array.length; i++) { + fileByteArray.push(u8array[i]); + } + + // Show the modal. + this.photoShareModal.file = file; + this.photoShareModal.previewImg = URL.createObjectURL(file); + this.photoShareModal.bytes = fileByteArray; + this.photoShareModal.visible = true; + }); + + reader.readAsArrayBuffer(file); + }, + closePhotoShareModal() { + this.photoShareModal.file = null; + this.photoShareModal.previewImg = null; + this.photoShareModal.bytes = null; + this.photoShareModal.visible = false; + }, + onFileUpload() { + const file = this.photoShareModal.file, + bytes = this.photoShareModal.bytes; + this.closePhotoShareModal(); + // Validate they can upload it here. if (!this.canUploadFile) { this.ChatClient("Photo sharing in this channel is not available."); @@ -2024,27 +2071,12 @@ export default { this.ChatClient(`Uploading file to chat: ${file.name} - ${file.size} bytes, ${file.type} format.`); - // Get image file data. - let reader = new FileReader(); - let rawData = new ArrayBuffer(); - reader.onload = e => { - rawData = e.target.result; + // Attach the file to the message. + msg.message = file.name; + msg.bytes = bytes; - let fileByteArray = [], - u8array = new Uint8Array(rawData); - for (let i = 0; i < u8array.length; i++) { - fileByteArray.push(u8array[i]); - } - - // Attach the file to the message. - msg.message = file.name; - msg.bytes = fileByteArray; - - // Send it to the chat server. - this.client.send(msg); - }; - - reader.readAsArrayBuffer(file); + // Send it to the chat server. + this.client.send(msg); }, // The image upload button handler. @@ -2054,7 +2086,7 @@ export default { input.accept = 'image/*'; input.onchange = e => { let file = e.target.files[0]; - this.onFileUpload(file); + this.onFileSelected(file); }; input.click(); }, @@ -2529,6 +2561,46 @@ export default { + + +