1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-25 12:56:41 +07:00

added image upload on paste to comment form

This commit is contained in:
Fedor Katurov 2021-03-10 10:58:20 +07:00
parent ebdb09a611
commit de1065f7b2
3 changed files with 60 additions and 1 deletions

View 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]);
};