Merge pull request #458 from idsoke/fix/433-images-to-gif-drag-drop

Add drag-and-drop support to Images to GIF file input
This commit is contained in:
Chesterkxng authored and GitHub committed 2026-10-13 23:34:11 +00:00
commit f311ce9434
7 files changed
+240 -63

No files matched your search

+4 -1
View File
@@ -242,8 +242,11 @@
},
"toolMultipleInput": {
"deleteFile": "Delete file",
"dropFilesHere": "Drop your files here",
"inputTitle": "Input {{type}}",
"noFilesSelected": "No files selected"
"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"
},
"toolOptions": {
"title": "Tool options"
@@ -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<HTMLInputElement>(null);
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
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<HTMLInputElement>
) => {
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}
>
<Box
width="100%"
height="100%"
onClick={value?.length ? undefined : handleImportClick}
sx={{
overflow: 'auto',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
flexWrap: 'wrap',
position: 'relative'
position: 'relative',
cursor: value?.length ? undefined : 'pointer'
}}
>
{value?.length ? (
@@ -175,9 +189,18 @@ export default function ToolMultipleAudioInput({
</Tooltip>
</Box>
))
) : isDragging ? (
<Typography variant="body2" color="primary">
{t('toolMultipleInput.dropFilesHere')}
</Typography>
) : (
<Typography variant="body2" color="text.secondary">
{t('toolMultipleInput.noFilesSelected')}
<Typography
variant="body2"
color="text.secondary"
align="center"
sx={{ px: 2 }}
>
{t('toolMultipleInput.selectFilesDescription')}
</Typography>
)}
</Box>
+58 -35
View File
@@ -1,11 +1,12 @@
import React, { useRef } from 'react';
import { Box } from '@mui/material';
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 useFileDrop from '../../hooks/useFileDrop';
interface MultiImageInputComponentProps {
accept: string[];
@@ -30,6 +31,42 @@ export default function ToolMultiImageInput({
}: MultiImageInputComponentProps) {
const { t } = useTranslation();
const fileInputRef = useRef<HTMLInputElement>(null);
const theme = useTheme();
const { isDragging, dropHandlers, addFiles } = useFileDrop({
accept,
onFiles: async (acceptedFiles) => {
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' }
);
}
} catch (err) {
console.warn('HEIC conversion failed:', file.name, err);
}
return {
file: processedFile,
order: value.length + index,
preview: URL.createObjectURL(processedFile)
};
})
);
onChange([...value, ...newFiles]);
}
});
const handleFileChange = async (
event: React.ChangeEvent<HTMLInputElement>
@@ -37,36 +74,7 @@ export default function ToolMultiImageInput({
const files = event.target.files;
if (!files) return;
const newFiles: MultiImageInput[] = await Promise.all(
Array.from(files).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' }
);
}
} catch (err) {
console.warn('HEIC conversion failed:', file.name, err);
}
return {
file: processedFile,
order: value.length + index,
preview: URL.createObjectURL(processedFile)
};
})
);
onChange([...value, ...newFiles]);
await addFiles(files);
event.target.value = '';
};
@@ -117,19 +125,25 @@ 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'
}}
{...dropHandlers}
>
<Box
width="100%"
height="100%"
onClick={value?.length ? undefined : handleImportClick}
sx={{
overflow: 'auto',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
flexWrap: 'wrap',
position: 'relative'
position: 'relative',
cursor: value?.length ? undefined : 'pointer'
}}
>
{value?.length ? (
@@ -185,9 +199,18 @@ export default function ToolMultiImageInput({
</Box>
</Box>
))
) : isDragging ? (
<Typography variant="body2" color="primary">
{t('toolMultipleInput.dropFilesHere')}
</Typography>
) : (
<Typography variant="body2" color="text.secondary">
{t('toolMultipleInput.noFilesSelected')}
<Typography
variant="body2"
color="text.secondary"
align="center"
sx={{ px: 2 }}
>
{t('toolMultipleInput.selectFilesDescription')}
</Typography>
)}
</Box>
+32 -9
View File
@@ -6,6 +6,7 @@ import InputFooter from './InputFooter';
import ClearIcon from '@mui/icons-material/Clear';
import PictureAsPdfIcon from '@mui/icons-material/PictureAsPdf';
import { useTranslation } from 'react-i18next';
import useFileDrop from '../../hooks/useFileDrop';
interface MultiPdfInputComponentProps {
accept: string[];
@@ -31,13 +32,20 @@ export default function ToolMultiFileInput({
const theme = useTheme();
const fileInputRef = useRef<HTMLInputElement>(null);
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
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<HTMLInputElement>
) => {
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}
>
<Box
width="100%"
height="100%"
onClick={value?.length ? undefined : handleImportClick}
sx={{
overflow: 'auto',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
flexWrap: 'wrap',
position: 'relative'
position: 'relative',
cursor: value?.length ? undefined : 'pointer'
}}
>
{value?.length ? (
@@ -176,9 +190,18 @@ export default function ToolMultiFileInput({
</Tooltip>
</Box>
))
) : isDragging ? (
<Typography variant="body2" color="primary">
{t('toolMultipleInput.dropFilesHere')}
</Typography>
) : (
<Typography variant="body2" color="text.secondary">
{t('toolMultipleInput.noFilesSelected')}
<Typography
variant="body2"
color="text.secondary"
align="center"
sx={{ px: 2 }}
>
{t('toolMultipleInput.selectFilesDescription')}
</Typography>
)}
</Box>
@@ -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<HTMLInputElement>(null);
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
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<HTMLInputElement>
) => {
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}
>
<Box
width="100%"
height="100%"
onClick={value?.length ? undefined : handleImportClick}
sx={{
overflow: 'auto',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
flexWrap: 'wrap',
position: 'relative'
position: 'relative',
cursor: value?.length ? undefined : 'pointer'
}}
>
{value?.length ? (
@@ -174,9 +188,18 @@ export default function ToolMultipleVideoInput({
</Tooltip>
</Box>
))
) : isDragging ? (
<Typography variant="body2" color="primary">
{t('toolMultipleInput.dropFilesHere')}
</Typography>
) : (
<Typography variant="body2" color="text.secondary">
{t('toolMultipleInput.noFilesSelected')}
<Typography
variant="body2"
color="text.secondary"
align="center"
sx={{ px: 2 }}
>
{t('toolMultipleInput.selectFilesDescription')}
</Typography>
)}
</Box>
+1
View File
@@ -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';
+81
View File
@@ -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<void>;
}
/**
* 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<HTMLElement>) => {
event.preventDefault();
dragCounter.current += 1;
setIsDragging(true);
},
onDragOver: (event: React.DragEvent<HTMLElement>) => {
event.preventDefault();
},
onDragLeave: (event: React.DragEvent<HTMLElement>) => {
event.preventDefault();
dragCounter.current = Math.max(0, dragCounter.current - 1);
if (dragCounter.current === 0) setIsDragging(false);
},
onDrop: async (event: React.DragEvent<HTMLElement>) => {
event.preventDefault();
dragCounter.current = 0;
setIsDragging(false);
if (event.dataTransfer.files.length > 0) {
await addFiles(event.dataTransfer.files);
}
}
};
return { isDragging, dropHandlers, addFiles };
}