feat: add hotKey to close tab (#8494)

This commit is contained in:
Curry Yang
2025-03-21 11:54:02 +08:00
committed by Jay Wu
parent 3038a2b23a
commit efda5ee098
3 changed files with 19 additions and 2 deletions

View File

@@ -35,6 +35,7 @@ export const keyboardShortcutDescriptions: Record<KeyboardShortcut, string> = {
'environment_showVariableSourceAndValue': 'Show variable source and value',
'beautifyRequestBody': 'Beautify Active Code Editors',
'graphql_explorer_focus_filter': 'Focus GraphQL Explorer Filter',
'close_tab': 'Close Tab',
};
/**
@@ -163,6 +164,10 @@ const defaultRegistry: HotKeyRegistry = {
macKeys: [{ shift: true, meta: true, keyCode: keyboardKeys.i.keyCode }],
winLinuxKeys: [{ ctrl: true, shift: true, keyCode: keyboardKeys.i.keyCode }],
},
close_tab: {
macKeys: [{ meta: true, keyCode: keyboardKeys.w.keyCode }],
winLinuxKeys: [{ ctrl: true, keyCode: keyboardKeys.w.keyCode }],
},
};
/**

View File

@@ -55,7 +55,8 @@ export type KeyboardShortcut =
| 'request_togglePin'
| 'environment_showVariableSourceAndValue'
| 'beautifyRequestBody'
| 'graphql_explorer_focus_filter';
| 'graphql_explorer_focus_filter'
| 'close_tab';
/**
* The collection of defined hotkeys.

View File

@@ -9,6 +9,7 @@ import type { RequestGroup } from '../../models/request-group';
import type { UnitTestSuite } from '../../models/unit-test-suite';
import type { WebSocketRequest } from '../../models/websocket-request';
import type { Workspace } from '../../models/workspace';
import { useDocBodyKeyboardShortcuts } from '../components/keydown-binder';
import { type BaseTab, type TabType } from '../components/tabs/tab';
import { TAB_ROUTER_PATH } from '../components/tabs/tab-list';
import { formatMethodName, getRequestMethodShortHand } from '../components/tags/method-tag';
@@ -38,7 +39,7 @@ export const useInsomniaTab = ({
unitTestSuite,
}: InsomniaTabProps) => {
const { appTabsRef, addTab, changeActiveTab } = useInsomniaTabContext();
const { appTabsRef, addTab, changeActiveTab, closeTabById } = useInsomniaTabContext();
const location = useLocation();
const [searchParams] = useSearchParams();
@@ -288,4 +289,14 @@ export const useInsomniaTab = ({
}
}
}, [addTab, appTabsRef, changeActiveTab, getCurrentTab, location.pathname, organizationId, packTabInfo]);
useDocBodyKeyboardShortcuts({
close_tab: event => {
event.preventDefault();
const currentActiveTabId = appTabsRef?.current?.[organizationId]?.activeTabId;
if (currentActiveTabId) {
closeTabById(currentActiveTabId);
}
},
});
};