mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-07-25 13:45:03 +08:00
feat: 添加 ESC 键快捷返回功能 (#670)
* feat: 添加 ESC 键快捷返回功能 - FullScreenPanel 组件支持 ESC 键关闭 - App.tsx 主页面支持 ESC 键返回主界面 - 优化键盘事件处理,合并多个监听器 - 使用事件捕获阶段避免冲突 - 适用于所有子页面:MCP、设置、Prompts、Skills 等 - 跨平台兼容:macOS、Windows、Linux * perf: 优化 ESC 键处理逻辑 - 使用 useRef 避免闭包陷阱,提升性能 - 修复输入框中按 ESC 会关闭面板的问题 - 检测焦点元素,不干扰输入框的 ESC 行为 - 改进用户体验,避免意外关闭导致数据丢失 * fix: enhance global keyboard shortcuts and improve useModelState sync - App & FullScreenPanel: Use `isTextEditableTarget` to prevent shortcuts (ESC, etc.) from triggering while editing text. - useModelState: Prevent overwriting user input during config synchronization. - App: Add `Cmd/Ctrl + ,` shortcut to open settings. - Add `isTextEditableTarget` utility.
This commit is contained in:
+28
-5
@@ -30,6 +30,7 @@ import { useProviderActions } from "@/hooks/useProviderActions";
|
||||
import { useProxyStatus } from "@/hooks/useProxyStatus";
|
||||
import { useLastValidValue } from "@/hooks/useLastValidValue";
|
||||
import { extractErrorMessage } from "@/utils/errorUtils";
|
||||
import { isTextEditableTarget } from "@/utils/domUtils";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { isWindows, isLinux } from "@/lib/platform";
|
||||
import { AppSwitcher } from "@/components/AppSwitcher";
|
||||
@@ -291,18 +292,40 @@ function App() {
|
||||
checkEnvOnSwitch();
|
||||
}, [activeApp]);
|
||||
|
||||
// 全局键盘快捷键
|
||||
const currentViewRef = useRef(currentView);
|
||||
|
||||
useEffect(() => {
|
||||
const handleGlobalShortcut = (event: KeyboardEvent) => {
|
||||
if (event.key !== "," || !(event.metaKey || event.ctrlKey)) {
|
||||
currentViewRef.current = currentView;
|
||||
}, [currentView]);
|
||||
|
||||
useEffect(() => {
|
||||
const handleKeyDown = (event: KeyboardEvent) => {
|
||||
// Cmd/Ctrl + , 打开设置
|
||||
if (event.key === "," && (event.metaKey || event.ctrlKey)) {
|
||||
event.preventDefault();
|
||||
setCurrentView("settings");
|
||||
return;
|
||||
}
|
||||
|
||||
// ESC 键返回
|
||||
if (event.key !== "Escape" || event.defaultPrevented) return;
|
||||
|
||||
// 如果有模态框打开(通过 overflow hidden 判断),则不处理全局 ESC,交给模态框处理
|
||||
if (document.body.style.overflow === "hidden") return;
|
||||
|
||||
const view = currentViewRef.current;
|
||||
if (view === "providers") return;
|
||||
|
||||
if (isTextEditableTarget(event.target)) return;
|
||||
|
||||
event.preventDefault();
|
||||
setCurrentView("settings");
|
||||
setCurrentView(view === "skillsDiscovery" ? "skills" : "providers");
|
||||
};
|
||||
|
||||
window.addEventListener("keydown", handleGlobalShortcut);
|
||||
window.addEventListener("keydown", handleKeyDown);
|
||||
return () => {
|
||||
window.removeEventListener("keydown", handleGlobalShortcut);
|
||||
window.removeEventListener("keydown", handleKeyDown);
|
||||
};
|
||||
}, []);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user