refactor: dropzone update

This commit is contained in:
2026-08-22 15:04:49 +05:00
parent 06c6716cf5
commit 7afd07e8c1
2 changed files with 13 additions and 10 deletions
+7 -10
View File
@@ -1,24 +1,21 @@
<script lang="ts"> <script lang="ts">
import { ACCEPTED_IMAGE_TYPES } from '$lib/core/io'; import { ACCEPTED_IMAGE_TYPES, isSupportedImage } from '$lib/core/io';
let { interface Props {
onFile,
onError,
label = 'Перетащите изображение сюда или нажмите, чтобы выбрать файл'
}: {
onFile: (file: File) => void; onFile: (file: File) => void;
onError?: (message: string) => void; onError?: (message: string) => void;
label?: string; label?: string;
} = $props(); }
let { onFile, onError, label = 'Перетащите изображение сюда или нажмите, чтобы выбрать файл' }: Props =
$props();
let input = $state<HTMLInputElement | undefined>(); let input = $state<HTMLInputElement | undefined>();
let dragging = $state(false); let dragging = $state(false);
const acceptedMimes = new Set(ACCEPTED_IMAGE_TYPES.split(','));
function accept(file: File | undefined | null) { function accept(file: File | undefined | null) {
if (!file) return; if (!file) return;
if (!acceptedMimes.has(file.type)) { if (!isSupportedImage(file)) {
onError?.( onError?.(
`Неподдерживаемый формат файла (${file.type || 'неизвестный'}). Поддерживаются PNG, JPEG, WebP, GIF и BMP.` `Неподдерживаемый формат файла (${file.type || 'неизвестный'}). Поддерживаются PNG, JPEG, WebP, GIF и BMP.`
); );
+6
View File
@@ -5,6 +5,12 @@ export type OutputMime = 'image/png' | 'image/jpeg' | 'image/webp';
export const ACCEPTED_IMAGE_TYPES = export const ACCEPTED_IMAGE_TYPES =
'image/png,image/jpeg,image/webp,image/gif,image/bmp,image/x-icon'; 'image/png,image/jpeg,image/webp,image/gif,image/bmp,image/x-icon';
const SUPPORTED_MIME_TYPES = new Set(ACCEPTED_IMAGE_TYPES.split(','));
export function isSupportedImage(file: File): boolean {
return SUPPORTED_MIME_TYPES.has(file.type);
}
export async function decodeFile(file: File): Promise<PixelImage> { export async function decodeFile(file: File): Promise<PixelImage> {
const bitmap = await createImageBitmap(file); const bitmap = await createImageBitmap(file);
try { try {