From 2a79d356674236e4b4e345ae02236d52fd609f73 Mon Sep 17 00:00:00 2001 From: leedom Date: Sat, 1 Apr 2023 17:34:38 +0800 Subject: [PATCH 01/12] feat: add optimize textarea height when inputing --- app/components/home.module.scss | 2 +- app/components/home.tsx | 23 +++++++++++++++-------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/app/components/home.module.scss b/app/components/home.module.scss index 764805d..d88a3d5 100644 --- a/app/components/home.module.scss +++ b/app/components/home.module.scss @@ -414,7 +414,7 @@ background-color: var(--white); color: var(--black); font-family: inherit; - padding: 10px 14px 50px; + padding: 10px 90px 10px 14px; resize: none; outline: none; } diff --git a/app/components/home.tsx b/app/components/home.tsx index de93510..2ff87b3 100644 --- a/app/components/home.tsx +++ b/app/components/home.tsx @@ -108,7 +108,7 @@ export function ChatList() { state.currentSessionIndex, state.selectSession, state.removeSession, - ] + ], ); return ( @@ -192,6 +192,7 @@ export function Chat(props: { const inputRef = useRef(null); const [userInput, setUserInput] = useState(""); const [isLoading, setIsLoading] = useState(false); + const [textareaRows, setTextareaRows] = useState(2); const { submitKey, shouldSubmit } = useSubmitHandler(); // prompt hints @@ -202,7 +203,7 @@ export function Chat(props: { setPromptHints(promptStore.search(text)); }, 100, - { leading: true, trailing: true } + { leading: true, trailing: true }, ); const onPromptSelect = (prompt: Prompt) => { @@ -216,7 +217,7 @@ export function Chat(props: { if (!dom) return; const paddingBottomNum: number = parseInt( window.getComputedStyle(dom).paddingBottom, - 10 + 10, ); dom.scrollTop = dom.scrollHeight - dom.offsetHeight + paddingBottomNum; }; @@ -237,6 +238,11 @@ export function Chat(props: { onSearch(text.slice(1)); } } + + // textarea rows optimize + const length = text.split("\n").length - 1; + const rowsLength = length < 2 ? 2 : length > 6 ? 6 : length; + setTextareaRows(rowsLength > 6 ? 6 : rowsLength); }; // submit user input @@ -247,6 +253,7 @@ export function Chat(props: { setUserInput(""); setPromptHints([]); inputRef.current?.focus(); + setTextareaRows(2); }; // stop response @@ -304,7 +311,7 @@ export function Chat(props: { preview: true, }, ] - : [] + : [], ) .concat( userInput.length > 0 @@ -316,7 +323,7 @@ export function Chat(props: { preview: true, }, ] - : [] + : [], ); // auto scroll @@ -354,7 +361,7 @@ export function Chat(props: { const newTopic = prompt(Locale.Chat.Rename, session.topic); if (newTopic && newTopic !== session.topic) { chatStore.updateCurrentSession( - (session) => (session.topic = newTopic!) + (session) => (session.topic = newTopic!), ); } }} @@ -485,7 +492,7 @@ export function Chat(props: { ref={inputRef} className={styles["chat-input"]} placeholder={Locale.Chat.Input(submitKey)} - rows={4} + rows={textareaRows} onInput={(e) => onInput(e.currentTarget.value)} value={userInput} onKeyDown={(e) => onInputKeyDown(e as any)} @@ -603,7 +610,7 @@ export function Home() { state.newSession, state.currentSessionIndex, state.removeSession, - ] + ], ); const loading = !useHasHydrated(); const [showSideBar, setShowSideBar] = useState(true); From 9ba59351c5f623b5710e56f6c9397d70b9c307ff Mon Sep 17 00:00:00 2001 From: Leedom <30711792+leedom92@users.noreply.github.com> Date: Sat, 1 Apr 2023 17:48:05 +0800 Subject: [PATCH 02/12] Update home.tsx --- app/components/home.tsx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app/components/home.tsx b/app/components/home.tsx index 2ff87b3..6c606ed 100644 --- a/app/components/home.tsx +++ b/app/components/home.tsx @@ -108,7 +108,7 @@ export function ChatList() { state.currentSessionIndex, state.selectSession, state.removeSession, - ], + ] ); return ( @@ -203,7 +203,7 @@ export function Chat(props: { setPromptHints(promptStore.search(text)); }, 100, - { leading: true, trailing: true }, + { leading: true, trailing: true } ); const onPromptSelect = (prompt: Prompt) => { @@ -217,7 +217,7 @@ export function Chat(props: { if (!dom) return; const paddingBottomNum: number = parseInt( window.getComputedStyle(dom).paddingBottom, - 10, + 10 ); dom.scrollTop = dom.scrollHeight - dom.offsetHeight + paddingBottomNum; }; @@ -311,7 +311,7 @@ export function Chat(props: { preview: true, }, ] - : [], + : [] ) .concat( userInput.length > 0 @@ -323,7 +323,7 @@ export function Chat(props: { preview: true, }, ] - : [], + : [] ); // auto scroll @@ -361,7 +361,7 @@ export function Chat(props: { const newTopic = prompt(Locale.Chat.Rename, session.topic); if (newTopic && newTopic !== session.topic) { chatStore.updateCurrentSession( - (session) => (session.topic = newTopic!), + (session) => (session.topic = newTopic!) ); } }} @@ -610,7 +610,7 @@ export function Home() { state.newSession, state.currentSessionIndex, state.removeSession, - ], + ] ); const loading = !useHasHydrated(); const [showSideBar, setShowSideBar] = useState(true); From a811637176219828ea42e7aedb5558292f8539e6 Mon Sep 17 00:00:00 2001 From: leedom Date: Sun, 2 Apr 2023 02:40:00 +0800 Subject: [PATCH 03/12] refactor: use the method of element-ui --- app/components/home.tsx | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/app/components/home.tsx b/app/components/home.tsx index 6c606ed..01f1489 100644 --- a/app/components/home.tsx +++ b/app/components/home.tsx @@ -37,6 +37,8 @@ import { REPO_URL } from "../constant"; import { ControllerPool } from "../requests"; import { Prompt, usePromptStore } from "../store/prompt"; +import calcTextareaHeight from "../calcTextareaHeight"; + export function Loading(props: { noLogo?: boolean }) { return (
@@ -108,7 +110,7 @@ export function ChatList() { state.currentSessionIndex, state.selectSession, state.removeSession, - ] + ], ); return ( @@ -179,6 +181,10 @@ export function PromptHints(props: { export function Chat(props: { showSideBar?: () => void; sideBarShowing?: boolean; + autoSize: { + minRows: number; + maxRows: number; + }; }) { type RenderMessage = Message & { preview?: boolean }; @@ -190,9 +196,10 @@ export function Chat(props: { const fontSize = useChatStore((state) => state.config.fontSize); const inputRef = useRef(null); + const [userInput, setUserInput] = useState(""); const [isLoading, setIsLoading] = useState(false); - const [textareaRows, setTextareaRows] = useState(2); + const [textareaStyle, setTextareaStyle] = useState({}); const { submitKey, shouldSubmit } = useSubmitHandler(); // prompt hints @@ -203,7 +210,7 @@ export function Chat(props: { setPromptHints(promptStore.search(text)); }, 100, - { leading: true, trailing: true } + { leading: true, trailing: true }, ); const onPromptSelect = (prompt: Prompt) => { @@ -217,7 +224,7 @@ export function Chat(props: { if (!dom) return; const paddingBottomNum: number = parseInt( window.getComputedStyle(dom).paddingBottom, - 10 + 10, ); dom.scrollTop = dom.scrollHeight - dom.offsetHeight + paddingBottomNum; }; @@ -239,10 +246,13 @@ export function Chat(props: { } } - // textarea rows optimize - const length = text.split("\n").length - 1; - const rowsLength = length < 2 ? 2 : length > 6 ? 6 : length; - setTextareaRows(rowsLength > 6 ? 6 : rowsLength); + resizeTextarea(); + }; + + // set style for textarea + const resizeTextarea = () => { + const { minRows, maxRows } = props.autoSize; + setTextareaStyle(calcTextareaHeight(inputRef.current, minRows, maxRows)); }; // submit user input @@ -253,7 +263,6 @@ export function Chat(props: { setUserInput(""); setPromptHints([]); inputRef.current?.focus(); - setTextareaRows(2); }; // stop response @@ -311,7 +320,7 @@ export function Chat(props: { preview: true, }, ] - : [] + : [], ) .concat( userInput.length > 0 @@ -323,7 +332,7 @@ export function Chat(props: { preview: true, }, ] - : [] + : [], ); // auto scroll @@ -361,7 +370,7 @@ export function Chat(props: { const newTopic = prompt(Locale.Chat.Rename, session.topic); if (newTopic && newTopic !== session.topic) { chatStore.updateCurrentSession( - (session) => (session.topic = newTopic!) + (session) => (session.topic = newTopic!), ); } }} @@ -490,9 +499,9 @@ export function Chat(props: {