diff --git a/src/components/ImageDrop.js b/src/components/ImageDrop.js index da41987..8bd71d3 100644 --- a/src/components/ImageDrop.js +++ b/src/components/ImageDrop.js @@ -18,10 +18,14 @@ function ImageDrop({ onDrop, dropText, children }) { function handleImageDrop(event) { event.preventDefault(); event.stopPropagation(); - const file = event.dataTransfer.files[0]; - if (file && file.type.startsWith("image")) { - onDrop(file); + const files = event.dataTransfer.files; + let imageFiles = []; + for (let file of files) { + if (file.type.startsWith("image")) { + imageFiles.push(file); + } } + onDrop(imageFiles); setDragging(false); } diff --git a/src/modals/SelectMapModal.js b/src/modals/SelectMapModal.js index 91de48a..d721f6e 100644 --- a/src/modals/SelectMapModal.js +++ b/src/modals/SelectMapModal.js @@ -51,9 +51,17 @@ function SelectMapModal({ const fileInputRef = useRef(); - function handleImageUpload(file) { + async function handleImagesUpload(files) { + for (let file of files) { + await handleImageUpload(file); + } + // Set file input to null to allow adding the same image 2 times in a row + fileInputRef.current.value = null; + } + + async function handleImageUpload(file) { if (!file) { - return; + return Promise.reject(); } let fileGridX = defaultMapSize; let fileGridY = defaultMapSize; @@ -86,11 +94,13 @@ function SelectMapModal({ let image = new Image(); setImageLoading(true); - blobToBuffer(file).then((buffer) => { - // Copy file to avoid permissions issues - const blob = new Blob([buffer]); - // Create and load the image temporarily to get its dimensions - const url = URL.createObjectURL(blob); + const buffer = await blobToBuffer(file); + // Copy file to avoid permissions issues + const blob = new Blob([buffer]); + // Create and load the image temporarily to get its dimensions + const url = URL.createObjectURL(blob); + + return new Promise((resolve, reject) => { image.onload = function () { handleMapAdd({ // Save as a buffer to send with msgpack @@ -109,11 +119,10 @@ function SelectMapModal({ }); setImageLoading(false); URL.revokeObjectURL(url); + resolve(); }; + image.onerror = reject; image.src = url; - - // Set file input to null to allow adding the same image 2 times in a row - fileInputRef.current.value = null; }); } @@ -172,12 +181,13 @@ function SelectMapModal({ return ( - + handleImageUpload(event.target.files[0])} + onChange={(event) => handleImagesUpload(event.target.files)} type="file" accept="image/*" style={{ display: "none" }} + multiple ref={fileInputRef} /> { - // Copy file to avoid permissions issues - const blob = new Blob([buffer]); - // Create and load the image temporarily to get its dimensions - const url = URL.createObjectURL(blob); + const buffer = await blobToBuffer(file); + + // Copy file to avoid permissions issues + const blob = new Blob([buffer]); + // Create and load the image temporarily to get its dimensions + const url = URL.createObjectURL(blob); + + return new Promise((resolve, reject) => { image.onload = function () { handleTokenAdd({ file: buffer, @@ -68,11 +79,10 @@ function SelectTokensModal({ isOpen, onRequestClose }) { hideInSidebar: false, }); setImageLoading(false); + resolve(); }; + image.onerror = reject; image.src = url; - - // Set file input to null to allow adding the same image 2 times in a row - fileInputRef.current.value = null; }); } @@ -96,13 +106,14 @@ function SelectTokensModal({ isOpen, onRequestClose }) { return ( - + handleImageUpload(event.target.files[0])} + onChange={(event) => handleImagesUpload(event.target.files)} type="file" accept="image/*" style={{ display: "none" }} ref={fileInputRef} + multiple />