Skip to content

Commit

Permalink
Merge pull request #12205 from nextcloud/fix/scrolling-collapsed
Browse files Browse the repository at this point in the history
fix(MessagesList): handle special case in scrolling
  • Loading branch information
DorraJaouad authored Apr 25, 2024
2 parents 3e6e3bf + 1a1fe23 commit 65b26f0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/components/MessagesList/MessagesList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -621,18 +621,21 @@ export default {
}

if (!isFocused) {
// Safeguard: scroll to before last read message
// Safeguard 1: scroll to first visible message before the read marker
const fallbackLastReadMessageId = this.$store.getters.getFirstDisplayableMessageIdBeforeReadMarker(this.token, this.visualLastReadMessageId)
if (fallbackLastReadMessageId) {
isFocused = this.focusMessage(fallbackLastReadMessageId, false, false)
}

if (!isFocused) {
// Safeguard 2: in case the fallback message is not found too
// scroll to bottom
this.scrollToBottom({ force: true })
} else {
this.$store.dispatch('setVisualLastReadMessageId', {
token: this.token,
id: fallbackLastReadMessageId,
})
} else {
// This is an ultimate safeguard in case the fallback message is not found too
// scroll to bottom
this.scrollToBottom({ force: true, smooth: true })
}
}

Expand Down Expand Up @@ -1083,7 +1086,7 @@ export default {
* @return {boolean} true if element was found, false otherwise
*/
focusMessage(messageId, smooth = true, highlightAnimation = true) {
const element = document.getElementById(`message_${messageId}`)
let element = document.getElementById(`message_${messageId}`)
if (!element) {
// Message id doesn't exist
// TODO: in some cases might need to trigger a scroll up if this is an older message
Expand All @@ -1092,6 +1095,11 @@ export default {
return false // element not found
}

if (element.offsetParent === null) {
console.debug('Message to focus is hidden, scrolling to its nearest visible parent', messageId)
element = element.closest('ul[style="display: none;"]').parentElement
}

console.debug('Scrolling to a focused message programmatically')
this.isFocusingMessage = true

Expand Down
12 changes: 12 additions & 0 deletions src/store/messagesStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ function hasMentionToSelf(context, message) {
return false
}

/**
* Returns whether the given message is presented in DOM and visible (none of the ancestors has `display: none`)
*
* @param {string} messageId store context
* @return {boolean} whether the message is visible in the UI
*/
function isMessageVisible(messageId) {
const element = document.getElementById(`message_${messageId}`)
return element !== null && element.offsetParent !== null
}

const state = {
/**
* Map of conversation token to message list
Expand Down Expand Up @@ -243,6 +254,7 @@ const getters = {

return getters.messagesList(token).findLast(message => {
return message.id < readMessageId
&& isMessageVisible(message.id)
&& !String(message.id).startsWith('temp-')
&& message.systemMessage !== 'reaction'
&& message.systemMessage !== 'reaction_deleted'
Expand Down

0 comments on commit 65b26f0

Please sign in to comment.