diff --git a/src/components/Carousel/Carousel.svelte b/src/components/Carousel/Carousel.svelte index 5880e58..1b50a77 100644 --- a/src/components/Carousel/Carousel.svelte +++ b/src/components/Carousel/Carousel.svelte @@ -22,6 +22,7 @@ getPagesCountWithoutClones, getClonesCount, getPartialPageSize, + getScrollsCount, } from '../../utils/page' import { get } from '../../utils/object' import { ProgressManager } from '../../utils/ProgressManager' @@ -152,7 +153,11 @@ let pagesCount = 0 let pagesCountWithoutClones = 1 - $: scrollsCount = Math.ceil(pagesCountWithoutClones / pagesToScroll) + $: scrollsCount = getScrollsCount({ + infinite, + pagesCountWithoutClones, + pagesToScroll, + }) let partialPageSize = 0 @@ -334,6 +339,7 @@ ) } async function showPrevPage(options) { + // TODO: return if disabled await changePage( () => store.prev({ infinite, @@ -344,6 +350,7 @@ ) } async function showNextPage(options) { + // TODO: return if disabled await changePage( () => store.next({ infinite, @@ -436,7 +443,7 @@ diff --git a/src/utils/page.js b/src/utils/page.js index 6a531ef..b8840f9 100644 --- a/src/utils/page.js +++ b/src/utils/page.js @@ -2,15 +2,10 @@ 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 + - Math.min(pagesCount - clonesCountTail - currentPageIndex, pagesToScroll), - 0), - pagesCount - 1 - ) + console.log('next', pagesCount, currentPageIndex, pagesCount - currentPageIndex) + return currentPageIndex + Math.min(pagesCount - (currentPageIndex+1) - pagesToScroll, pagesToScroll) } export function getNextPageIndexInfinte({ @@ -160,7 +155,7 @@ export function getCurrentPageIndexWithoutClones({ if (currentPageIndex === 0) return pagesCount - headClonesCount return Math.floor((currentPageIndex - headClonesCount) / pagesToScroll) } - return currentPageIndex + return Math.ceil(currentPageIndex / pagesToScroll) } export function getPagesCountWithoutClones({ @@ -214,3 +209,13 @@ export function getPartialPageSize({ _pages += pagesToShow + overlap } } + +export function getScrollsCount({ + infinite, + pagesCountWithoutClones, + pagesToScroll, +}) { + return infinite + ? Math.ceil(pagesCountWithoutClones / pagesToScroll) + : Math.round(pagesCountWithoutClones / pagesToScroll) +}