feat: enable Ctrl+W and Ctrl+K shortcuts when terminal is focused

This commit is contained in:
Ethanfly 2025-12-31 15:54:20 +08:00
parent 0e20b0df3a
commit de265bb5a8
2 changed files with 29 additions and 1 deletions

View File

@ -371,6 +371,7 @@ function App() {
onToggleInfoPanel={() => setShowInfoPanel(!showInfoPanel)} onToggleInfoPanel={() => setShowInfoPanel(!showInfoPanel)}
onOpenSFTP={() => setShowSFTP(true)} onOpenSFTP={() => setShowSFTP(true)}
showInfoPanel={showInfoPanel} showInfoPanel={showInfoPanel}
onCloseTab={() => closeTab(tab.id)}
/> />
</div> </div>
)) ))

View File

@ -7,7 +7,7 @@ import { WebglAddon } from '@xterm/addon-webgl';
import '@xterm/xterm/css/xterm.css'; import '@xterm/xterm/css/xterm.css';
import { FiCommand, FiRefreshCw, FiInfo, FiFolder, FiActivity, FiZap } from 'react-icons/fi'; import { FiCommand, FiRefreshCw, FiInfo, FiFolder, FiActivity, FiZap } from 'react-icons/fi';
function Terminal({ tabId, hostId, onConnectionChange, onShowCommandPalette, onToggleInfoPanel, onOpenSFTP, showInfoPanel }) { function Terminal({ tabId, hostId, onConnectionChange, onShowCommandPalette, onToggleInfoPanel, onOpenSFTP, showInfoPanel, onCloseTab }) {
const containerRef = useRef(null); const containerRef = useRef(null);
const terminalRef = useRef(null); const terminalRef = useRef(null);
const xtermRef = useRef(null); const xtermRef = useRef(null);
@ -26,6 +26,12 @@ function Terminal({ tabId, hostId, onConnectionChange, onShowCommandPalette, onT
const onConnectionChangeRef = useRef(onConnectionChange); const onConnectionChangeRef = useRef(onConnectionChange);
onConnectionChangeRef.current = onConnectionChange; onConnectionChangeRef.current = onConnectionChange;
const onCloseTabRef = useRef(onCloseTab);
onCloseTabRef.current = onCloseTab;
const onShowCommandPaletteRef = useRef(onShowCommandPalette);
onShowCommandPaletteRef.current = onShowCommandPalette;
const [connectionId, setConnectionId] = useState(null); const [connectionId, setConnectionId] = useState(null);
const [isConnecting, setIsConnecting] = useState(false); const [isConnecting, setIsConnecting] = useState(false);
const [error, setError] = useState(null); const [error, setError] = useState(null);
@ -225,6 +231,27 @@ function Terminal({ tabId, hostId, onConnectionChange, onShowCommandPalette, onT
} }
}); });
// 自定义按键处理 - 拦截 Ctrl+W 和 Ctrl+K
term.attachCustomKeyEventHandler((e) => {
// Ctrl+W: 关闭当前标签页
if ((e.ctrlKey || e.metaKey) && e.key === 'w') {
e.preventDefault();
if (onCloseTabRef.current) {
onCloseTabRef.current();
}
return false; // 阻止 xterm 处理
}
// Ctrl+K: 打开命令面板
if ((e.ctrlKey || e.metaKey) && e.key === 'k') {
e.preventDefault();
if (onShowCommandPaletteRef.current) {
onShowCommandPaletteRef.current();
}
return false;
}
return true; // 允许 xterm 处理其他按键
});
// 选中自动复制到剪贴板 // 选中自动复制到剪贴板
term.onSelectionChange(() => { term.onSelectionChange(() => {
const selection = term.getSelection(); const selection = term.getSelection();