mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-24 20:36:40 +07:00
27 lines
712 B
TypeScript
27 lines
712 B
TypeScript
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]);
|
|
},
|
|
[onUpload]
|
|
);
|
|
|
|
useEffect(() => {
|
|
if (!input) return;
|
|
|
|
input.addEventListener('paste', onPaste);
|
|
|
|
return () => input.removeEventListener('paste', onPaste);
|
|
}, [input, onPaste]);
|
|
};
|