#69 : Update adjacent indexes

This commit is contained in:
Vadim
2021-09-09 20:59:55 +03:00
parent de85bb806f
commit 7a410045f8
2 changed files with 76 additions and 31 deletions

View File

@@ -18,12 +18,13 @@
applyClones, applyClones,
getPageSizes, getPageSizes,
applyPageSizes, applyPageSizes,
getCurrentPageIndexWithoutClones, getCurrentScrollIndex,
getPagesCountWithoutClones, getPagesCountWithoutClones,
getClonesCount, getClonesCount,
getPartialPageSize, getPartialPageSize,
getScrollsCount, getScrollsCount,
getPageIndexByScrollIndex, getPageIndexByScrollIndex,
getIndexesOfPagesWithoutClonesInScroll,
} from '../../utils/page' } from '../../utils/page'
import { get } from '../../utils/object' import { get } from '../../utils/object'
import { ProgressManager } from '../../utils/ProgressManager' import { ProgressManager } from '../../utils/ProgressManager'
@@ -152,14 +153,14 @@
}) })
let currentPageIndex = 0 let currentPageIndex = 0
$: currentPageIndexWithoutClones = getCurrentPageIndexWithoutClones({ $: currentScrollIndex = getCurrentScrollIndex({
currentPageIndex, currentPageIndex,
pagesCount, pagesCount,
headClonesCount: clonesCount.head, headClonesCount: clonesCount.head,
infinite, infinite,
pagesToScroll, pagesToScroll,
}) })
$: dispatch('pageChange', currentPageIndexWithoutClones) $: dispatch('pageChange', currentScrollIndex)
let pagesCount = 0 let pagesCount = 0
let pagesCountWithoutClones = 1 let pagesCountWithoutClones = 1
@@ -198,10 +199,12 @@
// 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({
pageIndex: currentPageIndexWithoutClones, scrollIndex: currentScrollIndex,
scrollsCount,
pagesCount: pagesCountWithoutClones, pagesCount: pagesCountWithoutClones,
infinite, pagesToShow,
}) pagesToScroll,
}).pageIndexes
function initPageSizes() { function initPageSizes() {
const sizes = getPageSizes({ const sizes = getPageSizes({
@@ -274,8 +277,27 @@
pagesCountWithoutClones, pagesCountWithoutClones,
}) })
// load first and last child to clone them // load first and last child to clone them
// TODO: update
loaded = [0, pagesContainer.children.length - 1] 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()
@@ -424,7 +446,7 @@
<div class="sc-carousel__arrow-container"> <div class="sc-carousel__arrow-container">
<Arrow <Arrow
direction="prev" direction="prev"
disabled={!infinite && currentPageIndexWithoutClones === 0} disabled={!infinite && currentScrollIndex === 0}
on:click={showPrevPage} on:click={showPrevPage}
/> />
</div> </div>
@@ -468,7 +490,7 @@
<div class="sc-carousel__arrow-container"> <div class="sc-carousel__arrow-container">
<Arrow <Arrow
direction="next" direction="next"
disabled={!infinite && currentPageIndexWithoutClones === scrollsCount - 1} disabled={!infinite && currentScrollIndex === scrollsCount - 1}
on:click={showNextPage} on:click={showNextPage}
/> />
</div> </div>
@@ -478,13 +500,13 @@
{#if dots} {#if dots}
<slot <slot
name="dots" name="dots"
currentPageIndex={currentPageIndexWithoutClones} currentPageIndex={currentScrollIndex}
pagesCount={pagesCountWithoutClones} pagesCount={pagesCountWithoutClones}
showPage={handlePageChange} showPage={handlePageChange}
> >
<Dots <Dots
pagesCount={scrollsCount} pagesCount={scrollsCount}
currentPageIndex={currentPageIndexWithoutClones} currentPageIndex={currentScrollIndex}
on:pageChange={event => handlePageChange(event.detail)} on:pageChange={event => handlePageChange(event.detail)}
></Dots> ></Dots>
</slot> </slot>

View File

@@ -59,26 +59,49 @@ export function getPageIndex({
return pageIndex < 0 ? 0 : Math.min(pageIndex, pagesCount - 1) return pageIndex < 0 ? 0 : Math.min(pageIndex, pagesCount - 1)
} }
export function getAdjacentIndexes({ export function getIndexesOfPagesWithoutClonesInScroll({
pageIndex, scrollIndex,
pagesToShow,
pagesToScroll,
pagesCount, pagesCount,
infinite,
}) { }) {
if (pagesCount < 1) throw new Error('pagesCount must be at least 1') const overlap = scrollIndex === 0 ? 0 : pagesToShow - pagesToScroll
const _pageIndex = Math.max(0, Math.min(pageIndex, pagesCount - 1)) const from = scrollIndex * pagesToShow - scrollIndex * overlap
let rangeStart = _pageIndex - 1; const to = from + Math.max(pagesToShow, pagesToScroll) - 1
let rangeEnd = _pageIndex + 1; console.log('=======>', from, to)
rangeStart = rangeStart < 0 const indexes = []
? infinite for (let i=from; i<=Math.min(pagesCount - 1, to); i++) {
? pagesCount - 1 indexes.push(i)
: 0 }
: rangeStart return indexes
rangeEnd = rangeEnd > pagesCount - 1 }
? infinite
? 0 export function getAdjacentIndexes({
: pagesCount - 1 scrollIndex,
: rangeEnd scrollsCount,
return [...new Set([rangeStart, rangeEnd, _pageIndex])].sort((a, b) => a - b) pagesCount,
pagesToShow,
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 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)
const pageIndexes = []
scrollIndexes.forEach(scrollIndex => pageIndexes.push(
...getIndexesOfPagesWithoutClonesInScroll({
scrollIndex,
pagesToShow,
pagesToScroll,
pagesCount,
})
))
return {
scrollIndexes,
pageIndexes: [...new Set(pageIndexes)].sort((a, b) => a - b),
}
} }
export function getClones({ export function getClones({
@@ -143,7 +166,7 @@ export function applyPageSizes({
} }
} }
export function getCurrentPageIndexWithoutClones({ export function getCurrentScrollIndex({
currentPageIndex, currentPageIndex,
pagesCount, pagesCount,
headClonesCount, headClonesCount,