|
| 1 | +/** |
| 2 | + * Collect image `File`s from the clipboard and build `OnPasteImagesEvent` payloads with `blob:` URIs. |
| 3 | + */ |
| 4 | + |
| 5 | +import type { Editor } from '@tiptap/react'; |
| 6 | +import type { NativeSyntheticEvent } from 'react-native'; |
| 7 | + |
| 8 | +import type { OnPasteImagesEvent } from '../types'; |
| 9 | +import { adaptWebToNativeEvent } from './adaptWebToNativeEvent'; |
| 10 | +import { isImageBlocked } from './formats/formatRules'; |
| 11 | +import { readImageDimensionsFromBlob } from './pastedImageDimensions'; |
| 12 | + |
| 13 | +const isImageLikeClipboardFile = (file: File, reportedMime: string) => |
| 14 | + reportedMime.startsWith('image/') || file.type.startsWith('image/'); |
| 15 | + |
| 16 | +/** Browsers often expose the same paste as two `File`s (items vs files) with different `name`. */ |
| 17 | +function dedupeImageFiles(files: File[]): File[] { |
| 18 | + const seen = new Set<string>(); |
| 19 | + const out: File[] = []; |
| 20 | + for (const file of files) { |
| 21 | + const key = `${file.size}\0${file.lastModified}\0${file.type}`; |
| 22 | + if (seen.has(key)) continue; |
| 23 | + seen.add(key); |
| 24 | + out.push(file); |
| 25 | + } |
| 26 | + return out; |
| 27 | +} |
| 28 | + |
| 29 | +export function clipboardImageFiles(data: DataTransfer): File[] { |
| 30 | + const fromItems: File[] = []; |
| 31 | + for (const item of [...data.items]) { |
| 32 | + if (item == null || item.kind !== 'file') continue; |
| 33 | + const file = item.getAsFile(); |
| 34 | + if (!file) continue; |
| 35 | + if (isImageLikeClipboardFile(file, item.type)) fromItems.push(file); |
| 36 | + } |
| 37 | + if (fromItems.length > 0) return dedupeImageFiles(fromItems); |
| 38 | + |
| 39 | + const fromFiles: File[] = []; |
| 40 | + for (const file of [...data.files]) { |
| 41 | + if (isImageLikeClipboardFile(file, file.type)) fromFiles.push(file); |
| 42 | + } |
| 43 | + return dedupeImageFiles(fromFiles); |
| 44 | +} |
| 45 | + |
| 46 | +export async function buildPasteImagesPayload( |
| 47 | + files: File[] |
| 48 | +): Promise<OnPasteImagesEvent['images']> { |
| 49 | + return Promise.all( |
| 50 | + files.map(async (file) => { |
| 51 | + const uri = URL.createObjectURL(file); |
| 52 | + const { width, height } = await readImageDimensionsFromBlob(file, uri); |
| 53 | + return { |
| 54 | + uri, |
| 55 | + type: file.type || 'image/png', |
| 56 | + width, |
| 57 | + height, |
| 58 | + }; |
| 59 | + }) |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +export function handleClipboardPasteImages( |
| 64 | + event: ClipboardEvent, |
| 65 | + getEditor: () => Editor | null, |
| 66 | + getOnPasteImages: () => |
| 67 | + | ((e: NativeSyntheticEvent<OnPasteImagesEvent>) => void) |
| 68 | + | undefined |
| 69 | +): boolean { |
| 70 | + const clipboardData = event.clipboardData; |
| 71 | + if (!clipboardData) return false; |
| 72 | + |
| 73 | + const files = clipboardImageFiles(clipboardData); |
| 74 | + if (files.length === 0) return false; |
| 75 | + |
| 76 | + const ed = getEditor(); |
| 77 | + if (!ed || isImageBlocked(ed)) return false; |
| 78 | + |
| 79 | + const onPasteImages = getOnPasteImages(); |
| 80 | + if (!onPasteImages) return false; |
| 81 | + |
| 82 | + event.preventDefault(); |
| 83 | + |
| 84 | + (async () => { |
| 85 | + try { |
| 86 | + const images = await buildPasteImagesPayload(files); |
| 87 | + const editor = getEditor(); |
| 88 | + if (!editor || isImageBlocked(editor)) return; |
| 89 | + onPasteImages(adaptWebToNativeEvent(event, { images })); |
| 90 | + } catch (err) { |
| 91 | + console.error(err); |
| 92 | + } |
| 93 | + })(); |
| 94 | + |
| 95 | + return true; |
| 96 | +} |
0 commit comments