mirror of
https://github.com/Ku6epXBOCTuK/easy-png-tools.git
synced 2026-09-14 21:46:35 +00:00
feat: add ctrl+v flow
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { ACCEPTED_IMAGE_TYPES, isSupportedImage } from '$lib/core/io';
|
import { ACCEPTED_IMAGE_TYPES, isSupportedImage, unsupportedImageMessage } from '$lib/core/io';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
onFile: (file: File) => void;
|
onFile: (file: File) => void;
|
||||||
@@ -16,9 +16,7 @@
|
|||||||
function accept(file: File | undefined | null) {
|
function accept(file: File | undefined | null) {
|
||||||
if (!file) return;
|
if (!file) return;
|
||||||
if (!isSupportedImage(file)) {
|
if (!isSupportedImage(file)) {
|
||||||
onError?.(
|
onError?.(unsupportedImageMessage(file));
|
||||||
`Неподдерживаемый формат файла (${file.type || 'неизвестный'}). Поддерживаются PNG, JPEG, WebP, GIF и BMP.`
|
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
onFile(file);
|
onFile(file);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { imageInfo, type ImageInfo } from '$lib/core/analyze';
|
import { imageInfo, type ImageInfo } from '$lib/core/analyze';
|
||||||
import { decodeFile } from '$lib/core/io';
|
import { decodeFile, isSupportedImage, unsupportedImageMessage } from '$lib/core/io';
|
||||||
import type { PixelImage } from '$lib/core/types';
|
import type { PixelImage } from '$lib/core/types';
|
||||||
import { defaultParams, outputOf, sanitizeParams, type ToolEntry } from '$lib/registry';
|
import { defaultParams, outputOf, sanitizeParams, type ToolEntry } from '$lib/registry';
|
||||||
import Button from './ui/Button.svelte';
|
import Button from './ui/Button.svelte';
|
||||||
@@ -73,8 +73,28 @@
|
|||||||
status = 'idle';
|
status = 'idle';
|
||||||
values = defaultParams(tool);
|
values = defaultParams(tool);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function handlePaste(event: ClipboardEvent) {
|
||||||
|
const items = event.clipboardData?.items;
|
||||||
|
if (!items) return;
|
||||||
|
for (const item of items) {
|
||||||
|
if (!item.type.startsWith('image/')) continue;
|
||||||
|
const file = item.getAsFile();
|
||||||
|
if (file) {
|
||||||
|
event.preventDefault();
|
||||||
|
if (!isSupportedImage(file)) {
|
||||||
|
errorText = unsupportedImageMessage(file);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
handleFile(file);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
<svelte:window onpaste={handlePaste} />
|
||||||
|
|
||||||
<section>
|
<section>
|
||||||
<h1>{tool.title}</h1>
|
<h1>{tool.title}</h1>
|
||||||
<p class="description text-muted">{tool.description}</p>
|
<p class="description text-muted">{tool.description}</p>
|
||||||
|
|||||||
@@ -11,6 +11,10 @@ export function isSupportedImage(file: File): boolean {
|
|||||||
return SUPPORTED_MIME_TYPES.has(file.type);
|
return SUPPORTED_MIME_TYPES.has(file.type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function unsupportedImageMessage(file: File): string {
|
||||||
|
return `Неподдерживаемый формат файла (${file.type || 'неизвестный'}). Поддерживаются PNG, JPEG, WebP, GIF и BMP.`;
|
||||||
|
}
|
||||||
|
|
||||||
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