Fix bulk multi select

This commit is contained in:
Will Miao
2025-03-07 14:00:15 +08:00
parent 95dfcee90c
commit 59716ce3c3
3 changed files with 49 additions and 24 deletions

View File

@@ -5,6 +5,7 @@
gap: 12px; /* Reduced from var(--space-2) for tighter horizontal spacing */ gap: 12px; /* Reduced from var(--space-2) for tighter horizontal spacing */
margin-top: var(--space-2); margin-top: var(--space-2);
padding-top: 4px; /* 添加顶部内边距,为悬停动画提供空间 */ padding-top: 4px; /* 添加顶部内边距,为悬停动画提供空间 */
padding-bottom: 4px; /* 添加底部内边距,为悬停动画提供空间 */
max-width: 1400px; /* Container width control */ max-width: 1400px; /* Container width control */
margin-left: auto; margin-left: auto;
margin-right: auto; margin-right: auto;

View File

@@ -1,6 +1,7 @@
import { showToast } from '../utils/uiHelpers.js'; import { showToast } from '../utils/uiHelpers.js';
import { state } from '../state/index.js'; import { state } from '../state/index.js';
import { showLoraModal } from './LoraModal.js'; import { showLoraModal } from './LoraModal.js';
import { bulkManager } from '../managers/BulkManager.js';
export function createLoraCard(lora) { export function createLoraCard(lora) {
const card = document.createElement('div'); const card = document.createElement('div');
@@ -18,6 +19,11 @@ export function createLoraCard(lora) {
card.dataset.notes = lora.notes; card.dataset.notes = lora.notes;
card.dataset.meta = JSON.stringify(lora.civitai || {}); card.dataset.meta = JSON.stringify(lora.civitai || {});
// Apply selection state if in bulk mode and this card is in the selected set
if (state.bulkMode && state.selectedLoras.has(lora.file_path)) {
card.classList.add('selected');
}
const version = state.previewVersions.get(lora.file_path); const version = state.previewVersions.get(lora.file_path);
const previewUrl = lora.preview_url || '/loras_static/images/no-preview.png'; const previewUrl = lora.preview_url || '/loras_static/images/no-preview.png';
const versionedPreviewUrl = version ? `${previewUrl}?t=${version}` : previewUrl; const versionedPreviewUrl = version ? `${previewUrl}?t=${version}` : previewUrl;
@@ -64,8 +70,8 @@ export function createLoraCard(lora) {
card.addEventListener('click', () => { card.addEventListener('click', () => {
// Check if we're in bulk mode // Check if we're in bulk mode
if (state.bulkMode) { if (state.bulkMode) {
// Toggle selection // Toggle selection using the bulk manager
toggleCardSelection(card); bulkManager.toggleCardSelection(card);
} else { } else {
// Normal behavior - show modal // Normal behavior - show modal
const loraMeta = { const loraMeta = {
@@ -146,12 +152,6 @@ export function createLoraCard(lora) {
return card; return card;
} }
// Function to toggle selection of a card
function toggleCardSelection(card) {
card.classList.toggle('selected');
updateSelectedCount();
}
// Add a method to update card appearance based on bulk mode // Add a method to update card appearance based on bulk mode
export function updateCardsForBulkMode(isBulkMode) { export function updateCardsForBulkMode(isBulkMode) {
// Update the state // Update the state
@@ -165,4 +165,9 @@ export function updateCardsForBulkMode(isBulkMode) {
actionGroup.style.display = isBulkMode ? 'none' : 'flex'; actionGroup.style.display = isBulkMode ? 'none' : 'flex';
}); });
}); });
// Apply selection state to cards if entering bulk mode
if (isBulkMode) {
bulkManager.applySelectionState();
}
} }

View File

@@ -59,42 +59,61 @@ export class BulkManager {
} }
updateSelectedCount() { updateSelectedCount() {
const selectedCards = document.querySelectorAll('.lora-card.selected');
const countElement = document.getElementById('selectedCount'); const countElement = document.getElementById('selectedCount');
if (countElement) { if (countElement) {
countElement.textContent = `${selectedCards.length} selected`; countElement.textContent = `${state.selectedLoras.size} selected`;
} }
// Update state with selected loras
state.selectedLoras.clear();
selectedCards.forEach(card => {
state.selectedLoras.add(card.dataset.filepath);
});
} }
toggleCardSelection(card) { toggleCardSelection(card) {
card.classList.toggle('selected'); const filepath = card.dataset.filepath;
if (card.classList.contains('selected')) {
card.classList.remove('selected');
state.selectedLoras.delete(filepath);
} else {
card.classList.add('selected');
state.selectedLoras.add(filepath);
}
this.updateSelectedCount();
}
// Apply selection state to cards after they are refreshed
applySelectionState() {
if (!state.bulkMode) return;
document.querySelectorAll('.lora-card').forEach(card => {
const filepath = card.dataset.filepath;
if (state.selectedLoras.has(filepath)) {
card.classList.add('selected');
} else {
card.classList.remove('selected');
}
});
this.updateSelectedCount(); this.updateSelectedCount();
} }
async copyAllLorasSyntax() { async copyAllLorasSyntax() {
const selectedCards = document.querySelectorAll('.lora-card.selected'); if (state.selectedLoras.size === 0) {
if (selectedCards.length === 0) {
showToast('No LoRAs selected', 'warning'); showToast('No LoRAs selected', 'warning');
return; return;
} }
const loraSyntaxes = []; const loraSyntaxes = [];
selectedCards.forEach(card => { document.querySelectorAll('.lora-card').forEach(card => {
if (state.selectedLoras.has(card.dataset.filepath)) {
const usageTips = JSON.parse(card.dataset.usage_tips || '{}'); const usageTips = JSON.parse(card.dataset.usage_tips || '{}');
const strength = usageTips.strength || 1; const strength = usageTips.strength || 1;
loraSyntaxes.push(`<lora:${card.dataset.file_name}:${strength}>`); loraSyntaxes.push(`<lora:${card.dataset.file_name}:${strength}>`);
}
}); });
try { try {
await navigator.clipboard.writeText(loraSyntaxes.join(', ')); await navigator.clipboard.writeText(loraSyntaxes.join(', '));
showToast(`Copied ${selectedCards.length} LoRA syntaxes to clipboard`, 'success'); showToast(`Copied ${state.selectedLoras.size} LoRA syntaxes to clipboard`, 'success');
} catch (err) { } catch (err) {
console.error('Copy failed:', err); console.error('Copy failed:', err);
showToast('Copy failed', 'error'); showToast('Copy failed', 'error');