generated from nhcarrigan/template
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:
@@ -38,6 +38,7 @@
|
|||||||
let inputHistory = $state<string[]>([]);
|
let inputHistory = $state<string[]>([]);
|
||||||
let historyIndex = $state(-1);
|
let historyIndex = $state(-1);
|
||||||
let tempInput = $state("");
|
let tempInput = $state("");
|
||||||
|
let userHasTyped = $state(false); // Track if user manually typed (vs history navigation)
|
||||||
|
|
||||||
// Textarea resize state
|
// Textarea resize state
|
||||||
let textareaHeight = $state(48);
|
let textareaHeight = $state(48);
|
||||||
@@ -112,6 +113,13 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
function handleInputChange() {
|
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
|
// Reset history navigation when user types
|
||||||
historyIndex = -1;
|
historyIndex = -1;
|
||||||
tempInput = "";
|
tempInput = "";
|
||||||
@@ -156,6 +164,7 @@
|
|||||||
addToHistory(message);
|
addToHistory(message);
|
||||||
historyIndex = -1;
|
historyIndex = -1;
|
||||||
tempInput = "";
|
tempInput = "";
|
||||||
|
userHasTyped = false;
|
||||||
|
|
||||||
const wasCommand = await executeSlashCommand();
|
const wasCommand = await executeSlashCommand();
|
||||||
if (wasCommand) return;
|
if (wasCommand) return;
|
||||||
@@ -175,6 +184,7 @@
|
|||||||
addToHistory(message);
|
addToHistory(message);
|
||||||
historyIndex = -1;
|
historyIndex = -1;
|
||||||
tempInput = "";
|
tempInput = "";
|
||||||
|
userHasTyped = false;
|
||||||
|
|
||||||
isSubmitting = true;
|
isSubmitting = true;
|
||||||
inputValue = "";
|
inputValue = "";
|
||||||
@@ -308,8 +318,9 @@ User: ${formattedMessage}`;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle input history navigation (when command menu is closed)
|
// Handle input history navigation (when command menu is closed AND user hasn't typed)
|
||||||
if (event.key === "ArrowUp" && inputHistory.length > 0) {
|
// If user has typed something, let arrow keys navigate the cursor instead
|
||||||
|
if (event.key === "ArrowUp" && inputHistory.length > 0 && !userHasTyped) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
if (historyIndex === -1) {
|
if (historyIndex === -1) {
|
||||||
// Save current input before navigating history
|
// Save current input before navigating history
|
||||||
@@ -322,12 +333,13 @@ User: ${formattedMessage}`;
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.key === "ArrowDown" && historyIndex >= 0) {
|
if (event.key === "ArrowDown" && historyIndex >= 0 && !userHasTyped) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
historyIndex--;
|
historyIndex--;
|
||||||
if (historyIndex === -1) {
|
if (historyIndex === -1) {
|
||||||
// Restore the temp input when going back to current
|
// Restore the temp input when going back to current
|
||||||
inputValue = tempInput;
|
inputValue = tempInput;
|
||||||
|
userHasTyped = false; // Reset since we're back to empty/temp state
|
||||||
} else {
|
} else {
|
||||||
inputValue = inputHistory[historyIndex];
|
inputValue = inputHistory[historyIndex];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user