All checks were successful
Trigger cinny-mobile / dispatch (push) Successful in 2s
Sparkle 🎆/🎇 from the jumbo emoji with a lightweight particle fountain that piles on the floor. Prefer text/plain over sticky clipboard images on Linux so address-bar URLs do not also attach a leftover bitmap.
34 lines
1.2 KiB
TypeScript
34 lines
1.2 KiB
TypeScript
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]
|
|
);
|