Fix (issue 409): Command Palette toolbar button does not work when the shortcut is customized

This commit is contained in:
aleksey-hoffman committed 2026-04-27 17:54:25 +03:00
1 parent 9f20b22673
commit f073a5948f
3 files changed
+62 -21

No files matched your search

@@ -14,16 +14,8 @@ import { useShortcutsStore } from '@/stores/runtime/shortcuts';
const { t } = useI18n();
const shortcutsStore = useShortcutsStore();
function handleClick() {
const keyboardEvent = new KeyboardEvent('keydown', {
key: 'P',
ctrlKey: true,
shiftKey: true,
bubbles: true,
cancelable: true,
});
shortcutsStore.handleKeydown(keyboardEvent);
async function handleClick() {
await shortcutsStore.executeShortcut('toggleCommandPalette');
}
</script>
@@ -191,6 +191,36 @@ describe('shortcuts store', () => {
expect(executeCommandMock).not.toHaveBeenCalled();
});
it('executes a registered shortcut action without matching keyboard keys', async () => {
userSettingsStoreMock.userSettings.shortcuts = {
toggleCommandPalette: {
ctrl: true,
alt: true,
key: 'k',
},
};
const shortcutsStore = useShortcutsStore();
const toggleCommandPaletteHandler = vi.fn();
shortcutsStore.registerHandler('toggleCommandPalette', toggleCommandPaletteHandler);
const defaultShortcutEvent = new KeyboardEvent('keydown', {
key: 'P',
code: 'KeyP',
ctrlKey: true,
shiftKey: true,
bubbles: true,
cancelable: true,
});
await expect(shortcutsStore.handleKeydown(defaultShortcutEvent)).resolves.toBe(false);
expect(toggleCommandPaletteHandler).not.toHaveBeenCalled();
await expect(shortcutsStore.executeShortcut('toggleCommandPalette')).resolves.toBe(true);
expect(toggleCommandPaletteHandler).toHaveBeenCalledTimes(1);
});
it('executes extension shortcuts from the effective extensions store keybindings', async () => {
const shortcutsStore = useShortcutsStore();
+30 -11
View File
@@ -846,6 +846,7 @@ function isDialogOpened(): boolean {
}
type ShortcutHandler = () => void | boolean | Promise<void | boolean>;
type ShortcutHandlerResult = ReturnType<ShortcutHandler>;
type HandlerRegistration = {
handler: ShortcutHandler;
@@ -956,6 +957,32 @@ export const useShortcutsStore = defineStore('shortcuts', () => {
isShortcutCaptureActive.value = isActive;
}
function callShortcutHandler(shortcutId: ShortcutId): ShortcutHandlerResult | null {
const definition = getShortcutDefinition(shortcutId);
if (!definition) return null;
const registration = handlers.value.get(shortcutId);
if (!registration) return null;
if (!checkConditions(definition, registration)) {
return null;
}
return registration.handler();
}
async function executeShortcut(shortcutId: ShortcutId): Promise<boolean> {
const result = callShortcutHandler(shortcutId);
if (result === null) return false;
if (result instanceof Promise) {
const asyncResult = await result;
return asyncResult !== false;
}
return result !== false;
}
function findMatchingShortcut(event: KeyboardEvent): ShortcutId | null {
for (const definition of definitions.value) {
const keys = getShortcutKeys(definition.id);
@@ -1063,17 +1090,8 @@ export const useShortcutsStore = defineStore('shortcuts', () => {
if (matchingShortcutIds.length === 0) return false;
for (const shortcutId of matchingShortcutIds) {
const definition = getShortcutDefinition(shortcutId);
if (!definition) continue;
const registration = handlers.value.get(shortcutId);
if (!registration) continue;
if (!checkConditions(definition, registration)) {
continue;
}
const result = registration.handler();
const result = callShortcutHandler(shortcutId);
if (result === null) continue;
if (result instanceof Promise) {
event.preventDefault();
@@ -1157,6 +1175,7 @@ export const useShortcutsStore = defineStore('shortcuts', () => {
registerHandler,
unregisterHandler,
setShortcutCaptureActive,
executeShortcut,
findMatchingShortcut,
findConflictingShortcut,
handleKeydown,