From 39b05730d4d4436c6cd9e8f747dabbbdbffb1dac Mon Sep 17 00:00:00 2001 From: Vadim Date: Wed, 8 Sep 2021 00:09:11 +0300 Subject: [PATCH] #69 : Apply partial offset --- src/components/Carousel/Carousel.svelte | 12 ++---------- src/store.js | 2 ++ src/utils/page.js | 20 ++++---------------- 3 files changed, 8 insertions(+), 26 deletions(-) diff --git a/src/components/Carousel/Carousel.svelte b/src/components/Carousel/Carousel.svelte index bc55785..9ca96ac 100644 --- a/src/components/Carousel/Carousel.svelte +++ b/src/components/Carousel/Carousel.svelte @@ -21,7 +21,6 @@ getCurrentPageIndexWithoutClones, getPagesCountWithoutClones, getClonesCount, - getIsPartialOffset, } from '../../utils/page' import { get } from '../../utils/object' import { ProgressManager } from '../../utils/ProgressManager' @@ -292,14 +291,6 @@ // _duration is an offset animation time _duration = animated ? duration : 0 - const isPartialOffset = getIsPartialOffset({ - pagesCountWithoutClones, - headClonesCount: clonesCount.head, - pagesToScroll, - currentPageIndexWithoutClones, - }) - console.log('isPartialOffset', isPartialOffset) - offset = -currentPageIndex * pageWidth setTimeout(() => { @@ -315,7 +306,7 @@ if (currentPageIndex === 0) { await showPage(pagesCount - clonesCount.total, { animated: false }) jumped = true - } else if (currentPageIndex === pagesCount - clonesCount.head ) { + } else if (currentPageIndex === pagesCount - clonesCount.tail) { await showPage(clonesCount.head, { animated: false }) jumped = true } @@ -363,6 +354,7 @@ infinite, pagesCount, pagesToScroll, + clonesCountTail: clonesCount.tail, }), options, ) diff --git a/src/store.js b/src/store.js index 6f117e2..58be6b2 100644 --- a/src/store.js +++ b/src/store.js @@ -40,12 +40,14 @@ function createStore() { infinite, pagesCount, pagesToScroll, + clonesCountTail, }) { update(store => { const newCurrentPageIndex = getNextPageIndexFn(infinite)({ currentPageIndex: store.currentPageIndex, pagesCount, pagesToScroll, + clonesCountTail, }) return { ...store, diff --git a/src/utils/page.js b/src/utils/page.js index cb3489c..875fd32 100644 --- a/src/utils/page.js +++ b/src/utils/page.js @@ -2,6 +2,7 @@ export function getNextPageIndexLimited({ currentPageIndex, pagesCount, pagesToScroll, + clonesCountTail, }) { if (pagesCount < 1) throw new Error('pagesCount must be at least 1') return Math.min(Math.max(currentPageIndex + pagesToScroll, 0), pagesCount - 1) @@ -11,9 +12,10 @@ export function getNextPageIndexInfinte({ currentPageIndex, pagesCount, pagesToScroll, + clonesCountTail, }) { if (pagesCount < 1) throw new Error('pagesCount must be at least 1') - const newCurrentPageIndex = Math.max(currentPageIndex, 0) + pagesToScroll + const newCurrentPageIndex = Math.max(currentPageIndex, 0) + Math.min(pagesCount - clonesCountTail - currentPageIndex, pagesToScroll) return newCurrentPageIndex > pagesCount - 1 ? 0 : Math.max(newCurrentPageIndex, 0) } @@ -36,7 +38,7 @@ export function getPrevPageIndexInfinte({ pagesToScroll, }) { if (pagesCount < 1) throw new Error('pagesCount must be at least 1') - const newCurrentPageIndex = Math.min(currentPageIndex, pagesCount - 1) - pagesToScroll + const newCurrentPageIndex = Math.min(currentPageIndex, pagesCount - 1) - Math.min(currentPageIndex, pagesToScroll) return newCurrentPageIndex >= 0 ? Math.min(newCurrentPageIndex, pagesCount - 1) : pagesCount - 1 } @@ -168,7 +170,6 @@ export function getPagesCountWithoutClones({ export function getClonesCount({ infinite, - // pagesToScroll, pagesToShow, partialPageSize, }) { @@ -190,16 +191,3 @@ export function getClonesCount({ total: clonesCount.head + clonesCount.tail, } } - -export function getIsPartialOffset({ - pagesCountWithoutClones, - headClonesCount, - pagesToScroll, - currentPageIndexWithoutClones, -}) { - return ( - pagesCountWithoutClones + headClonesCount === - currentPageIndexWithoutClones + pagesToScroll - ) -} -