mirror of
https://github.com/john-okeefe/foliate-js.git
synced 2026-09-09 11:29:14 -04:00
Expose relocation reason
And fix history not updated when scrolling in scroll mode
This commit is contained in:
+8
-8
@@ -221,9 +221,9 @@ export class FixedLayout extends HTMLElement {
|
|||||||
? spread.left ?? spread.right : spread.right ?? spread.left)
|
? spread.left ?? spread.right : spread.right ?? spread.left)
|
||||||
return this.book.sections.indexOf(section)
|
return this.book.sections.indexOf(section)
|
||||||
}
|
}
|
||||||
#reportLocation() {
|
#reportLocation(reason) {
|
||||||
this.dispatchEvent(new CustomEvent('relocate', { detail:
|
this.dispatchEvent(new CustomEvent('relocate', { detail:
|
||||||
{ range: null, index: this.index, fraction: 0, size: 1 } }))
|
{ reason, range: null, index: this.index, fraction: 0, size: 1 } }))
|
||||||
}
|
}
|
||||||
getSpreadOf(section) {
|
getSpreadOf(section) {
|
||||||
const spreads = this.#spreads
|
const spreads = this.#spreads
|
||||||
@@ -234,7 +234,7 @@ export class FixedLayout extends HTMLElement {
|
|||||||
if (center === section) return { index, side: 'center' }
|
if (center === section) return { index, side: 'center' }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async goToSpread(index, side) {
|
async goToSpread(index, side, reason) {
|
||||||
if (index < 0 || index > this.#spreads.length - 1) return
|
if (index < 0 || index > this.#spreads.length - 1) return
|
||||||
if (index === this.#index) {
|
if (index === this.#index) {
|
||||||
this.#render(side)
|
this.#render(side)
|
||||||
@@ -255,7 +255,7 @@ export class FixedLayout extends HTMLElement {
|
|||||||
const right = { index: indexR, src: srcR }
|
const right = { index: indexR, src: srcR }
|
||||||
await this.#showSpread({ left, right, side })
|
await this.#showSpread({ left, right, side })
|
||||||
}
|
}
|
||||||
this.#reportLocation()
|
this.#reportLocation(reason)
|
||||||
}
|
}
|
||||||
async select(target) {
|
async select(target) {
|
||||||
await this.goTo(target)
|
await this.goTo(target)
|
||||||
@@ -271,13 +271,13 @@ export class FixedLayout extends HTMLElement {
|
|||||||
}
|
}
|
||||||
async next() {
|
async next() {
|
||||||
const s = this.rtl ? this.#goLeft() : this.#goRight()
|
const s = this.rtl ? this.#goLeft() : this.#goRight()
|
||||||
if (s) this.#reportLocation()
|
if (s) this.#reportLocation('page')
|
||||||
else return this.goToSpread(this.#index + 1, this.rtl ? 'right' : 'left')
|
else return this.goToSpread(this.#index + 1, this.rtl ? 'right' : 'left', 'page')
|
||||||
}
|
}
|
||||||
async prev() {
|
async prev() {
|
||||||
const s = this.rtl ? this.#goRight() : this.#goLeft()
|
const s = this.rtl ? this.#goRight() : this.#goLeft()
|
||||||
if (s) this.#reportLocation()
|
if (s) this.#reportLocation('page')
|
||||||
else return this.goToSpread(this.#index - 1, this.rtl ? 'left' : 'right')
|
else return this.goToSpread(this.#index - 1, this.rtl ? 'left' : 'right', 'page')
|
||||||
}
|
}
|
||||||
getContents() {
|
getContents() {
|
||||||
return Array.from(this.#root.querySelectorAll('iframe'), frame => ({
|
return Array.from(this.#root.querySelectorAll('iframe'), frame => ({
|
||||||
|
|||||||
+4
-4
@@ -664,7 +664,7 @@ export class Paginator extends HTMLElement {
|
|||||||
index: this.#adjacentIndex(dir),
|
index: this.#adjacentIndex(dir),
|
||||||
anchor: dir < 0 ? () => 1 : () => 0,
|
anchor: dir < 0 ? () => 1 : () => 0,
|
||||||
})
|
})
|
||||||
}).then(() => this.dispatchEvent(new Event('snapend')))
|
})
|
||||||
}
|
}
|
||||||
#onTouchStart(e) {
|
#onTouchStart(e) {
|
||||||
const touch = e.changedTouches[0]
|
const touch = e.changedTouches[0]
|
||||||
@@ -807,7 +807,7 @@ export class Paginator extends HTMLElement {
|
|||||||
if (reason !== 'anchor') this.#anchor = range
|
if (reason !== 'anchor') this.#anchor = range
|
||||||
|
|
||||||
const index = this.#index
|
const index = this.#index
|
||||||
const detail = { range, index }
|
const detail = { reason, range, index }
|
||||||
if (this.scrolled) detail.fraction = this.start / this.viewSize
|
if (this.scrolled) detail.fraction = this.start / this.viewSize
|
||||||
else if (this.pages > 0) {
|
else if (this.pages > 0) {
|
||||||
const { page, pages } = this
|
const { page, pages } = this
|
||||||
@@ -881,7 +881,7 @@ export class Paginator extends HTMLElement {
|
|||||||
}
|
}
|
||||||
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, 'page', true).then(() => page <= 0)
|
||||||
}
|
}
|
||||||
#scrollNext(distance) {
|
#scrollNext(distance) {
|
||||||
if (!this.#view) return true
|
if (!this.#view) return true
|
||||||
@@ -893,7 +893,7 @@ export class Paginator extends HTMLElement {
|
|||||||
if (this.atEnd) return
|
if (this.atEnd) return
|
||||||
const page = this.page + 1
|
const page = this.page + 1
|
||||||
const pages = this.pages
|
const pages = this.pages
|
||||||
return this.#scrollToPage(page, null, true).then(() => page >= pages - 1)
|
return this.#scrollToPage(page, 'page', true).then(() => page >= pages - 1)
|
||||||
}
|
}
|
||||||
get atStart() {
|
get atStart() {
|
||||||
return this.#adjacentIndex(-1) == null && this.page <= 1
|
return this.#adjacentIndex(-1) == null && this.page <= 1
|
||||||
|
|||||||
@@ -126,8 +126,6 @@ export class View extends HTMLElement {
|
|||||||
this.renderer.setAttribute('exportparts', 'head,foot,filter')
|
this.renderer.setAttribute('exportparts', 'head,foot,filter')
|
||||||
this.renderer.addEventListener('load', e => this.#onLoad(e.detail))
|
this.renderer.addEventListener('load', e => this.#onLoad(e.detail))
|
||||||
this.renderer.addEventListener('relocate', e => this.#onRelocate(e.detail))
|
this.renderer.addEventListener('relocate', e => this.#onRelocate(e.detail))
|
||||||
this.renderer.addEventListener('snapend', () =>
|
|
||||||
this.history.replaceState(this.lastLocation.cfi))
|
|
||||||
this.renderer.addEventListener('create-overlayer', e =>
|
this.renderer.addEventListener('create-overlayer', e =>
|
||||||
e.detail.attach(this.#createOverlayer(e.detail)))
|
e.detail.attach(this.#createOverlayer(e.detail)))
|
||||||
this.renderer.open(book)
|
this.renderer.open(book)
|
||||||
@@ -163,13 +161,14 @@ export class View extends HTMLElement {
|
|||||||
#emit(name, detail, cancelable) {
|
#emit(name, detail, cancelable) {
|
||||||
return this.dispatchEvent(new CustomEvent(name, { detail, cancelable }))
|
return this.dispatchEvent(new CustomEvent(name, { detail, cancelable }))
|
||||||
}
|
}
|
||||||
#onRelocate({ range, index, fraction, size }) {
|
#onRelocate({ reason, range, index, fraction, size }) {
|
||||||
if (!this.#sectionProgress) return
|
const progress = this.#sectionProgress?.getProgress(index, fraction, size) ?? {}
|
||||||
const progress = this.#sectionProgress.getProgress(index, fraction, size)
|
const tocItem = this.#tocProgress?.getProgress(index, range)
|
||||||
const tocItem = this.#tocProgress.getProgress(index, range)
|
const pageItem = this.#pageProgress?.getProgress(index, range)
|
||||||
const pageItem = this.#pageProgress.getProgress(index, range)
|
|
||||||
const cfi = this.getCFI(index, range)
|
const cfi = this.getCFI(index, range)
|
||||||
this.lastLocation = { ...progress, tocItem, pageItem, cfi, range }
|
this.lastLocation = { ...progress, tocItem, pageItem, cfi, range }
|
||||||
|
if (reason === 'snap' || reason === 'page' || reason === 'scroll')
|
||||||
|
this.history.replaceState(cfi)
|
||||||
this.#emit('relocate', this.lastLocation)
|
this.#emit('relocate', this.lastLocation)
|
||||||
}
|
}
|
||||||
#onLoad({ doc, index }) {
|
#onLoad({ doc, index }) {
|
||||||
@@ -337,11 +336,9 @@ export class View extends HTMLElement {
|
|||||||
}
|
}
|
||||||
async prev(distance) {
|
async prev(distance) {
|
||||||
await this.renderer.prev(distance)
|
await this.renderer.prev(distance)
|
||||||
this.history.replaceState(this.lastLocation.cfi)
|
|
||||||
}
|
}
|
||||||
async next(distance) {
|
async next(distance) {
|
||||||
await this.renderer.next(distance)
|
await this.renderer.next(distance)
|
||||||
this.history.replaceState(this.lastLocation.cfi)
|
|
||||||
}
|
}
|
||||||
goLeft() {
|
goLeft() {
|
||||||
return this.book.dir === 'rtl' ? this.next() : this.prev()
|
return this.book.dir === 'rtl' ? this.next() : this.prev()
|
||||||
|
|||||||
Reference in New Issue
Block a user