Skip to content

Commit 052123b

Browse files
committed
Add an EventTarget for transforming the book
And use it to do paginator-specific CSS replacements. And use it to catch loading errors in reader.js. Fixes #14 Closes #18
1 parent d4696ce commit 052123b

File tree

4 files changed

+44
-18
lines changed

4 files changed

+44
-18
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ The following methods are consumed by `progress.js`, for getting the correct TOC
112112
- `.splitTOCHref(href)`: given an href string (from the TOC), returns an array, the first element of which is the `id` of the section (see above), and the second element is the fragment identifier (can be any type; see below). May be async.
113113
- `.getTOCFragment(doc, id)`: given a `Document` object and a fragment identifier (the one provided by `.splitTOCHref()`; see above), returns a `Node` representing the target linked by the TOC item
114114

115+
In addition, the `.transformTarget`, if present, can be used to transform the contents of the book as it loads. It is an `EventTarget` with a custom event `"data"`, whose `.detail` is `{ data, type, name }`, where `.data` is either a string or `Blob`, or a `Promise` thereof, `.type` the content type string, and `.name` the identifier of the resource. Event handlers should mutate `.data` to transform the data.
116+
115117
Almost all of the properties and methods are optional. At minimum it needs `.sections` and the `.load()` method for the sections, as otherwise there won't be anything to render.
116118

117119
### Archived Files

epub.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,7 @@ class Loader {
706706
#children = new Map()
707707
#refCount = new Map()
708708
allowScript = false
709+
eventTarget = new EventTarget()
709710
constructor({ loadText, loadBlob, resources }) {
710711
this.loadText = loadText
711712
this.loadBlob = loadBlob
@@ -714,9 +715,15 @@ class Loader {
714715
// needed only when replacing in (X)HTML w/o parsing (see below)
715716
//.filter(({ mediaType }) => ![MIME.XHTML, MIME.HTML].includes(mediaType))
716717
}
717-
createURL(href, data, type, parent) {
718+
async createURL(href, data, type, parent) {
718719
if (!data) return ''
719-
const url = URL.createObjectURL(new Blob([data], { type }))
720+
const detail = { data, type }
721+
Object.defineProperty(detail, 'name', { value: href }) // readonly
722+
const event = new CustomEvent('data', { detail })
723+
this.eventTarget.dispatchEvent(event)
724+
const newData = await event.detail.data
725+
const newType = await event.detail.type
726+
const url = URL.createObjectURL(new Blob([newData], { type: newType }))
720727
this.#cache.set(href, url)
721728
this.#refCount.set(href, 1)
722729
if (parent) {
@@ -767,7 +774,9 @@ class Loader {
767774
// prevent circular references
768775
&& parents.every(p => p !== href)
769776
if (shouldReplace) return this.loadReplaced(item, parents)
770-
return this.createURL(href, await this.loadBlob(href), mediaType, parent)
777+
// NOTE: this can be replaced with `Promise.try()`
778+
const tryLoadBlob = Promise.resolve().then(() => this.loadBlob(href))
779+
return this.createURL(href, tryLoadBlob, mediaType, parent)
771780
}
772781
async loadHref(href, base, parents = []) {
773782
if (isExternal(href)) return href
@@ -779,7 +788,12 @@ class Loader {
779788
async loadReplaced(item, parents = []) {
780789
const { href, mediaType } = item
781790
const parent = parents.at(-1)
782-
const str = await this.loadText(href)
791+
let str = ''
792+
try {
793+
str = await this.loadText(href)
794+
} catch (e) {
795+
return this.createURL(href, Promise.reject(e), mediaType, parent)
796+
}
783797
if (!str) return null
784798

785799
// note that one can also just use `replaceString` for everything:
@@ -851,23 +865,10 @@ class Loader {
851865
(_, url) => this.loadHref(url, href, parents)
852866
.then(url => `url("${url}")`))
853867
// apart from `url()`, strings can be used for `@import` (but why?!)
854-
const replacedImports = await replaceSeries(replacedUrls,
868+
return replaceSeries(replacedUrls,
855869
/@import\s*["']([^"'\n]*?)["']/gi,
856870
(_, url) => this.loadHref(url, href, parents)
857871
.then(url => `@import "${url}"`))
858-
const w = window?.innerWidth ?? 800
859-
const h = window?.innerHeight ?? 600
860-
return replacedImports
861-
// unprefix as most of the props are (only) supported unprefixed
862-
.replace(/(?<=[{\s;])-epub-/gi, '')
863-
// replace vw and vh as they cause problems with layout
864-
.replace(/(\d*\.?\d+)vw/gi, (_, d) => parseFloat(d) * w / 100 + 'px')
865-
.replace(/(\d*\.?\d+)vh/gi, (_, d) => parseFloat(d) * h / 100 + 'px')
866-
// `page-break-*` unsupported in columns; replace with `column-break-*`
867-
.replace(/page-break-(after|before|inside)\s*:/gi, (_, x) =>
868-
`-webkit-column-break-${x}:`)
869-
.replace(/break-(after|before|inside)\s*:\s*(avoid-)?page/gi, (_, x, y) =>
870-
`break-${x}: ${y ?? ''}column`)
871872
}
872873
// find & replace all possible relative paths for all assets without parsing
873874
replaceString(str, href, parents = []) {
@@ -965,6 +966,7 @@ ${doc.querySelector('parsererror').innerText}`)
965966
.then(this.#encryption.getDecoder(uri)),
966967
resources: this.resources,
967968
})
969+
this.transformTarget = this.#loader.eventTarget
968970
this.sections = this.resources.spine.map((spineItem, index) => {
969971
const { idref, linear, properties = [] } = spineItem
970972
const item = this.resources.getItemByID(idref)

paginator.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,22 @@ export class Paginator extends HTMLElement {
636636
open(book) {
637637
this.bookDir = book.dir
638638
this.sections = book.sections
639+
book.transformTarget?.addEventListener('data', ({ detail }) => {
640+
if (detail.type !== 'text/css') return
641+
const w = innerWidth
642+
const h = innerHeight
643+
detail.data = Promise.resolve(detail.data).then(data => data
644+
// unprefix as most of the props are (only) supported unprefixed
645+
.replace(/(?<=[{\s;])-epub-/gi, '')
646+
// replace vw and vh as they cause problems with layout
647+
.replace(/(\d*\.?\d+)vw/gi, (_, d) => parseFloat(d) * w / 100 + 'px')
648+
.replace(/(\d*\.?\d+)vh/gi, (_, d) => parseFloat(d) * h / 100 + 'px')
649+
// `page-break-*` unsupported in columns; replace with `column-break-*`
650+
.replace(/page-break-(after|before|inside)\s*:/gi, (_, x) =>
651+
`-webkit-column-break-${x}:`)
652+
.replace(/break-(after|before|inside)\s*:\s*(avoid-)?page/gi, (_, x, y) =>
653+
`break-${x}: ${y ?? ''}column`))
654+
})
639655
}
640656
#createView() {
641657
if (this.#view) {

reader.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ class Reader {
111111
this.view.addEventListener('relocate', this.#onRelocate.bind(this))
112112

113113
const { book } = this.view
114+
book.transformTarget?.addEventListener('data', ({ detail }) => {
115+
detail.data = Promise.resolve(detail.data).catch(e => {
116+
console.error(new Error(`Failed to load ${detail.name}`, { cause: e }))
117+
return ''
118+
})
119+
})
114120
this.view.renderer.setStyles?.(getCSS(this.style))
115121
this.view.renderer.next()
116122

0 commit comments

Comments
 (0)