Change scroll component to use scrollToItem method in virtual scroller

It is required to keep scroll position after merge new statuses
This commit is contained in:
AkiraFukushima 2021-07-23 20:39:06 +09:00
parent 2743dd422e
commit 475a114658
No known key found for this signature in database
GPG Key ID: B6E51BAC4DE1A957
3 changed files with 23 additions and 20 deletions

View File

@ -1,7 +1,6 @@
<template> <template>
<div id="contents" ref="contents" :style="customWidth" @mouseup="dragEnd" @mousemove="resize"> <div id="contents" ref="contents" :style="customWidth" @mouseup="dragEnd" @mousemove="resize">
<div <div
id="scrollable"
:class="openSideBar ? 'timeline-wrapper-with-side-bar' : 'timeline-wrapper'" :class="openSideBar ? 'timeline-wrapper-with-side-bar' : 'timeline-wrapper'"
v-loading="loading" v-loading="loading"
:element-loading-text="$t('message.loading')" :element-loading-text="$t('message.loading')"
@ -85,17 +84,11 @@ export default {
.timeline-wrapper { .timeline-wrapper {
height: 100%; height: 100%;
width: 100%; width: 100%;
/* overflow: auto; */
/* transition: all 0.5s; */
/* scroll-behavior: auto; */
} }
.timeline-wrapper-with-side-bar { .timeline-wrapper-with-side-bar {
height: 100%; height: 100%;
width: calc(100% - var(--current-sidebar-width)); width: calc(100% - var(--current-sidebar-width));
/* overflow: auto; */
/* transition: all 0.5s; */
/* scroll-behavior: auto; */
} }
#resizer { #resizer {

View File

@ -156,7 +156,6 @@ export default {
this.$store.commit('TimelineSpace/Contents/Home/changeHeading', false) this.$store.commit('TimelineSpace/Contents/Home/changeHeading', false)
} else if (event.target.scrollTop <= 5 && !this.heading) { } else if (event.target.scrollTop <= 5 && !this.heading) {
const currentPos = this.unread.length const currentPos = this.unread.length
console.log(currentPos)
if (currentPos === 0) { if (currentPos === 0) {
this.$store.commit('TimelineSpace/Contents/Home/changeHeading', true) this.$store.commit('TimelineSpace/Contents/Home/changeHeading', true)
} }

View File

@ -2,7 +2,7 @@
<div id="notifications" v-shortkey="shortcutEnabled ? { next: ['j'] } : {}" @shortkey="handleKey"> <div id="notifications" v-shortkey="shortcutEnabled ? { next: ['j'] } : {}" @shortkey="handleKey">
<div class="unread">{{ unread.length > 0 ? unread.length : '' }}</div> <div class="unread">{{ unread.length > 0 ? unread.length : '' }}</div>
<div v-shortkey="{ linux: ['ctrl', 'r'], mac: ['meta', 'r'] }" @shortkey="reload()"></div> <div v-shortkey="{ linux: ['ctrl', 'r'], mac: ['meta', 'r'] }" @shortkey="reload()"></div>
<DynamicScroller :items="handledNotifications" :min-item-size="20" class="scroller" page-mode> <DynamicScroller :items="handledNotifications" :min-item-size="20" id="scroller" class="scroller" ref="scroller">
<template v-slot="{ item, index, active }"> <template v-slot="{ item, index, active }">
<DynamicScrollerItem :item="item" :active="active" :size-dependencies="[item.url]" :data-index="index" :watchData="true"> <DynamicScrollerItem :item="item" :active="active" :size-dependencies="[item.url]" :data-index="index" :watchData="true">
<notification <notification
@ -20,7 +20,6 @@
</DynamicScrollerItem> </DynamicScrollerItem>
</template> </template>
</DynamicScroller> </DynamicScroller>
<div class="loading-card" v-loading="lazyLoading" :element-loading-background="backgroundColor"></div>
<div :class="openSideBar ? 'upper-with-side-bar' : 'upper'" v-show="!heading"> <div :class="openSideBar ? 'upper-with-side-bar' : 'upper'" v-show="!heading">
<el-button type="primary" icon="el-icon-arrow-up" @click="upper" circle> </el-button> <el-button type="primary" icon="el-icon-arrow-up" @click="upper" circle> </el-button>
</div> </div>
@ -30,7 +29,6 @@
<script> <script>
import { mapState, mapGetters } from 'vuex' import { mapState, mapGetters } from 'vuex'
import Notification from '~/src/renderer/components/organisms/Notification' import Notification from '~/src/renderer/components/organisms/Notification'
import scrollTop from '../../utils/scroll'
import reloadable from '~/src/renderer/components/mixins/reloadable' import reloadable from '~/src/renderer/components/mixins/reloadable'
import { Event } from '~/src/renderer/components/event' import { Event } from '~/src/renderer/components/event'
@ -71,7 +69,7 @@ export default {
mounted() { mounted() {
this.$store.commit('TimelineSpace/SideMenu/changeUnreadNotifications', false) this.$store.commit('TimelineSpace/SideMenu/changeUnreadNotifications', false)
this.$store.dispatch('TimelineSpace/Contents/Notifications/resetBadge') this.$store.dispatch('TimelineSpace/Contents/Notifications/resetBadge')
document.getElementById('scrollable').addEventListener('scroll', this.onScroll) document.getElementById('scroller').addEventListener('scroll', this.onScroll)
Event.$on('focus-timeline', () => { Event.$on('focus-timeline', () => {
// If focusedId does not change, we have to refresh focusedId because Toot component watch change events. // If focusedId does not change, we have to refresh focusedId because Toot component watch change events.
@ -98,9 +96,9 @@ export default {
this.$store.commit('TimelineSpace/Contents/Notifications/changeHeading', true) this.$store.commit('TimelineSpace/Contents/Notifications/changeHeading', true)
this.$store.commit('TimelineSpace/Contents/Notifications/mergeNotifications') this.$store.commit('TimelineSpace/Contents/Notifications/mergeNotifications')
this.$store.commit('TimelineSpace/Contents/Notifications/archiveNotifications') this.$store.commit('TimelineSpace/Contents/Notifications/archiveNotifications')
if (document.getElementById('scrollable') !== undefined && document.getElementById('scrollable') !== null) { if (document.getElementById('scroller') !== undefined && document.getElementById('scroller') !== null) {
document.getElementById('scrollable').removeEventListener('scroll', this.onScroll) document.getElementById('scroller').removeEventListener('scroll', this.onScroll)
document.getElementById('scrollable').scrollTop = 0 document.getElementById('scroller').scrollTop = 0
} }
}, },
watch: { watch: {
@ -129,7 +127,7 @@ export default {
methods: { methods: {
onScroll(event) { onScroll(event) {
if ( if (
event.target.clientHeight + event.target.scrollTop >= document.getElementById('notifications').clientHeight - 10 && event.target.clientHeight + event.target.scrollTop >= document.getElementById('scroller').scrollHeight - 10 &&
!this.lazyloading !this.lazyloading
) { ) {
this.$store this.$store
@ -145,11 +143,15 @@ export default {
}) })
} }
// for unread control // for unread control
if (event.target.scrollTop > 10 && this.heading) { if (event.target.scrollTop > 5 && this.heading) {
this.$store.commit('TimelineSpace/Contents/Notifications/changeHeading', false) this.$store.commit('TimelineSpace/Contents/Notifications/changeHeading', false)
} else if (event.target.scrollTop <= 10 && !this.heading) { } else if (event.target.scrollTop <= 5 && !this.heading) {
this.$store.commit('TimelineSpace/Contents/Notifications/changeHeading', true) const currentPos = this.unread.length
if (currentPos === 0) {
this.$store.commit('TimelineSpace/Contents/Notifications/changeHeading', true)
}
this.$store.commit('TimelineSpace/Contents/Notifications/mergeNotifications') this.$store.commit('TimelineSpace/Contents/Notifications/mergeNotifications')
this.$refs.scroller.scrollToItem(currentPos)
this.$store.dispatch('TimelineSpace/Contents/Notifications/resetBadge') this.$store.dispatch('TimelineSpace/Contents/Notifications/resetBadge')
} }
}, },
@ -166,7 +168,7 @@ export default {
this.$store.commit('TimelineSpace/Contents/Notifications/updateToot', message) this.$store.commit('TimelineSpace/Contents/Notifications/updateToot', message)
}, },
upper() { upper() {
scrollTop(document.getElementById('scrollable'), 0) this.$refs.scroller.scrollToItem(0)
this.focusedId = null this.focusedId = null
}, },
focusNext() { focusNext() {
@ -204,6 +206,14 @@ export default {
<style lang="scss" scoped> <style lang="scss" scoped>
#notifications { #notifications {
height: 100%;
overflow: auto;
scroll-behavior: auto;
.scroller {
height: 100%;
}
.unread { .unread {
position: fixed; position: fixed;
right: 24px; right: 24px;
@ -212,6 +222,7 @@ export default {
color: #ffffff; color: #ffffff;
padding: 4px 8px; padding: 4px 8px;
border-radius: 0 0 2px 2px; border-radius: 0 0 2px 2px;
z-index: 1;
&:empty { &:empty {
display: none; display: none;