import { useCallback, ClipboardEventHandler } from 'react'; import { getDataTransferFiles } from '../utils/dom'; import { readClipboardImage, isTauri, isLinux } from '../utils/tauri'; export const useFilePasteHandler = (onPaste: (file: File[]) => void): ClipboardEventHandler => useCallback( (evt) => { const files = getDataTransferFiles(evt.clipboardData); if (files && files.length > 0) { evt.preventDefault(); onPaste(files); return; } // Address-bar / plain-text copies still win. On Linux the native clipboard // often still has a previous image (or a junk bitmap) alongside text/plain; // don't treat those as an image paste. const text = evt.clipboardData?.getData('text/plain')?.trim() ?? ''; if (text.length > 0) { return; } // Browser clipboardData.files is empty for raw image copies on Linux Tauri/Electron. if (!(isTauri() && isLinux())) return; // preventDefault before the async read, or the editor also inserts text. evt.preventDefault(); void readClipboardImage().then((clipboardImage) => { if (clipboardImage) onPaste([clipboardImage]); }); }, [onPaste] );