From 7c5046ccff530210c92ac8f3aa99275e4727e434 Mon Sep 17 00:00:00 2001 From: josc146 Date: Thu, 27 Apr 2023 15:29:56 +0800 Subject: [PATCH] feat: resizable input box (#246, #266) --- src/components/ConversationCard/index.jsx | 3 +- src/components/InputBox/index.jsx | 76 ++++++++++++++++++----- src/content-script/styles.scss | 10 +-- 3 files changed, 67 insertions(+), 22 deletions(-) diff --git a/src/components/ConversationCard/index.jsx b/src/components/ConversationCard/index.jsx index 1f14dba..11b5d6e 100644 --- a/src/components/ConversationCard/index.jsx +++ b/src/components/ConversationCard/index.jsx @@ -365,6 +365,8 @@ function ConversationCard(props) { { const newQuestion = new ConversationItemData('question', question + '\n
') const newAnswer = new ConversationItemData( @@ -382,7 +384,6 @@ function ConversationCard(props) { updateAnswer(e, false, 'error') } }} - port={port} /> ) diff --git a/src/components/InputBox/index.jsx b/src/components/InputBox/index.jsx index 34f983f..b7db950 100644 --- a/src/components/InputBox/index.jsx +++ b/src/components/InputBox/index.jsx @@ -3,13 +3,37 @@ import PropTypes from 'prop-types' import { updateRefHeight } from '../../utils' import { useTranslation } from 'react-i18next' -export function InputBox({ onSubmit, enabled, port }) { +export function InputBox({ onSubmit, enabled, port, reverseResizeDir }) { const { t } = useTranslation() const [value, setValue] = useState('') + const reverseDivRef = useRef(null) const inputRef = useRef(null) + const resizedRef = useRef(false) + + const virtualInputRef = reverseResizeDir ? reverseDivRef : inputRef useEffect(() => { - updateRefHeight(inputRef) + const onResizeY = () => { + if (virtualInputRef.current.h !== virtualInputRef.current.offsetHeight) { + virtualInputRef.current.h = virtualInputRef.current.offsetHeight + if (!resizedRef.current) { + resizedRef.current = true + virtualInputRef.current.style.maxHeight = '' + } + } + } + virtualInputRef.current.h = virtualInputRef.current.offsetHeight + virtualInputRef.current.addEventListener('mousemove', onResizeY) + }, []) + + useEffect(() => { + if (!resizedRef.current) { + if (!reverseResizeDir) { + updateRefHeight(inputRef) + virtualInputRef.current.h = virtualInputRef.current.offsetHeight + virtualInputRef.current.style.maxHeight = '160px' + } + } }) useEffect(() => { @@ -33,20 +57,37 @@ export function InputBox({ onSubmit, enabled, port }) { return (
-