From 44c5eca4e998624b4bcddad551a58d7c103aec2f Mon Sep 17 00:00:00 2001 From: John Factotum <50942278+johnfactotum@users.noreply.github.com> Date: Fri, 11 Aug 2023 08:14:30 +0800 Subject: [PATCH] Paginator: add scroll amount option --- paginator.js | 24 ++++++++++++------------ view.js | 8 ++++---- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/paginator.js b/paginator.js index c47fbe3..339177f 100644 --- a/paginator.js +++ b/paginator.js @@ -872,22 +872,22 @@ export class Paginator extends HTMLElement { const resolved = await target if (this.#canGoToIndex(resolved.index)) return this.#goTo(resolved) } - #scrollPrev() { + #scrollPrev(distance) { if (!this.#view) return true if (this.scrolled) { - if (this.start > 0) - return this.#scrollTo(Math.max(0, this.start - this.size), null, true) + if (this.start > 0) return this.#scrollTo( + Math.max(0, this.start - (distance ?? this.size)), null, true) return true } if (this.atStart) return const page = this.page - 1 return this.#scrollToPage(page, null, true).then(() => page <= 0) } - #scrollNext() { + #scrollNext(distance) { if (!this.#view) return true if (this.scrolled) { - if (this.viewSize - this.end > 2) - return this.#scrollTo(Math.min(this.viewSize, this.end), null, true) + if (this.viewSize - this.end > 2) return this.#scrollTo( + Math.min(this.viewSize, distance ? this.start + distance : this.end), null, true) return true } if (this.atEnd) return @@ -905,11 +905,11 @@ export class Paginator extends HTMLElement { for (let index = this.#index + dir; this.#canGoToIndex(index); index += dir) if (this.sections[index]?.linear !== 'no') return index } - async #turnPage(dir) { + async #turnPage(dir, distance) { if (this.#locked) return this.#locked = true 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({ index: this.#adjacentIndex(dir), anchor: prev ? () => 1 : () => 0, @@ -917,11 +917,11 @@ export class Paginator extends HTMLElement { if (shouldGo || !this.pageAnimation) await wait(100) this.#locked = false } - prev() { - return this.#turnPage(-1) + prev(distance) { + return this.#turnPage(-1, distance) } - next() { - return this.#turnPage(1) + next(distance) { + return this.#turnPage(1, distance) } prevSection() { return this.goTo({ index: this.#adjacentIndex(-1) }) diff --git a/view.js b/view.js index f59f550..cd7e2c0 100644 --- a/view.js +++ b/view.js @@ -335,12 +335,12 @@ export class View extends HTMLElement { console.error(`Could not get ${target}`) } } - async prev() { - await this.renderer.prev() + async prev(distance) { + await this.renderer.prev(distance) this.history.replaceState(this.lastLocation.cfi) } - async next() { - await this.renderer.next() + async next(distance) { + await this.renderer.next(distance) this.history.replaceState(this.lastLocation.cfi) } goLeft() {