#69 : Update adjacent indexes

This commit is contained in:
Vadim
2021-09-09 21:35:54 +03:00
parent 7a410045f8
commit f830b69bea
2 changed files with 21 additions and 25 deletions

View File

@@ -199,6 +199,7 @@
// used for lazy loading images, preloaded only current, adjacent and cloanable images // used for lazy loading images, preloaded only current, adjacent and cloanable images
$: loaded = getAdjacentIndexes({ $: loaded = getAdjacentIndexes({
infinite,
scrollIndex: currentScrollIndex, scrollIndex: currentScrollIndex,
scrollsCount, scrollsCount,
pagesCount: pagesCountWithoutClones, pagesCount: pagesCountWithoutClones,
@@ -276,28 +277,7 @@
pagesToShow, pagesToShow,
pagesCountWithoutClones, pagesCountWithoutClones,
}) })
// load first and last child to clone them
scrollsCount = getScrollsCount({
infinite,
pagesCountWithoutClones,
pagesToScroll,
})
loaded = [
...getIndexesOfPagesWithoutClonesInScroll({
scrollIndex: 0,
pagesToShow,
pagesToScroll,
pagesCount: pagesCountWithoutClones,
}),
...getIndexesOfPagesWithoutClonesInScroll({
scrollIndex: scrollsCount - 1,
pagesToShow,
pagesToScroll,
pagesCount: pagesCountWithoutClones,
}),
]
await tick() await tick()
infinite && addClones() infinite && addClones()

View File

@@ -77,18 +77,34 @@ export function getIndexesOfPagesWithoutClonesInScroll({
} }
export function getAdjacentIndexes({ export function getAdjacentIndexes({
infinite,
scrollIndex, scrollIndex,
scrollsCount, scrollsCount,
pagesCount, pagesCount,
pagesToShow, pagesToShow,
pagesToScroll, pagesToScroll,
}) { }) {
// not checking is infinite or not, as first and last scrolls are always shown to be cloned
const _scrollIndex = Math.max(0, Math.min(scrollIndex, scrollsCount - 1)) const _scrollIndex = Math.max(0, Math.min(scrollIndex, scrollsCount - 1))
const rangeStart = Math.max(0, _scrollIndex - 1)
const rangeEnd = Math.min(_scrollIndex + 1, scrollsCount - 1)
const scrollIndexes = [...new Set([rangeStart, rangeEnd, _scrollIndex])].sort((a, b) => a - b) let rangeStart = _scrollIndex - 1
let rangeEnd = _scrollIndex + 1
rangeStart = rangeStart < 0
? infinite
? scrollsCount - 1
: 0
: rangeStart
rangeEnd = rangeEnd > scrollsCount - 1
? infinite
? 0
: scrollsCount - 1
: rangeEnd
const scrollIndexes = [...new Set([
rangeStart,
_scrollIndex,
rangeEnd,
scrollsCount - 1, // needed to clone last scroll pages
])].sort((a, b) => a - b)
const pageIndexes = [] const pageIndexes = []
scrollIndexes.forEach(scrollIndex => pageIndexes.push( scrollIndexes.forEach(scrollIndex => pageIndexes.push(
...getIndexesOfPagesWithoutClonesInScroll({ ...getIndexesOfPagesWithoutClonesInScroll({