Skip to content

Commit

Permalink
Finally fix some books not rendering sometimes
Browse files Browse the repository at this point in the history
So I hope
  • Loading branch information
johnfactotum committed Jun 7, 2019
1 parent d5195ec commit d0b8fb9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 65 deletions.
38 changes: 28 additions & 10 deletions src/assets/viewer.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,34 @@
book.open(decodeURI(fileName)) // works for non-flatpak
.catch(() => book.open(fileName)) // works for flatpak
.catch(() => dispatch({ type: 'book-error' }))

render(book)

const displayed = rendition.display()
displayed.then(() => dispatch({ type: 'rendition-ready' }))
book.ready.then(() => dispatch({ type: 'book-ready' }))
}
const display = (lastLocation, cached) => {
rendition = book.renderTo('viewer', {})

// HACK: no idea why but have to do it twice
// otherwise it will fail, but only on rare occassions ¯\_(ツ)_/¯
const displayed = rendition.display(lastLocation)
.then(() => rendition.display(lastLocation))

if (cached) {
book.locations.load(cached)
displayed
.then(() => dispatch({ type: 'book-displayed' }))
.then(() => dispatch({
type: 'locations-ready',
payload: rendition.currentLocation().start.percentage
}))
} else {
displayed
.then(() => dispatch({ type: 'book-displayed' }))
.then(() => book.locations.generate(1600))
.then(() => locations = book.locations.save())
.then(() => dispatch({
type: 'locations-generated',
payload: rendition.currentLocation().start.percentage
}))
}
book.loaded.resources
.then(resources => resources.createUrl(book.cover))
.then(blobUrl => fetch(blobUrl))
Expand Down Expand Up @@ -166,11 +188,7 @@
rendition.on('keydown', handleKeydown)
document.addEventListener('keydown', handleKeydown, false)
}
const render = book => {
if (rendition) rendition.destroy()
rendition = book.renderTo('viewer', {})


const setupRendition = () => {
rendition.on('rendered', section => {
dispatch({ type: 'section', payload: section.href })
})
Expand Down
92 changes: 37 additions & 55 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1134,19 +1134,9 @@ class BookViewerWindow {
box.show_all()
this.container.pack_start(box, true, true, 0)
}
bookDisplayed() {
this.spinner.destroy()
this.webView.opacity = 1
this.webView.grab_focus()
}
bookReady() {
this.scriptGet('book.package.metadata.title', title =>
this.headerBar.title = title)

const goTo = x => this.scriptRun(`rendition.display('${x}')`)
const withHistory = f => x =>
this.scriptGet(`rendition.currentLocation().start.cfi`,
cfi => { this.navbar.pushHistory(cfi); f(x) })

this.scriptGet('book.package.metadata.identifier', key => {
this.storage = new Storage(key)
Expand All @@ -1155,47 +1145,41 @@ class BookViewerWindow {
const lastLocation = this.storage.get('lastLocation')
const display = lastLocation ? `"${lastLocation}"` : 'undefined'
const cached = this.cache.get('locations')
if (cached) this.scriptRun(`
book.locations.load(${cached})
rendition.display(${display})
.then(() => dispatch({ type: 'book-displayed' }))
.then(() => dispatch({
type: 'locations-ready',
payload: rendition.currentLocation().start.percentage
}))
`)
else this.scriptRun(`
rendition.display(${display})
.then(() => dispatch({ type: 'book-displayed' }))
.then(() => book.locations.generate(1600))
.then(() => locations = book.locations.save())
.then(() => dispatch({
type: 'locations-generated',
payload: rendition.currentLocation().start.percentage
}))`)

this.scriptGet('book.navigation.toc', toc => {
this.buildToc(toc, withHistory(goTo))

this.buildAnnotations(withHistory(goTo),
values => this.storage.set('annotations', values),
value => this.scriptRun(`
rendition.annotations.remove("${value}", 'highlight')`))

const annotations = this.storage.get('annotations', [])
this.annotationsPopover.init(annotations)
annotations.forEach(({ value, color }) =>
this.scriptRun(`addAnnotation('${value}', '${color}')`))

const cleanCfi = cfi => cfi.replace(/(^epubcfi\(|\)$)/g, '')
this.buildBookmarks(withHistory(goTo), add => {
this.scriptGet(`rendition.currentLocation().start.cfi`, cfi => {
add(cleanCfi(cfi), cfi)
})
}, values => this.storage.set('bookmarks', values))
this.bookmarksPopover.init(this.storage.get('bookmarks', [])
.map(x => [cleanCfi(x), x]))
})
this.scriptRun(`display(${display}, ${cached || null})`)
})
}
bookDisplayed() {
this.spinner.destroy()
this.webView.opacity = 1
this.webView.grab_focus()

const goTo = x => this.scriptRun(`rendition.display('${x}')`)
const withHistory = f => x =>
this.scriptGet(`rendition.currentLocation().start.cfi`,
cfi => { this.navbar.pushHistory(cfi); f(x) })

this.scriptGet('book.navigation.toc', toc => {
this.buildToc(toc, withHistory(goTo))

this.buildAnnotations(withHistory(goTo),
values => this.storage.set('annotations', values),
value => this.scriptRun(`
rendition.annotations.remove("${value}", 'highlight')`))

const annotations = this.storage.get('annotations', [])
this.annotationsPopover.init(annotations)
annotations.forEach(({ value, color }) =>
this.scriptRun(`addAnnotation('${value}', '${color}')`))

const cleanCfi = cfi => cfi.replace(/(^epubcfi\(|\)$)/g, '')
this.buildBookmarks(withHistory(goTo), add => {
this.scriptGet(`rendition.currentLocation().start.cfi`, cfi => {
add(cleanCfi(cfi), cfi)
})
}, values => this.storage.set('bookmarks', values))
this.bookmarksPopover.init(this.storage.get('bookmarks', [])
.map(x => [cleanCfi(x), x]))
})

this.buildNavbar(
Expand All @@ -1215,8 +1199,7 @@ class BookViewerWindow {
})

this.scriptRun(`lookupEnabled = ${settings.get_boolean('lookup-enabled')}`)
}
renditionReady() {

this.buildView(({ font, spacing, theme, useDefault, justify, hyphenate }) => {
settings.set_string('font', font.name)
settings.set_double('spacing', spacing)
Expand Down Expand Up @@ -1319,6 +1302,8 @@ class BookViewerWindow {
const theme = settings.get_string('theme')
if (theme !== this.viewPopover.theme) this.viewPopover.theme = theme
})

this.scriptRun(`setupRendition()`)
}
handleAction({ type, payload }) {
switch (type) {
Expand All @@ -1328,9 +1313,6 @@ class BookViewerWindow {
case 'book-ready':
this.bookReady()
break
case 'rendition-ready':
this.renditionReady()
break
case 'book-displayed':
this.bookDisplayed()
break
Expand Down

0 comments on commit d0b8fb9

Please sign in to comment.