Skip to content

Commit

Permalink
Handle shortcuts for single key shortcut and Ctrl or Meta combinations
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablo Rodríguez Montalvo committed Sep 13, 2024
1 parent 502dad6 commit 7c0bc3b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions src/pods/toolbar/shortcut/shortcut.const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,31 @@ export const SHORTCUTS: Shortcut = {
delete: {
description: 'Delete',
id: 'delete-button-shortcut',
targetKey: ['Backspace'],
targetKey: ['backspace'],
targetKeyLabel: 'Backspace',
},
copy: {
description: 'Copy',
id: 'copy-button-shortcut',
targetKey: ['c'],
targetKey: ['Ctrl+c', 'Meta+c'],
targetKeyLabel: 'Ctrl + C',
},
paste: {
description: 'Paste',
id: 'paste-button-shortcut',
targetKey: ['v'],
targetKey: ['Ctrl+v', 'Meta+v'],
targetKeyLabel: 'Ctrl + V',
},
undo: {
description: 'Undo',
id: 'undo-button-shortcut',
targetKey: ['z'],
targetKey: ['Ctrl+z', 'Meta+z'],
targetKeyLabel: 'Ctrl + Z',
},
redo: {
description: 'Redo',
id: 'red-button-shortcut',
targetKey: ['y'],
targetKey: ['Ctrl+y', 'Meta+y'],
targetKeyLabel: 'Ctrl + Y',
},
};
12 changes: 6 additions & 6 deletions src/pods/toolbar/shortcut/shortcut.hook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ export const useShortcut = ({ targetKey, callback }: ShortcutHookProps) => {
//const isAltKeyPressed = event.getModifierState('Alt');
//const isCtrlKeyPressed = event.getModifierState('Control');
const isCtrlOrCmdPressed = event.ctrlKey || event.metaKey;
const ctrlKey = isMacOS() ? 'Meta' : 'Ctrl';
const pressedKey = event.key.toLowerCase();

if (
(isWindowsOrLinux() && isCtrlOrCmdPressed) ||
(isMacOS() && isCtrlOrCmdPressed)
targetKey.includes(pressedKey) ||
(isCtrlOrCmdPressed && targetKey.includes(`${ctrlKey}+${pressedKey}`))
) {
if (targetKey.includes(event.key)) {
event.preventDefault();
callback();
}
event.preventDefault();
callback();
}
};

Expand Down

0 comments on commit 7c0bc3b

Please sign in to comment.