matrix-chat/src/views/chat.vue

113 lines
3.1 KiB
Vue
Raw Normal View History

2020-11-06 00:06:26 +00:00
<template>
<div>
2021-03-15 15:38:37 +00:00
<div ref="chatContainer" class="chatContainer">
2021-03-20 21:31:33 +00:00
<div @scroll="onScroll()" ref="msgContainer" id="messagesContainer" class="messagesContainer">
2021-03-15 23:20:23 +00:00
<div v-if="loadingStatus" @click="loadEvents()" class="loadMore">{{loadingStatus}}</div>
2021-03-16 19:45:47 +00:00
<p v-if="room.timeline.length === 0" class="chatInfo">this room is empty</p>
2021-03-20 21:31:33 +00:00
<timeline :timeline="room.timeline" :group-timeline="isGroup()" :user="user" :roomId="room.roomId" />
2020-11-06 00:06:26 +00:00
</div>
2021-03-20 21:31:33 +00:00
<icon v-if="showScrollBtn" @click.native="scroll.scrollToBottom()" id="scrollDown" ic="./sym/expand_more-black-24dp.svg" />
2020-11-06 00:06:26 +00:00
</div>
2021-03-15 21:32:12 +00:00
<newMessage :onResize="height=>resize(height)" :roomId="room.roomId"/>
2021-03-15 23:20:23 +00:00
<topBanner :room="room" :close-chat="()=>closeChat()" :open-chat-info="()=>openChatInfo()"/>
2020-11-06 00:06:26 +00:00
</div>
</template>
<script>
2021-03-15 15:38:37 +00:00
import newMessage from '@/components/newMessage.vue';
2021-03-15 21:32:12 +00:00
import topBanner from '@/components/topBanner.vue';
2021-03-15 23:20:23 +00:00
import Icon from '@/components/icon';
import {matrix} from '@/main';
2021-03-16 19:45:47 +00:00
import splitArray from '@/lib/splitArray.js'
import timeline from '@/components/timeline';
2021-03-20 21:31:33 +00:00
import scrollHandler from "@/lib/scrollHandler";
2020-11-06 00:06:26 +00:00
export default {
name: 'chat',
components: {
2021-03-16 19:45:47 +00:00
timeline,
2020-11-09 03:28:41 +00:00
Icon,
2021-03-15 15:38:37 +00:00
newMessage,
2021-03-16 19:45:47 +00:00
topBanner
2020-11-08 22:36:13 +00:00
},
2021-03-12 23:42:24 +00:00
props: {
2021-03-15 21:32:12 +00:00
room: [Object, undefined],
user: String,
2021-03-15 23:20:23 +00:00
closeChat: Function,
openChatInfo: Function
2021-03-12 23:42:24 +00:00
},
2020-11-09 03:28:41 +00:00
methods:{
2021-03-20 21:31:33 +00:00
onScroll(){
2021-03-15 23:20:23 +00:00
if (this.$refs.msgContainer.scrollTop === 0) this.loadEvents();
2021-03-20 21:31:33 +00:00
this.showScrollBtn = this.scroll.getScrollBottom() > 400;
2020-12-25 00:50:22 +00:00
},
2021-03-15 15:38:37 +00:00
resize(height){
2021-03-16 10:52:38 +00:00
this.$refs.chatContainer.style.height = `calc(100% - ${height}px - 3.5rem)`;
2021-03-15 21:32:12 +00:00
},
isGroup(){
return Object.keys(this.room.currentState.members).length > 2;
2021-03-15 23:20:23 +00:00
},
async loadEvents(){
2021-03-20 21:31:33 +00:00
let scrollBottom = this.scroll.getScrollBottom();
2021-03-15 23:20:23 +00:00
this.loadingStatus = 'loading ...';
await matrix.client.paginateEventTimeline(this.room.getLiveTimeline(), {backwards: true})
.then(state => this.loadingStatus = state?'load more':false);
2021-03-20 21:31:33 +00:00
this.scroll.setScrollBottom(scrollBottom);
},
getUser(userId){
return matrix.client.getUser(userId);
2021-03-16 19:45:47 +00:00
},
splitArray
2020-11-09 03:28:41 +00:00
},
2020-11-08 22:36:13 +00:00
data(){
return {
showScrollBtn: false,
2021-03-15 21:32:12 +00:00
scrollOnUpdate: true,
2021-03-20 21:31:33 +00:00
loadingStatus: 'load more',
scroll: ()=>{}
2020-11-08 22:36:13 +00:00
}
},
updated(){
2021-03-20 21:31:33 +00:00
if(this.scroll.getScrollBottom() < 400) this.scroll.scrollToBottom();
2021-03-15 21:32:12 +00:00
},
mounted(){
2021-03-20 21:31:33 +00:00
this.scroll = new scrollHandler(this.$refs.msgContainer);
this.scroll.scrollToBottom();
2020-11-06 00:06:26 +00:00
}
}
</script>
2021-03-15 15:38:37 +00:00
<style scoped lang="scss">
.chatContainer{
2020-11-06 00:06:26 +00:00
position: absolute;
margin: 0;
left: 0;
2021-03-16 10:52:38 +00:00
top: 3.5rem;
2020-12-25 00:50:22 +00:00
width: 100%;
2021-03-15 15:38:37 +00:00
height: calc(100% - 7rem);
.messagesContainer{
height: 100%;
overflow-y: auto;
}
#scrollDown{
position: absolute;
background-color: #fff;
bottom: 1rem;
right: 1rem;
display: block;
height: 2rem;
width: 2rem;
}
2020-12-25 00:50:22 +00:00
}
2021-03-15 23:20:23 +00:00
.loadMore{
position: relative;
background-color: #2d2d2d;
padding: 0.5rem;
border-radius: 0.5rem;
width: fit-content;
left: 50%;
transform: translate(-50%,0);
margin-top: 0.5rem;
cursor: pointer;
}
2021-03-16 19:45:47 +00:00
</style>