mirror of
https://github.com/john-okeefe/foliate-js.git
synced 2026-09-09 11:29:14 -04:00
FXL: page-spread-center and spread none
This commit is contained in:
@@ -567,6 +567,16 @@ class Loader {
|
|||||||
const getHTMLFragment = (doc, id) => doc.getElementById(id)
|
const getHTMLFragment = (doc, id) => doc.getElementById(id)
|
||||||
?? doc.querySelector(`[name="${CSS.escape(id)}"]`)
|
?? doc.querySelector(`[name="${CSS.escape(id)}"]`)
|
||||||
|
|
||||||
|
const getPageSpread = properties => {
|
||||||
|
for (const p of properties) {
|
||||||
|
if (p === 'page-spread-left' || p === 'rendition:page-spread-left')
|
||||||
|
return 'left'
|
||||||
|
if (p === 'page-spread-right' || p === 'rendition:page-spread-right')
|
||||||
|
return 'right'
|
||||||
|
if (p === 'rendition:page-spread-center') return 'center'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class EPUB {
|
export class EPUB {
|
||||||
parser = new DOMParser()
|
parser = new DOMParser()
|
||||||
#encryption
|
#encryption
|
||||||
@@ -624,9 +634,7 @@ export class EPUB {
|
|||||||
size: this.getSize(item.href),
|
size: this.getSize(item.href),
|
||||||
cfi: this.resources.cfis[index],
|
cfi: this.resources.cfis[index],
|
||||||
linear,
|
linear,
|
||||||
forceLeft: properties.includes('page-spread-left'),
|
pageSpread: getPageSpread(properties),
|
||||||
forceRight: properties.includes('page-spread-right'),
|
|
||||||
forceCenter: properties.includes('page-spread-center'),
|
|
||||||
resolveHref: href => resolveURL(href, item.href),
|
resolveHref: href => resolveURL(href, item.href),
|
||||||
}
|
}
|
||||||
}).filter(s => s)
|
}).filter(s => s)
|
||||||
|
|||||||
+25
-11
@@ -35,6 +35,7 @@ class Container {
|
|||||||
#portrait = false
|
#portrait = false
|
||||||
#left
|
#left
|
||||||
#right
|
#right
|
||||||
|
#center
|
||||||
#side
|
#side
|
||||||
constructor() {
|
constructor() {
|
||||||
Object.assign(this.#element.style, {
|
Object.assign(this.#element.style, {
|
||||||
@@ -83,8 +84,8 @@ class Container {
|
|||||||
}
|
}
|
||||||
render(side = this.#side) {
|
render(side = this.#side) {
|
||||||
if (!side) return
|
if (!side) return
|
||||||
const left = this.#left
|
const left = this.#left ?? {}
|
||||||
const right = this.#right
|
const right = this.#center ?? this.#right
|
||||||
const target = side === 'left' ? left : right
|
const target = side === 'left' ? left : right
|
||||||
const { width, height } = this.#element.getBoundingClientRect()
|
const { width, height } = this.#element.getBoundingClientRect()
|
||||||
const portrait = height > width
|
const portrait = height > width
|
||||||
@@ -121,15 +122,22 @@ class Container {
|
|||||||
element.style.display = 'none'
|
element.style.display = 'none'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
transform(left, 'left')
|
if (this.#center) {
|
||||||
transform(right, 'right')
|
transform(this.#center)
|
||||||
|
} else {
|
||||||
|
transform(left)
|
||||||
|
transform(right)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
async showSpread({ left, right, center, side }) {
|
async showSpread({ left, right, center, side }) {
|
||||||
this.#element.replaceChildren()
|
this.#element.replaceChildren()
|
||||||
this.#left = null
|
this.#left = null
|
||||||
this.#right = null
|
this.#right = null
|
||||||
|
this.#center = null
|
||||||
if (center) {
|
if (center) {
|
||||||
// TODO
|
this.#center = await this.#createFrame(center)
|
||||||
|
this.#side = side
|
||||||
|
this.render()
|
||||||
} else {
|
} else {
|
||||||
this.#left = await this.#createFrame(left)
|
this.#left = await this.#createFrame(left)
|
||||||
this.#right = await this.#createFrame(right)
|
this.#right = await this.#createFrame(right)
|
||||||
@@ -163,28 +171,34 @@ export class FixedLayout {
|
|||||||
#container = new Container()
|
#container = new Container()
|
||||||
constructor({ book, onLoad, onRelocated }) {
|
constructor({ book, onLoad, onRelocated }) {
|
||||||
this.book = book
|
this.book = book
|
||||||
this.#container.defaultViewport = book.rendition?.viewport
|
|
||||||
this.onLoad = onLoad
|
this.onLoad = onLoad
|
||||||
this.onRelocated = onRelocated
|
this.onRelocated = onRelocated
|
||||||
|
|
||||||
|
const { rendition } = book
|
||||||
|
this.#container.spread = rendition?.spread
|
||||||
|
this.#container.defaultViewport = rendition?.viewport
|
||||||
|
|
||||||
const rtl = book.dir === 'rtl'
|
const rtl = book.dir === 'rtl'
|
||||||
const ltr = !rtl
|
const ltr = !rtl
|
||||||
this.rtl = rtl
|
this.rtl = rtl
|
||||||
this.#spreads = book.sections.reduce((arr, section) => {
|
|
||||||
|
if (rendition?.spread === 'none')
|
||||||
|
this.#spreads = book.sections.map(section => ({ center: section }))
|
||||||
|
else this.#spreads = book.sections.reduce((arr, section) => {
|
||||||
const last = arr[arr.length - 1]
|
const last = arr[arr.length - 1]
|
||||||
const { linear, forceCenter, forceLeft, forceRight } = section
|
const { linear, pageSpread } = section
|
||||||
if (linear === 'no') return arr
|
if (linear === 'no') return arr
|
||||||
const newSpread = () => {
|
const newSpread = () => {
|
||||||
const spread = {}
|
const spread = {}
|
||||||
arr.push(spread)
|
arr.push(spread)
|
||||||
return spread
|
return spread
|
||||||
}
|
}
|
||||||
if (forceCenter) newSpread().center = section
|
if (pageSpread === 'center') newSpread().center = section
|
||||||
else if (forceLeft) {
|
else if (pageSpread === 'left') {
|
||||||
const spread = last.center || last.left || ltr ? newSpread() : last
|
const spread = last.center || last.left || ltr ? newSpread() : last
|
||||||
spread.left = section
|
spread.left = section
|
||||||
}
|
}
|
||||||
else if (forceRight) {
|
else if (pageSpread === 'right') {
|
||||||
const spread = last.center || last.right || rtl ? newSpread() : last
|
const spread = last.center || last.right || rtl ? newSpread() : last
|
||||||
spread.right = section
|
spread.right = section
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user