From 8adabfe4c8a64a2a0bcac496046f49413d935c9d Mon Sep 17 00:00:00 2001 From: flbraun Date: Sat, 11 Nov 2023 22:32:22 +0100 Subject: [PATCH] When navigating the result list, wrap around when reaching the top/bottom --- src/renderer/palette.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/renderer/palette.js b/src/renderer/palette.js index b92ddd8..1049764 100644 --- a/src/renderer/palette.js +++ b/src/renderer/palette.js @@ -57,22 +57,29 @@ const makePalette = (searchInput, resultlist) => { } const navigateResult = (incr) => { + if (![-1, 1].includes(incr)) { + throw new Error('incr must be -1 or 1') + } + const resultlen = resultlist.children.length if (resultlen == 0) { return // there is nothing to navigate through } - const current = Array.prototype.indexOf.call(resultlist.children, selectedResult) - - let next = current + incr - // honor bounds of resultlist - if (next < 0) next = 0 - if (next > resultlen - 1) next = resultlen - 1 - // todo: when out of bounds, wrap to top/bottom + const currentIndex = Array.prototype.indexOf.call(resultlist.children, selectedResult) + + // determine new index to highlight + let newIndex = currentIndex + incr + // when going out of bounds, wrap around + if (newIndex < 0) { + newIndex = resultlen - 1 + } else if (newIndex >= resultlen) { + newIndex = 0 + } if (selectedResult !== null) { selectedResult.classList.remove('focus') } - const nextElem = resultlist.childNodes.item(next) + const nextElem = resultlist.childNodes.item(newIndex) nextElem.classList.add('focus') selectedResult = nextElem }