From 60288ad20a4fa5da0fc94c3988927af6da7b8c39 Mon Sep 17 00:00:00 2001
From: Dornfeld Capital <75278543+dornfeld-capital@users.noreply.github.com>
Date: Mon, 10 Apr 2023 22:41:29 -0500
Subject: [PATCH] improve plugin hotkey support (#426)
---
components/Chat/ChatInput.tsx | 9 +++++-
components/Chat/PluginSelect.tsx | 53 +++++++++++++++++++++++++++++---
2 files changed, 56 insertions(+), 6 deletions(-)
diff --git a/components/Chat/ChatInput.tsx b/components/Chat/ChatInput.tsx
index 1399f73..e286573 100644
--- a/components/Chat/ChatInput.tsx
+++ b/components/Chat/ChatInput.tsx
@@ -284,9 +284,16 @@ export const ChatInput = ({
{showPluginSelect && (
-
+
{
+ if (e.key === 'Escape') {
+ e.preventDefault();
+ setShowPluginSelect(false);
+ textareaRef.current?.focus();
+ }
+ }}
onPluginChange={(plugin: Plugin) => {
setPlugin(plugin);
setShowPluginSelect(false);
diff --git a/components/Chat/PluginSelect.tsx b/components/Chat/PluginSelect.tsx
index e86d6eb..4624ae2 100644
--- a/components/Chat/PluginSelect.tsx
+++ b/components/Chat/PluginSelect.tsx
@@ -7,13 +7,53 @@ import { Plugin, PluginList } from '@/types/plugin';
interface Props {
plugin: Plugin | null;
onPluginChange: (plugin: Plugin) => void;
+ onKeyDown: (e: React.KeyboardEvent) => void;
}
-export const PluginSelect: FC = ({ plugin, onPluginChange }) => {
+export const PluginSelect: FC = ({
+ plugin,
+ onPluginChange,
+ onKeyDown,
+}) => {
const { t } = useTranslation('chat');
const selectRef = useRef(null);
+ const handleKeyDown = (e: React.KeyboardEvent) => {
+ const selectElement = selectRef.current;
+ const optionCount = selectElement?.options.length || 0;
+
+ if (e.key === '/' && e.metaKey) {
+ e.preventDefault();
+ if (selectElement) {
+ selectElement.selectedIndex =
+ (selectElement.selectedIndex + 1) % optionCount;
+ selectElement.dispatchEvent(new Event('change'));
+ }
+ } else if (e.key === '/' && e.shiftKey && e.metaKey) {
+ e.preventDefault();
+ if (selectElement) {
+ selectElement.selectedIndex =
+ (selectElement.selectedIndex - 1 + optionCount) % optionCount;
+ selectElement.dispatchEvent(new Event('change'));
+ }
+ } else if (e.key === 'Enter') {
+ e.preventDefault();
+ if (selectElement) {
+ selectElement.dispatchEvent(new Event('change'));
+ }
+
+ onPluginChange(
+ PluginList.find(
+ (plugin) =>
+ plugin.name === selectElement?.selectedOptions[0].innerText,
+ ) as Plugin,
+ );
+ } else {
+ onKeyDown(e);
+ }
+ };
+
useEffect(() => {
if (selectRef.current) {
selectRef.current.focus();
@@ -22,7 +62,7 @@ export const PluginSelect: FC = ({ plugin, onPluginChange }) => {
return (
-
+