-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add keyboard shortcut for delete-button
- Implemented useShortcut hook for managing keyboard shortcuts. - Updated ToolbarButton component to use the useShortcut hook. - Configured DeleteButton to use the new shortcut functionality with the 'Delete' key.
- Loading branch information
1 parent
86b5564
commit 8210364
Showing
8 changed files
with
123 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export function isMacOS() { | ||
return navigator.userAgent.toLowerCase().includes('mac'); | ||
} | ||
|
||
export function isWindowsOrLinux() { | ||
return !isMacOS(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 41 additions & 7 deletions
48
src/pods/toolbar/components/toolbar-button/toolbar-button.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { ShortcutOptions } from './shortcut.model'; | ||
|
||
interface Shortcut { | ||
[key: string]: ShortcutOptions; | ||
} | ||
|
||
export const SHORTCUTS: Shortcut = { | ||
delete: { | ||
description: 'Delete', | ||
id: 'delete-button-shortcut', | ||
targetKey: ['Backspace'], | ||
targetKeyLabel: 'Backspace', | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { isMacOS, isWindowsOrLinux } from '@/common/helpers/platform.helpers'; | ||
import { useEffect } from 'react'; | ||
|
||
export interface ShortcutHookProps { | ||
targetKey: string[]; | ||
callback: () => void; | ||
} | ||
|
||
const useShortcut = ({ targetKey, callback }: ShortcutHookProps) => { | ||
const handleKeyPress = (event: KeyboardEvent) => { | ||
const isAltKeyPressed = event.getModifierState('Alt'); | ||
const isCtrlKeyPressed = event.getModifierState('Control'); | ||
|
||
if ( | ||
(isWindowsOrLinux() && isAltKeyPressed) || | ||
(isMacOS() && isCtrlKeyPressed) | ||
) { | ||
if (targetKey.includes(event.key)) { | ||
event.preventDefault(); | ||
callback(); | ||
} | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
const onKeyDown = (event: KeyboardEvent) => handleKeyPress(event); | ||
window.addEventListener('keydown', onKeyDown); | ||
|
||
return () => { | ||
window.removeEventListener('keydown', onKeyDown); | ||
}; | ||
}, [targetKey, callback]); | ||
}; | ||
|
||
export default useShortcut; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface ShortcutOptions { | ||
id: string; | ||
targetKey: string[]; | ||
targetKeyLabel: string; | ||
description: string; | ||
} |