diff --git a/web/app/components/base/chat/chat/chat-input-area/hooks.ts b/web/app/components/base/chat/chat/chat-input-area/hooks.ts index 3559cb8799..5f54012a09 100644 --- a/web/app/components/base/chat/chat/chat-input-area/hooks.ts +++ b/web/app/components/base/chat/chat/chat-input-area/hooks.ts @@ -1,36 +1,47 @@ import { useCallback, - useEffect, - useMemo, useRef, useState, } from 'react' import type { TextAreaRef } from 'rc-textarea' export const useTextAreaHeight = () => { + const wrapperRef = useRef(null) const textareaRef = useRef(null) - const [height, setHeight] = useState(0) + const textValueRef = useRef(null) + const holdSpaceRef = useRef(null) + const [isMultipleLine, setIsMultipleLine] = useState(false) - const handleComputeHeight = () => { + const handleComputeHeight = useCallback(() => { const textareaElement = textareaRef.current?.resizableTextArea.textArea - if (textareaElement) { - const { height } = textareaElement.getBoundingClientRect() + if (wrapperRef.current && textareaElement && textValueRef.current && holdSpaceRef.current) { + const { width: wrapperWidth } = wrapperRef.current.getBoundingClientRect() + const { height: textareaHeight } = textareaElement.getBoundingClientRect() + const { width: textValueWidth } = textValueRef.current.getBoundingClientRect() + const { width: holdSpaceWidth } = holdSpaceRef.current.getBoundingClientRect() - setHeight(height) + if (textareaHeight > 32) { + setIsMultipleLine(true) + } + else { + if (textValueWidth + holdSpaceWidth >= wrapperWidth) + setIsMultipleLine(true) + else + setIsMultipleLine(false) + } } - } - - useEffect(() => { - handleComputeHeight() }, []) const handleTextareaResize = useCallback(() => { handleComputeHeight() - }, []) + }, [handleComputeHeight]) return { + wrapperRef, textareaRef, + textValueRef, + holdSpaceRef, handleTextareaResize, - isMultipleLine: useMemo(() => height > 32, [height]), + isMultipleLine, } } diff --git a/web/app/components/base/chat/chat/chat-input-area/index.tsx b/web/app/components/base/chat/chat/chat-input-area/index.tsx index be49248cb6..c79df7d7bb 100644 --- a/web/app/components/base/chat/chat/chat-input-area/index.tsx +++ b/web/app/components/base/chat/chat/chat-input-area/index.tsx @@ -1,49 +1,66 @@ import { memo, + useState, } from 'react' import Textarea from 'rc-textarea' -import { RiSendPlane2Fill } from '@remixicon/react' import { useTextAreaHeight } from './hooks' -import Button from '@/app/components/base/button' -import { FileUploaderInChatInput } from '@/app/components/base/file-uploader' +import Operation from './operation' import cn from '@/utils/classnames' +import { FileListFlexOperation } from '@/app/components/base/file-uploader' const ChatInputArea = () => { const { + wrapperRef, textareaRef, + textValueRef, + holdSpaceRef, handleTextareaResize, isMultipleLine, } = useTextAreaHeight() + const [value, setValue] = useState('') + + const operation = return (
-