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);
|
2026-09-19 17:28:25 -04:00
|
|
|
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
|
|
|
},
|
2026-05-21 23:30:50 -04:00
|
|
|
[onPaste],
|
2023-06-12 21:15:23 +10:00
|
|
|
);
|