mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-12-29 16:16:02 +00:00
Fix rendering issue with ToolMultipleVideoInput as well as merge functionality.
This commit is contained in:
@@ -1,92 +1,177 @@
|
||||
import React from 'react';
|
||||
import { Box, Typography, IconButton } from '@mui/material';
|
||||
import BaseFileInput from './BaseFileInput';
|
||||
import { BaseFileInputProps } from './file-input-utils';
|
||||
import DeleteIcon from '@mui/icons-material/Delete';
|
||||
import { ReactNode, useContext, useEffect, 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 { CustomSnackBarContext } from '../../contexts/CustomSnackBarContext';
|
||||
import { isArray } from 'lodash';
|
||||
import VideoFileIcon from '@mui/icons-material/VideoFile';
|
||||
|
||||
interface ToolMultipleVideoInputProps
|
||||
extends Omit<BaseFileInputProps, 'accept' | 'value' | 'onChange'> {
|
||||
value: File[] | null;
|
||||
onChange: (files: File[]) => void;
|
||||
accept?: string[];
|
||||
interface MultiVideoInputComponentProps {
|
||||
accept: string[];
|
||||
title?: string;
|
||||
type: 'video';
|
||||
value: MultiVideoInput[];
|
||||
onChange: (file: MultiVideoInput[]) => void;
|
||||
}
|
||||
|
||||
export interface MultiVideoInput {
|
||||
file: File;
|
||||
order: number;
|
||||
}
|
||||
|
||||
export default function ToolMultipleVideoInput({
|
||||
value,
|
||||
onChange,
|
||||
accept = ['video/*', '.mkv'],
|
||||
accept,
|
||||
title,
|
||||
...props
|
||||
}: ToolMultipleVideoInputProps) {
|
||||
// For preview, use the first file if available
|
||||
const preview =
|
||||
value && value.length > 0 ? URL.createObjectURL(value[0]) : undefined;
|
||||
type
|
||||
}: MultiVideoInputComponentProps) {
|
||||
console.log('ToolMultipleVideoInput rendering with value:', value);
|
||||
|
||||
// Handler for file selection
|
||||
const handleFileChange = (file: File | null) => {
|
||||
if (file) {
|
||||
// Add to existing files, avoiding duplicates by name
|
||||
const files = value ? [...value] : [];
|
||||
if (!files.some((f) => f.name === file.name && f.size === file.size)) {
|
||||
onChange([...files, file]);
|
||||
}
|
||||
}
|
||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const files = event.target.files;
|
||||
console.log('File change event:', files);
|
||||
if (files)
|
||||
onChange([
|
||||
...value,
|
||||
...Array.from(files).map((file) => ({ file, order: value.length }))
|
||||
]);
|
||||
};
|
||||
|
||||
// Handler for removing a file
|
||||
const handleRemove = (idx: number) => {
|
||||
if (!value) return;
|
||||
const newFiles = value.slice();
|
||||
newFiles.splice(idx, 1);
|
||||
onChange(newFiles);
|
||||
const handleImportClick = () => {
|
||||
console.log('Import clicked');
|
||||
fileInputRef.current?.click();
|
||||
};
|
||||
|
||||
function handleClear() {
|
||||
console.log('Clear clicked');
|
||||
onChange([]);
|
||||
}
|
||||
|
||||
function fileNameTruncate(fileName: string) {
|
||||
const maxLength = 15;
|
||||
if (fileName.length > maxLength) {
|
||||
return fileName.slice(0, maxLength) + '...';
|
||||
}
|
||||
return fileName;
|
||||
}
|
||||
|
||||
const sortList = () => {
|
||||
const list = [...value];
|
||||
list.sort((a, b) => a.order - b.order);
|
||||
onChange(list);
|
||||
};
|
||||
|
||||
const reorderList = (sourceIndex: number, destinationIndex: number) => {
|
||||
if (destinationIndex === sourceIndex) {
|
||||
return;
|
||||
}
|
||||
const list = [...value];
|
||||
|
||||
if (destinationIndex === 0) {
|
||||
list[sourceIndex].order = list[0].order - 1;
|
||||
sortList();
|
||||
return;
|
||||
}
|
||||
|
||||
if (destinationIndex === list.length - 1) {
|
||||
list[sourceIndex].order = list[list.length - 1].order + 1;
|
||||
sortList();
|
||||
return;
|
||||
}
|
||||
|
||||
if (destinationIndex < sourceIndex) {
|
||||
list[sourceIndex].order =
|
||||
(list[destinationIndex].order + list[destinationIndex - 1].order) / 2;
|
||||
sortList();
|
||||
return;
|
||||
}
|
||||
|
||||
list[sourceIndex].order =
|
||||
(list[destinationIndex].order + list[destinationIndex + 1].order) / 2;
|
||||
sortList();
|
||||
};
|
||||
|
||||
return (
|
||||
<BaseFileInput
|
||||
value={null} // We handle preview manually
|
||||
onChange={handleFileChange}
|
||||
accept={accept}
|
||||
title={title || 'Input Videos'}
|
||||
type="video"
|
||||
{...props}
|
||||
>
|
||||
{() => (
|
||||
<Box>
|
||||
<InputHeader
|
||||
title={title || 'Input ' + type.charAt(0).toUpperCase() + type.slice(1)}
|
||||
/>
|
||||
<Box
|
||||
sx={{
|
||||
width: '100%',
|
||||
height: '300px',
|
||||
border: value?.length ? 0 : 1,
|
||||
borderRadius: 2,
|
||||
boxShadow: '5',
|
||||
bgcolor: 'background.paper',
|
||||
position: 'relative'
|
||||
}}
|
||||
>
|
||||
<Box
|
||||
width="100%"
|
||||
height="100%"
|
||||
sx={{
|
||||
position: 'relative',
|
||||
width: '100%',
|
||||
height: '100%',
|
||||
overflow: 'auto',
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center'
|
||||
justifyContent: 'center',
|
||||
flexWrap: 'wrap',
|
||||
position: 'relative'
|
||||
}}
|
||||
>
|
||||
{preview && (
|
||||
<video
|
||||
src={preview}
|
||||
controls
|
||||
style={{ maxWidth: '100%', maxHeight: '100%' }}
|
||||
/>
|
||||
)}
|
||||
{value && value.length > 0 && (
|
||||
<Box mt={2} width="100%">
|
||||
<Typography variant="subtitle2">Selected Videos:</Typography>
|
||||
{value.map((file, idx) => (
|
||||
<Box key={idx} display="flex" alignItems="center" mb={1}>
|
||||
<Typography variant="body2" sx={{ flex: 1 }}>
|
||||
{file.name}
|
||||
{value?.length ? (
|
||||
value.map((file, index) => (
|
||||
<Box
|
||||
key={index}
|
||||
sx={{
|
||||
margin: 1,
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'space-between',
|
||||
width: '200px',
|
||||
border: 1,
|
||||
borderRadius: 1,
|
||||
padding: 1
|
||||
}}
|
||||
>
|
||||
<Box sx={{ display: 'flex', alignItems: 'center' }}>
|
||||
<VideoFileIcon />
|
||||
<Typography sx={{ marginLeft: 1 }}>
|
||||
{fileNameTruncate(file.file.name)}
|
||||
</Typography>
|
||||
<IconButton size="small" onClick={() => handleRemove(idx)}>
|
||||
<DeleteIcon fontSize="small" />
|
||||
</IconButton>
|
||||
</Box>
|
||||
))}
|
||||
</Box>
|
||||
<Box
|
||||
sx={{ cursor: 'pointer' }}
|
||||
onClick={() => {
|
||||
const updatedFiles = value.filter((_, i) => i !== index);
|
||||
onChange(updatedFiles);
|
||||
}}
|
||||
>
|
||||
✖
|
||||
</Box>
|
||||
</Box>
|
||||
))
|
||||
) : (
|
||||
<Typography variant="body2" color="text.secondary">
|
||||
No files selected
|
||||
</Typography>
|
||||
)}
|
||||
</Box>
|
||||
)}
|
||||
</BaseFileInput>
|
||||
</Box>
|
||||
|
||||
<InputFooter handleImport={handleImportClick} handleClear={handleClear} />
|
||||
<input
|
||||
ref={fileInputRef}
|
||||
style={{ display: 'none' }}
|
||||
type="file"
|
||||
accept={accept.join(',')}
|
||||
onChange={handleFileChange}
|
||||
multiple={true}
|
||||
/>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user