Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 677903c

Browse files
committed
Fix: Reset scrollIntoView state earlier
1 parent 0d07613 commit 677903c

File tree

3 files changed

+46
-34
lines changed

3 files changed

+46
-34
lines changed

src/components/structures/RoomView.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1296,8 +1296,11 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
12961296
});
12971297
}
12981298
this.updateTopUnreadMessagesBar();
1299+
};
12991300

1300-
if (this.state.initialEventId && this.state.initialEventScrollIntoView) {
1301+
private resetJumpToEvent = (eventId?: string) => {
1302+
if (this.state.initialEventId && this.state.initialEventScrollIntoView &&
1303+
this.state.initialEventId === eventId) {
13011304
debuglog("Removing scroll_into_view flag from initial event");
13021305
dis.dispatch<ViewRoomPayload>({
13031306
action: Action.ViewRoom,
@@ -2064,6 +2067,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
20642067
eventScrollIntoView={this.state.initialEventScrollIntoView}
20652068
eventPixelOffset={this.state.initialEventPixelOffset}
20662069
onScroll={this.onMessageListScroll}
2070+
onEventScrolledIntoView={this.resetJumpToEvent}
20672071
onReadMarkerUpdated={this.updateTopUnreadMessagesBar}
20682072
showUrlPreview={this.state.showUrlPreview}
20692073
className={this.messagePanelClassNames}

src/components/structures/ThreadView.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,9 @@ export default class ThreadView extends React.Component<IProps, IState> {
216216
}
217217
};
218218

219-
private resetJumpToEvent = (): void => {
220-
if (this.props.initialEvent && this.props.initialEventScrollIntoView) {
219+
private resetJumpToEvent = (event?: string): void => {
220+
if (this.props.initialEvent && this.props.initialEventScrollIntoView &&
221+
event === this.props.initialEvent?.getId()) {
221222
dis.dispatch<ViewRoomPayload>({
222223
action: Action.ViewRoom,
223224
room_id: this.props.room.roomId,
@@ -375,7 +376,7 @@ export default class ThreadView extends React.Component<IProps, IState> {
375376
eventId={this.props.initialEvent?.getId()}
376377
highlightedEventId={highlightedEventId}
377378
eventScrollIntoView={this.props.initialEventScrollIntoView}
378-
onScroll={this.resetJumpToEvent}
379+
onEventScrolledIntoView={this.resetJumpToEvent}
379380
onPaginationRequest={this.onPaginationRequest}
380381
/>
381382
</div> }

src/components/structures/TimelinePanel.tsx

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ interface IProps {
127127
// callback which is called when the panel is scrolled.
128128
onScroll?(event: Event): void;
129129

130+
onEventScrolledIntoView?(eventId?: string): void;
131+
130132
// callback which is called when the read-up-to mark is updated.
131133
onReadMarkerUpdated?(): void;
132134

@@ -1128,6 +1130,40 @@ class TimelinePanel extends React.Component<IProps, IState> {
11281130
return this.loadTimeline(initialEvent, pixelOffset, offsetBase, props.eventScrollIntoView);
11291131
}
11301132

1133+
private scrollIntoView(eventId?: string, pixelOffset?: number, offsetBase?: number): void {
1134+
const doScroll = () => {
1135+
if (eventId) {
1136+
debuglog("TimelinePanel scrolling to eventId " + eventId +
1137+
" at position " + (offsetBase * 100) + "% + " + pixelOffset);
1138+
this.messagePanel.current.scrollToEvent(
1139+
eventId,
1140+
pixelOffset,
1141+
offsetBase,
1142+
);
1143+
} else {
1144+
debuglog("TimelinePanel scrolling to bottom");
1145+
this.messagePanel.current.scrollToBottom();
1146+
}
1147+
};
1148+
1149+
debuglog("TimelinePanel scheduling scroll to event");
1150+
this.props.onEventScrolledIntoView?.(eventId);
1151+
// Ensure the correct scroll position pre render, if the messages have already been loaded to DOM,
1152+
// to avoid it jumping around
1153+
doScroll();
1154+
1155+
// Ensure the correct scroll position post render for correct behaviour.
1156+
//
1157+
// requestAnimationFrame runs our code immediately after the DOM update but before the next repaint.
1158+
//
1159+
// If the messages have just been loaded for the first time, this ensures we'll repeat setting the
1160+
// correct scroll position after React has re-rendered the TimelinePanel and MessagePanel and
1161+
// updated the DOM.
1162+
window.requestAnimationFrame(() => {
1163+
doScroll();
1164+
});
1165+
}
1166+
11311167
/**
11321168
* (re)-load the event timeline, and initialise the scroll state, centered
11331169
* around the given event.
@@ -1180,37 +1216,8 @@ class TimelinePanel extends React.Component<IProps, IState> {
11801216
return;
11811217
}
11821218

1183-
const doScroll = () => {
1184-
if (eventId) {
1185-
debuglog("TimelinePanel scrolling to eventId " + eventId +
1186-
" at position " + (offsetBase * 100) + "% + " + pixelOffset);
1187-
this.messagePanel.current.scrollToEvent(
1188-
eventId,
1189-
pixelOffset,
1190-
offsetBase,
1191-
);
1192-
} else {
1193-
debuglog("TimelinePanel scrolling to bottom");
1194-
this.messagePanel.current.scrollToBottom();
1195-
}
1196-
};
1197-
11981219
if (scrollIntoView) {
1199-
debuglog("TimelinePanel scheduling scroll to event");
1200-
// Ensure the correct scroll position pre render, if the messages have already been loaded to DOM, to
1201-
// avoid it jumping around
1202-
doScroll();
1203-
1204-
// Ensure the correct scroll position post render for correct behaviour.
1205-
//
1206-
// requestAnimationFrame runs our code immediately after the DOM update but before the next repaint.
1207-
//
1208-
// If the messages have just been loaded for the first time, this ensures we'll repeat setting the
1209-
// correct scroll position after React has re-rendered the TimelinePanel and MessagePanel and updated
1210-
// the DOM.
1211-
window.requestAnimationFrame(doScroll);
1212-
} else {
1213-
debuglog("TimelinePanel ignoring scroll, as requested");
1220+
this.scrollIntoView(eventId, pixelOffset, offsetBase);
12141221
}
12151222

12161223
if (this.props.sendReadReceiptOnLoad) {

0 commit comments

Comments
 (0)