Paginator: add scroll amount option

This commit is contained in:
John Factotum
2023-08-11 08:14:30 +08:00
parent 9b366ad1c4
commit 44c5eca4e9
2 changed files with 16 additions and 16 deletions
+12 -12
View File
@@ -872,22 +872,22 @@ export class Paginator extends HTMLElement {
const resolved = await target const resolved = await target
if (this.#canGoToIndex(resolved.index)) return this.#goTo(resolved) if (this.#canGoToIndex(resolved.index)) return this.#goTo(resolved)
} }
#scrollPrev() { #scrollPrev(distance) {
if (!this.#view) return true if (!this.#view) return true
if (this.scrolled) { if (this.scrolled) {
if (this.start > 0) if (this.start > 0) return this.#scrollTo(
return this.#scrollTo(Math.max(0, this.start - this.size), null, true) Math.max(0, this.start - (distance ?? this.size)), null, true)
return true return true
} }
if (this.atStart) return if (this.atStart) return
const page = this.page - 1 const page = this.page - 1
return this.#scrollToPage(page, null, true).then(() => page <= 0) return this.#scrollToPage(page, null, true).then(() => page <= 0)
} }
#scrollNext() { #scrollNext(distance) {
if (!this.#view) return true if (!this.#view) return true
if (this.scrolled) { if (this.scrolled) {
if (this.viewSize - this.end > 2) if (this.viewSize - this.end > 2) return this.#scrollTo(
return this.#scrollTo(Math.min(this.viewSize, this.end), null, true) Math.min(this.viewSize, distance ? this.start + distance : this.end), null, true)
return true return true
} }
if (this.atEnd) return if (this.atEnd) return
@@ -905,11 +905,11 @@ export class Paginator extends HTMLElement {
for (let index = this.#index + dir; this.#canGoToIndex(index); index += dir) for (let index = this.#index + dir; this.#canGoToIndex(index); index += dir)
if (this.sections[index]?.linear !== 'no') return index if (this.sections[index]?.linear !== 'no') return index
} }
async #turnPage(dir) { async #turnPage(dir, distance) {
if (this.#locked) return if (this.#locked) return
this.#locked = true this.#locked = true
const prev = dir === -1 const prev = dir === -1
const shouldGo = await (prev ? this.#scrollPrev() : this.#scrollNext()) const shouldGo = await (prev ? this.#scrollPrev(distance) : this.#scrollNext(distance))
if (shouldGo) await this.#goTo({ if (shouldGo) await this.#goTo({
index: this.#adjacentIndex(dir), index: this.#adjacentIndex(dir),
anchor: prev ? () => 1 : () => 0, anchor: prev ? () => 1 : () => 0,
@@ -917,11 +917,11 @@ export class Paginator extends HTMLElement {
if (shouldGo || !this.pageAnimation) await wait(100) if (shouldGo || !this.pageAnimation) await wait(100)
this.#locked = false this.#locked = false
} }
prev() { prev(distance) {
return this.#turnPage(-1) return this.#turnPage(-1, distance)
} }
next() { next(distance) {
return this.#turnPage(1) return this.#turnPage(1, distance)
} }
prevSection() { prevSection() {
return this.goTo({ index: this.#adjacentIndex(-1) }) return this.goTo({ index: this.#adjacentIndex(-1) })
+4 -4
View File
@@ -335,12 +335,12 @@ export class View extends HTMLElement {
console.error(`Could not get ${target}`) console.error(`Could not get ${target}`)
} }
} }
async prev() { async prev(distance) {
await this.renderer.prev() await this.renderer.prev(distance)
this.history.replaceState(this.lastLocation.cfi) this.history.replaceState(this.lastLocation.cfi)
} }
async next() { async next(distance) {
await this.renderer.next() await this.renderer.next(distance)
this.history.replaceState(this.lastLocation.cfi) this.history.replaceState(this.lastLocation.cfi)
} }
goLeft() { goLeft() {