import React, { useState, useEffect } from 'react'; const ImageEditor = ({ record, property, onChange }) => { const [currentValue, setCurrentValue] = useState(record.params[property.name] || ''); const [showEditor, setShowEditor] = useState(false); const [images, setImages] = useState([]); const [loading, setLoading] = useState(false); const [activeTab, setActiveTab] = useState('gallery'); const [uploadFile, setUploadFile] = useState(null); // Загрузка галереи изображений useEffect(() => { if (showEditor) { loadGallery(); } }, [showEditor]); const loadGallery = async () => { setLoading(true); try { const response = await fetch('/api/images/gallery?folder=all'); const result = await response.json(); if (result.success) { setImages(result.data); } } catch (error) { console.error('Error loading gallery:', error); } finally { setLoading(false); } }; const handleImageSelect = (imagePath) => { setCurrentValue(imagePath); onChange(property.name, imagePath); setShowEditor(false); }; const handleFileUpload = async () => { if (!uploadFile) return; const formData = new FormData(); formData.append('image', uploadFile); setLoading(true); try { const response = await fetch('/api/images/upload', { method: 'POST', body: formData }); const result = await response.json(); if (result.success) { handleImageSelect(result.data.path); await loadGallery(); // Обновляем галерею } else { alert('Ошибка загрузки: ' + result.error); } } catch (error) { alert('Ошибка загрузки: ' + error.message); } finally { setLoading(false); } }; const getFolderFromPropertyName = () => { const name = property.name.toLowerCase(); if (name.includes('route')) return 'routes'; if (name.includes('guide')) return 'guides'; if (name.includes('article')) return 'articles'; return 'general'; }; return (
{/* Поле ввода и кнопка */}
{ setCurrentValue(e.target.value); onChange(property.name, e.target.value); }} placeholder="Путь к изображению" style={{ flex: 1, padding: '8px 12px', border: '1px solid #ddd', borderRadius: '4px', marginRight: '8px' }} />
{/* Предварительный просмотр */} {currentValue && (
Preview { e.target.style.display = 'none'; }} />
)} {/* Редактор изображений */} {showEditor && (
{/* Вкладки */}
{/* Контент вкладки Галерея */} {activeTab === 'gallery' && (
{loading ? (
Загрузка...
) : (
{images.map((image) => (
handleImageSelect(image.path)} style={{ cursor: 'pointer', border: currentValue === image.path ? '2px solid #007bff' : '1px solid #ddd', borderRadius: '4px', overflow: 'hidden', backgroundColor: 'white' }} > {image.name}
{image.name}
{image.folder}
))}
)}
)} {/* Контент вкладки Загрузить */} {activeTab === 'upload' && (
setUploadFile(e.target.files[0])} style={{ marginBottom: '12px' }} />
Поддерживаются: JPG, PNG, GIF (макс. 5МБ)
{uploadFile && (
Выбран файл: {uploadFile.name}
)}
)}
)}
); }; export default ImageEditor;