From f556d3e2a0af9286a282553e3303b529f212449f Mon Sep 17 00:00:00 2001 From: idris Date: Wed, 2 Sep 2026 00:14:21 +0700 Subject: [PATCH 1/2] Add drag-and-drop support to the Images to GIF file input The "No files selected" box looked like a drop zone but only accepted clicks on the separate "Import from file" button, which caused image files dropped there to open in the browser instead. The box now accepts drag-and-drop, is clickable when empty, and shows drag/hover feedback consistent with the single-file input's behavior. Addresses one of the points raised in #433. --- public/locales/en/translation.json | 4 +- .../input/ToolMultipleImageInput.tsx | 97 ++++++++++++++++--- 2 files changed, 88 insertions(+), 13 deletions(-) diff --git a/public/locales/en/translation.json b/public/locales/en/translation.json index 5b4efe41..3b20b654 100644 --- a/public/locales/en/translation.json +++ b/public/locales/en/translation.json @@ -242,8 +242,10 @@ }, "toolMultipleInput": { "deleteFile": "Delete file", + "dropFilesHere": "Drop your files here", "inputTitle": "Input {{type}}", - "noFilesSelected": "No files selected" + "noFilesSelected": "No files selected", + "selectFilesDescription": "Click here to select files from your device, or drag and drop files here" }, "toolOptions": { "title": "Tool options" diff --git a/src/components/input/ToolMultipleImageInput.tsx b/src/components/input/ToolMultipleImageInput.tsx index 6804caad..4d58c1ed 100644 --- a/src/components/input/ToolMultipleImageInput.tsx +++ b/src/components/input/ToolMultipleImageInput.tsx @@ -1,11 +1,12 @@ -import React, { useRef } from 'react'; -import { Box } from '@mui/material'; +import React, { useContext, useRef, useState } from 'react'; +import { Box, useTheme } from '@mui/material'; import Typography from '@mui/material/Typography'; import InputHeader from '../InputHeader'; import InputFooter from './InputFooter'; import ImageIcon from '@mui/icons-material/Image'; import { useTranslation } from 'react-i18next'; import { heicTo, isHeic } from 'heic-to'; +import { CustomSnackBarContext } from '../../contexts/CustomSnackBarContext'; interface MultiImageInputComponentProps { accept: string[]; @@ -30,15 +31,33 @@ export default function ToolMultiImageInput({ }: MultiImageInputComponentProps) { const { t } = useTranslation(); const fileInputRef = useRef(null); + const theme = useTheme(); + const { showSnackBar } = useContext(CustomSnackBarContext); + const [isDragging, setIsDragging] = useState(false); - const handleFileChange = async ( - event: React.ChangeEvent - ) => { - const files = event.target.files; - if (!files) return; + const isAcceptableFile = (file: File) => + accept.some((acceptType) => { + // Handle wildcards like "image/*" + if (acceptType.endsWith('/*')) { + const category = acceptType.split('/')[0]; + return file.type.startsWith(category); + } + return acceptType === file.type; + }); + + const addFiles = async (files: FileList | File[]) => { + const acceptedFiles = Array.from(files).filter(isAcceptableFile); + const rejectedCount = Array.from(files).length - acceptedFiles.length; + if (rejectedCount > 0) { + showSnackBar( + `Invalid file type. Please use ${accept.join(', ')}`, + 'error' + ); + } + if (acceptedFiles.length === 0) return; const newFiles: MultiImageInput[] = await Promise.all( - Array.from(files).map(async (file, index) => { + acceptedFiles.map(async (file, index) => { let processedFile = file; try { @@ -67,10 +86,46 @@ export default function ToolMultiImageInput({ ); onChange([...value, ...newFiles]); + }; + + const handleFileChange = async ( + event: React.ChangeEvent + ) => { + const files = event.target.files; + if (!files) return; + + await addFiles(files); event.target.value = ''; }; + const handleDrop = async (event: React.DragEvent) => { + event.preventDefault(); + event.stopPropagation(); + setIsDragging(false); + + if (event.dataTransfer.files && event.dataTransfer.files.length > 0) { + await addFiles(event.dataTransfer.files); + } + }; + + const handleDragOver = (event: React.DragEvent) => { + event.preventDefault(); + event.stopPropagation(); + }; + + const handleDragEnter = (event: React.DragEvent) => { + event.preventDefault(); + event.stopPropagation(); + setIsDragging(true); + }; + + const handleDragLeave = (event: React.DragEvent) => { + event.preventDefault(); + event.stopPropagation(); + setIsDragging(false); + }; + const handleImportClick = () => { fileInputRef.current?.click(); }; @@ -117,19 +172,28 @@ export default function ToolMultiImageInput({ borderRadius: 2, boxShadow: '5', bgcolor: 'background.paper', - position: 'relative' + position: 'relative', + borderColor: isDragging ? theme.palette.primary.main : undefined, + borderWidth: isDragging ? 2 : 1, + borderStyle: isDragging ? 'dashed' : 'solid' }} + onDrop={handleDrop} + onDragOver={handleDragOver} + onDragEnter={handleDragEnter} + onDragLeave={handleDragLeave} > {value?.length ? ( @@ -185,9 +249,18 @@ export default function ToolMultiImageInput({ )) + ) : isDragging ? ( + + {t('toolMultipleInput.dropFilesHere')} + ) : ( - - {t('toolMultipleInput.noFilesSelected')} + + {t('toolMultipleInput.selectFilesDescription')} )} From b2e6f0ba30b13a2619020f64c1a63470dae39065 Mon Sep 17 00:00:00 2001 From: idris Date: Tue, 13 Oct 2026 23:05:34 +0700 Subject: [PATCH 2/2] feat: extend drag-and-drop to all multiple-file inputs. Extract the drop logic from ToolMultipleImageInput into a shared useFileDrop hook and use it in the image, video, audio and PDF inputs: empty drop zone is clickable and accepts dropped files; dashed border and 'Drop your files here' while dragging; fix border flicker when dragging over child elements by counting dragenter/dragleave instead of toggling on every leave; keep accepting HEIC/HEIF files that browsers report with an empty MIME type, and support extension entries in `accept` (e.g. .mp3); reject unsupported files with a translated error snackbar; give files added in one batch distinct, increasing `order` values; add toolMultipleInput.invalidFileType translation key --- public/locales/en/translation.json | 1 + .../input/ToolMultipleAudioInput.tsx | 41 +++++-- .../input/ToolMultipleImageInput.tsx | 116 +++++------------- src/components/input/ToolMultiplePdfInput.tsx | 41 +++++-- .../input/ToolMultipleVideoInput.tsx | 41 +++++-- src/hooks/index.ts | 1 + src/hooks/useFileDrop.ts | 81 ++++++++++++ 7 files changed, 212 insertions(+), 110 deletions(-) create mode 100644 src/hooks/useFileDrop.ts diff --git a/public/locales/en/translation.json b/public/locales/en/translation.json index 532cf094..8998c196 100644 --- a/public/locales/en/translation.json +++ b/public/locales/en/translation.json @@ -244,6 +244,7 @@ "deleteFile": "Delete file", "dropFilesHere": "Drop your files here", "inputTitle": "Input {{type}}", + "invalidFileType": "Invalid file type. Please select files of a supported type.", "noFilesSelected": "No files selected", "selectFilesDescription": "Click here to select files from your device, or drag and drop files here" }, diff --git a/src/components/input/ToolMultipleAudioInput.tsx b/src/components/input/ToolMultipleAudioInput.tsx index e1682e77..ee0f10d8 100644 --- a/src/components/input/ToolMultipleAudioInput.tsx +++ b/src/components/input/ToolMultipleAudioInput.tsx @@ -6,6 +6,7 @@ import InputFooter from './InputFooter'; import MusicNoteIcon from '@mui/icons-material/MusicNote'; import ClearIcon from '@mui/icons-material/Clear'; import { useTranslation } from 'react-i18next'; +import useFileDrop from '../../hooks/useFileDrop'; interface MultiAudioInputComponentProps { accept: string[]; @@ -31,13 +32,20 @@ export default function ToolMultipleAudioInput({ const theme = useTheme(); const fileInputRef = useRef(null); - const handleFileChange = (event: React.ChangeEvent) => { - const files = event.target.files; - if (files) + const { isDragging, dropHandlers, addFiles } = useFileDrop({ + accept, + onFiles: (files) => onChange([ ...value, - ...Array.from(files).map((file) => ({ file, order: value.length })) - ]); + ...files.map((file, index) => ({ file, order: value.length + index })) + ]) + }); + + const handleFileChange = async ( + event: React.ChangeEvent + ) => { + if (event.target.files) await addFiles(event.target.files); + event.target.value = ''; }; const handleImportClick = () => { @@ -110,19 +118,25 @@ export default function ToolMultipleAudioInput({ borderRadius: 2, boxShadow: '5', bgcolor: 'background.paper', - position: 'relative' + position: 'relative', + borderColor: isDragging ? 'primary.main' : undefined, + borderWidth: isDragging ? 2 : undefined, + borderStyle: isDragging ? 'dashed' : undefined }} + {...dropHandlers} > {value?.length ? ( @@ -175,9 +189,18 @@ export default function ToolMultipleAudioInput({ )) + ) : isDragging ? ( + + {t('toolMultipleInput.dropFilesHere')} + ) : ( - - {t('toolMultipleInput.noFilesSelected')} + + {t('toolMultipleInput.selectFilesDescription')} )} diff --git a/src/components/input/ToolMultipleImageInput.tsx b/src/components/input/ToolMultipleImageInput.tsx index 4d58c1ed..cc54c9ef 100644 --- a/src/components/input/ToolMultipleImageInput.tsx +++ b/src/components/input/ToolMultipleImageInput.tsx @@ -1,4 +1,4 @@ -import React, { useContext, useRef, useState } from 'react'; +import React, { useRef } from 'react'; import { Box, useTheme } from '@mui/material'; import Typography from '@mui/material/Typography'; import InputHeader from '../InputHeader'; @@ -6,7 +6,7 @@ import InputFooter from './InputFooter'; import ImageIcon from '@mui/icons-material/Image'; import { useTranslation } from 'react-i18next'; import { heicTo, isHeic } from 'heic-to'; -import { CustomSnackBarContext } from '../../contexts/CustomSnackBarContext'; +import useFileDrop from '../../hooks/useFileDrop'; interface MultiImageInputComponentProps { accept: string[]; @@ -32,61 +32,41 @@ export default function ToolMultiImageInput({ const { t } = useTranslation(); const fileInputRef = useRef(null); const theme = useTheme(); - const { showSnackBar } = useContext(CustomSnackBarContext); - const [isDragging, setIsDragging] = useState(false); + const { isDragging, dropHandlers, addFiles } = useFileDrop({ + accept, + onFiles: async (acceptedFiles) => { + const newFiles: MultiImageInput[] = await Promise.all( + acceptedFiles.map(async (file, index) => { + let processedFile = file; - const isAcceptableFile = (file: File) => - accept.some((acceptType) => { - // Handle wildcards like "image/*" - if (acceptType.endsWith('/*')) { - const category = acceptType.split('/')[0]; - return file.type.startsWith(category); - } - return acceptType === file.type; - }); + try { + if (await isHeic(file)) { + const convertedBlob = await heicTo({ + blob: file, + type: 'image/jpeg' + }); - const addFiles = async (files: FileList | File[]) => { - const acceptedFiles = Array.from(files).filter(isAcceptableFile); - const rejectedCount = Array.from(files).length - acceptedFiles.length; - if (rejectedCount > 0) { - showSnackBar( - `Invalid file type. Please use ${accept.join(', ')}`, - 'error' - ); - } - if (acceptedFiles.length === 0) return; - - const newFiles: MultiImageInput[] = await Promise.all( - acceptedFiles.map(async (file, index) => { - let processedFile = file; - - try { - if (await isHeic(file)) { - const convertedBlob = await heicTo({ - blob: file, - type: 'image/jpeg' - }); - - processedFile = new File( - [convertedBlob], - file.name.replace(/\.[^/.]+$/, '') + '.jpg', - { type: 'image/jpeg' } - ); + processedFile = new File( + [convertedBlob], + file.name.replace(/\.[^/.]+$/, '') + '.jpg', + { type: 'image/jpeg' } + ); + } + } catch (err) { + console.warn('HEIC conversion failed:', file.name, err); } - } catch (err) { - console.warn('HEIC conversion failed:', file.name, err); - } - return { - file: processedFile, - order: value.length + index, - preview: URL.createObjectURL(processedFile) - }; - }) - ); + return { + file: processedFile, + order: value.length + index, + preview: URL.createObjectURL(processedFile) + }; + }) + ); - onChange([...value, ...newFiles]); - }; + onChange([...value, ...newFiles]); + } + }); const handleFileChange = async ( event: React.ChangeEvent @@ -99,33 +79,6 @@ export default function ToolMultiImageInput({ event.target.value = ''; }; - const handleDrop = async (event: React.DragEvent) => { - event.preventDefault(); - event.stopPropagation(); - setIsDragging(false); - - if (event.dataTransfer.files && event.dataTransfer.files.length > 0) { - await addFiles(event.dataTransfer.files); - } - }; - - const handleDragOver = (event: React.DragEvent) => { - event.preventDefault(); - event.stopPropagation(); - }; - - const handleDragEnter = (event: React.DragEvent) => { - event.preventDefault(); - event.stopPropagation(); - setIsDragging(true); - }; - - const handleDragLeave = (event: React.DragEvent) => { - event.preventDefault(); - event.stopPropagation(); - setIsDragging(false); - }; - const handleImportClick = () => { fileInputRef.current?.click(); }; @@ -177,10 +130,7 @@ export default function ToolMultiImageInput({ borderWidth: isDragging ? 2 : 1, borderStyle: isDragging ? 'dashed' : 'solid' }} - onDrop={handleDrop} - onDragOver={handleDragOver} - onDragEnter={handleDragEnter} - onDragLeave={handleDragLeave} + {...dropHandlers} > (null); - const handleFileChange = (event: React.ChangeEvent) => { - const files = event.target.files; - if (files) + const { isDragging, dropHandlers, addFiles } = useFileDrop({ + accept, + onFiles: (files) => onChange([ ...value, - ...Array.from(files).map((file) => ({ file, order: value.length })) - ]); + ...files.map((file, index) => ({ file, order: value.length + index })) + ]) + }); + + const handleFileChange = async ( + event: React.ChangeEvent + ) => { + if (event.target.files) await addFiles(event.target.files); + event.target.value = ''; }; const handleImportClick = () => { @@ -111,19 +119,25 @@ export default function ToolMultiFileInput({ borderRadius: 2, boxShadow: '5', bgcolor: 'background.paper', - position: 'relative' + position: 'relative', + borderColor: isDragging ? 'primary.main' : undefined, + borderWidth: isDragging ? 2 : undefined, + borderStyle: isDragging ? 'dashed' : undefined }} + {...dropHandlers} > {value?.length ? ( @@ -176,9 +190,18 @@ export default function ToolMultiFileInput({ )) + ) : isDragging ? ( + + {t('toolMultipleInput.dropFilesHere')} + ) : ( - - {t('toolMultipleInput.noFilesSelected')} + + {t('toolMultipleInput.selectFilesDescription')} )} diff --git a/src/components/input/ToolMultipleVideoInput.tsx b/src/components/input/ToolMultipleVideoInput.tsx index d861df18..997c2ac4 100644 --- a/src/components/input/ToolMultipleVideoInput.tsx +++ b/src/components/input/ToolMultipleVideoInput.tsx @@ -6,6 +6,7 @@ import InputFooter from './InputFooter'; import VideoFileIcon from '@mui/icons-material/VideoFile'; import ClearIcon from '@mui/icons-material/Clear'; import { useTranslation } from 'react-i18next'; +import useFileDrop from '../../hooks/useFileDrop'; interface MultiVideoInputComponentProps { accept: string[]; @@ -30,13 +31,20 @@ export default function ToolMultipleVideoInput({ const { t } = useTranslation(); const fileInputRef = useRef(null); - const handleFileChange = (event: React.ChangeEvent) => { - const files = event.target.files; - if (files) + const { isDragging, dropHandlers, addFiles } = useFileDrop({ + accept, + onFiles: (files) => onChange([ ...value, - ...Array.from(files).map((file) => ({ file, order: value.length })) - ]); + ...files.map((file, index) => ({ file, order: value.length + index })) + ]) + }); + + const handleFileChange = async ( + event: React.ChangeEvent + ) => { + if (event.target.files) await addFiles(event.target.files); + event.target.value = ''; }; const handleImportClick = () => { @@ -109,19 +117,25 @@ export default function ToolMultipleVideoInput({ borderRadius: 2, boxShadow: '5', bgcolor: 'background.paper', - position: 'relative' + position: 'relative', + borderColor: isDragging ? 'primary.main' : undefined, + borderWidth: isDragging ? 2 : undefined, + borderStyle: isDragging ? 'dashed' : undefined }} + {...dropHandlers} > {value?.length ? ( @@ -174,9 +188,18 @@ export default function ToolMultipleVideoInput({ )) + ) : isDragging ? ( + + {t('toolMultipleInput.dropFilesHere')} + ) : ( - - {t('toolMultipleInput.noFilesSelected')} + + {t('toolMultipleInput.selectFilesDescription')} )} diff --git a/src/hooks/index.ts b/src/hooks/index.ts index 19299255..7013894a 100644 --- a/src/hooks/index.ts +++ b/src/hooks/index.ts @@ -2,3 +2,4 @@ export { default as useDebounce } from './useDebounce'; export { default as useTimeout } from './useTimeout'; export { default as usePrevious } from './usePrevious'; export { default as useUpdateEffect } from './useUpdateEffect'; +export { default as useFileDrop } from './useFileDrop'; diff --git a/src/hooks/useFileDrop.ts b/src/hooks/useFileDrop.ts new file mode 100644 index 00000000..6fccfe93 --- /dev/null +++ b/src/hooks/useFileDrop.ts @@ -0,0 +1,81 @@ +import { useContext, useRef, useState } from 'react'; +import { useTranslation } from 'react-i18next'; +import { CustomSnackBarContext } from '../contexts/CustomSnackBarContext'; + +const HEIC_EXTENSIONS = ['heic', 'heif']; + +const getExtension = (name: string) => + name.includes('.') ? name.split('.').pop()!.toLowerCase() : ''; + +export const isAcceptedFile = (file: File, accept: string[]): boolean => { + const extension = getExtension(file.name); + return accept.some((acceptType) => { + if (acceptType.startsWith('.')) { + return extension === acceptType.slice(1).toLowerCase(); + } + if (acceptType.endsWith('/*')) { + const category = acceptType.split('/')[0]; + if (file.type) return file.type.startsWith(`${category}/`); + // Some browsers report an empty type for HEIC/HEIF files + return category === 'image' && HEIC_EXTENSIONS.includes(extension); + } + if (acceptType === file.type) return true; + return ( + !file.type && + /^image\/hei[cf]$/.test(acceptType) && + HEIC_EXTENSIONS.includes(extension) + ); + }); +}; + +interface UseFileDropOptions { + accept: string[]; + onFiles: (files: File[]) => void | Promise; +} + +/** + * Shared drag-and-drop / file filtering logic for the multiple file inputs. + * Spread `dropHandlers` on the drop zone and pass picker files to `addFiles`. + */ +export default function useFileDrop({ accept, onFiles }: UseFileDropOptions) { + const { t } = useTranslation(); + const { showSnackBar } = useContext(CustomSnackBarContext); + const [isDragging, setIsDragging] = useState(false); + // dragenter/dragleave also fire for child elements, so count them + const dragCounter = useRef(0); + + const addFiles = async (files: FileList | File[]) => { + const all = Array.from(files); + const accepted = all.filter((file) => isAcceptedFile(file, accept)); + if (accepted.length < all.length) { + showSnackBar(t('toolMultipleInput.invalidFileType'), 'error'); + } + if (accepted.length > 0) await onFiles(accepted); + }; + + const dropHandlers = { + onDragEnter: (event: React.DragEvent) => { + event.preventDefault(); + dragCounter.current += 1; + setIsDragging(true); + }, + onDragOver: (event: React.DragEvent) => { + event.preventDefault(); + }, + onDragLeave: (event: React.DragEvent) => { + event.preventDefault(); + dragCounter.current = Math.max(0, dragCounter.current - 1); + if (dragCounter.current === 0) setIsDragging(false); + }, + onDrop: async (event: React.DragEvent) => { + event.preventDefault(); + dragCounter.current = 0; + setIsDragging(false); + if (event.dataTransfer.files.length > 0) { + await addFiles(event.dataTransfer.files); + } + } + }; + + return { isDragging, dropHandlers, addFiles }; +}