FXL: page-spread-center and spread none

This commit is contained in:
John Factotum
2022-10-29 16:10:56 +00:00
committed by GitHub
parent 602c8381c2
commit 3aad499696
2 changed files with 36 additions and 14 deletions
+11 -3
View File
@@ -567,6 +567,16 @@ class Loader {
const getHTMLFragment = (doc, id) => doc.getElementById(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 {
parser = new DOMParser()
#encryption
@@ -624,9 +634,7 @@ export class EPUB {
size: this.getSize(item.href),
cfi: this.resources.cfis[index],
linear,
forceLeft: properties.includes('page-spread-left'),
forceRight: properties.includes('page-spread-right'),
forceCenter: properties.includes('page-spread-center'),
pageSpread: getPageSpread(properties),
resolveHref: href => resolveURL(href, item.href),
}
}).filter(s => s)
+25 -11
View File
@@ -35,6 +35,7 @@ class Container {
#portrait = false
#left
#right
#center
#side
constructor() {
Object.assign(this.#element.style, {
@@ -83,8 +84,8 @@ class Container {
}
render(side = this.#side) {
if (!side) return
const left = this.#left
const right = this.#right
const left = this.#left ?? {}
const right = this.#center ?? this.#right
const target = side === 'left' ? left : right
const { width, height } = this.#element.getBoundingClientRect()
const portrait = height > width
@@ -121,15 +122,22 @@ class Container {
element.style.display = 'none'
}
}
transform(left, 'left')
transform(right, 'right')
if (this.#center) {
transform(this.#center)
} else {
transform(left)
transform(right)
}
}
async showSpread({ left, right, center, side }) {
this.#element.replaceChildren()
this.#left = null
this.#right = null
this.#center = null
if (center) {
// TODO
this.#center = await this.#createFrame(center)
this.#side = side
this.render()
} else {
this.#left = await this.#createFrame(left)
this.#right = await this.#createFrame(right)
@@ -163,28 +171,34 @@ export class FixedLayout {
#container = new Container()
constructor({ book, onLoad, onRelocated }) {
this.book = book
this.#container.defaultViewport = book.rendition?.viewport
this.onLoad = onLoad
this.onRelocated = onRelocated
const { rendition } = book
this.#container.spread = rendition?.spread
this.#container.defaultViewport = rendition?.viewport
const rtl = book.dir === 'rtl'
const ltr = !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 { linear, forceCenter, forceLeft, forceRight } = section
const { linear, pageSpread } = section
if (linear === 'no') return arr
const newSpread = () => {
const spread = {}
arr.push(spread)
return spread
}
if (forceCenter) newSpread().center = section
else if (forceLeft) {
if (pageSpread === 'center') newSpread().center = section
else if (pageSpread === 'left') {
const spread = last.center || last.left || ltr ? newSpread() : last
spread.left = section
}
else if (forceRight) {
else if (pageSpread === 'right') {
const spread = last.center || last.right || rtl ? newSpread() : last
spread.right = section
}