From 80aa672034941caa5243d3a889c4a156a49ae88d Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Mon, 18 Oct 2021 17:34:13 +1100 Subject: [PATCH] link pgup and down in messages list Fixes #1919 --- .../conversation/SessionMessagesList.tsx | 12 ++++++ .../SessionMessagesListContainer.tsx | 40 ++++++++++++++++--- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/ts/components/session/conversation/SessionMessagesList.tsx b/ts/components/session/conversation/SessionMessagesList.tsx index 5bcbafbc7..0a1e79a4e 100644 --- a/ts/components/session/conversation/SessionMessagesList.tsx +++ b/ts/components/session/conversation/SessionMessagesList.tsx @@ -1,5 +1,7 @@ import React from 'react'; import { useSelector } from 'react-redux'; +// tslint:disable-next-line: no-submodule-imports +import useKey from 'react-use/lib/useKey'; import { PropsForDataExtractionNotification, QuoteClickOptions } from '../../../models/messageType'; import { PropsForExpirationTimer, @@ -19,9 +21,19 @@ import { SessionLastSeenIndicator } from './SessionLastSeenIndicator'; export const SessionMessagesList = (props: { scrollToQuoteMessage: (options: QuoteClickOptions) => Promise; + onPageUpPressed: () => void; + onPageDownPressed: () => void; }) => { const messagesProps = useSelector(getSortedMessagesTypesOfSelectedConversation); + useKey('PageUp', () => { + props.onPageUpPressed(); + }); + + useKey('PageDown', () => { + props.onPageDownPressed(); + }); + return ( <> {messagesProps.map(messageProps => { diff --git a/ts/components/session/conversation/SessionMessagesListContainer.tsx b/ts/components/session/conversation/SessionMessagesListContainer.tsx index 552e003e5..fc12d0a97 100644 --- a/ts/components/session/conversation/SessionMessagesListContainer.tsx +++ b/ts/components/session/conversation/SessionMessagesListContainer.tsx @@ -30,7 +30,7 @@ import { import { SessionMessagesList } from './SessionMessagesList'; export type SessionMessageListProps = { - messageContainerRef: React.RefObject; + messageContainerRef: React.RefObject; }; type Props = SessionMessageListProps & { @@ -82,7 +82,7 @@ class SessionMessagesListContainerInner extends React.Component { const newFirstMesssageId = this.props.messagesProps[0]?.propsForMessage.id; const messageAddedWasMoreRecentOne = prevFirstMesssageId !== newFirstMesssageId; - if (isSameConvo && snapShot?.realScrollTop && prevMsgLength !== newMsgLength) { + if (isSameConvo && snapShot?.realScrollTop && prevMsgLength !== newMsgLength && currentRef) { if (messageAddedWasMoreRecentOne) { if (snapShot.scrollHeight - snapShot.realScrollTop < 50) { // consider that we were scrolled to bottom @@ -105,13 +105,13 @@ class SessionMessagesListContainerInner extends React.Component { public getSnapshotBeforeUpdate() { const messageContainer = this.props.messageContainerRef.current; - const scrollTop = messageContainer.scrollTop; - const scrollHeight = messageContainer.scrollHeight; + const scrollTop = messageContainer?.scrollTop || undefined; + const scrollHeight = messageContainer?.scrollHeight || undefined; // as we use column-reverse for displaying message list // the top is < 0 // tslint:disable-next-line: restrict-plus-operands - const realScrollTop = scrollHeight + scrollTop; + const realScrollTop = scrollHeight && scrollTop ? scrollHeight + scrollTop : undefined; return { realScrollTop, fakeScrollTop: scrollTop, @@ -147,7 +147,11 @@ class SessionMessagesListContainerInner extends React.Component { key="typing-bubble" /> - + @@ -239,6 +243,30 @@ class SessionMessagesListContainerInner extends React.Component { messageContainer.scrollTop = messageContainer.scrollHeight - messageContainer.clientHeight; } + private scrollPgUp() { + const messageContainer = this.props.messageContainerRef.current; + if (!messageContainer) { + return; + } + messageContainer.scrollBy({ + top: Math.floor(-messageContainer.clientHeight * 2) / 3, + behavior: 'smooth', + }); + } + + private scrollPgDown() { + const messageContainer = this.props.messageContainerRef.current; + if (!messageContainer) { + return; + } + + // tslint:disable-next-line: restrict-plus-operands + messageContainer.scrollBy({ + top: Math.floor(+messageContainer.clientHeight * 2) / 3, + behavior: 'smooth', + }); + } + private async scrollToQuoteMessage(options: QuoteClickOptions) { const { quoteAuthor, quoteId, referencedMessageNotFound } = options;