mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-24 20:36:40 +07:00
added image upload on paste to comment form
This commit is contained in:
parent
ebdb09a611
commit
de1065f7b2
3 changed files with 60 additions and 1 deletions
|
@ -14,7 +14,7 @@ import { EMPTY_COMMENT } from '~/redux/node/constants';
|
||||||
import { CommentFormDropzone } from '~/components/comment/CommentFormDropzone';
|
import { CommentFormDropzone } from '~/components/comment/CommentFormDropzone';
|
||||||
import styles from './styles.module.scss';
|
import styles from './styles.module.scss';
|
||||||
import { ERROR_LITERAL } from '~/constants/errors';
|
import { ERROR_LITERAL } from '~/constants/errors';
|
||||||
import { Group } from '~/components/containers/Group';
|
import { useInputPasteUpload } from '~/utils/hooks/useInputPasteUpload';
|
||||||
|
|
||||||
interface IProps {
|
interface IProps {
|
||||||
comment?: IComment;
|
comment?: IComment;
|
||||||
|
@ -47,6 +47,7 @@ const CommentForm: FC<IProps> = ({ comment, nodeId, onCancelEdit }) => {
|
||||||
}, [formik]);
|
}, [formik]);
|
||||||
|
|
||||||
const error = formik.status || formik.errors.text;
|
const error = formik.status || formik.errors.text;
|
||||||
|
useInputPasteUpload(textarea, uploader.uploadFiles);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<CommentFormDropzone onUpload={uploader.uploadFiles}>
|
<CommentFormDropzone onUpload={uploader.uploadFiles}>
|
||||||
|
|
24
src/utils/hooks/useInputPasteUpload.ts
Normal file
24
src/utils/hooks/useInputPasteUpload.ts
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
import { useCallback, useEffect } from 'react';
|
||||||
|
import { getImageFromPaste } from '~/utils/uploader';
|
||||||
|
|
||||||
|
// useInputPasteUpload attaches event listener to input, that calls onUpload if user pasted any image
|
||||||
|
export const useInputPasteUpload = (
|
||||||
|
input: HTMLTextAreaElement | HTMLInputElement | undefined,
|
||||||
|
onUpload: (files: File[]) => void
|
||||||
|
) => {
|
||||||
|
const onPaste = useCallback(async event => {
|
||||||
|
const image = await getImageFromPaste(event);
|
||||||
|
|
||||||
|
if (!image) return;
|
||||||
|
|
||||||
|
onUpload([image]);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!input) return;
|
||||||
|
|
||||||
|
input.addEventListener('paste', onPaste);
|
||||||
|
|
||||||
|
return () => input.removeEventListener('paste', onPaste);
|
||||||
|
}, [input, onPaste]);
|
||||||
|
};
|
|
@ -74,3 +74,37 @@ export const fakeUploader = ({
|
||||||
export const getFileType = (file: File): keyof typeof UPLOAD_TYPES | undefined =>
|
export const getFileType = (file: File): keyof typeof UPLOAD_TYPES | undefined =>
|
||||||
(file.type && Object.keys(FILE_MIMES).find(mime => FILE_MIMES[mime].includes(file.type))) ||
|
(file.type && Object.keys(FILE_MIMES).find(mime => FILE_MIMES[mime].includes(file.type))) ||
|
||||||
undefined;
|
undefined;
|
||||||
|
|
||||||
|
// getImageFromPaste returns any images from paste event
|
||||||
|
export const getImageFromPaste = (event: ClipboardEvent): Promise<File | undefined> => {
|
||||||
|
const items = event.clipboardData?.items;
|
||||||
|
|
||||||
|
return new Promise(resolve => {
|
||||||
|
for (let index in items) {
|
||||||
|
const item = items[index];
|
||||||
|
|
||||||
|
if (item.kind === 'file' && item.type.match(/^image\//)) {
|
||||||
|
const blob = item.getAsFile();
|
||||||
|
const reader = new FileReader();
|
||||||
|
const type = item.type;
|
||||||
|
|
||||||
|
reader.onload = function(e) {
|
||||||
|
if (!e.target?.result) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve(
|
||||||
|
new File([e.target?.result], 'paste.png', {
|
||||||
|
type,
|
||||||
|
lastModified: new Date().getTime(),
|
||||||
|
})
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
reader.readAsArrayBuffer(blob);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// resolve(undefined);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue