Fix bug when browsing around the end

This commit is contained in:
Leo Vasanko 2023-11-05 16:49:24 +00:00
parent 4f370440d9
commit cdc936d2d5

View File

@ -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