mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-18 01:10:14 +01:00
refactor(useHandleKeyUp): enter from beginning only
This commit is contained in:
parent
5ef71a7a36
commit
d69465ea3d
1 changed files with 15 additions and 9 deletions
|
|
@ -10,6 +10,14 @@ const invalidKeys = {
|
||||||
Escape: true,
|
Escape: true,
|
||||||
Backspace: true,
|
Backspace: true,
|
||||||
Enter: true,
|
Enter: true,
|
||||||
|
ArrowUp: true,
|
||||||
|
ArrowDown: true,
|
||||||
|
ArrowLeft: true,
|
||||||
|
ArrowRight: true,
|
||||||
|
Home: true,
|
||||||
|
End: true,
|
||||||
|
PageUp: true,
|
||||||
|
PageDown: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -19,21 +27,19 @@ const shouldTriggerCommand = (
|
||||||
textAreaRef: React.RefObject<HTMLTextAreaElement>,
|
textAreaRef: React.RefObject<HTMLTextAreaElement>,
|
||||||
commandChar: string,
|
commandChar: string,
|
||||||
) => {
|
) => {
|
||||||
const text = textAreaRef.current?.value;
|
const textArea = textAreaRef.current;
|
||||||
if (!(text && text[text.length - 1] === commandChar)) {
|
if (!textArea) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const startPos = textAreaRef.current?.selectionStart;
|
const text = textArea.value;
|
||||||
if (!startPos) {
|
const cursorPosition = textArea.selectionStart;
|
||||||
|
|
||||||
|
if (cursorPosition !== text.length || text.length !== 1 || text[0] !== commandChar) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const isAtStart = startPos === 1;
|
return true;
|
||||||
const isPrecededBySpace = textAreaRef.current?.value.charAt(startPos - 2) === ' ';
|
|
||||||
|
|
||||||
const shouldTrigger = isAtStart || isPrecededBySpace;
|
|
||||||
return shouldTrigger;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue