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

Bugfix/auto scroll to chapter #169

Open
wants to merge 5 commits into
base: develop
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
2 changes: 1 addition & 1 deletion src/components/books/book-chapters-list.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<section class="list">
<ol class="chapters">
<li v-for="(chapter, index) in chapters" :key="index">
<li v-for="(chapter, index) in chapters" :key="index" :chapter="chapter.id">
<router-link
:to="`${chapter.id}`"
append
Expand Down
1 change: 1 addition & 0 deletions src/components/layout/app-sidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<ol class="chapters">
<li v-for="chapter in chapters.sort((a, b) => parseInt(a.id) - parseInt(b.id))"
:key="chapter.id"
:chapter="chapter.id"
:class="['chapter', { 'current-chapter': chapter.id == selectedChapter }]"
@click="$emit('chapterChanged', chapter.id)">
<a v-bind:alt="chapter.title"><h5>{{chapter.title}}</h5></a>
Expand Down
2 changes: 1 addition & 1 deletion src/components/not-found.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<section class="list">
<h4>{{$t('not-found.suggestions')}}</h4>
<ol class="chapters">
<li v-for="(translation, index) in translations" :key="index">
<li v-for="(translation, index) in translations" :key="index" :chapter="index+1">
<a @click="changeBookLanguage(translation.language)">
<h5>
<img class="flag" width="16" height="16"
Expand Down
3 changes: 1 addition & 2 deletions src/components/reader/article-scroller.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
</template>

<script>
import Vue from 'vue';
import LoadingSpinner from 'components/loading-spinner';

export default {
Expand Down Expand Up @@ -39,7 +38,7 @@ export default {
},
methods: {
addScrollHandler() {
Vue.nextTick(() => {
this.$nextTick(() => {
window.addEventListener('scroll', this.scrollHandler);
});
},
Expand Down
32 changes: 23 additions & 9 deletions src/mixins/reader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Vue from 'vue';
export default {
methods: {
async loadTop() {
Expand Down Expand Up @@ -50,18 +49,33 @@ export default {
if(!article) return null;
return `chapter-element-${article.chapterId}`;
},
scrollToChapter(chapterId, offset = -100) {
Dr-Wade marked this conversation as resolved.
Show resolved Hide resolved
scrollToChapter(chapterId, offset = -105) {
let article = this.articles.find((a) => a.chapterId === chapterId);
if (!article) return false;
var self = this;
window.scrollTo(0,0);
if (chapterId >= 2) {
Vue.nextTick()
.then(() => {
let chapterElementRect = document.querySelector('#'+self.getElementName(article)).getBoundingClientRect();
window.scrollTo(0, chapterElementRect.top + offset);
});
var targetY = null;
const fireCallbackOnTargetHit = function(event) {
// When we reach the top of the document, we can scroll to the chapter
if (window.scrollY == 0) {
if (chapterId >= 2) {
self.$nextTick()
.then(() => {
let chapterElementRect = document.querySelector('#'+self.getElementName(article)).getBoundingClientRect();
targetY = Math.round(chapterElementRect.top)
window.scrollTo(0, targetY);
});
}
}
// When we have reached the target chapter, we can remove the callback and scroll by an offset to account for the header
else if (window.scrollY == targetY) {
window.removeEventListener('scroll', fireCallbackOnTargetHit)
window.scrollBy(0, offset)
}
}
window.addEventListener('scroll', fireCallbackOnTargetHit)
window.scrollTo(0,0);
// We fire the callback manually to ensure we catch it even if the user is already at the top of the page
fireCallbackOnTargetHit()
},
setCurrentChapter(chapterId) {
if (this.isPublication)
Expand Down
2 changes: 1 addition & 1 deletion src/styles/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ ol.chapters{
}

&:before{
content: counter(li);
content: attr(chapter);
display: inline-block;
position: absolute;

Expand Down