okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-0243-guifix into f-0243-rel9caf39495

main
1.0 KiB19 linesraw
1/** The pictures on the clipboard, named so each one is told apart in the composer (a pasted screenshot arrives as "image.png"). */
2export function pastedFiles(data: DataTransfer | null | undefined): File[] {
3  const out: File[] = [];
4  const found: File[] = Array.from(data?.files ?? []);
5  // A screenshot from the clipboard (Win+Shift+S) often comes as an item and not in `files` (#3748).
6  if (!found.some(f => f.type.startsWith('image/'))) {
7    for (const item of Array.from(data?.items ?? [])) {
8      if (item.kind !== 'file' || !item.type.startsWith('image/')) continue;
9      const file = item.getAsFile();
10      if (file) found.push(file);
11    }
12  }
13  for (const file of found) {
14    if (!file.type.startsWith('image/')) continue;
15    const extension = file.type.split('/')[1]?.replace('jpeg', 'jpg').replace(/[+].*/, '') || 'png';
16    out.push(new File([file], `Pasted picture ${new Date().toLocaleTimeString().replace(/[^0-9]+/g, '-').replace(/-$/, '')}${out.length ? `-${out.length + 1}` : ''}.${extension}`, { type: file.type }));
17  }
18  return out;
19}