From 80b860e98a04aef51fd0d4574a9a18b4251776ce Mon Sep 17 00:00:00 2001 From: Will Miao <13051207myq@gmail.com> Date: Tue, 18 Feb 2025 20:08:36 +0800 Subject: [PATCH] Add refresh metadata functionality to context menu and improve item state handling --- routes/api_routes.py | 4 +--- static/css/components/menu.css | 10 +++++++++ static/js/api/loraApi.js | 29 ++++++++++++++++++++++++++ static/js/components/ContextMenu.js | 22 +++++++++++++++++-- templates/components/context_menu.html | 3 +++ 5 files changed, 63 insertions(+), 5 deletions(-) diff --git a/routes/api_routes.py b/routes/api_routes.py index e8fc9bb0..686743ad 100644 --- a/routes/api_routes.py +++ b/routes/api_routes.py @@ -71,11 +71,9 @@ class ApiRoutes: # Check if model is from CivitAI local_metadata = await self._load_local_metadata(metadata_path) - if not local_metadata.get('from_civitai', True): - return web.json_response({"success": True, "notice": "Not from CivitAI"}) # Fetch and update metadata - civitai_metadata = await self.civitai_client.get_model_by_hash(data["sha256"]) + civitai_metadata = await self.civitai_client.get_model_by_hash(local_metadata["sha256"]) if not civitai_metadata: return await self._handle_not_found_on_civitai(metadata_path, local_metadata) diff --git a/static/css/components/menu.css b/static/css/components/menu.css index 819cfe72..e53399ba 100644 --- a/static/css/components/menu.css +++ b/static/css/components/menu.css @@ -26,6 +26,16 @@ color: var(--lora-text); } +.context-menu-item.disabled { + opacity: 0.5; + cursor: not-allowed; +} + +.context-menu-item.disabled:hover { + background: var(--lora-surface); + color: var(--text-color); +} + .context-menu-separator { height: 1px; background-color: var(--border-color); diff --git a/static/js/api/loraApi.js b/static/js/api/loraApi.js index 521e963e..0dad5659 100644 --- a/static/js/api/loraApi.js +++ b/static/js/api/loraApi.js @@ -286,4 +286,33 @@ export async function refreshLoras() { state.loadingManager.hide(); state.loadingManager.restoreProgressBar(); } +} + +export async function refreshSingleLoraMetadata(filePath) { + try { + const response = await fetch('/api/fetch-civitai', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ file_path: filePath }) + }); + + if (!response.ok) { + throw new Error('Failed to refresh metadata'); + } + + const data = await response.json(); + + if (data.success) { + showToast('Metadata refreshed successfully', 'success'); + // Reload the current view to show updated data + await resetAndReload(); + } else { + throw new Error(data.error || 'Failed to refresh metadata'); + } + } catch (error) { + console.error('Error refreshing metadata:', error); + showToast(error.message, 'error'); + } } \ No newline at end of file diff --git a/static/js/components/ContextMenu.js b/static/js/components/ContextMenu.js index af7961fd..4cbb9c01 100644 --- a/static/js/components/ContextMenu.js +++ b/static/js/components/ContextMenu.js @@ -1,3 +1,5 @@ +import { refreshSingleLoraMetadata } from '../api/loraApi.js'; + export class LoraContextMenu { constructor() { this.menu = document.getElementById('loraContextMenu'); @@ -18,8 +20,16 @@ export class LoraContextMenu { }); this.menu.addEventListener('click', (e) => { - const action = e.target.closest('.context-menu-item')?.dataset.action; - if (!action || !this.currentCard) return; + const menuItem = e.target.closest('.context-menu-item'); + if (!menuItem || !this.currentCard) return; + + // Don't process disabled items + if (menuItem.classList.contains('disabled')) { + return; + } + + const action = menuItem.dataset.action; + if (!action) return; switch(action) { case 'detail': @@ -44,6 +54,9 @@ export class LoraContextMenu { case 'move': moveManager.showMoveModal(this.currentCard.dataset.filepath); break; + case 'refresh-metadata': + refreshSingleLoraMetadata(this.currentCard.dataset.filepath); + break; } this.hideMenu(); @@ -54,6 +67,11 @@ export class LoraContextMenu { this.currentCard = card; this.menu.style.display = 'block'; + // Update civitai menu item state + const civitaiItem = this.menu.querySelector('[data-action="civitai"]'); + const fromCivitai = card.dataset.from_civitai === 'true'; + civitaiItem.classList.toggle('disabled', !fromCivitai); + // 获取菜单尺寸 const menuRect = this.menu.getBoundingClientRect(); diff --git a/templates/components/context_menu.html b/templates/components/context_menu.html index 43b2451a..8f4c1daa 100644 --- a/templates/components/context_menu.html +++ b/templates/components/context_menu.html @@ -5,6 +5,9 @@
View on Civitai
+
+ Refresh Civitai Data +
Copy Model Name