mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-05-20 04:30:17 +09:00
Refactor auto scroll functionality.
This commit is contained in:
parent
98e323af4c
commit
520c2850b4
@ -1,7 +1,6 @@
|
|||||||
import { useDebouncedCallback } from "use-debounce";
|
import { useDebouncedCallback } from "use-debounce";
|
||||||
import React, {
|
import React, {
|
||||||
Fragment,
|
Fragment,
|
||||||
RefObject,
|
|
||||||
useCallback,
|
useCallback,
|
||||||
useEffect,
|
useEffect,
|
||||||
useMemo,
|
useMemo,
|
||||||
@ -450,53 +449,12 @@ export function ChatAction(props: {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function useScrollToBottom(
|
|
||||||
scrollRef: RefObject<HTMLDivElement>,
|
|
||||||
detach: boolean = false,
|
|
||||||
messages: ChatMessage[],
|
|
||||||
) {
|
|
||||||
// for auto-scroll
|
|
||||||
const [autoScroll, setAutoScroll] = useState(true);
|
|
||||||
const scrollDomToBottom = useCallback(() => {
|
|
||||||
const dom = scrollRef.current;
|
|
||||||
if (dom) {
|
|
||||||
requestAnimationFrame(() => {
|
|
||||||
setAutoScroll(true);
|
|
||||||
dom.scrollTo(0, dom.scrollHeight);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, [scrollRef]);
|
|
||||||
|
|
||||||
// auto scroll
|
|
||||||
useEffect(() => {
|
|
||||||
if (autoScroll && !detach) {
|
|
||||||
scrollDomToBottom();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// auto scroll when messages length changes
|
|
||||||
const lastMessagesLength = useRef(messages.length);
|
|
||||||
useEffect(() => {
|
|
||||||
if (messages.length > lastMessagesLength.current && !detach) {
|
|
||||||
scrollDomToBottom();
|
|
||||||
}
|
|
||||||
lastMessagesLength.current = messages.length;
|
|
||||||
}, [messages.length, detach, scrollDomToBottom]);
|
|
||||||
|
|
||||||
return {
|
|
||||||
scrollRef,
|
|
||||||
autoScroll,
|
|
||||||
setAutoScroll,
|
|
||||||
scrollDomToBottom,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function ChatActions(props: {
|
export function ChatActions(props: {
|
||||||
uploadImage: () => void;
|
uploadImage: () => void;
|
||||||
setAttachImages: (images: string[]) => void;
|
setAttachImages: (images: string[]) => void;
|
||||||
setUploading: (uploading: boolean) => void;
|
setUploading: (uploading: boolean) => void;
|
||||||
showPromptModal: () => void;
|
showPromptModal: () => void;
|
||||||
scrollToBottom: () => void;
|
scrollChatToBottom: () => void;
|
||||||
showPromptHints: () => void;
|
showPromptHints: () => void;
|
||||||
hitBottom: boolean;
|
hitBottom: boolean;
|
||||||
uploading: boolean;
|
uploading: boolean;
|
||||||
@ -608,7 +566,7 @@ export function ChatActions(props: {
|
|||||||
)}
|
)}
|
||||||
{!props.hitBottom && (
|
{!props.hitBottom && (
|
||||||
<ChatAction
|
<ChatAction
|
||||||
onClick={props.scrollToBottom}
|
onClick={props.scrollChatToBottom}
|
||||||
text={Locale.Chat.InputActions.ToBottom}
|
text={Locale.Chat.InputActions.ToBottom}
|
||||||
icon={<BottomIcon />}
|
icon={<BottomIcon />}
|
||||||
/>
|
/>
|
||||||
@ -997,37 +955,12 @@ function _Chat() {
|
|||||||
|
|
||||||
const [showExport, setShowExport] = useState(false);
|
const [showExport, setShowExport] = useState(false);
|
||||||
|
|
||||||
|
const scrollRef = useRef<HTMLDivElement>(null);
|
||||||
const inputRef = useRef<HTMLTextAreaElement>(null);
|
const inputRef = useRef<HTMLTextAreaElement>(null);
|
||||||
const [userInput, setUserInput] = useState("");
|
const [userInput, setUserInput] = useState("");
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
const { submitKey, shouldSubmit } = useSubmitHandler();
|
const { submitKey, shouldSubmit } = useSubmitHandler();
|
||||||
const scrollRef = useRef<HTMLDivElement>(null);
|
|
||||||
const isScrolledToBottom = scrollRef?.current
|
|
||||||
? Math.abs(
|
|
||||||
scrollRef.current.scrollHeight -
|
|
||||||
(scrollRef.current.scrollTop + scrollRef.current.clientHeight),
|
|
||||||
) <= 1
|
|
||||||
: false;
|
|
||||||
const isAttachWithTop = useMemo(() => {
|
|
||||||
const lastMessage = scrollRef.current?.lastElementChild as HTMLElement;
|
|
||||||
// if scrolllRef is not ready or no message, return false
|
|
||||||
if (!scrollRef?.current || !lastMessage) return false;
|
|
||||||
const topDistance =
|
|
||||||
lastMessage!.getBoundingClientRect().top -
|
|
||||||
scrollRef.current.getBoundingClientRect().top;
|
|
||||||
// leave some space for user question
|
|
||||||
return topDistance < 100;
|
|
||||||
}, [scrollRef?.current?.scrollHeight]);
|
|
||||||
|
|
||||||
const isTyping = userInput !== "";
|
|
||||||
|
|
||||||
// if user is typing, should auto scroll to bottom
|
|
||||||
// if user is not typing, should auto scroll to bottom only if already at bottom
|
|
||||||
const { setAutoScroll, scrollDomToBottom } = useScrollToBottom(
|
|
||||||
scrollRef,
|
|
||||||
(isScrolledToBottom || isAttachWithTop) && !isTyping,
|
|
||||||
session.messages,
|
|
||||||
);
|
|
||||||
const [hitBottom, setHitBottom] = useState(true);
|
const [hitBottom, setHitBottom] = useState(true);
|
||||||
const isMobileScreen = useMobileScreen();
|
const isMobileScreen = useMobileScreen();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
@ -1104,6 +1037,7 @@ function _Chat() {
|
|||||||
|
|
||||||
const doSubmit = (userInput: string) => {
|
const doSubmit = (userInput: string) => {
|
||||||
if (userInput.trim() === "" && isEmpty(attachImages)) return;
|
if (userInput.trim() === "" && isEmpty(attachImages)) return;
|
||||||
|
|
||||||
const matchCommand = chatCommands.match(userInput);
|
const matchCommand = chatCommands.match(userInput);
|
||||||
if (matchCommand.matched) {
|
if (matchCommand.matched) {
|
||||||
setUserInput("");
|
setUserInput("");
|
||||||
@ -1111,16 +1045,19 @@ function _Chat() {
|
|||||||
matchCommand.invoke();
|
matchCommand.invoke();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
chatStore
|
|
||||||
.onUserInput(userInput, attachImages)
|
chatStore.onUserInput(userInput, attachImages).then(() => {
|
||||||
.then(() => setIsLoading(false));
|
setIsLoading(false);
|
||||||
|
autoScrollChatToBottom();
|
||||||
|
});
|
||||||
|
|
||||||
setAttachImages([]);
|
setAttachImages([]);
|
||||||
chatStore.setLastInput(userInput);
|
chatStore.setLastInput(userInput);
|
||||||
setUserInput("");
|
setUserInput("");
|
||||||
setPromptHints([]);
|
setPromptHints([]);
|
||||||
if (!isMobileScreen) inputRef.current?.focus();
|
autoScrollChatToBottom();
|
||||||
setAutoScroll(true);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const onPromptSelect = (prompt: RenderPrompt) => {
|
const onPromptSelect = (prompt: RenderPrompt) => {
|
||||||
@ -1420,14 +1357,33 @@ function _Chat() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setHitBottom(isHitBottom);
|
setHitBottom(isHitBottom);
|
||||||
setAutoScroll(isHitBottom);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function scrollToBottom() {
|
function scrollChatToBottom() {
|
||||||
setMsgRenderIndex(renderMessages.length - CHAT_PAGE_SIZE);
|
const dom = scrollRef.current;
|
||||||
scrollDomToBottom();
|
if (dom) {
|
||||||
|
setMsgRenderIndex(renderMessages.length - CHAT_PAGE_SIZE);
|
||||||
|
requestAnimationFrame(() => {
|
||||||
|
dom.scrollTo(0, dom.scrollHeight);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// scroll if auto-scroll is enabled in the settings
|
||||||
|
function autoScrollChatToBottom() {
|
||||||
|
if (config.enableAutoScroll) scrollChatToBottom();
|
||||||
|
}
|
||||||
|
|
||||||
|
// scroll to the bottom on mount
|
||||||
|
useEffect(() => {
|
||||||
|
scrollChatToBottom();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
// keep scroll the chat as it gets longer, but only if the chat is already scrolled to the bottom (sticky bottom)
|
||||||
|
useEffect(() => {
|
||||||
|
if (hitBottom) scrollChatToBottom();
|
||||||
|
});
|
||||||
|
|
||||||
// clear context index = context length + index in messages
|
// clear context index = context length + index in messages
|
||||||
const clearContextIndex =
|
const clearContextIndex =
|
||||||
(session.clearContextIndex ?? -1) >= 0
|
(session.clearContextIndex ?? -1) >= 0
|
||||||
@ -1775,10 +1731,7 @@ function _Chat() {
|
|||||||
ref={scrollRef}
|
ref={scrollRef}
|
||||||
onScroll={(e) => onChatBodyScroll(e.currentTarget)}
|
onScroll={(e) => onChatBodyScroll(e.currentTarget)}
|
||||||
onMouseDown={() => inputRef.current?.blur()}
|
onMouseDown={() => inputRef.current?.blur()}
|
||||||
onTouchStart={() => {
|
onTouchStart={() => inputRef.current?.blur()}
|
||||||
inputRef.current?.blur();
|
|
||||||
setAutoScroll(false);
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
{messages
|
{messages
|
||||||
// TODO
|
// TODO
|
||||||
@ -2050,7 +2003,7 @@ function _Chat() {
|
|||||||
setAttachImages={setAttachImages}
|
setAttachImages={setAttachImages}
|
||||||
setUploading={setUploading}
|
setUploading={setUploading}
|
||||||
showPromptModal={() => setShowPromptModal(true)}
|
showPromptModal={() => setShowPromptModal(true)}
|
||||||
scrollToBottom={scrollToBottom}
|
scrollChatToBottom={scrollChatToBottom}
|
||||||
hitBottom={hitBottom}
|
hitBottom={hitBottom}
|
||||||
uploading={uploading}
|
uploading={uploading}
|
||||||
showPromptHints={() => {
|
showPromptHints={() => {
|
||||||
@ -2083,8 +2036,8 @@ function _Chat() {
|
|||||||
onInput={(e) => onInput(e.currentTarget.value)}
|
onInput={(e) => onInput(e.currentTarget.value)}
|
||||||
value={userInput}
|
value={userInput}
|
||||||
onKeyDown={onInputKeyDown}
|
onKeyDown={onInputKeyDown}
|
||||||
onFocus={scrollToBottom}
|
onFocus={autoScrollChatToBottom}
|
||||||
onClick={scrollToBottom}
|
onClick={autoScrollChatToBottom}
|
||||||
onPaste={handlePaste}
|
onPaste={handlePaste}
|
||||||
rows={inputRows}
|
rows={inputRows}
|
||||||
autoFocus={autoFocus}
|
autoFocus={autoFocus}
|
||||||
|
Loading…
Reference in New Issue
Block a user