Files
cinny/src/app/hooks/useFilePasteHandler.ts
T

19 lines
704 B
TypeScript
Raw Normal View History

2023-06-12 21:15:23 +10:00
import { useCallback, ClipboardEventHandler } from 'react';
import { getDataTransferFiles } from '../utils/dom';
export const useFilePasteHandler = (onPaste: (file: File[]) => void): ClipboardEventHandler =>
useCallback(
(evt) => {
const files = getDataTransferFiles(evt.clipboardData);
if (files) {
// [Gitea #132] An image copied from a web page arrives with a
// text/html `<img src=… alt=…>` fragment and a text/plain URL beside
// the bitmap. Only the file is wanted — stop the editor's own
// HTML/text insertion so nothing else lands in the composer.
evt.preventDefault();
onPaste(files);
}
2023-06-12 21:15:23 +10:00
},
[onPaste],
2023-06-12 21:15:23 +10:00
);