From 78bf777365f2f76f0318bf6b55fb0349491e16a1 Mon Sep 17 00:00:00 2001 From: Jing Hua Date: Thu, 30 Mar 2023 16:01:01 +0800 Subject: [PATCH] feat: option for enterToSubmit / shiftEnterToSubmit Fixes #88 --- public/locales/da/main.json | 3 +- public/locales/en/main.json | 3 +- public/locales/es/main.json | 3 +- public/locales/fr/main.json | 3 +- public/locales/ja/main.json | 3 +- public/locales/ms/main.json | 3 +- public/locales/nb/main.json | 3 +- public/locales/sv/main.json | 3 +- public/locales/zh-CN/main.json | 3 +- public/locales/zh-HK/main.json | 3 +- public/locales/zh-TW/main.json | 3 +- .../ChatContent/Message/MessageContent.tsx | 18 ++++++++---- .../SettingsMenu/EnterToSubmitToggle.tsx | 28 +++++++++++++++++++ src/components/SettingsMenu/SettingsMenu.tsx | 6 +++- src/store/config-slice.ts | 9 ++++++ src/store/store.ts | 1 + 16 files changed, 78 insertions(+), 17 deletions(-) create mode 100644 src/components/SettingsMenu/EnterToSubmitToggle.tsx diff --git a/public/locales/da/main.json b/public/locales/da/main.json index c36c233..1df37dc 100644 --- a/public/locales/da/main.json +++ b/public/locales/da/main.json @@ -35,5 +35,6 @@ }, "newFolder": "Ny mappe", "cloneChat": "Clone Chat", - "cloned": "Cloned" + "cloned": "Cloned", + "enterToSubmit": "Enter to submit" } diff --git a/public/locales/en/main.json b/public/locales/en/main.json index 9109993..aa23c87 100644 --- a/public/locales/en/main.json +++ b/public/locales/en/main.json @@ -35,5 +35,6 @@ }, "newFolder": "New Folder", "cloneChat": "Clone Chat", - "cloned": "Cloned" + "cloned": "Cloned", + "enterToSubmit": "Enter to submit" } diff --git a/public/locales/es/main.json b/public/locales/es/main.json index 0032916..772de5f 100644 --- a/public/locales/es/main.json +++ b/public/locales/es/main.json @@ -35,5 +35,6 @@ }, "newFolder": "Nueva Carpeta", "cloneChat": "Clone Chat", - "cloned": "Cloned" + "cloned": "Cloned", + "enterToSubmit": "Enter to submit" } diff --git a/public/locales/fr/main.json b/public/locales/fr/main.json index 1b161e0..39228ef 100644 --- a/public/locales/fr/main.json +++ b/public/locales/fr/main.json @@ -35,5 +35,6 @@ }, "newFolder": "Nouveau Dossier", "cloneChat": "Clone Chat", - "cloned": "Cloned" + "cloned": "Cloned", + "enterToSubmit": "Enter to submit" } diff --git a/public/locales/ja/main.json b/public/locales/ja/main.json index ad44dc8..5652cb1 100644 --- a/public/locales/ja/main.json +++ b/public/locales/ja/main.json @@ -35,5 +35,6 @@ }, "newFolder": "新しいフォルダー", "cloneChat": "Clone Chat", - "cloned": "Cloned" + "cloned": "Cloned", + "enterToSubmit": "Enter to submit" } diff --git a/public/locales/ms/main.json b/public/locales/ms/main.json index 3e8ef87..b6aa64b 100644 --- a/public/locales/ms/main.json +++ b/public/locales/ms/main.json @@ -35,5 +35,6 @@ }, "newFolder": "Folder Baru", "cloneChat": "Clone Chat", - "cloned": "Cloned" + "cloned": "Cloned", + "enterToSubmit": "Enter to submit" } diff --git a/public/locales/nb/main.json b/public/locales/nb/main.json index 5b9a501..569d36c 100644 --- a/public/locales/nb/main.json +++ b/public/locales/nb/main.json @@ -35,5 +35,6 @@ }, "newFolder": "Ny mappe", "cloneChat": "Clone Chat", - "cloned": "Cloned" + "cloned": "Cloned", + "enterToSubmit": "Enter to submit" } diff --git a/public/locales/sv/main.json b/public/locales/sv/main.json index 071d571..473fbb2 100644 --- a/public/locales/sv/main.json +++ b/public/locales/sv/main.json @@ -35,5 +35,6 @@ }, "newFolder": "Ny mapp", "cloneChat": "Clone Chat", - "cloned": "Cloned" + "cloned": "Cloned", + "enterToSubmit": "Enter to submit" } diff --git a/public/locales/zh-CN/main.json b/public/locales/zh-CN/main.json index f603917..bb910e2 100644 --- a/public/locales/zh-CN/main.json +++ b/public/locales/zh-CN/main.json @@ -35,5 +35,6 @@ }, "newFolder": "新文件夹", "cloneChat": "Clone Chat", - "cloned": "Cloned" + "cloned": "Cloned", + "enterToSubmit": "Enter to submit" } diff --git a/public/locales/zh-HK/main.json b/public/locales/zh-HK/main.json index f2a8ddc..9706ad7 100644 --- a/public/locales/zh-HK/main.json +++ b/public/locales/zh-HK/main.json @@ -35,5 +35,6 @@ }, "newFolder": "新資料夾", "cloneChat": "Clone Chat", - "cloned": "Cloned" + "cloned": "Cloned", + "enterToSubmit": "Enter to submit" } diff --git a/public/locales/zh-TW/main.json b/public/locales/zh-TW/main.json index 92ee295..41b5a24 100644 --- a/public/locales/zh-TW/main.json +++ b/public/locales/zh-TW/main.json @@ -35,5 +35,6 @@ }, "newFolder": "新資料夾", "cloneChat": "Clone Chat", - "cloned": "Cloned" + "cloned": "Cloned", + "enterToSubmit": "Enter to submit" } diff --git a/src/components/Chat/ChatContent/Message/MessageContent.tsx b/src/components/Chat/ChatContent/Message/MessageContent.tsx index 5da5e97..1901fdf 100644 --- a/src/components/Chat/ChatContent/Message/MessageContent.tsx +++ b/src/components/Chat/ChatContent/Message/MessageContent.tsx @@ -342,12 +342,20 @@ const EditView = ({ }; const handleKeyDown = (e: React.KeyboardEvent) => { - if ((e.ctrlKey || e.shiftKey) && e.key === 'Enter') { - e.preventDefault(); + if (e.key === 'Enter') { + const enterToSubmit = useStore.getState().enterToSubmit; if (sticky) { - handleSaveAndSubmit(); - resetTextAreaHeight(); - } else handleSave(); + if ( + (enterToSubmit && !e.shiftKey) || + (!enterToSubmit && (e.ctrlKey || e.shiftKey)) + ) { + e.preventDefault(); + handleSaveAndSubmit(); + resetTextAreaHeight(); + } + } else if (e.ctrlKey || e.shiftKey) { + handleSave(); + } } }; diff --git a/src/components/SettingsMenu/EnterToSubmitToggle.tsx b/src/components/SettingsMenu/EnterToSubmitToggle.tsx new file mode 100644 index 0000000..5b2157a --- /dev/null +++ b/src/components/SettingsMenu/EnterToSubmitToggle.tsx @@ -0,0 +1,28 @@ +import React, { useEffect, useState } from 'react'; +import { useTranslation } from 'react-i18next'; +import useStore from '@store/store'; +import Toggle from '@components/Toggle'; + +const EnterToSubmitToggle = () => { + const { t } = useTranslation(); + + const setEnterToSubmit = useStore((state) => state.setEnterToSubmit); + + const [isChecked, setIsChecked] = useState( + useStore.getState().enterToSubmit + ); + + useEffect(() => { + setEnterToSubmit(isChecked); + }, [isChecked]); + + return ( + + ); +}; + +export default EnterToSubmitToggle; diff --git a/src/components/SettingsMenu/SettingsMenu.tsx b/src/components/SettingsMenu/SettingsMenu.tsx index f66bd86..e0e980c 100644 --- a/src/components/SettingsMenu/SettingsMenu.tsx +++ b/src/components/SettingsMenu/SettingsMenu.tsx @@ -9,6 +9,7 @@ import LanguageSelector from '@components/LanguageSelector'; import AutoTitleToggle from './AutoTitleToggle'; import PromptLibraryMenu from '@components/PromptLibraryMenu'; import ChatConfigMenu from '@components/ChatConfigMenu'; +import EnterToSubmitToggle from './EnterToSubmitToggle'; const SettingsMenu = () => { const { t } = useTranslation(); @@ -38,7 +39,10 @@ const SettingsMenu = () => {
- +
+ + +
diff --git a/src/store/config-slice.ts b/src/store/config-slice.ts index 3c44806..359d29c 100644 --- a/src/store/config-slice.ts +++ b/src/store/config-slice.ts @@ -11,6 +11,7 @@ export interface ConfigSlice { defaultChatConfig: ConfigInterface; defaultSystemMessage: string; hideSideMenu: boolean; + enterToSubmit: boolean; setOpenConfig: (openConfig: boolean) => void; setTheme: (theme: Theme) => void; setAutoTitle: (autoTitle: boolean) => void; @@ -18,6 +19,7 @@ export interface ConfigSlice { setDefaultSystemMessage: (defaultSystemMessage: string) => void; setHideMenuOptions: (hideMenuOptions: boolean) => void; setHideSideMenu: (hideSideMenu: boolean) => void; + setEnterToSubmit: (enterToSubmit: boolean) => void; } export const createConfigSlice: StoreSlice = (set, get) => ({ @@ -26,6 +28,7 @@ export const createConfigSlice: StoreSlice = (set, get) => ({ hideMenuOptions: false, hideSideMenu: false, autoTitle: false, + enterToSubmit: true, defaultChatConfig: _defaultChatConfig, defaultSystemMessage: _defaultSystemMessage, setOpenConfig: (openConfig: boolean) => { @@ -70,4 +73,10 @@ export const createConfigSlice: StoreSlice = (set, get) => ({ hideSideMenu: hideSideMenu, })); }, + setEnterToSubmit: (enterToSubmit: boolean) => { + set((prev: ConfigSlice) => ({ + ...prev, + enterToSubmit: enterToSubmit, + })); + }, }); diff --git a/src/store/store.ts b/src/store/store.ts index cc07336..f02cb88 100644 --- a/src/store/store.ts +++ b/src/store/store.ts @@ -61,6 +61,7 @@ const useStore = create()( hideSideMenu: state.hideSideMenu, foldersName: state.foldersName, foldersExpanded: state.foldersExpanded, + enterToSubmit: state.enterToSubmit, }), version: 7, migrate: (persistedState, version) => {