Add scroll behavior to chapter

This commit is contained in:
Ramon Wenger 2023-02-13 14:27:16 +01:00
parent 04d06b3332
commit 51c9ac9a13
2 changed files with 17 additions and 4 deletions

View File

@ -3,6 +3,7 @@
:data-scrollto="chapter.id" :data-scrollto="chapter.id"
class="chapter" class="chapter"
data-cy="chapter" data-cy="chapter"
ref="wrapper"
> >
<div <div
:class="{ 'hideable-element--greyed-out': titleGreyedOut }" :class="{ 'hideable-element--greyed-out': titleGreyedOut }"
@ -66,7 +67,7 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { computed } from 'vue'; import { computed, onMounted, ref } from 'vue';
import ContentBlock from '@/components/ContentBlock.vue'; import ContentBlock from '@/components/ContentBlock.vue';
import AddContentButton from '@/components/AddContentButton.vue'; import AddContentButton from '@/components/AddContentButton.vue';
import BookmarkActions from '@/components/notes/BookmarkActions.vue'; import BookmarkActions from '@/components/notes/BookmarkActions.vue';
@ -81,6 +82,7 @@ import UPDATE_CHAPTER_BOOKMARK_MUTATION from '@/graphql/gql/mutations/updateChap
import CHAPTER_QUERY from '@/graphql/gql/queries/chapterQuery.gql'; import CHAPTER_QUERY from '@/graphql/gql/queries/chapterQuery.gql';
import { getMe } from '@/mixins/me'; import { getMe } from '@/mixins/me';
import { useMutation } from '@vue/apollo-composable'; import { useMutation } from '@vue/apollo-composable';
import { useRoute } from 'vue-router';
export interface Props { export interface Props {
chapter: Chapter; chapter: Chapter;
@ -91,6 +93,20 @@ export interface Props {
const { schoolClass } = getMe(); const { schoolClass } = getMe();
const store = useStore(); const store = useStore();
const route = useRoute();
const wrapper = ref<HTMLElement | null>(null);
onMounted(() => {
const element = wrapper.value;
if (element !== null) {
if (route.hash === `#${props.chapter.id}`) {
setTimeout(() => {
const rect = element.getBoundingClientRect();
window.scrollTo({ top: rect.y, behavior: 'smooth' });
}, 750);
}
}
});
const props = withDefaults(defineProps<Props>(), { const props = withDefaults(defineProps<Props>(), {
index: 0, index: 0,

View File

@ -209,8 +209,6 @@ const { mutate: doDeleteContentBlock } = useMutation(DELETE_CONTENT_BLOCK_MUTATI
}, },
})); }));
const top = ref(0);
onMounted(() => { onMounted(() => {
const element = wrapper.value; const element = wrapper.value;
if (element !== null) { if (element !== null) {
@ -218,7 +216,6 @@ onMounted(() => {
setTimeout(() => { setTimeout(() => {
const rect = element.getBoundingClientRect(); const rect = element.getBoundingClientRect();
window.scrollTo({ top: rect.y, behavior: 'smooth' }); window.scrollTo({ top: rect.y, behavior: 'smooth' });
top.value = rect.y;
}, 750); }, 750);
} }
} }