mirror of
https://github.com/willmiao/ComfyUI-Lora-Manager.git
synced 2026-03-21 21:22:11 -03:00
Add keyboard navigation support and related styles for enhanced user experience
This commit is contained in:
@@ -804,4 +804,131 @@ export class VirtualScroller {
|
||||
console.log(`Removed item with file path ${filePath} from virtual scroller data`);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Add keyboard navigation methods
|
||||
handlePageUpDown(direction) {
|
||||
// Prevent duplicate animations by checking last trigger time
|
||||
const now = Date.now();
|
||||
if (this.lastPageNavTime && now - this.lastPageNavTime < 300) {
|
||||
return; // Ignore rapid repeated triggers
|
||||
}
|
||||
this.lastPageNavTime = now;
|
||||
|
||||
const scrollContainer = this.scrollContainer;
|
||||
const viewportHeight = scrollContainer.clientHeight;
|
||||
|
||||
// Calculate scroll distance (one viewport minus 10% overlap for context)
|
||||
const scrollDistance = viewportHeight * 0.9;
|
||||
|
||||
// Determine the new scroll position
|
||||
const newScrollTop = scrollContainer.scrollTop + (direction === 'down' ? scrollDistance : -scrollDistance);
|
||||
|
||||
// Remove any existing transition indicators
|
||||
this.removeExistingTransitionIndicator();
|
||||
|
||||
// Scroll to the new position with smooth animation
|
||||
scrollContainer.scrollTo({
|
||||
top: newScrollTop,
|
||||
behavior: 'smooth'
|
||||
});
|
||||
|
||||
// Page transition indicator removed
|
||||
// this.showTransitionIndicator();
|
||||
|
||||
// Force render after scrolling
|
||||
setTimeout(() => this.renderItems(), 100);
|
||||
setTimeout(() => this.renderItems(), 300);
|
||||
}
|
||||
|
||||
// Helper to remove existing indicators
|
||||
removeExistingTransitionIndicator() {
|
||||
const existingIndicator = document.querySelector('.page-transition-indicator');
|
||||
if (existingIndicator) {
|
||||
existingIndicator.remove();
|
||||
}
|
||||
}
|
||||
|
||||
// Create a more contained transition indicator - commented out as it's no longer needed
|
||||
/*
|
||||
showTransitionIndicator() {
|
||||
const container = this.containerElement;
|
||||
const indicator = document.createElement('div');
|
||||
indicator.className = 'page-transition-indicator';
|
||||
|
||||
// Get container position to properly position the indicator
|
||||
const containerRect = container.getBoundingClientRect();
|
||||
|
||||
// Style the indicator to match just the container area
|
||||
indicator.style.position = 'fixed';
|
||||
indicator.style.top = `${containerRect.top}px`;
|
||||
indicator.style.left = `${containerRect.left}px`;
|
||||
indicator.style.width = `${containerRect.width}px`;
|
||||
indicator.style.height = `${containerRect.height}px`;
|
||||
|
||||
document.body.appendChild(indicator);
|
||||
|
||||
// Remove after animation completes
|
||||
setTimeout(() => {
|
||||
if (indicator.parentNode) {
|
||||
indicator.remove();
|
||||
}
|
||||
}, 500);
|
||||
}
|
||||
*/
|
||||
|
||||
scrollToTop() {
|
||||
this.removeExistingTransitionIndicator();
|
||||
|
||||
// Page transition indicator removed
|
||||
// this.showTransitionIndicator();
|
||||
|
||||
this.scrollContainer.scrollTo({
|
||||
top: 0,
|
||||
behavior: 'smooth'
|
||||
});
|
||||
|
||||
// Force render after scrolling
|
||||
setTimeout(() => this.renderItems(), 100);
|
||||
}
|
||||
|
||||
scrollToBottom() {
|
||||
this.removeExistingTransitionIndicator();
|
||||
|
||||
// Page transition indicator removed
|
||||
// this.showTransitionIndicator();
|
||||
|
||||
// Start loading all remaining pages to ensure content is available
|
||||
this.loadRemainingPages().then(() => {
|
||||
// After loading all content, scroll to the very bottom
|
||||
const maxScroll = this.scrollContainer.scrollHeight - this.scrollContainer.clientHeight;
|
||||
this.scrollContainer.scrollTo({
|
||||
top: maxScroll,
|
||||
behavior: 'smooth'
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// New method to load all remaining pages
|
||||
async loadRemainingPages() {
|
||||
// If we're already at the end or loading, don't proceed
|
||||
if (!this.hasMore || this.isLoading) return;
|
||||
|
||||
console.log('Loading all remaining pages for End key navigation...');
|
||||
|
||||
// Keep loading pages until we reach the end
|
||||
while (this.hasMore && !this.isLoading) {
|
||||
await this.loadMoreItems();
|
||||
|
||||
// Force render after each page load
|
||||
this.renderItems();
|
||||
|
||||
// Small delay to prevent overwhelming the browser
|
||||
await new Promise(resolve => setTimeout(resolve, 50));
|
||||
}
|
||||
|
||||
console.log('Finished loading all pages');
|
||||
|
||||
// Final render to ensure all content is displayed
|
||||
this.renderItems();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,6 +131,9 @@ async function initializeVirtualScroll(pageType) {
|
||||
// Add grid class for CSS styling
|
||||
grid.classList.add('virtual-scroll');
|
||||
|
||||
// Setup keyboard navigation
|
||||
setupKeyboardNavigation();
|
||||
|
||||
} catch (error) {
|
||||
console.error(`Error initializing virtual scroller for ${pageType}:`, error);
|
||||
showToast(`Failed to initialize ${pageType} page. Please reload.`, 'error');
|
||||
@@ -145,6 +148,61 @@ async function initializeVirtualScroll(pageType) {
|
||||
}
|
||||
}
|
||||
|
||||
// Add keyboard navigation setup function
|
||||
function setupKeyboardNavigation() {
|
||||
// Keep track of the last keypress time to prevent multiple rapid triggers
|
||||
let lastKeyTime = 0;
|
||||
const keyDelay = 300; // ms between allowed keypresses
|
||||
|
||||
// Store the event listener reference so we can remove it later if needed
|
||||
const keyboardNavHandler = (event) => {
|
||||
// Only handle keyboard events when not in form elements
|
||||
if (event.target.matches('input, textarea, select')) return;
|
||||
|
||||
// Prevent rapid keypresses
|
||||
const now = Date.now();
|
||||
if (now - lastKeyTime < keyDelay) return;
|
||||
lastKeyTime = now;
|
||||
|
||||
// Handle navigation keys
|
||||
if (event.key === 'PageUp') {
|
||||
event.preventDefault();
|
||||
if (state.virtualScroller) {
|
||||
state.virtualScroller.handlePageUpDown('up');
|
||||
}
|
||||
} else if (event.key === 'PageDown') {
|
||||
event.preventDefault();
|
||||
if (state.virtualScroller) {
|
||||
state.virtualScroller.handlePageUpDown('down');
|
||||
}
|
||||
} else if (event.key === 'Home') {
|
||||
event.preventDefault();
|
||||
if (state.virtualScroller) {
|
||||
state.virtualScroller.scrollToTop();
|
||||
}
|
||||
} else if (event.key === 'End') {
|
||||
event.preventDefault();
|
||||
if (state.virtualScroller) {
|
||||
state.virtualScroller.scrollToBottom();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Add the event listener
|
||||
document.addEventListener('keydown', keyboardNavHandler);
|
||||
|
||||
// Store the handler in state for potential cleanup
|
||||
state.keyboardNavHandler = keyboardNavHandler;
|
||||
}
|
||||
|
||||
// Add cleanup function to remove keyboard navigation when needed
|
||||
export function cleanupKeyboardNavigation() {
|
||||
if (state.keyboardNavHandler) {
|
||||
document.removeEventListener('keydown', state.keyboardNavHandler);
|
||||
state.keyboardNavHandler = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Export a method to refresh the virtual scroller when filters change
|
||||
export function refreshVirtualScroll() {
|
||||
if (state.virtualScroller) {
|
||||
|
||||
Reference in New Issue
Block a user