Skip to content

Commit 104dec2

Browse files
committed
fix: fix chat input multiline cursor navigation issue
1 parent f0ac71f commit 104dec2

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
@@ -217,6 +217,8 @@ const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, previews, setP
217217
const [inputHistory] = useState(new ChatInputHistory(10))
218218

219219
const inputRef = useRef(null)
220+
const isKeyboardCursorAtStart = useRef(true)
221+
const isKeyboardCursorAtEnd = useRef(true)
220222
const getChatmessageApi = useApi(chatmessageApi.getInternalChatmessageFromChatflow)
221223
const getAllExecutionsApi = useApi(executionsApi.getAllExecutions)
222224
const getIsChatflowStreamingApi = useApi(chatflowsApi.getIsChatflowStreaming)
@@ -1184,15 +1186,25 @@ const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, previews, setP
11841186
scrollToBottom()
11851187
}, 100)
11861188
}
1189+
// Allow cursor to navigate easily in multiline text (except at the end or start of the text)
1190+
const handleKeyboardCursorPosition = (e) => {
1191+
const targetValue = e.target.value ?? ''
1192+
const cursorPosition = e.target.selectionStart
1193+
isKeyboardCursorAtStart.current = cursorPosition === 0
1194+
isKeyboardCursorAtEnd.current = targetValue.length > 0 ? cursorPosition === targetValue.length : cursorPosition === 0
1195+
}
1196+
11871197
// Prevent blank submissions and allow for multiline input
11881198
const handleEnter = (e) => {
1199+
const isCursorPositionAtStart = isKeyboardCursorAtStart.current
1200+
const isCursorPositionAtEnd = isKeyboardCursorAtEnd.current
11891201
// Check if IME composition is in progress
11901202
const isIMEComposition = e.isComposing || e.keyCode === 229
1191-
if (e.key === 'ArrowUp' && !isIMEComposition) {
1203+
if (e.key === 'ArrowUp' && !isIMEComposition && isCursorPositionAtStart) {
11921204
e.preventDefault()
11931205
const previousInput = inputHistory.getPreviousInput(userInput)
11941206
setUserInput(previousInput)
1195-
} else if (e.key === 'ArrowDown' && !isIMEComposition) {
1207+
} else if (e.key === 'ArrowDown' && !isIMEComposition && isCursorPositionAtEnd) {
11961208
e.preventDefault()
11971209
const nextInput = inputHistory.getNextInput()
11981210
setUserInput(nextInput)
@@ -2969,6 +2981,7 @@ const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, previews, setP
29692981
sx={{ width: '100%' }}
29702982
disabled={getInputDisabled()}
29712983
onKeyDown={handleEnter}
2984+
onKeyUp={handleKeyboardCursorPosition}
29722985
id='userInput'
29732986
name='userInput'
29742987
placeholder={loading ? 'Waiting for response...' : 'Type your question...'}

0 commit comments

Comments
 (0)