From 494ff38ef3b3df4f8a0a70098202eea57c600bd3 Mon Sep 17 00:00:00 2001 From: advplyr Date: Sun, 2 Jun 2024 16:20:41 -0500 Subject: [PATCH] Fix:Epub rendition resize on screen orientation change #1213 --- components/readers/EpubReader.vue | 58 +++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/components/readers/EpubReader.vue b/components/readers/EpubReader.vue index 3260d640..75176f8e 100644 --- a/components/readers/EpubReader.vue +++ b/components/readers/EpubReader.vue @@ -34,6 +34,7 @@ export default { currentLocationNum: 0, currentLocationCfi: null, inittingDisplay: true, + isRefreshingUI: false, ereaderSettings: { theme: 'dark', fontScale: 100, @@ -43,7 +44,7 @@ export default { }, watch: { isPlayerOpen() { - this.updateHeight() + this.refreshUI() } }, computed: { @@ -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() @@ -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) + } } }, - beforeDestroy() { - this.book?.destroy() - }, 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) + }, } @@ -404,4 +440,4 @@ export default { max-height: calc(100% - 132px); overflow: hidden; } - \ No newline at end of file +