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