Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for "Open Current Chapter" button #3334

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions client/components/app/MediaPlayerContainer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
ref="audioPlayer"
:chapters="chapters"
:current-chapter="currentChapter"
:libraryItem="streamLibraryItem"
:paused="!isPlaying"
:loading="playerLoading"
:bookmarks="bookmarks"
Expand Down
13 changes: 12 additions & 1 deletion client/components/player/PlayerUi.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
<ui-tooltip direction="top" :text="$strings.LabelVolume">
<controls-volume-control ref="volumeControl" v-model="volume" @input="setVolume" class="mx-2 hidden sm:block" />
</ui-tooltip>

<ui-tooltip v-if="hasEbookFile" direction="top" :text="$strings.ButtonOpenCurrentChapter">
<button :aria-label="$strings.ButtonOpenCurrentChapter" class="text-gray-300 hover:text-white mx-1 lg:mx-2" @mousedown.prevent @mouseup.prevent @click.stop="openEbook">
<span class="material-symbols text-2xl">auto_stories</span>
</button>
</ui-tooltip>
<ui-tooltip v-if="!hideSleepTimer" direction="top" :text="$strings.LabelSleepTimer">
<button :aria-label="$strings.LabelSleepTimer" class="text-gray-300 hover:text-white mx-1 lg:mx-2" @mousedown.prevent @mouseup.prevent @click.stop="$emit('showSleepTimer')">
<span v-if="!sleepTimerSet" class="material-symbols text-2xl">snooze</span>
Expand Down Expand Up @@ -77,6 +81,7 @@ export default {
type: Array,
default: () => []
},
libraryItem: Object,
sleepTimerSet: Boolean,
sleepTimerRemaining: Number,
sleepTimerType: String,
Expand Down Expand Up @@ -173,6 +178,9 @@ export default {
useChapterTrack() {
const _useChapterTrack = this.$store.getters['user/getUserSetting']('useChapterTrack') || false
return this.chapters.length ? _useChapterTrack : false
},
hasEbookFile() {
return this.libraryItem.media && this.libraryItem.media.ebookFile
}
},
methods: {
Expand Down Expand Up @@ -352,6 +360,9 @@ export default {
else if (action === this.$hotkeys.AudioPlayer.INCREASE_PLAYBACK_RATE) this.increasePlaybackRate()
else if (action === this.$hotkeys.AudioPlayer.DECREASE_PLAYBACK_RATE) this.decreasePlaybackRate()
else if (action === this.$hotkeys.AudioPlayer.CLOSE) this.closePlayer()
},
openEbook() {
this.$store.commit('showEReader', { libraryItem: this.libraryItem, keepProgress: true })
}
},
mounted() {
Expand Down
56 changes: 51 additions & 5 deletions client/components/readers/EpubReader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ export default {
})
}
},
initEpub() {
async initEpub() {
/** @type {EpubReader} */
const reader = this

Expand All @@ -330,8 +330,26 @@ export default {
flow: 'paginated'
})

// load saved progress
reader.rendition.display(this.savedEbookLocation || reader.book.locations.start)
await reader.book.ready
// load saved progress or initial chapter
var initialLocation = this.savedEbookLocation || this.initialChapterHref || reader.book.locations.start
try {
const initialChapterTitle = this.getCurrentAudioChapter()
if (initialChapterTitle) {
const href = this.findChapterHref(initialChapterTitle)
if (href) {
try {
initialLocation = 'text/' + href
} catch (error) {
console.error('Failed to navigate to chapter:', error)
}
}
}
} catch (error) {
return
}

reader.rendition.display(initialLocation)

reader.rendition.on('rendered', () => {
this.applyTheme()
Expand Down Expand Up @@ -439,13 +457,41 @@ export default {
this.rendition.getContents().forEach((c) => {
c.addStylesheetRules(this.themeRules)
})
},
getCurrentAudioChapter() {
try {
var currentTime = this.$store.getters['user/getUserMediaProgress'](this.libraryItemId).currentTime
var audioBookChapters = this.$store.state.streamLibraryItem.media.chapters
} catch (error) {
return null
}
if (!audioBookChapters) return null
const chapter = audioBookChapters.find((chapter) => chapter.start <= currentTime && currentTime < chapter.end)
return chapter.title || audioBookChapters[audioBookChapters.length - 1].title
},
findChapterHref(title) {
const normalizeString = (str) => {
return str.replace(/[^a-zA-Z0-9]/g, '').toLowerCase()
}
const findInToc = (items) => {
for (let item of items) {
if (normalizeString(item.label) === normalizeString(title)) return item.href
if (item.subitems) {
const result = findInToc(item.subitems)
if (result) return result
}
}
return null
}
return findInToc(this.rendition.book.navigation.toc)
}
},
mounted() {
async mounted() {
this.windowWidth = window.innerWidth
this.windowHeight = window.innerHeight
window.addEventListener('resize', this.resize)
this.initEpub()

await this.initEpub()
},
beforeDestroy() {
window.removeEventListener('resize', this.resize)
Expand Down
1 change: 1 addition & 0 deletions client/strings/en-us.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"ButtonNextChapter": "Next Chapter",
"ButtonNextItemInQueue": "Next Item in Queue",
"ButtonOk": "Ok",
"ButtonOpenCurrentChapter": "Open Current Chapter",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only want to add the string to the English file, not all of the other files unless you know the translation (Weblate will handle the other languages)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do i undo all the changes? do i just undo locally and commit again?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you did all of the changes in one commit you can do git revert [SHA], or checkout the files from a specific commit and then do another commit, or undo it manually.

Thanks for doing that, it makes it easier to review the changes. :)

"ButtonOpenFeed": "Open Feed",
"ButtonOpenManager": "Open Manager",
"ButtonPause": "Pause",
Expand Down
Loading