Fix regression with new message loading

Event listeners never got registered because of timing issue (MatrixClient was not ready yet). Issue #85.
This commit is contained in:
N-Pex 2021-03-04 11:31:21 +01:00
parent d628501529
commit b0aea0bd81
3 changed files with 56 additions and 15 deletions

View file

@ -42,7 +42,7 @@
.join-user-info { .join-user-info {
display: flex; display: flex;
flex-wrap: nowrap; flex-wrap: nowrap;
max-width: 300px; max-width: 500px;
} }
} }

View file

@ -408,8 +408,6 @@ export default {
mounted() { mounted() {
const container = this.$refs.chatContainer; const container = this.$refs.chatContainer;
this.scrollPosition = new ScrollPosition(container); this.scrollPosition = new ScrollPosition(container);
this.$matrix.on("Room.timeline", this.onEvent);
this.$matrix.on("RoomMember.typing", this.onUserTyping);
this.chatContainerSize = this.$refs.chatContainerResizer.$el.clientHeight; this.chatContainerSize = this.$refs.chatContainerResizer.$el.clientHeight;
}, },
@ -513,6 +511,9 @@ export default {
); );
// Clear old events // Clear old events
this.$matrix.off("Room.timeline", this.onEvent);
this.$matrix.off("RoomMember.typing", this.onUserTyping);
this.events = []; this.events = [];
this.timelineWindow = null; this.timelineWindow = null;
this.typingMembers = []; this.typingMembers = [];
@ -526,7 +527,11 @@ export default {
// Public room? // Public room?
if (this.roomId && this.roomId.startsWith("#")) { if (this.roomId && this.roomId.startsWith("#")) {
this.onRoomNotJoined(); this.onRoomNotJoined();
} else if (this.roomId) {
this.onRoomNotJoined(); // Private room we are not joined to. What to do? We redirect to join
// screen, maybe the user has an invite already?
} }
this.initialLoadDone = true;
return; // no room return; // no room
} }
@ -543,9 +548,13 @@ export default {
methods: { methods: {
onRoomJoined(initialEventId) { onRoomJoined(initialEventId) {
// Listen to events
this.$matrix.on("Room.timeline", this.onEvent);
this.$matrix.on("RoomMember.typing", this.onUserTyping);
console.log("Read up to " + initialEventId); console.log("Read up to " + initialEventId);
initialEventId = null; //initialEventId = null;
this.timelineWindow = new TimelineWindow( this.timelineWindow = new TimelineWindow(
this.$matrix.matrixClient, this.$matrix.matrixClient,

View file

@ -349,27 +349,59 @@ class Util {
} }
getFirstVisibleElement(parentNode) { getFirstVisibleElement(parentNode) {
const y = parentNode.scrollTop; const visible = this.findVisibleElements(parentNode);
return this.getElementAtY(parentNode, y); if (visible && visible.length > 0) {
return visible[0];
}
return null;
} }
getLastVisibleElement(parentNode) { getLastVisibleElement(parentNode) {
const y = parentNode.scrollTop + parentNode.clientHeight; const visible = this.findVisibleElements(parentNode);
return this.getElementAtY(parentNode, y); if (visible && visible.length > 0) {
return visible[visible.length - 1];
}
return null;
} }
getElementAtY(parentNode, y) { findVisibleElements(parentNode) {
const middle = this.findOneVisibleElement(parentNode);
if (middle) {
var nodes = [parentNode.children[middle]];
var i = middle - 1;
while (i >= 0 && this.isChildVisible(parentNode, parentNode.children[i])) {
nodes.splice(0,0,parentNode.children[i]);
i-=1;
}
i = middle + 1;
while (i < parentNode.children.length && this.isChildVisible(parentNode, parentNode.children[i])) {
nodes.push(parentNode.children[i]);
i+=1;
}
return nodes;
}
return null; // No visible found
}
isChildVisible(parentNode, childNode) {
let top = parentNode.scrollTop;
let bottom = top + parentNode.clientHeight;
const childTop = childNode.offsetTop;
const childBottom = childTop + childNode.clientHeight;
return ((childTop >= top && childTop < bottom) || (childBottom > top && childBottom <= bottom));
}
findOneVisibleElement(parentNode) {
let start = 0; let start = 0;
let end = parentNode.children.length - 1; let end = parentNode.children.length - 1;
let top = parentNode.scrollTop;
while (start <= end) { while (start <= end) {
let middle = Math.floor((start + end) / 2); let middle = Math.floor((start + end) / 2);
const yMiddleTop = parentNode.children[middle].offsetTop; let childNode = parentNode.children[middle];
const yMiddleBottom = yMiddleTop + parentNode.children[middle].clientHeight; if (this.isChildVisible(parentNode, childNode)) {
if (yMiddleTop <= y && yMiddleBottom >= y) {
// found the key // found the key
return parentNode.children[middle]; return middle;
} else if (yMiddleBottom < y) { } else if (childNode.offsetTop < top) {
// continue searching to the right // continue searching to the right
start = middle + 1; start = middle + 1;
} else { } else {