feat: obrazky sa zmensuju na 1000 px aj pri vlozeni URL a zo schranky
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e875b44f92
commit
6100a7af57
2 files changed
+74
-10
No files matched your search
@@ -2,7 +2,7 @@
|
||||
|
||||
import React, { useRef, useState } from 'react';
|
||||
import { Upload, Loader2, Image as ImageIcon } from 'lucide-react';
|
||||
import { shrinkImage, uploadFile } from '@/lib/upload';
|
||||
import { shrinkImage, shrinkRemoteImage, uploadFile } from '@/lib/upload';
|
||||
|
||||
interface ImageUploadInputProps {
|
||||
label?: string;
|
||||
@@ -27,16 +27,13 @@ export const ImageUploadInput: React.FC<ImageUploadInputProps> = ({
|
||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||
const [isUploading, setIsUploading] = useState(false);
|
||||
|
||||
const handleFileChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const file = e.target.files?.[0];
|
||||
if (!file) return;
|
||||
|
||||
const uploadImage = async (file: File) => {
|
||||
setIsUploading(true);
|
||||
try {
|
||||
const small = await shrinkImage(file);
|
||||
onChange(await uploadFile(small, small.name));
|
||||
} catch (err: any) {
|
||||
alert(`Chyba pri nahrávaní obrázka: ${err.message}`);
|
||||
} catch (err) {
|
||||
alert(`Chyba pri nahrávaní obrázka: ${(err as Error).message}`);
|
||||
} finally {
|
||||
setIsUploading(false);
|
||||
if (fileInputRef.current) {
|
||||
@@ -45,6 +42,43 @@ export const ImageUploadInput: React.FC<ImageUploadInputProps> = ({
|
||||
}
|
||||
};
|
||||
|
||||
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const file = e.target.files?.[0];
|
||||
if (file) uploadImage(file);
|
||||
};
|
||||
|
||||
// Vložená URL: príliš veľký obrázok sa zmenší a uloží k nám (posledná spracovaná adresa, aby sa nerobilo dvakrát)
|
||||
const lastCheckedUrl = useRef('');
|
||||
const checkUrl = async (url: string) => {
|
||||
url = url.trim();
|
||||
if (!url || url === lastCheckedUrl.current) return;
|
||||
lastCheckedUrl.current = url;
|
||||
setIsUploading(true);
|
||||
try {
|
||||
const smaller = await shrinkRemoteImage(url);
|
||||
if (smaller) {
|
||||
lastCheckedUrl.current = smaller;
|
||||
onChange(smaller);
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn('Obrázok z URL sa nepodarilo zmenšiť:', err);
|
||||
} finally {
|
||||
setIsUploading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handlePaste = (e: React.ClipboardEvent<HTMLInputElement>) => {
|
||||
// obrázok zo schránky (Ctrl+V) → nahrá sa ako súbor
|
||||
const file = [...e.clipboardData.files].find((f) => f.type.startsWith('image/'));
|
||||
if (file) {
|
||||
e.preventDefault();
|
||||
uploadImage(file);
|
||||
return;
|
||||
}
|
||||
const text = e.clipboardData.getData('text');
|
||||
if (/^https?:\/\//i.test(text.trim())) setTimeout(() => checkUrl(text), 0);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-1.5">
|
||||
{label && <label className="block text-[11px] font-semibold text-gray-700">{label}</label>}
|
||||
@@ -71,6 +105,8 @@ export const ImageUploadInput: React.FC<ImageUploadInputProps> = ({
|
||||
type="text"
|
||||
value={value || ''}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
onPaste={handlePaste}
|
||||
onBlur={(e) => checkUrl(e.target.value)}
|
||||
placeholder={placeholder}
|
||||
className="flex-1 min-w-0 text-xs font-mono border border-gray-300 rounded-lg p-2 bg-white"
|
||||
/>
|
||||
|
||||
Reference in new issue
Block a user