Fluff: Jump to bottom when editing last message in chat. (up arrow)

This commit is contained in:
Paul 2021-07-06 23:03:29 +01:00
parent 9bbbf5ffc8
commit 8cfbe1ca3f
3 changed files with 9 additions and 1 deletions

View file

@ -18,6 +18,7 @@ export function internalEmit(ns: string, event: string, ...args: any[]) {
// Event structure: namespace/event // Event structure: namespace/event
/// Event List /// Event List
// - MessageArea/jump_to_bottom
// - MessageRenderer/edit_last // - MessageRenderer/edit_last
// - MessageRenderer/edit_message // - MessageRenderer/edit_message
// - Intermediate/open_profile // - Intermediate/open_profile

View file

@ -12,7 +12,7 @@ import {
} from "preact/hooks"; } from "preact/hooks";
import { defer } from "../../../lib/defer"; import { defer } from "../../../lib/defer";
import { internalEmit } from "../../../lib/eventEmitter"; import { internalEmit, internalSubscribe } from "../../../lib/eventEmitter";
import { SingletonMessageRenderer } from "../../../lib/renderer/Singleton"; import { SingletonMessageRenderer } from "../../../lib/renderer/Singleton";
import { RenderState, ScrollState } from "../../../lib/renderer/types"; import { RenderState, ScrollState } from "../../../lib/renderer/types";
@ -132,6 +132,12 @@ export function MessageArea({ id }: Props) {
const atTop = (offset = 0) => const atTop = (offset = 0) =>
ref.current ? ref.current.scrollTop <= offset : false; 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. // ? Handle events from renderer.
useEffect(() => { useEffect(() => {
SingletonMessageRenderer.addListener("state", setState); SingletonMessageRenderer.addListener("state", setState);

View file

@ -60,6 +60,7 @@ function MessageRenderer({ id, state, queue }: Props) {
for (let i = state.messages.length - 1; i >= 0; i--) { for (let i = state.messages.length - 1; i >= 0; i--) {
if (state.messages[i].author === userId) { if (state.messages[i].author === userId) {
setEditing(state.messages[i]._id); setEditing(state.messages[i]._id);
internalEmit('MessageArea', 'jump_to_bottom');
return; return;
} }
} }