cleanup: remove duplicate native OS drop listener from LibraryPanel; Canvas is canonical drop target
Browse files- src/components/LibraryPanel.tsx +56 -258
src/components/LibraryPanel.tsx
CHANGED
|
@@ -3,7 +3,6 @@ import { X, Search, Folder, Grid, Plus, Tag, Trash2, RefreshCw, Upload, Edit3, C
|
|
| 3 |
import { useAppStore } from '../store';
|
| 4 |
import { invoke } from '@tauri-apps/api/core';
|
| 5 |
import { listen } from '@tauri-apps/api/event';
|
| 6 |
-
import { getCurrentWindow } from '@tauri-apps/api/window';
|
| 7 |
|
| 8 |
interface LibItem { id: string; url: string; source_url: string; title: string; data_url: string; hash: string; width: number; height: number; colors: string[]; tags: string[]; created_at: number; }
|
| 9 |
|
|
@@ -18,104 +17,52 @@ export const LibraryPanel = () => {
|
|
| 18 |
const [editingItem, setEditingItem] = useState<LibItem | null>(null);
|
| 19 |
const fileInputRef = useRef<HTMLInputElement>(null);
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
const loadLibrary = useCallback(() => {
|
| 22 |
setIsLoading(true);
|
| 23 |
-
invoke<LibItem[]>('library_items').then(items => {
|
| 24 |
-
setLibraryItems(items);
|
| 25 |
-
const tags = new Set<string>();
|
| 26 |
-
items.forEach(item => (item.tags || []).forEach(t => tags.add(t)));
|
| 27 |
-
setAllTags(Array.from(tags).sort());
|
| 28 |
-
setIsLoading(false);
|
| 29 |
-
}).catch(() => setIsLoading(false));
|
| 30 |
}, []);
|
| 31 |
|
| 32 |
useEffect(() => { if (isLibraryOpen) loadLibrary(); }, [isLibraryOpen, loadLibrary]);
|
| 33 |
-
useEffect(() => { const unlisten = listen<any>('board://image_added',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
const
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
const
|
| 43 |
-
|
| 44 |
-
const imageExts = ['.png', '.jpg', '.jpeg', '.webp', '.gif', '.bmp', '.avif', '.tiff'];
|
| 45 |
-
for (const filePath of paths) {
|
| 46 |
-
const lower = filePath.toLowerCase();
|
| 47 |
-
if (!imageExts.some(ext => lower.endsWith(ext))) continue;
|
| 48 |
-
try {
|
| 49 |
-
const item = await invoke<LibItem>('library_import_local', { path: filePath });
|
| 50 |
-
// Add to canvas immediately
|
| 51 |
-
const w = Math.min(500, item.width || 300);
|
| 52 |
-
const h = item.height ? w * (item.height / item.width) : w;
|
| 53 |
-
setImages(prev => [...prev, {
|
| 54 |
-
id: crypto.randomUUID(),
|
| 55 |
-
url: item.data_url,
|
| 56 |
-
x: (-pan.x + window.innerWidth / 3 + Math.random() * 120) / zoom,
|
| 57 |
-
y: (-pan.y + window.innerHeight / 3 + Math.random() * 120) / zoom,
|
| 58 |
-
width: w, height: h, aspectRatio: w / h,
|
| 59 |
-
}]);
|
| 60 |
-
} catch (err) { console.error(`Failed to import ${filePath}:`, err); }
|
| 61 |
-
}
|
| 62 |
loadLibrary();
|
| 63 |
-
}
|
| 64 |
-
}
|
| 65 |
-
|
| 66 |
-
}
|
| 67 |
|
| 68 |
-
|
| 69 |
-
const handleBrowserDrop = useCallback(async (e: React.DragEvent) => {
|
| 70 |
e.preventDefault(); e.stopPropagation(); setIsDragOver(false);
|
| 71 |
-
|
| 72 |
-
for (const file of files) {
|
| 73 |
-
if (!file.type.startsWith('image/')) continue;
|
| 74 |
-
const reader = new FileReader();
|
| 75 |
-
reader.onload = async (ev) => {
|
| 76 |
-
const dataUrl = ev.target?.result as string;
|
| 77 |
-
if (!dataUrl) return;
|
| 78 |
-
try {
|
| 79 |
-
const item = await invoke<LibItem>('library_import_data_url', { dataUrl, title: file.name.replace(/\.[^/.]+$/, '') });
|
| 80 |
-
const w = Math.min(500, item.width || 300);
|
| 81 |
-
const h = item.height ? w * (item.height / item.width) : w;
|
| 82 |
-
setImages(prev => [...prev, { id: crypto.randomUUID(), url: item.data_url, x: (-pan.x + window.innerWidth / 3 + Math.random() * 100) / zoom, y: (-pan.y + window.innerHeight / 3 + Math.random() * 100) / zoom, width: w, height: h, aspectRatio: w / h }]);
|
| 83 |
-
} catch (err) { console.error('Import failed:', err); }
|
| 84 |
-
};
|
| 85 |
-
reader.readAsDataURL(file);
|
| 86 |
-
}
|
| 87 |
-
loadLibrary();
|
| 88 |
-
}, [pan, zoom, setImages, loadLibrary]);
|
| 89 |
-
|
| 90 |
-
// ─── File input upload ───
|
| 91 |
-
const handleFileUpload = (e: React.ChangeEvent<HTMLInputElement>) => {
|
| 92 |
-
const files = e.target.files; if (!files) return;
|
| 93 |
-
Array.from(files).forEach(file => {
|
| 94 |
-
if (!file.type.startsWith('image/')) return;
|
| 95 |
-
const reader = new FileReader();
|
| 96 |
-
reader.onload = async (ev) => {
|
| 97 |
-
const dataUrl = ev.target?.result as string; if (!dataUrl) return;
|
| 98 |
-
try {
|
| 99 |
-
const item = await invoke<LibItem>('library_import_data_url', { dataUrl, title: file.name.replace(/\.[^/.]+$/, '') });
|
| 100 |
-
const w = Math.min(500, item.width || 300);
|
| 101 |
-
const h = item.height ? w * (item.height / item.width) : w;
|
| 102 |
-
setImages(prev => [...prev, { id: crypto.randomUUID(), url: item.data_url, x: (-pan.x + window.innerWidth / 3 + Math.random() * 100) / zoom, y: (-pan.y + window.innerHeight / 3 + Math.random() * 100) / zoom, width: w, height: h, aspectRatio: w / h }]);
|
| 103 |
-
loadLibrary();
|
| 104 |
-
} catch (err) { console.error('Upload failed:', err); }
|
| 105 |
-
};
|
| 106 |
-
reader.readAsDataURL(file);
|
| 107 |
-
});
|
| 108 |
-
if (e.target) e.target.value = '';
|
| 109 |
};
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
const h = item.height ? w * (item.height / item.width) : w;
|
| 115 |
-
setImages(prev => [...prev, { id: crypto.randomUUID(), url: item.data_url || item.url, x: (-pan.x + window.innerWidth / 3) / zoom, y: (-pan.y + window.innerHeight / 3) / zoom, width: w, height: h, aspectRatio: w / h }]);
|
| 116 |
};
|
| 117 |
|
| 118 |
-
// ─── Library-to-canvas drag ───
|
| 119 |
const handleDragStart = (e: React.DragEvent, item: LibItem) => {
|
| 120 |
const payload = JSON.stringify({ id: item.id, data_url: item.data_url, width: item.width, height: item.height, title: item.title });
|
| 121 |
e.dataTransfer.setData('application/x-muse-library-item', payload);
|
|
@@ -123,90 +70,42 @@ export const LibraryPanel = () => {
|
|
| 123 |
e.dataTransfer.effectAllowed = 'copy';
|
| 124 |
};
|
| 125 |
|
| 126 |
-
const handleDelete = (id: string) =>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
const filtered = libraryItems.filter(img => {
|
| 129 |
if (activeTag && !(img.tags || []).includes(activeTag)) return false;
|
| 130 |
-
if (search)
|
| 131 |
-
|
|
|
|
| 132 |
});
|
| 133 |
|
| 134 |
-
|
| 135 |
-
if (editingItem) {
|
| 136 |
-
return <MetadataEditor item={editingItem} onClose={() => setEditingItem(null)} onUpdate={(updated) => { setEditingItem(updated); setLibraryItems(prev => prev.map(i => i.id === updated.id ? updated : i)); const tags = new Set<string>(); libraryItems.forEach(item => (item.id === updated.id ? updated : item).tags.forEach(t => tags.add(t))); setAllTags(Array.from(tags).sort()); }} onDelete={() => { handleDelete(editingItem.id); setEditingItem(null); }} onAddToCanvas={() => handleAddToCanvas(editingItem)} isOpen={isLibraryOpen} onClosePanel={() => setIsLibraryOpen(false)} />;
|
| 137 |
-
}
|
| 138 |
|
| 139 |
return (
|
| 140 |
-
<div className={`absolute left-0 top-0 h-full w-[45%] max-w-[500px] bg-[#1C1C1E] shadow-2xl flex flex-col z-[60] transform transition-transform duration-500 ease-[cubic-bezier(0.19,1,0.22,1)] ${isLibraryOpen ? 'translate-x-0' : '-translate-x-full'}`}
|
| 141 |
-
onDragOver={(e) => { e.preventDefault(); e.dataTransfer.dropEffect = 'copy'; setIsDragOver(true); }}
|
| 142 |
-
onDragLeave={() => setIsDragOver(false)}
|
| 143 |
-
onDrop={handleBrowserDrop}
|
| 144 |
-
>
|
| 145 |
-
{/* Drop overlay */}
|
| 146 |
{isDragOver && <div className="absolute inset-0 z-50 bg-[#0A84FF]/10 border-2 border-dashed border-[#0A84FF] rounded-xl m-3 flex items-center justify-center pointer-events-none"><div className="text-[#0A84FF] font-semibold text-sm flex flex-col items-center gap-2"><Upload size={32} />Drop images to import</div></div>}
|
| 147 |
|
| 148 |
-
{/* Header */}
|
| 149 |
<div className="flex items-center justify-between p-4 border-b border-[#3A3A3E]">
|
| 150 |
-
<div className="flex items-center gap-2 text-[#E0E0E0] text-[14px] font-medium">
|
| 151 |
-
|
| 152 |
-
<span className="text-[11px] text-[#808080] ml-1">({libraryItems.length})</span>
|
| 153 |
-
</div>
|
| 154 |
-
<div className="flex items-center gap-1">
|
| 155 |
-
<button onClick={loadLibrary} className="text-[#A0A0A0] hover:text-[#E0E0E0] p-1.5 rounded-md hover:bg-white/5" title="Refresh"><RefreshCw size={14} className={isLoading ? 'animate-spin' : ''} /></button>
|
| 156 |
-
<button onClick={() => setIsLibraryOpen(false)} className="text-[#A0A0A0] hover:text-[#E0E0E0] p-1.5 rounded-md hover:bg-white/5"><X size={16} /></button>
|
| 157 |
-
</div>
|
| 158 |
</div>
|
| 159 |
|
| 160 |
-
{/* Search + Tags */}
|
| 161 |
<div className="px-4 py-3 bg-[#2A2A2E] flex flex-col gap-3">
|
| 162 |
-
<div className="relative">
|
| 163 |
-
|
| 164 |
-
<input type="text" value={search} onChange={e => setSearch(e.target.value)} placeholder="Search by name, tag, or URL..." className="w-full bg-[#1C1C1E] text-[#E0E0E0] pl-9 pr-3 py-2 text-[13px] rounded-lg border border-[#3A3A3E] focus:border-[#0A84FF] outline-none placeholder:text-[#606060]" />
|
| 165 |
-
</div>
|
| 166 |
-
{allTags.length > 0 && (
|
| 167 |
-
<div className="flex items-center gap-1.5 overflow-x-auto pb-1 hide-scrollbar">
|
| 168 |
-
<button onClick={() => setActiveTag(null)} className={`whitespace-nowrap px-3 py-1 rounded-full text-[11px] font-medium transition-colors ${!activeTag ? 'bg-[#0A84FF] text-white' : 'bg-[#3A3A3E] text-[#C0C0C0] hover:bg-[#4A4A4E]'}`}>All</button>
|
| 169 |
-
{allTags.map(tag => (
|
| 170 |
-
<button key={tag} onClick={() => setActiveTag(activeTag === tag ? null : tag)} className={`whitespace-nowrap px-3 py-1 rounded-full text-[11px] font-medium flex items-center gap-1 transition-colors ${activeTag === tag ? 'bg-[#0A84FF] text-white' : 'bg-[#3A3A3E] text-[#C0C0C0] hover:bg-[#4A4A4E]'}`}><Tag size={10} />{tag}</button>
|
| 171 |
-
))}
|
| 172 |
-
</div>
|
| 173 |
-
)}
|
| 174 |
</div>
|
| 175 |
|
| 176 |
-
{/* Grid */}
|
| 177 |
<div className="flex-1 overflow-y-auto bg-[#1C1C1E] p-4 custom-scrollbar">
|
| 178 |
-
<div className="flex justify-between items-center mb-4">
|
| 179 |
-
<span className="text-[11px] font-medium text-[#808080] uppercase tracking-widest">Images ({filtered.length})</span>
|
| 180 |
-
<Grid size={14} className="text-[#808080]" />
|
| 181 |
-
</div>
|
| 182 |
<div className="grid grid-cols-3 gap-3">
|
| 183 |
-
{/
|
| 184 |
-
<div onClick={() => fileInputRef.current?.click()} className="aspect-square bg-white/5 hover:bg-white/10 border border-dashed border-white/20 hover:border-[#0A84FF]/50 rounded-xl cursor-pointer flex flex-col items-center justify-center text-[#A0A0A0] hover:text-[#E0E0E0] transition-all group shadow-sm">
|
| 185 |
-
<div className="w-8 h-8 rounded-full bg-black/20 flex items-center justify-center mb-2 group-hover:scale-110 transition-transform"><Plus size={16} /></div>
|
| 186 |
-
<span className="text-[11px] font-medium">Import Files</span>
|
| 187 |
-
<span className="text-[9px] text-[#606060] mt-0.5">or drag & drop</span>
|
| 188 |
-
</div>
|
| 189 |
<input ref={fileInputRef} type="file" accept="image/png,image/jpeg,image/webp,image/gif,image/bmp,image/avif" multiple className="hidden" onChange={handleFileUpload} />
|
| 190 |
-
|
| 191 |
-
{filtered.map((img) => (
|
| 192 |
-
<div key={img.id} className="aspect-square bg-[#2A2A2E] rounded-xl cursor-pointer group relative overflow-hidden ring-1 ring-[#3A3A3E] hover:ring-[#0A84FF] transition-all"
|
| 193 |
-
draggable onDragStart={(e) => handleDragStart(e, img)} onClick={() => handleAddToCanvas(img)}
|
| 194 |
-
>
|
| 195 |
-
<img src={img.data_url || img.url} className="w-full h-full object-cover opacity-90 group-hover:opacity-100 group-hover:scale-105 transition-all duration-500" draggable={false} />
|
| 196 |
-
<div className="absolute inset-0 bg-gradient-to-t from-black/80 via-transparent to-transparent opacity-0 group-hover:opacity-100 transition-opacity flex flex-col justify-between p-2 pointer-events-none">
|
| 197 |
-
<div className="flex justify-end gap-1 pointer-events-auto">
|
| 198 |
-
<button onClick={(e) => { e.stopPropagation(); setEditingItem(img); }} className="w-5 h-5 rounded bg-black/50 flex items-center justify-center text-white/80 hover:text-white hover:bg-black/70" title="Edit metadata"><Edit3 size={10} /></button>
|
| 199 |
-
<button onClick={(e) => { e.stopPropagation(); handleDelete(img.id); }} className="w-5 h-5 rounded bg-black/50 flex items-center justify-center text-red-400 hover:text-red-300 hover:bg-red-500/20" title="Delete"><Trash2 size={10} /></button>
|
| 200 |
-
</div>
|
| 201 |
-
<div className="pointer-events-auto">
|
| 202 |
-
<span className="text-[10px] text-white font-medium truncate block">{img.title || 'Reference'}</span>
|
| 203 |
-
{img.tags?.length > 0 && <div className="flex gap-1 mt-0.5 overflow-hidden">{img.tags.slice(0, 3).map(t => <span key={t} className="text-[9px] bg-white/20 text-white/90 px-1 rounded">{t}</span>)}</div>}
|
| 204 |
-
<div className="text-[9px] text-white/50 mt-0.5">{img.width}×{img.height}</div>
|
| 205 |
-
</div>
|
| 206 |
-
</div>
|
| 207 |
-
{img.colors?.length > 0 && <div className="absolute bottom-0 left-0 right-0 h-1 flex opacity-0 group-hover:opacity-100 transition-opacity">{img.colors.slice(0, 6).map((c, ci) => <div key={ci} className="flex-1" style={{ backgroundColor: c }} />)}</div>}
|
| 208 |
-
</div>
|
| 209 |
-
))}
|
| 210 |
{filtered.length === 0 && !isLoading && <div className="col-span-3 text-center text-[#808080] py-16 text-sm flex flex-col items-center gap-3"><Folder size={32} className="opacity-20" /><p>Library is empty</p><p className="text-xs opacity-60">Drag files here, use Import, or capture from browser.</p></div>}
|
| 211 |
{isLoading && <div className="col-span-3 text-center text-[#808080] py-12"><RefreshCw size={20} className="animate-spin mx-auto mb-2" />Loading...</div>}
|
| 212 |
</div>
|
|
@@ -215,112 +114,11 @@ export const LibraryPanel = () => {
|
|
| 215 |
);
|
| 216 |
};
|
| 217 |
|
| 218 |
-
// ─── Metadata Editor Sub-panel ───────────────────────────────────────────────
|
| 219 |
function MetadataEditor({ item, onClose, onUpdate, onDelete, onAddToCanvas, isOpen, onClosePanel }: { item: LibItem; onClose: () => void; onUpdate: (item: LibItem) => void; onDelete: () => void; onAddToCanvas: () => void; isOpen: boolean; onClosePanel: () => void }) {
|
| 220 |
-
const [title, setTitle] = useState(item.title);
|
| 221 |
-
const [newTag, setNewTag] = useState('');
|
| 222 |
-
const [saving, setSaving] = useState(false);
|
| 223 |
-
|
| 224 |
useEffect(() => { setTitle(item.title); }, [item.id]);
|
| 225 |
-
|
| 226 |
-
const
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
invoke<LibItem>('library_update_metadata', { id: item.id, title: title.trim() || null, tags: null }).then(updated => { onUpdate(updated); setSaving(false); }).catch(() => setSaving(false));
|
| 230 |
-
};
|
| 231 |
-
|
| 232 |
-
const addTag = () => {
|
| 233 |
-
if (!newTag.trim()) return;
|
| 234 |
-
invoke<LibItem>('library_add_tag', { id: item.id, tag: newTag.trim() }).then(updated => { onUpdate(updated); setNewTag(''); }).catch(() => {});
|
| 235 |
-
};
|
| 236 |
-
|
| 237 |
-
const removeTag = (tag: string) => {
|
| 238 |
-
invoke<LibItem>('library_remove_tag', { id: item.id, tag }).then(updated => { onUpdate(updated); }).catch(() => {});
|
| 239 |
-
};
|
| 240 |
-
|
| 241 |
-
const copyColor = (hex: string) => { navigator.clipboard.writeText(hex); };
|
| 242 |
-
|
| 243 |
-
return (
|
| 244 |
-
<div className={`absolute left-0 top-0 h-full w-[45%] max-w-[500px] bg-[#1C1C1E] shadow-2xl flex flex-col z-[60] transform transition-transform duration-500 ease-[cubic-bezier(0.19,1,0.22,1)] ${isOpen ? 'translate-x-0' : '-translate-x-full'}`}>
|
| 245 |
-
{/* Header */}
|
| 246 |
-
<div className="flex items-center justify-between p-4 border-b border-[#3A3A3E]">
|
| 247 |
-
<button onClick={onClose} className="flex items-center gap-2 text-[#0A84FF] text-[13px] font-medium hover:text-[#5AAFFF]"><ChevronLeft size={16} /> Back to Library</button>
|
| 248 |
-
<button onClick={onClosePanel} className="text-[#A0A0A0] hover:text-[#E0E0E0] p-1.5 rounded-md hover:bg-white/5"><X size={16} /></button>
|
| 249 |
-
</div>
|
| 250 |
-
|
| 251 |
-
{/* Preview */}
|
| 252 |
-
<div className="h-[200px] bg-[#0D0D0F] flex items-center justify-center border-b border-[#3A3A3E] shrink-0">
|
| 253 |
-
<img src={item.data_url || item.url} className="max-w-full max-h-full object-contain" />
|
| 254 |
-
</div>
|
| 255 |
-
|
| 256 |
-
{/* Metadata form */}
|
| 257 |
-
<div className="flex-1 overflow-y-auto p-5 flex flex-col gap-5">
|
| 258 |
-
{/* Title */}
|
| 259 |
-
<div>
|
| 260 |
-
<label className="text-[10px] font-bold text-[#808080] uppercase tracking-widest mb-1.5 block">Title</label>
|
| 261 |
-
<input value={title} onChange={e => setTitle(e.target.value)} onBlur={saveTitle} onKeyDown={e => { if (e.key === 'Enter') saveTitle(); }} className="w-full bg-[#2A2A2E] text-[#E0E0E0] px-3 py-2 text-[13px] rounded-lg border border-[#3A3A3E] focus:border-[#0A84FF] outline-none" />
|
| 262 |
-
</div>
|
| 263 |
-
|
| 264 |
-
{/* Dimensions */}
|
| 265 |
-
<div>
|
| 266 |
-
<label className="text-[10px] font-bold text-[#808080] uppercase tracking-widest mb-1.5 block">Dimensions</label>
|
| 267 |
-
<div className="text-[13px] text-[#C0C0C0]">{item.width} × {item.height} px</div>
|
| 268 |
-
</div>
|
| 269 |
-
|
| 270 |
-
{/* Tags */}
|
| 271 |
-
<div>
|
| 272 |
-
<label className="text-[10px] font-bold text-[#808080] uppercase tracking-widest mb-1.5 block">Tags</label>
|
| 273 |
-
<div className="flex flex-wrap gap-2 mb-2">
|
| 274 |
-
{(item.tags || []).map(tag => (
|
| 275 |
-
<span key={tag} className="flex items-center gap-1 bg-[#3A3A3E] text-[#E0E0E0] px-2.5 py-1 rounded-full text-[11px] font-medium group">
|
| 276 |
-
<Tag size={10} className="text-[#0A84FF]" />{tag}
|
| 277 |
-
<button onClick={() => removeTag(tag)} className="ml-0.5 text-[#808080] hover:text-red-400 opacity-0 group-hover:opacity-100 transition-opacity"><X size={10} /></button>
|
| 278 |
-
</span>
|
| 279 |
-
))}
|
| 280 |
-
</div>
|
| 281 |
-
<div className="flex gap-2">
|
| 282 |
-
<input value={newTag} onChange={e => setNewTag(e.target.value)} onKeyDown={e => { if (e.key === 'Enter') addTag(); }} placeholder="Add tag..." className="flex-1 bg-[#2A2A2E] text-[#E0E0E0] px-3 py-1.5 text-[12px] rounded-lg border border-[#3A3A3E] focus:border-[#0A84FF] outline-none placeholder:text-[#606060]" />
|
| 283 |
-
<button onClick={addTag} disabled={!newTag.trim()} className="px-3 py-1.5 rounded-lg bg-[#0A84FF]/20 text-[#0A84FF] text-[12px] font-medium disabled:opacity-30">Add</button>
|
| 284 |
-
</div>
|
| 285 |
-
</div>
|
| 286 |
-
|
| 287 |
-
{/* Colors */}
|
| 288 |
-
{item.colors?.length > 0 && (
|
| 289 |
-
<div>
|
| 290 |
-
<label className="text-[10px] font-bold text-[#808080] uppercase tracking-widest mb-1.5 block">Extracted Palette</label>
|
| 291 |
-
<div className="flex gap-2">
|
| 292 |
-
{item.colors.map((c, i) => (
|
| 293 |
-
<button key={i} onClick={() => copyColor(c)} className="w-10 h-10 rounded-lg shadow-md hover:scale-110 transition-transform border border-white/10 relative group" style={{ backgroundColor: c }} title={`${c} — click to copy`}>
|
| 294 |
-
<Copy size={10} className="absolute top-1 right-1 text-white opacity-0 group-hover:opacity-80" />
|
| 295 |
-
</button>
|
| 296 |
-
))}
|
| 297 |
-
</div>
|
| 298 |
-
<p className="text-[9px] text-[#606060] mt-1">Click swatch to copy HEX</p>
|
| 299 |
-
</div>
|
| 300 |
-
)}
|
| 301 |
-
|
| 302 |
-
{/* Source URL */}
|
| 303 |
-
{item.source_url && (
|
| 304 |
-
<div>
|
| 305 |
-
<label className="text-[10px] font-bold text-[#808080] uppercase tracking-widest mb-1.5 block">Source</label>
|
| 306 |
-
<div className="flex items-center gap-2 text-[12px] text-[#0A84FF] truncate">
|
| 307 |
-
<ExternalLink size={12} /><span className="truncate">{item.source_url}</span>
|
| 308 |
-
</div>
|
| 309 |
-
</div>
|
| 310 |
-
)}
|
| 311 |
-
|
| 312 |
-
{/* Date */}
|
| 313 |
-
<div>
|
| 314 |
-
<label className="text-[10px] font-bold text-[#808080] uppercase tracking-widest mb-1.5 block">Added</label>
|
| 315 |
-
<div className="text-[12px] text-[#A0A0A0]">{new Date(item.created_at * 1000).toLocaleString()}</div>
|
| 316 |
-
</div>
|
| 317 |
-
</div>
|
| 318 |
-
|
| 319 |
-
{/* Actions */}
|
| 320 |
-
<div className="p-4 border-t border-[#3A3A3E] flex gap-2">
|
| 321 |
-
<button onClick={onAddToCanvas} className="flex-1 py-2.5 rounded-xl bg-[#0A84FF] text-white text-[13px] font-semibold hover:bg-[#0A84FF]/90 transition-colors">Add to Canvas</button>
|
| 322 |
-
<button onClick={onDelete} className="px-4 py-2.5 rounded-xl bg-red-500/10 text-red-400 text-[13px] font-semibold hover:bg-red-500/20 transition-colors"><Trash2 size={14} /></button>
|
| 323 |
-
</div>
|
| 324 |
-
</div>
|
| 325 |
-
);
|
| 326 |
}
|
|
|
|
| 3 |
import { useAppStore } from '../store';
|
| 4 |
import { invoke } from '@tauri-apps/api/core';
|
| 5 |
import { listen } from '@tauri-apps/api/event';
|
|
|
|
| 6 |
|
| 7 |
interface LibItem { id: string; url: string; source_url: string; title: string; data_url: string; hash: string; width: number; height: number; colors: string[]; tags: string[]; created_at: number; }
|
| 8 |
|
|
|
|
| 17 |
const [editingItem, setEditingItem] = useState<LibItem | null>(null);
|
| 18 |
const fileInputRef = useRef<HTMLInputElement>(null);
|
| 19 |
|
| 20 |
+
const rebuildTags = (items: LibItem[]) => {
|
| 21 |
+
const tags = new Set<string>();
|
| 22 |
+
items.forEach(item => (item.tags || []).forEach(t => tags.add(t)));
|
| 23 |
+
setAllTags(Array.from(tags).sort());
|
| 24 |
+
};
|
| 25 |
+
|
| 26 |
const loadLibrary = useCallback(() => {
|
| 27 |
setIsLoading(true);
|
| 28 |
+
invoke<LibItem[]>('library_items').then(items => { setLibraryItems(items); rebuildTags(items); setIsLoading(false); }).catch(() => setIsLoading(false));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
}, []);
|
| 30 |
|
| 31 |
useEffect(() => { if (isLibraryOpen) loadLibrary(); }, [isLibraryOpen, loadLibrary]);
|
| 32 |
+
useEffect(() => { const unlisten = listen<any>('board://image_added', loadLibrary); return () => { unlisten.then(fn => fn()); }; }, [loadLibrary]);
|
| 33 |
+
useEffect(() => { const handler = () => loadLibrary(); window.addEventListener('muse:library-refresh', handler); return () => window.removeEventListener('muse:library-refresh', handler); }, [loadLibrary]);
|
| 34 |
+
|
| 35 |
+
const addToCanvas = (item: LibItem) => {
|
| 36 |
+
const w = Math.min(500, item.width || 300);
|
| 37 |
+
const h = item.height ? w * (item.height / item.width) : w;
|
| 38 |
+
setImages(prev => [...prev, { id: crypto.randomUUID(), url: item.data_url || item.url, sourceUrl: item.source_url || item.url, x: (-pan.x + window.innerWidth / 3) / zoom, y: (-pan.y + window.innerHeight / 3) / zoom, width: w, height: h, aspectRatio: w / h }]);
|
| 39 |
+
};
|
| 40 |
|
| 41 |
+
const importFileAsDataUrl = (file: File) => {
|
| 42 |
+
if (!file.type.startsWith('image/')) return;
|
| 43 |
+
const reader = new FileReader();
|
| 44 |
+
reader.onload = async (ev) => {
|
| 45 |
+
const dataUrl = ev.target?.result as string;
|
| 46 |
+
if (!dataUrl) return;
|
| 47 |
+
try {
|
| 48 |
+
const item = await invoke<LibItem>('library_import_data_url', { dataUrl, title: file.name.replace(/\.[^/.]+$/, '') });
|
| 49 |
+
addToCanvas(item);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
loadLibrary();
|
| 51 |
+
} catch (err) { console.error('Import failed:', err); }
|
| 52 |
+
};
|
| 53 |
+
reader.readAsDataURL(file);
|
| 54 |
+
};
|
| 55 |
|
| 56 |
+
const handleBrowserDrop = (e: React.DragEvent) => {
|
|
|
|
| 57 |
e.preventDefault(); e.stopPropagation(); setIsDragOver(false);
|
| 58 |
+
Array.from(e.dataTransfer.files).forEach(importFileAsDataUrl);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
};
|
| 60 |
|
| 61 |
+
const handleFileUpload = (e: React.ChangeEvent<HTMLInputElement>) => {
|
| 62 |
+
if (e.target.files) Array.from(e.target.files).forEach(importFileAsDataUrl);
|
| 63 |
+
e.target.value = '';
|
|
|
|
|
|
|
| 64 |
};
|
| 65 |
|
|
|
|
| 66 |
const handleDragStart = (e: React.DragEvent, item: LibItem) => {
|
| 67 |
const payload = JSON.stringify({ id: item.id, data_url: item.data_url, width: item.width, height: item.height, title: item.title });
|
| 68 |
e.dataTransfer.setData('application/x-muse-library-item', payload);
|
|
|
|
| 70 |
e.dataTransfer.effectAllowed = 'copy';
|
| 71 |
};
|
| 72 |
|
| 73 |
+
const handleDelete = (id: string) => invoke('library_remove_item', { id }).then(() => { const next = libraryItems.filter(i => i.id !== id); setLibraryItems(next); rebuildTags(next); if (editingItem?.id === id) setEditingItem(null); });
|
| 74 |
+
|
| 75 |
+
const updateItem = (updated: LibItem) => {
|
| 76 |
+
const next = libraryItems.map(i => i.id === updated.id ? updated : i);
|
| 77 |
+
setLibraryItems(next); rebuildTags(next); setEditingItem(updated);
|
| 78 |
+
};
|
| 79 |
|
| 80 |
const filtered = libraryItems.filter(img => {
|
| 81 |
if (activeTag && !(img.tags || []).includes(activeTag)) return false;
|
| 82 |
+
if (!search) return true;
|
| 83 |
+
const q = search.toLowerCase();
|
| 84 |
+
return (img.tags || []).some(t => t.toLowerCase().includes(q)) || (img.title || '').toLowerCase().includes(q) || (img.source_url || img.url || '').toLowerCase().includes(q);
|
| 85 |
});
|
| 86 |
|
| 87 |
+
if (editingItem) return <MetadataEditor item={editingItem} onClose={() => setEditingItem(null)} onUpdate={updateItem} onDelete={() => { handleDelete(editingItem.id); setEditingItem(null); }} onAddToCanvas={() => addToCanvas(editingItem)} isOpen={isLibraryOpen} onClosePanel={() => setIsLibraryOpen(false)} />;
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
return (
|
| 90 |
+
<div className={`absolute left-0 top-0 h-full w-[45%] max-w-[500px] bg-[#1C1C1E] shadow-2xl flex flex-col z-[60] transform transition-transform duration-500 ease-[cubic-bezier(0.19,1,0.22,1)] ${isLibraryOpen ? 'translate-x-0' : '-translate-x-full'}`} onDragOver={(e) => { e.preventDefault(); e.dataTransfer.dropEffect = 'copy'; setIsDragOver(true); }} onDragLeave={() => setIsDragOver(false)} onDrop={handleBrowserDrop}>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
{isDragOver && <div className="absolute inset-0 z-50 bg-[#0A84FF]/10 border-2 border-dashed border-[#0A84FF] rounded-xl m-3 flex items-center justify-center pointer-events-none"><div className="text-[#0A84FF] font-semibold text-sm flex flex-col items-center gap-2"><Upload size={32} />Drop images to import</div></div>}
|
| 92 |
|
|
|
|
| 93 |
<div className="flex items-center justify-between p-4 border-b border-[#3A3A3E]">
|
| 94 |
+
<div className="flex items-center gap-2 text-[#E0E0E0] text-[14px] font-medium"><Folder size={16} className="text-[#0A84FF]" /> Asset Library <span className="text-[11px] text-[#808080] ml-1">({libraryItems.length})</span></div>
|
| 95 |
+
<div className="flex items-center gap-1"><button onClick={loadLibrary} className="text-[#A0A0A0] hover:text-[#E0E0E0] p-1.5 rounded-md hover:bg-white/5" title="Refresh"><RefreshCw size={14} className={isLoading ? 'animate-spin' : ''} /></button><button onClick={() => setIsLibraryOpen(false)} className="text-[#A0A0A0] hover:text-[#E0E0E0] p-1.5 rounded-md hover:bg-white/5"><X size={16} /></button></div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
</div>
|
| 97 |
|
|
|
|
| 98 |
<div className="px-4 py-3 bg-[#2A2A2E] flex flex-col gap-3">
|
| 99 |
+
<div className="relative"><Search size={14} className="absolute left-3 top-1/2 -translate-y-1/2 text-[#808080]" /><input type="text" value={search} onChange={e => setSearch(e.target.value)} placeholder="Search by name, tag, or URL..." className="w-full bg-[#1C1C1E] text-[#E0E0E0] pl-9 pr-3 py-2 text-[13px] rounded-lg border border-[#3A3A3E] focus:border-[#0A84FF] outline-none placeholder:text-[#606060]" /></div>
|
| 100 |
+
{allTags.length > 0 && <div className="flex items-center gap-1.5 overflow-x-auto pb-1 hide-scrollbar"><button onClick={() => setActiveTag(null)} className={`whitespace-nowrap px-3 py-1 rounded-full text-[11px] font-medium transition-colors ${!activeTag ? 'bg-[#0A84FF] text-white' : 'bg-[#3A3A3E] text-[#C0C0C0] hover:bg-[#4A4A4E]'}`}>All</button>{allTags.map(tag => <button key={tag} onClick={() => setActiveTag(activeTag === tag ? null : tag)} className={`whitespace-nowrap px-3 py-1 rounded-full text-[11px] font-medium flex items-center gap-1 transition-colors ${activeTag === tag ? 'bg-[#0A84FF] text-white' : 'bg-[#3A3A3E] text-[#C0C0C0] hover:bg-[#4A4A4E]'}`}><Tag size={10} />{tag}</button>)}</div>}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
</div>
|
| 102 |
|
|
|
|
| 103 |
<div className="flex-1 overflow-y-auto bg-[#1C1C1E] p-4 custom-scrollbar">
|
| 104 |
+
<div className="flex justify-between items-center mb-4"><span className="text-[11px] font-medium text-[#808080] uppercase tracking-widest">Images ({filtered.length})</span><Grid size={14} className="text-[#808080]" /></div>
|
|
|
|
|
|
|
|
|
|
| 105 |
<div className="grid grid-cols-3 gap-3">
|
| 106 |
+
<div onClick={() => fileInputRef.current?.click()} className="aspect-square bg-white/5 hover:bg-white/10 border border-dashed border-white/20 hover:border-[#0A84FF]/50 rounded-xl cursor-pointer flex flex-col items-center justify-center text-[#A0A0A0] hover:text-[#E0E0E0] transition-all group shadow-sm"><div className="w-8 h-8 rounded-full bg-black/20 flex items-center justify-center mb-2 group-hover:scale-110 transition-transform"><Plus size={16} /></div><span className="text-[11px] font-medium">Import Files</span><span className="text-[9px] text-[#606060] mt-0.5">or drag here</span></div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
<input ref={fileInputRef} type="file" accept="image/png,image/jpeg,image/webp,image/gif,image/bmp,image/avif" multiple className="hidden" onChange={handleFileUpload} />
|
| 108 |
+
{filtered.map(img => <div key={img.id} className="aspect-square bg-[#2A2A2E] rounded-xl cursor-pointer group relative overflow-hidden ring-1 ring-[#3A3A3E] hover:ring-[#0A84FF] transition-all" draggable onDragStart={(e) => handleDragStart(e, img)} onClick={() => addToCanvas(img)}><img src={img.data_url || img.url} className="w-full h-full object-cover opacity-90 group-hover:opacity-100 group-hover:scale-105 transition-all duration-500" draggable={false} /><div className="absolute inset-0 bg-gradient-to-t from-black/80 via-transparent to-transparent opacity-0 group-hover:opacity-100 transition-opacity flex flex-col justify-between p-2 pointer-events-none"><div className="flex justify-end gap-1 pointer-events-auto"><button onClick={(e) => { e.stopPropagation(); setEditingItem(img); }} className="w-5 h-5 rounded bg-black/50 flex items-center justify-center text-white/80 hover:text-white hover:bg-black/70" title="Edit metadata"><Edit3 size={10} /></button><button onClick={(e) => { e.stopPropagation(); handleDelete(img.id); }} className="w-5 h-5 rounded bg-black/50 flex items-center justify-center text-red-400 hover:text-red-300 hover:bg-red-500/20" title="Delete"><Trash2 size={10} /></button></div><div className="pointer-events-auto"><span className="text-[10px] text-white font-medium truncate block">{img.title || 'Reference'}</span>{img.tags?.length > 0 && <div className="flex gap-1 mt-0.5 overflow-hidden">{img.tags.slice(0, 3).map(t => <span key={t} className="text-[9px] bg-white/20 text-white/90 px-1 rounded">{t}</span>)}</div>}<div className="text-[9px] text-white/50 mt-0.5">{img.width}×{img.height}</div></div></div>{img.colors?.length > 0 && <div className="absolute bottom-0 left-0 right-0 h-1 flex opacity-0 group-hover:opacity-100 transition-opacity">{img.colors.slice(0, 6).map((c, ci) => <div key={ci} className="flex-1" style={{ backgroundColor: c }} />)}</div>}</div>)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
{filtered.length === 0 && !isLoading && <div className="col-span-3 text-center text-[#808080] py-16 text-sm flex flex-col items-center gap-3"><Folder size={32} className="opacity-20" /><p>Library is empty</p><p className="text-xs opacity-60">Drag files here, use Import, or capture from browser.</p></div>}
|
| 110 |
{isLoading && <div className="col-span-3 text-center text-[#808080] py-12"><RefreshCw size={20} className="animate-spin mx-auto mb-2" />Loading...</div>}
|
| 111 |
</div>
|
|
|
|
| 114 |
);
|
| 115 |
};
|
| 116 |
|
|
|
|
| 117 |
function MetadataEditor({ item, onClose, onUpdate, onDelete, onAddToCanvas, isOpen, onClosePanel }: { item: LibItem; onClose: () => void; onUpdate: (item: LibItem) => void; onDelete: () => void; onAddToCanvas: () => void; isOpen: boolean; onClosePanel: () => void }) {
|
| 118 |
+
const [title, setTitle] = useState(item.title); const [newTag, setNewTag] = useState('');
|
|
|
|
|
|
|
|
|
|
| 119 |
useEffect(() => { setTitle(item.title); }, [item.id]);
|
| 120 |
+
const saveTitle = () => { if (title.trim() === item.title) return; invoke<LibItem>('library_update_metadata', { id: item.id, title: title.trim() || null, tags: null }).then(onUpdate).catch(() => {}); };
|
| 121 |
+
const addTag = () => { if (!newTag.trim()) return; invoke<LibItem>('library_add_tag', { id: item.id, tag: newTag.trim() }).then(updated => { onUpdate(updated); setNewTag(''); }).catch(() => {}); };
|
| 122 |
+
const removeTag = (tag: string) => invoke<LibItem>('library_remove_tag', { id: item.id, tag }).then(onUpdate).catch(() => {});
|
| 123 |
+
return <div className={`absolute left-0 top-0 h-full w-[45%] max-w-[500px] bg-[#1C1C1E] shadow-2xl flex flex-col z-[60] transform transition-transform duration-500 ease-[cubic-bezier(0.19,1,0.22,1)] ${isOpen ? 'translate-x-0' : '-translate-x-full'}`}><div className="flex items-center justify-between p-4 border-b border-[#3A3A3E]"><button onClick={onClose} className="flex items-center gap-2 text-[#0A84FF] text-[13px] font-medium"><ChevronLeft size={16} /> Back</button><button onClick={onClosePanel} className="text-[#A0A0A0] p-1.5"><X size={16} /></button></div><div className="h-[200px] bg-[#0D0D0F] flex items-center justify-center border-b border-[#3A3A3E]"><img src={item.data_url || item.url} className="max-w-full max-h-full object-contain" /></div><div className="flex-1 overflow-y-auto p-5 flex flex-col gap-5"><div><label className="text-[10px] font-bold text-[#808080] uppercase tracking-widest mb-1.5 block">Title</label><input value={title} onChange={e => setTitle(e.target.value)} onBlur={saveTitle} onKeyDown={e => { if (e.key === 'Enter') saveTitle(); }} className="w-full bg-[#2A2A2E] text-[#E0E0E0] px-3 py-2 text-[13px] rounded-lg border border-[#3A3A3E] focus:border-[#0A84FF] outline-none" /></div><div><label className="text-[10px] font-bold text-[#808080] uppercase tracking-widest mb-1.5 block">Dimensions</label><div className="text-[13px] text-[#C0C0C0]">{item.width} × {item.height} px</div></div><div><label className="text-[10px] font-bold text-[#808080] uppercase tracking-widest mb-1.5 block">Tags</label><div className="flex flex-wrap gap-2 mb-2">{(item.tags || []).map(tag => <span key={tag} className="flex items-center gap-1 bg-[#3A3A3E] text-[#E0E0E0] px-2.5 py-1 rounded-full text-[11px] font-medium group"><Tag size={10} className="text-[#0A84FF]" />{tag}<button onClick={() => removeTag(tag)} className="ml-0.5 text-[#808080] hover:text-red-400"><X size={10} /></button></span>)}</div><div className="flex gap-2"><input value={newTag} onChange={e => setNewTag(e.target.value)} onKeyDown={e => { if (e.key === 'Enter') addTag(); }} placeholder="Add tag..." className="flex-1 bg-[#2A2A2E] text-[#E0E0E0] px-3 py-1.5 text-[12px] rounded-lg border border-[#3A3A3E] focus:border-[#0A84FF] outline-none" /><button onClick={addTag} disabled={!newTag.trim()} className="px-3 py-1.5 rounded-lg bg-[#0A84FF]/20 text-[#0A84FF] text-[12px] font-medium disabled:opacity-30">Add</button></div></div>{item.colors?.length > 0 && <div><label className="text-[10px] font-bold text-[#808080] uppercase tracking-widest mb-1.5 block">Palette</label><div className="flex gap-2">{item.colors.map((c, i) => <button key={i} onClick={() => navigator.clipboard.writeText(c)} className="w-10 h-10 rounded-lg shadow-md hover:scale-110 transition-transform border border-white/10 relative group" style={{ backgroundColor: c }} title={c}><Copy size={10} className="absolute top-1 right-1 text-white opacity-0 group-hover:opacity-80" /></button>)}</div></div>}{item.source_url && <div><label className="text-[10px] font-bold text-[#808080] uppercase tracking-widest mb-1.5 block">Source</label><div className="flex items-center gap-2 text-[12px] text-[#0A84FF] truncate"><ExternalLink size={12} /><span className="truncate">{item.source_url}</span></div></div>}</div><div className="p-4 border-t border-[#3A3A3E] flex gap-2"><button onClick={onAddToCanvas} className="flex-1 py-2.5 rounded-xl bg-[#0A84FF] text-white text-[13px] font-semibold">Add to Canvas</button><button onClick={onDelete} className="px-4 py-2.5 rounded-xl bg-red-500/10 text-red-400 text-[13px] font-semibold"><Trash2 size={14} /></button></div></div>;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
}
|