From 8cfbe1ca3fb3389164285b1fb176bed726358fee Mon Sep 17 00:00:00 2001 From: Paul Date: Tue, 6 Jul 2021 23:03:29 +0100 Subject: [PATCH] Fluff: Jump to bottom when editing last message in chat. (up arrow) --- src/lib/eventEmitter.ts | 1 + src/pages/channels/messaging/MessageArea.tsx | 8 +++++++- src/pages/channels/messaging/MessageRenderer.tsx | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/lib/eventEmitter.ts b/src/lib/eventEmitter.ts index e2881c8d..192bae46 100644 --- a/src/lib/eventEmitter.ts +++ b/src/lib/eventEmitter.ts @@ -18,6 +18,7 @@ export function internalEmit(ns: string, event: string, ...args: any[]) { // Event structure: namespace/event /// Event List +// - MessageArea/jump_to_bottom // - MessageRenderer/edit_last // - MessageRenderer/edit_message // - Intermediate/open_profile diff --git a/src/pages/channels/messaging/MessageArea.tsx b/src/pages/channels/messaging/MessageArea.tsx index 2cc14690..23675f4d 100644 --- a/src/pages/channels/messaging/MessageArea.tsx +++ b/src/pages/channels/messaging/MessageArea.tsx @@ -12,7 +12,7 @@ import { } from "preact/hooks"; import { defer } from "../../../lib/defer"; -import { internalEmit } from "../../../lib/eventEmitter"; +import { internalEmit, internalSubscribe } from "../../../lib/eventEmitter"; import { SingletonMessageRenderer } from "../../../lib/renderer/Singleton"; import { RenderState, ScrollState } from "../../../lib/renderer/types"; @@ -132,6 +132,12 @@ export function MessageArea({ id }: Props) { const atTop = (offset = 0) => ref.current ? ref.current.scrollTop <= offset : false; + // ? Handle global jump to bottom, e.g. when editing last message in chat. + useEffect(() => { + return internalSubscribe('MessageArea', 'jump_to_bottom', + () => setScrollState({ type: 'ScrollToBottom' })); + }, []); + // ? Handle events from renderer. useEffect(() => { SingletonMessageRenderer.addListener("state", setState); diff --git a/src/pages/channels/messaging/MessageRenderer.tsx b/src/pages/channels/messaging/MessageRenderer.tsx index 91856852..d573578b 100644 --- a/src/pages/channels/messaging/MessageRenderer.tsx +++ b/src/pages/channels/messaging/MessageRenderer.tsx @@ -60,6 +60,7 @@ function MessageRenderer({ id, state, queue }: Props) { for (let i = state.messages.length - 1; i >= 0; i--) { if (state.messages[i].author === userId) { setEditing(state.messages[i]._id); + internalEmit('MessageArea', 'jump_to_bottom'); return; } }