fix: Ctrl+W only closes the active tab, not all tabs

This commit is contained in:
Ethanfly 2025-12-31 15:56:12 +08:00
parent de265bb5a8
commit 0bafc358aa
2 changed files with 11 additions and 2 deletions

View File

@ -366,6 +366,7 @@ function App() {
<Terminal
tabId={tab.id}
hostId={tab.hostId}
isActive={activeTabId === tab.id}
onConnectionChange={(connected) => handleConnectionChange(tab.id, connected)}
onShowCommandPalette={openCommandPalette}
onToggleInfoPanel={() => setShowInfoPanel(!showInfoPanel)}

View File

@ -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();