mirror of
https://github.com/Ku6epXBOCTuK/easy-png-tools.git
synced 2026-09-14 21:46:35 +00:00
refactor: dropzone update
This commit is contained in:
@@ -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.`
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user