Support using slash ("/") anywhere in UserInput; previously only supported at the beginning of UserInput

This commit is contained in:
jasongwq 2024-06-23 12:47:13 +08:00
parent b6735bffe4
commit ecc1dd6474
1 changed files with 11 additions and 4 deletions

View File

@ -671,6 +671,8 @@ function _Chat() {
const config = useAppConfig(); const config = useAppConfig();
const fontSize = config.fontSize; const fontSize = config.fontSize;
const slashOffsetRef = useRef(0);
const [showExport, setShowExport] = useState(false); const [showExport, setShowExport] = useState(false);
const inputRef = useRef<HTMLTextAreaElement>(null); const inputRef = useRef<HTMLTextAreaElement>(null);
@ -751,10 +753,13 @@ function _Chat() {
setPromptHints([]); setPromptHints([]);
} else if (text.startsWith(ChatCommandPrefix)) { } else if (text.startsWith(ChatCommandPrefix)) {
setPromptHints(chatCommands.search(text)); setPromptHints(chatCommands.search(text));
} else if (!config.disablePromptHint && n < SEARCH_TEXT_LIMIT) { } else if (
!config.disablePromptHint &&
text.length < slashOffsetRef.current + SEARCH_TEXT_LIMIT
) {
// check if need to trigger auto completion // check if need to trigger auto completion
if (text.startsWith("/")) { if (text.slice(slashOffsetRef.current).startsWith("/")) {
let searchText = text.slice(1); let searchText = text.slice(slashOffsetRef.current + 1);
onSearch(searchText); onSearch(searchText);
} }
} }
@ -792,7 +797,7 @@ function _Chat() {
setUserInput(""); setUserInput("");
} else { } else {
// or fill the prompt // or fill the prompt
setUserInput(prompt.content); setUserInput(prompt.content + userInput.slice(0, slashOffsetRef.current));
} }
inputRef.current?.focus(); inputRef.current?.focus();
}, 30); }, 30);
@ -843,6 +848,8 @@ function _Chat() {
setUserInput(localStorage.getItem(LAST_INPUT_KEY) ?? ""); setUserInput(localStorage.getItem(LAST_INPUT_KEY) ?? "");
e.preventDefault(); e.preventDefault();
return; return;
} else if (e.key === "/") {
slashOffsetRef.current = userInput.length;
} }
if (shouldSubmit(e) && promptHints.length === 0) { if (shouldSubmit(e) && promptHints.length === 0) {
doSubmit(userInput); doSubmit(userInput);