Skip to content

Commit 56c86d8

Browse files
committed
fix: fix chat input multiline cursor navigation issue
1 parent 1343d57 commit 56c86d8

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

packages/ui/src/views/chatmessage/ChatMessage.jsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, previews, setP
218218
const [inputHistory] = useState(new ChatInputHistory(10))
219219

220220
const inputRef = useRef(null)
221+
const isKeyboardCursorAtStart = useRef(true)
222+
const isKeyboardCursorAtEnd = useRef(true)
221223
const getChatmessageApi = useApi(chatmessageApi.getInternalChatmessageFromChatflow)
222224
const getAllExecutionsApi = useApi(executionsApi.getAllExecutions)
223225
const getIsChatflowStreamingApi = useApi(chatflowsApi.getIsChatflowStreaming)
@@ -1231,15 +1233,25 @@ const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, previews, setP
12311233
scrollToBottom()
12321234
}, 100)
12331235
}
1236+
// Allow cursor to navigate easily in multiline text (except at the end or start of the text)
1237+
const handleKeyboardCursorPosition = (e) => {
1238+
const targetValue = e.target.value ?? ''
1239+
const cursorPosition = e.target.selectionStart
1240+
isKeyboardCursorAtStart.current = cursorPosition === 0
1241+
isKeyboardCursorAtEnd.current = targetValue.length > 0 ? cursorPosition === targetValue.length : cursorPosition === 0
1242+
}
1243+
12341244
// Prevent blank submissions and allow for multiline input
12351245
const handleEnter = (e) => {
1246+
const isCursorPositionAtStart = isKeyboardCursorAtStart.current
1247+
const isCursorPositionAtEnd = isKeyboardCursorAtEnd.current
12361248
// Check if IME composition is in progress
12371249
const isIMEComposition = e.isComposing || e.keyCode === 229
1238-
if (e.key === 'ArrowUp' && !isIMEComposition) {
1250+
if (e.key === 'ArrowUp' && !isIMEComposition && isCursorPositionAtStart) {
12391251
e.preventDefault()
12401252
const previousInput = inputHistory.getPreviousInput(userInput)
12411253
setUserInput(previousInput)
1242-
} else if (e.key === 'ArrowDown' && !isIMEComposition) {
1254+
} else if (e.key === 'ArrowDown' && !isIMEComposition && isCursorPositionAtEnd) {
12431255
e.preventDefault()
12441256
const nextInput = inputHistory.getNextInput()
12451257
setUserInput(nextInput)
@@ -3028,6 +3040,7 @@ const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, previews, setP
30283040
sx={{ width: '100%' }}
30293041
disabled={getInputDisabled()}
30303042
onKeyDown={handleEnter}
3043+
onKeyUp={handleKeyboardCursorPosition}
30313044
id='userInput'
30323045
name='userInput'
30333046
placeholder={loading ? 'Waiting for response...' : 'Type your question...'}

0 commit comments

Comments
 (0)