Skip to content

Commit

Permalink
Fix:Epub rendition resize on screen orientation change #1213
Browse files Browse the repository at this point in the history
  • Loading branch information
advplyr committed Jun 2, 2024
1 parent b7e6cbf commit 494ff38
Showing 1 changed file with 46 additions and 10 deletions.
56 changes: 46 additions & 10 deletions components/readers/EpubReader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default {
currentLocationNum: 0,
currentLocationCfi: null,
inittingDisplay: true,
isRefreshingUI: false,
ereaderSettings: {
theme: 'dark',
fontScale: 100,
Expand All @@ -43,7 +44,7 @@ export default {
},
watch: {
isPlayerOpen() {
this.updateHeight()
this.refreshUI()
}
},
computed: {
Expand Down Expand Up @@ -135,11 +136,6 @@ export default {
goToChapter(href) {
return this.rendition?.display(href)
},
updateHeight() {
if (this.rendition && this.rendition.resize) {
this.rendition.resize(window.innerWidth, window.innerHeight - this.readerHeightOffset)
}
},
prev() {
if (this.rendition) {
this.rendition.prev()
Expand Down Expand Up @@ -382,14 +378,54 @@ export default {
this.rendition.getContents().forEach((c) => {
c.addStylesheetRules(this.themeRules)
})
},
async screenOrientationChange() {
if (this.isRefreshingUI) return
this.isRefreshingUI = true
const windowWidth = window.innerWidth
this.refreshUI()
// Window width does not always change right away. Wait up to 250ms for a change.
// iPhone 10 on iOS 16 took between 100 - 200ms to update when going from portrait to landscape
// but landscape to portrait was immediate
for (let i = 0; i < 5; i++) {
await new Promise((resolve) => setTimeout(resolve, 50))
if (window.innerWidth !== windowWidth) {
this.refreshUI()
break
}
}
this.isRefreshingUI = false
},
refreshUI() {
if (this.rendition?.resize) {
this.rendition.resize(window.innerWidth, window.innerHeight - this.readerHeightOffset)
}
}
},
mounted() {
this.initEpub()
if (screen.orientation) {
// Not available on ios
screen.orientation.addEventListener('change', this.screenOrientationChange)
} else {
document.addEventListener('orientationchange', this.screenOrientationChange)
}
window.addEventListener('resize', this.screenOrientationChange)
},
beforeDestroy() {
this.book?.destroy()
if (screen.orientation) {
// Not available on ios
screen.orientation.removeEventListener('change', this.screenOrientationChange)
} else {
document.removeEventListener('orientationchange', this.screenOrientationChange)
}
window.removeEventListener('resize', this.screenOrientationChange)
},
mounted() {
this.initEpub()
}
}
</script>
Expand All @@ -404,4 +440,4 @@ export default {
max-height: calc(100% - 132px);
overflow: hidden;
}
</style>
</style>

0 comments on commit 494ff38

Please sign in to comment.