This commit is contained in:
2026-07-18 16:15:47 +02:00
parent 3ff1edf8bf
commit 4dc7f10ae2

View File

@@ -11301,43 +11301,62 @@ document.addEventListener('keydown', (e) => {
const active = document.activeElement; const active = document.activeElement;
if (isArrow && active && active.classList.contains('thumb')) { if (isArrow && active && active.classList.contains('thumb')) {
const currentIndex = thumbs.indexOf(active); const activeRect = active.getBoundingClientRect();
if (currentIndex === -1) return; const activeCenter = {
x: activeRect.left + activeRect.width / 2,
y: activeRect.top + activeRect.height / 2
};
let nextIndex = currentIndex; let bestMatch = null;
let minDistance = Infinity;
thumbs.forEach(thumb => {
if (thumb === active) return;
const rect = thumb.getBoundingClientRect();
if (rect.width === 0 && rect.height === 0) return;
const center = {
x: rect.left + rect.width / 2,
y: rect.top + rect.height / 2
};
let isCandidate = false;
let primaryDist = 0;
let secondaryDist = 0;
if (e.key === 'ArrowRight') { if (e.key === 'ArrowRight') {
nextIndex = currentIndex + 1; isCandidate = center.x > activeCenter.x;
primaryDist = center.x - activeCenter.x;
secondaryDist = Math.abs(center.y - activeCenter.y);
} else if (e.key === 'ArrowLeft') { } else if (e.key === 'ArrowLeft') {
nextIndex = currentIndex - 1; isCandidate = center.x < activeCenter.x;
} else if (e.key === 'ArrowDown' || e.key === 'ArrowUp') { primaryDist = activeCenter.x - center.x;
const currentRow = thumbs.filter(t => t.offsetTop === active.offsetTop); secondaryDist = Math.abs(center.y - activeCenter.y);
const columns = currentRow.length; } else if (e.key === 'ArrowDown') {
isCandidate = center.y > activeCenter.y;
let maxCols = columns; primaryDist = center.y - activeCenter.y;
const firstRowTop = thumbs[0].offsetTop; secondaryDist = Math.abs(center.x - activeCenter.x);
const firstRow = thumbs.filter(t => t.offsetTop === firstRowTop); } else if (e.key === 'ArrowUp') {
if (firstRow.length > columns) maxCols = firstRow.length; isCandidate = center.y < activeCenter.y;
primaryDist = activeCenter.y - center.y;
if (e.key === 'ArrowDown') { secondaryDist = Math.abs(center.x - activeCenter.x);
nextIndex = currentIndex + maxCols;
if (nextIndex >= thumbs.length) {
const lastRowTop = thumbs[thumbs.length - 1].offsetTop;
if (active.offsetTop < lastRowTop) {
nextIndex = thumbs.length - 1;
} else {
nextIndex = currentIndex;
}
}
} else {
nextIndex = currentIndex - maxCols;
}
} }
if (nextIndex >= 0 && nextIndex < thumbs.length && nextIndex !== currentIndex) { if (isCandidate) {
// Weight secondary distance heavily to prioritize items directly in the travel path
const distance = primaryDist + (secondaryDist * 5);
if (distance < minDistance) {
minDistance = distance;
bestMatch = thumb;
}
}
});
if (bestMatch) {
e.preventDefault(); e.preventDefault();
thumbs[nextIndex].focus(); bestMatch.focus();
thumbs[nextIndex].scrollIntoView({ behavior: 'smooth', block: 'nearest' }); bestMatch.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
} }
} }
}); });