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

feat(chat): lock scroll to bottom of chat #4712

Merged
merged 6 commits into from
Sep 12, 2022
Merged
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
3 changes: 1 addition & 2 deletions components/ui/CaretDivider/CaretDivider.less
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
}
border-bottom: 1px solid @red;
position: relative;
margin-top: @light-spacing;
margin-bottom: 0.5rem + @light-spacing;
margin: 1.25rem 0;
pointer-events: none;
}
14 changes: 7 additions & 7 deletions components/views/chat/conversation/Conversation.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<div class="conversation" ref="container">
<UiChatEncrypted />
<UiChatInfiniteScroll
:isLoading="isLoadingMore"
:noMore="noMore"
@loadMore="loadMore"
/>
<UiLoadersMessage :count="3" v-if="false" />
<div class="messages">
<template v-for="(item, i) of chatItems">
<TypographyHorizontalRuleText
Expand All @@ -17,11 +24,4 @@
/>
</template>
</div>
<UiChatEncrypted />
<UiChatInfiniteScroll
:isLoading="isLoadingMore"
:noMore="noMore"
@loadMore="loadMore"
/>
<UiLoadersMessage :count="3" v-if="false" />
</div>
8 changes: 7 additions & 1 deletion components/views/chat/conversation/Conversation.less
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.conversation {
display: flex;
flex-direction: column-reverse;
flex-direction: column;
flex: 1;
overflow-y: auto;
justify-content: space-between;
Expand All @@ -13,6 +13,12 @@
margin: 0 @normal-spacing;
padding-bottom: @normal-spacing;
margin-top: auto;
display: flex;
flex-direction: column;

.caret_divider + .chat-message-container.show-header {
margin-top: 0;
}
}

&-infiniteScroll.is-reversed {
Expand Down
37 changes: 36 additions & 1 deletion components/views/chat/conversation/Conversation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export default Vue.extend({
numMessages: MESSAGE_PAGE_SIZE,
isLoadingMore: false,
isBlurred: false,
mutationObserver: null as MutationObserver | null,
isLockedToBottom: true,
}
},
computed: {
Expand Down Expand Up @@ -94,20 +96,53 @@ export default Vue.extend({
}
return messages
},
isLastChatItemAuthor(): boolean {
const lastItem = this.chatItems.at(-1)
if (!lastItem || !iridium.connector) {
return false
}
return lastItem.message.from === iridium.connector.id
},
noMore(): boolean {
return (
this.numMessages >=
this.messages.filter((message) => !message.replyToId).length
)
},
},
async mounted() {
mounted() {
window.addEventListener('blur', async () => {
this.isBlurred = true
})
window.addEventListener('focus', async () => {
this.isBlurred = false
})
const container = this.$refs.container as HTMLElement
if (!container) {
return
}
container.addEventListener('scroll', () => {
this.isLockedToBottom =
container.scrollTop === container.scrollHeight - container.clientHeight
})
const scrollToBottom = () => {
const y = container.scrollHeight - container.clientHeight
container.scrollTo(0, y)
}
scrollToBottom()
this.mutationObserver = new MutationObserver(() => {
if (this.isLockedToBottom || this.isLastChatItemAuthor) {
scrollToBottom()
}
})
this.mutationObserver.observe(container, {
childList: true,
subtree: true,
attributes: true,
})
},
beforeDestroy() {
this.mutationObserver?.disconnect()
},
methods: {
loadMore() {
Expand Down
6 changes: 5 additions & 1 deletion components/views/chat/message/Message.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<UiContextMenu class="chat-message-container" :items="contextMenuValues">
<UiContextMenu
class="chat-message-container"
:class="{ 'show-header': showHeader }"
:items="contextMenuValues"
>
<MessageNotice v-if="isNotice" :message="message" />
<div
v-else
Expand Down
7 changes: 6 additions & 1 deletion components/views/chat/message/Message.less
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
}
}

.chat-message-container {
&.show-header {
margin-top: @normal-spacing;
}
}

.chat-message {
display: grid;
text-align: left;
Expand Down Expand Up @@ -46,7 +52,6 @@
'avatar body body'
'avatar footer footer';
grid-template-columns: 52px auto 1fr;
margin-top: @normal-spacing;

.timestamp {
margin-left: @light-spacing;
Expand Down
2 changes: 1 addition & 1 deletion locales/en-US.js
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ export default {
recent_messages: 'Go to most recent messages',
infinite_scroll: {
loading: 'Loading ...',
no_more: 'No more data.',
no_more: 'No more messages.',
},
add_reaction: 'Add reaction',
},
Expand Down