Skip to content

Commit 5f3c1a1

Browse files
committed
fix: fix chat input multiline cursor navigation issue
1 parent e995e45 commit 5f3c1a1

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

219219
const inputRef = useRef(null)
220+
const inputCursorPositionRef = useRef('start')
220221
const getChatmessageApi = useApi(chatmessageApi.getInternalChatmessageFromChatflow)
221222
const getAllExecutionsApi = useApi(executionsApi.getAllExecutions)
222223
const getIsChatflowStreamingApi = useApi(chatflowsApi.getIsChatflowStreaming)
@@ -1184,15 +1185,26 @@ const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, previews, setP
11841185
scrollToBottom()
11851186
}, 100)
11861187
}
1188+
// Allow cursor to navigate easily in multiline text (except at the end or start of the text)
1189+
const handleKeyboardCursorPosition = (e) => {
1190+
const targetValue = e.target.value ?? ''
1191+
inputCursorPositionRef.current = e.target.selectionStart
1192+
const cursorPosition = inputCursorPositionRef.current
1193+
const isCursorAtStart = cursorPosition === 0
1194+
const isCursorAtEnd = targetValue.length > 0 ? cursorPosition === targetValue.length : cursorPosition === 0
1195+
inputCursorPositionRef.current = isCursorAtStart ? 'start' : isCursorAtEnd ? 'end' : 'middle'
1196+
}
1197+
11871198
// Prevent blank submissions and allow for multiline input
11881199
const handleEnter = (e) => {
1200+
const cursorPosition = inputCursorPositionRef.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 && cursorPosition === 'start') {
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 && cursorPosition === 'end') {
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)