fix: arrow keys navigate cursor when user has typed input

- Added userHasTyped flag to track manual input vs history navigation
- Arrow keys only navigate history when input is empty or from history
- Clearing input re-enables history navigation

Closes #58
This commit is contained in:
2026-01-23 14:36:39 -08:00
committed by Naomi Carrigan
parent 9abf4b1bdf
commit 371e4efde3
+15 -3
View File
@@ -38,6 +38,7 @@
let inputHistory = $state<string[]>([]);
let historyIndex = $state(-1);
let tempInput = $state("");
let userHasTyped = $state(false); // Track if user manually typed (vs history navigation)
// Textarea resize state
let textareaHeight = $state(48);
@@ -112,6 +113,13 @@
});
function handleInputChange() {
// If input is empty, allow history navigation again
// Otherwise, mark that user has manually typed
if (inputValue === "") {
userHasTyped = false;
} else {
userHasTyped = true;
}
// Reset history navigation when user types
historyIndex = -1;
tempInput = "";
@@ -156,6 +164,7 @@
addToHistory(message);
historyIndex = -1;
tempInput = "";
userHasTyped = false;
const wasCommand = await executeSlashCommand();
if (wasCommand) return;
@@ -175,6 +184,7 @@
addToHistory(message);
historyIndex = -1;
tempInput = "";
userHasTyped = false;
isSubmitting = true;
inputValue = "";
@@ -308,8 +318,9 @@ User: ${formattedMessage}`;
}
}
// Handle input history navigation (when command menu is closed)
if (event.key === "ArrowUp" && inputHistory.length > 0) {
// Handle input history navigation (when command menu is closed AND user hasn't typed)
// If user has typed something, let arrow keys navigate the cursor instead
if (event.key === "ArrowUp" && inputHistory.length > 0 && !userHasTyped) {
event.preventDefault();
if (historyIndex === -1) {
// Save current input before navigating history
@@ -322,12 +333,13 @@ User: ${formattedMessage}`;
return;
}
if (event.key === "ArrowDown" && historyIndex >= 0) {
if (event.key === "ArrowDown" && historyIndex >= 0 && !userHasTyped) {
event.preventDefault();
historyIndex--;
if (historyIndex === -1) {
// Restore the temp input when going back to current
inputValue = tempInput;
userHasTyped = false; // Reset since we're back to empty/temp state
} else {
inputValue = inputHistory[historyIndex];
}