2024-06-22 22:06:16 +01:00
|
|
|
import { Box, Stack, TextField } from '@mui/material';
|
|
|
|
|
import Typography from '@mui/material/Typography';
|
|
|
|
|
import Button from '@mui/material/Button';
|
|
|
|
|
import PublishIcon from '@mui/icons-material/Publish';
|
|
|
|
|
import ContentPasteIcon from '@mui/icons-material/ContentPaste';
|
2024-06-23 01:42:50 +01:00
|
|
|
import React, { useContext, useRef } from 'react';
|
|
|
|
|
import { CustomSnackBarContext } from '../../contexts/CustomSnackBarContext';
|
2024-06-21 20:06:07 +01:00
|
|
|
|
2024-06-22 22:06:16 +01:00
|
|
|
export default function ToolTextInput({
|
|
|
|
|
value,
|
|
|
|
|
onChange,
|
|
|
|
|
title = 'Input text'
|
|
|
|
|
}: {
|
2024-06-21 20:06:07 +01:00
|
|
|
title?: string;
|
2024-06-22 22:06:16 +01:00
|
|
|
value: string;
|
|
|
|
|
onChange: (value: string) => void;
|
2024-06-21 20:06:07 +01:00
|
|
|
}) {
|
2024-06-23 01:42:50 +01:00
|
|
|
const { showSnackBar } = useContext(CustomSnackBarContext);
|
|
|
|
|
const fileInputRef = useRef<HTMLInputElement>(null);
|
|
|
|
|
|
|
|
|
|
const handleCopy = () => {
|
|
|
|
|
navigator.clipboard
|
|
|
|
|
.writeText(value)
|
|
|
|
|
.then(() => showSnackBar('Text copied', 'success'))
|
|
|
|
|
.catch((err) => {
|
2024-06-23 00:47:12 -07:00
|
|
|
showSnackBar('Failed to copy: ', err);
|
2024-06-23 01:42:50 +01:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
const file = event.target.files?.[0];
|
|
|
|
|
if (file) {
|
|
|
|
|
const reader = new FileReader();
|
|
|
|
|
reader.onload = (e) => {
|
|
|
|
|
const text = e.target?.result;
|
|
|
|
|
if (typeof text === 'string') {
|
|
|
|
|
onChange(text);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
reader.readAsText(file);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleImportClick = () => {
|
|
|
|
|
fileInputRef.current?.click();
|
|
|
|
|
};
|
2024-06-21 20:06:07 +01:00
|
|
|
return (
|
|
|
|
|
<Box>
|
|
|
|
|
<Typography fontSize={30} color={'primary'}>
|
|
|
|
|
{title}
|
|
|
|
|
</Typography>
|
|
|
|
|
<TextField
|
|
|
|
|
value={value}
|
|
|
|
|
onChange={(event) => onChange(event.target.value)}
|
|
|
|
|
fullWidth
|
|
|
|
|
multiline
|
|
|
|
|
rows={10}
|
|
|
|
|
/>
|
|
|
|
|
<Stack mt={1} direction={'row'} spacing={2}>
|
2024-06-23 01:42:50 +01:00
|
|
|
<Button onClick={handleImportClick} startIcon={<PublishIcon />}>
|
|
|
|
|
Import from file
|
|
|
|
|
</Button>
|
|
|
|
|
<Button onClick={handleCopy} startIcon={<ContentPasteIcon />}>
|
|
|
|
|
Copy to clipboard
|
|
|
|
|
</Button>
|
2024-06-21 20:06:07 +01:00
|
|
|
</Stack>
|
2024-06-23 01:42:50 +01:00
|
|
|
<input
|
|
|
|
|
type="file"
|
|
|
|
|
accept="*"
|
|
|
|
|
ref={fileInputRef}
|
|
|
|
|
style={{ display: 'none' }}
|
|
|
|
|
onChange={handleFileChange}
|
|
|
|
|
/>
|
2024-06-21 20:06:07 +01:00
|
|
|
</Box>
|
2024-06-22 22:06:16 +01:00
|
|
|
);
|
2024-06-21 20:06:07 +01:00
|
|
|
}
|