From cdc936d2d5df6c0d4d6c22afe7c8b26bc5acbd98 Mon Sep 17 00:00:00 2001 From: Leo Vasanko Date: Sun, 5 Nov 2023 16:49:24 +0000 Subject: [PATCH] Fix bug when browsing around the end --- cista-front/src/components/FileExplorer.vue | 22 ++++++++++----------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/cista-front/src/components/FileExplorer.vue b/cista-front/src/components/FileExplorer.vue index 45ea629..17694cc 100644 --- a/cista-front/src/components/FileExplorer.vue +++ b/cista-front/src/components/FileExplorer.vue @@ -205,23 +205,21 @@ defineExpose({ cursor.value = null return } + const N = documents.length const mod = (a: number, b: number) => ((a % b) + b) % b - const index = cursor.value !== null ? documents.indexOf(cursor.value) : -1 - const moveto = mod(index + d, documents.length + 1) + const increment = (i: number, d: number) => mod(i + d, N + 1) + const index = cursor.value !== null ? documents.indexOf(cursor.value) : documents.length + const moveto = increment(index, d) cursor.value = documents[moveto] ?? null - const tr = document.getElementById( - `file-${cursor.value.key}` - ) as HTMLTableRowElement | null + const tr = cursor.value ? document.getElementById(`file-${cursor.value.key}`) : null if (select) { // Go forwards, possibly wrapping over the end; the last entry is not toggled let [begin, end] = d > 0 ? [index, moveto] : [moveto, index] - for (let p = begin; p !== end; p = mod(p + 1, documents.length)) { - const doc = documents[p] - if (documentStore.selected.has(doc.key)) { - documentStore.selected.delete(doc.key) - } else { - documentStore.selected.add(doc.key) - } + for (let p = begin; p !== end; p = increment(p, 1)) { + if (p === N) continue + const key = documents[p].key + if (documentStore.selected.has(key)) documentStore.selected.delete(key) + else documentStore.selected.add(key) } } // @ts-ignore