#69 : Fix showPage logic fot non infinite case

This commit is contained in:
Vadim
2021-09-09 12:35:57 +03:00
parent 6cc9e12971
commit de85bb806f
2 changed files with 43 additions and 5 deletions

View File

@@ -23,6 +23,7 @@
getClonesCount,
getPartialPageSize,
getScrollsCount,
getPageIndexByScrollIndex,
} from '../../utils/page'
import { get } from '../../utils/object'
import { ProgressManager } from '../../utils/ProgressManager'
@@ -60,6 +61,7 @@
* Page to start on
*/
export let initialPageIndex = 0
$: initialScrollIndex = initialPageIndex
/**
* Transition duration (ms)
@@ -116,11 +118,19 @@
export let pagesToScroll = 1
export async function goTo(pageIndex, options) {
const scrollIndex = pageIndex
const animated = get(options, 'animated', true)
if (typeof pageIndex !== 'number') {
if (typeof scrollIndex !== 'number') {
throw new Error('pageIndex should be a number')
}
await showPage(pageIndex * pagesToScroll + clonesCount.head, { animated })
await showPage(getPageIndexByScrollIndex({
infinite,
scrollIndex,
clonesCountHead: clonesCount.head,
pagesToScroll,
pagesCount,
pagesToShow,
}), { animated })
}
export async function goToPrev(options) {
@@ -269,9 +279,17 @@
await tick()
infinite && addClones()
// TODO: validate initialPageIndex, initialPageIndex is an initialScrollIndex
store.init(initialPageIndex * pagesToScroll + clonesCount.head)
initPageSizes()
// TODO: validate initialScrollIndex
store.init(getPageIndexByScrollIndex({
infinite,
scrollIndex: initialScrollIndex,
clonesCountHead: clonesCount.head,
pagesToScroll,
pagesCount,
pagesToShow,
}))
}
addResizeEventListener(initPageSizes)
@@ -284,7 +302,14 @@
})
async function handlePageChange(pageIndex) {
await showPage(pageIndex * pagesToScroll + clonesCount.head)
await showPage(getPageIndexByScrollIndex({
infinite,
scrollIndex: pageIndex,
clonesCountHead: clonesCount.head,
pagesToScroll,
pagesCount,
pagesToShow,
}))
}
function offsetPage(options) {

View File

@@ -219,3 +219,16 @@ export function getScrollsCount({
? Math.ceil(pagesCountWithoutClones / pagesToScroll)
: Math.round(pagesCountWithoutClones / pagesToScroll)
}
export function getPageIndexByScrollIndex({
infinite,
scrollIndex,
clonesCountHead,
pagesToScroll,
pagesCount,
pagesToShow,
}) {
return infinite
? clonesCountHead + scrollIndex * pagesToScroll
: Math.min(scrollIndex * pagesToScroll, pagesCount - pagesToShow)
}