From 0bafc358aa7837ba404374db26cf222a4883da1a Mon Sep 17 00:00:00 2001 From: Ethanfly Date: Wed, 31 Dec 2025 15:56:12 +0800 Subject: [PATCH] fix: Ctrl+W only closes the active tab, not all tabs --- src/App.js | 1 + src/components/Terminal.js | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/App.js b/src/App.js index c0e75cc..1a3bd9f 100644 --- a/src/App.js +++ b/src/App.js @@ -366,6 +366,7 @@ function App() { handleConnectionChange(tab.id, connected)} onShowCommandPalette={openCommandPalette} onToggleInfoPanel={() => setShowInfoPanel(!showInfoPanel)} diff --git a/src/components/Terminal.js b/src/components/Terminal.js index 9ec11aa..95d3d92 100644 --- a/src/components/Terminal.js +++ b/src/components/Terminal.js @@ -7,7 +7,7 @@ import { WebglAddon } from '@xterm/addon-webgl'; import '@xterm/xterm/css/xterm.css'; import { FiCommand, FiRefreshCw, FiInfo, FiFolder, FiActivity, FiZap } from 'react-icons/fi'; -function Terminal({ tabId, hostId, onConnectionChange, onShowCommandPalette, onToggleInfoPanel, onOpenSFTP, showInfoPanel, onCloseTab }) { +function Terminal({ tabId, hostId, isActive, onConnectionChange, onShowCommandPalette, onToggleInfoPanel, onOpenSFTP, showInfoPanel, onCloseTab }) { const containerRef = useRef(null); const terminalRef = useRef(null); const xtermRef = useRef(null); @@ -32,6 +32,9 @@ function Terminal({ tabId, hostId, onConnectionChange, onShowCommandPalette, onT const onShowCommandPaletteRef = useRef(onShowCommandPalette); onShowCommandPaletteRef.current = onShowCommandPalette; + const isActiveRef = useRef(isActive); + isActiveRef.current = isActive; + const [connectionId, setConnectionId] = useState(null); const [isConnecting, setIsConnecting] = useState(false); const [error, setError] = useState(null); @@ -231,8 +234,13 @@ function Terminal({ tabId, hostId, onConnectionChange, onShowCommandPalette, onT } }); - // 自定义按键处理 - 拦截 Ctrl+W 和 Ctrl+K + // 自定义按键处理 - 拦截 Ctrl+W 和 Ctrl+K(仅当前活动标签页响应) term.attachCustomKeyEventHandler((e) => { + // 只有活动标签页才响应快捷键 + if (!isActiveRef.current) { + return true; + } + // Ctrl+W: 关闭当前标签页 if ((e.ctrlKey || e.metaKey) && e.key === 'w') { e.preventDefault();