Skip to content

Commit 97f1a34

Browse files
committed
(refacto) replace infinite-scroll by observer api
1 parent f384994 commit 97f1a34

File tree

4 files changed

+64
-54
lines changed

4 files changed

+64
-54
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@
7676
"dependencies": {
7777
"emoji-picker-element": "^1.8.1",
7878
"linkifyjs": "^2.1.9",
79-
"v-click-outside": "^3.1.2",
80-
"vue-infinite-loading": "https://github.com/antoine92190/vue-infinite-loading/tarball/master"
79+
"v-click-outside": "^3.1.2"
8180
},
8281
"optionalDependencies": {
8382
"lamejs": "^1.2.0"

src/lib/Room/Room.scss

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@
5454
margin-bottom: 20px;
5555
}
5656

57-
.vac-infinite-loading {
58-
height: 68px;
59-
}
60-
6157
.vac-icon-scroll {
6258
position: absolute;
6359
bottom: 80px;
@@ -230,10 +226,6 @@
230226
margin-top: 50px;
231227
}
232228

233-
.vac-infinite-loading {
234-
height: 58px;
235-
}
236-
237229
.vac-box-footer {
238230
padding: 7px 2px 7px 7px;
239231

src/lib/Room/Room.vue

Lines changed: 63 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
</room-header>
3131

3232
<div
33+
id="messages-list"
3334
ref="scrollContainer"
3435
class="vac-container-scroll"
3536
@scroll="onContainerScroll"
@@ -53,32 +54,16 @@
5354
</div>
5455
</div>
5556
</transition>
56-
<transition name="vac-fade-message">
57-
<infinite-loading
58-
v-if="messages.length"
59-
:class="{ 'vac-infinite-loading': !messagesLoaded }"
60-
force-use-infinite-wrapper=".vac-container-scroll"
61-
web-component-name="vue-advanced-chat"
62-
spinner="spiral"
63-
direction="top"
64-
:distance="40"
65-
@infinite="loadMoreMessages"
66-
>
67-
<template #spinner>
68-
<loader :show="true" :infinite="true">
69-
<template v-for="(idx, name) in $scopedSlots" #[name]="data">
70-
<slot :name="name" v-bind="data" />
71-
</template>
72-
</loader>
73-
</template>
74-
<template #no-results>
75-
<div />
76-
</template>
77-
<template #no-more>
78-
<div />
57+
<div
58+
v-if="messages.length && !messagesLoaded"
59+
id="infinite-loader-messages"
60+
>
61+
<loader :show="true" :infinite="true">
62+
<template v-for="(idx, name) in $scopedSlots" #[name]="data">
63+
<slot :name="name" v-bind="data" />
7964
</template>
80-
</infinite-loading>
81-
</transition>
65+
</loader>
66+
</div>
8267
<transition-group :key="roomId" name="vac-fade-message" tag="span">
8368
<div v-for="(m, i) in messages" :key="m.indexId || m._id">
8469
<message
@@ -316,7 +301,6 @@
316301
</template>
317302

318303
<script>
319-
import InfiniteLoading from 'vue-infinite-loading'
320304
import vClickOutside from 'v-click-outside'
321305
import { Database } from 'emoji-picker-element'
322306
@@ -335,7 +319,7 @@ import Message from '../Message/Message'
335319
import filteredItems from '../../utils/filter-items'
336320
import Recorder from '../../utils/recorder'
337321
338-
const { detectMobile, iOSDevice } = require('../../utils/mobile-detection')
322+
const { detectMobile } = require('../../utils/mobile-detection')
339323
340324
const debounce = (func, delay) => {
341325
let inDebounce
@@ -350,7 +334,6 @@ const debounce = (func, delay) => {
350334
export default {
351335
name: 'Room',
352336
components: {
353-
InfiniteLoading,
354337
Loader,
355338
SvgIcon,
356339
EmojiPickerContainer,
@@ -422,6 +405,8 @@ export default {
422405
messageReply: null,
423406
infiniteState: null,
424407
loadingMessages: false,
408+
observer: null,
409+
showLoader: true,
425410
loadingMoreMessages: false,
426411
files: [],
427412
fileDialog: false,
@@ -502,6 +487,7 @@ export default {
502487
} else {
503488
if (this.infiniteState) this.infiniteState.loaded()
504489
this.focusTextarea(true)
490+
setTimeout(() => this.initIntersectionObserver())
505491
}
506492
},
507493
room: {
@@ -596,6 +582,47 @@ export default {
596582
},
597583
598584
methods: {
585+
initIntersectionObserver() {
586+
if (this.observer) {
587+
this.showLoader = true
588+
this.observer.disconnect()
589+
}
590+
591+
const loader = document.getElementById('infinite-loader-messages')
592+
593+
if (loader) {
594+
const options = {
595+
root: document.getElementById('messages-list'),
596+
rootMargin: '60px',
597+
threshold: 0
598+
}
599+
600+
this.observer = new IntersectionObserver(entries => {
601+
if (entries[0].isIntersecting) {
602+
this.loadMoreMessages()
603+
}
604+
}, options)
605+
606+
this.observer.observe(loader)
607+
}
608+
},
609+
preventTopScroll() {
610+
const container = this.$refs.scrollContainer
611+
const prevScrollHeight = container.scrollHeight
612+
613+
const observer = new ResizeObserver(_ => {
614+
if (container.scrollHeight !== prevScrollHeight) {
615+
this.$refs.scrollContainer.scrollTo({
616+
top: container.scrollHeight - prevScrollHeight
617+
})
618+
observer.disconnect()
619+
}
620+
})
621+
622+
for (var i = 0; i < container.children.length; i++) {
623+
observer.observe(container.children[i])
624+
}
625+
},
599626
getTextareaRef() {
600627
return this.$refs.roomTextarea
601628
},
@@ -962,26 +989,25 @@ export default {
962989
963990
this.resetMessage(true)
964991
},
965-
loadMoreMessages(infiniteState) {
966-
if (this.loadingMessages) {
967-
this.infiniteState = infiniteState
968-
return
969-
}
992+
loadMoreMessages() {
993+
if (this.loadingMessages) return
970994
971995
setTimeout(
972996
() => {
973997
if (this.loadingMoreMessages) return
974998
975999
if (this.messagesLoaded || !this.room.roomId) {
976-
return infiniteState.complete()
1000+
this.loadingMoreMessages = false
1001+
this.showLoader = false
1002+
return
9771003
}
9781004
979-
this.infiniteState = infiniteState
1005+
this.preventTopScroll()
9801006
this.$emit('fetch-messages')
9811007
this.loadingMoreMessages = true
9821008
},
983-
// prevent scroll bouncing issue on iOS devices
984-
iOSDevice() ? 500 : 0
1009+
// prevent scroll bouncing speed
1010+
500
9851011
)
9861012
},
9871013
messageActionHandler({ action, message }) {

src/utils/mobile-detection.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,3 @@ function getUserAgent() {
2121

2222
return userAgent
2323
}
24-
25-
export function iOSDevice() {
26-
return (
27-
['iPad', 'iPhone', 'iPod'].includes(navigator.platform) ||
28-
(navigator.userAgent.includes('Mac') && 'ontouchend' in document)
29-
)
30-
}

0 commit comments

Comments
 (0)