2020-04-28 19:46:55 +05:00
|
|
|
import React, { useCallback, useEffect } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { useDropzone } from 'react-dropzone';
|
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
import { closePopup } from '../../../lib/popup';
|
|
|
|
|
|
|
|
import { useModal } from '../../../hooks';
|
2020-08-04 01:32:46 +05:00
|
|
|
import TextFileAddModal from './TextFileAddModal';
|
2020-04-28 19:46:55 +05:00
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
import styles from './AttachmentAddZone.module.scss';
|
2020-04-28 19:46:55 +05:00
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
const AttachmentAddZone = React.memo(({ children, onCreate }) => {
|
2020-04-28 19:46:55 +05:00
|
|
|
const [t] = useTranslation();
|
|
|
|
const [modal, openModal, handleModalClose] = useModal();
|
|
|
|
|
|
|
|
const submit = useCallback(
|
|
|
|
(file) => {
|
|
|
|
onCreate({
|
|
|
|
file,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[onCreate],
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleDropAccepted = useCallback(
|
|
|
|
(files) => {
|
|
|
|
submit(files[0]);
|
|
|
|
},
|
|
|
|
[submit],
|
|
|
|
);
|
|
|
|
|
|
|
|
const { getRootProps, getInputProps, isDragActive } = useDropzone({
|
|
|
|
multiple: false,
|
|
|
|
noClick: true,
|
|
|
|
noKeyboard: true,
|
|
|
|
onDropAccepted: handleDropAccepted,
|
|
|
|
});
|
|
|
|
|
|
|
|
const handleFileCreate = useCallback(
|
|
|
|
(file) => {
|
|
|
|
submit(file);
|
|
|
|
},
|
|
|
|
[submit],
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const handlePaste = (event) => {
|
2021-03-20 03:33:16 +05:00
|
|
|
if (!event.clipboardData) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const file = event.clipboardData.files[0];
|
|
|
|
|
|
|
|
if (file) {
|
|
|
|
submit(file);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const item = event.clipboardData.items[0];
|
2020-04-28 19:46:55 +05:00
|
|
|
|
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item.kind === 'file') {
|
|
|
|
submit(item.getAsFile());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
['input', 'textarea'].includes(event.target.tagName.toLowerCase()) &&
|
|
|
|
event.target === document.activeElement
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
closePopup();
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
item.getAsString((content) => {
|
|
|
|
openModal({
|
|
|
|
content,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
window.addEventListener('paste', handlePaste);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener('paste', handlePaste);
|
|
|
|
};
|
|
|
|
}, [openModal, submit]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{/* eslint-disable-next-line react/jsx-props-no-spreading */}
|
2020-05-23 18:05:22 +05:00
|
|
|
<div {...getRootProps()} className={styles.wrapper}>
|
2020-04-28 19:46:55 +05:00
|
|
|
{isDragActive && <div className={styles.dropzone}>{t('common.dropFileToUpload')}</div>}
|
|
|
|
{children}
|
2020-05-23 18:05:22 +05:00
|
|
|
{/* eslint-disable-next-line react/jsx-props-no-spreading */}
|
|
|
|
<input {...getInputProps()} />
|
2020-04-28 19:46:55 +05:00
|
|
|
</div>
|
|
|
|
{modal && (
|
2020-08-04 01:32:46 +05:00
|
|
|
<TextFileAddModal
|
2020-04-28 19:46:55 +05:00
|
|
|
content={modal.content}
|
|
|
|
onCreate={handleFileCreate}
|
|
|
|
onClose={handleModalClose}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
AttachmentAddZone.propTypes = {
|
2020-04-28 19:46:55 +05:00
|
|
|
children: PropTypes.element.isRequired,
|
|
|
|
onCreate: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
export default AttachmentAddZone;
|