Skip to content

Commit

Permalink
Add keyboard shortcut for delete-button
Browse files Browse the repository at this point in the history
- 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
LourdesRsdp committed Aug 8, 2024
1 parent 86b5564 commit 8210364
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 16 deletions.
4 changes: 3 additions & 1 deletion src/common/components/inline-edit/inline-edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ export const EditableComponent: React.FC<Props> = props => {
if (isEditing && event.key === 'Escape') {
setIsEditing(false);
setEditText(text);
} else if (isEditing && event.key === 'Enter') {
}

if (isEditing && event.key === 'Enter') {
setIsEditing(false);
onTextSubmit(inputRef.current?.value || '');
}
Expand Down
7 changes: 7 additions & 0 deletions src/common/helpers/platform.helpers.ts
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();
}
2 changes: 1 addition & 1 deletion src/core/providers/canvas/canvas.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ export interface CanvasContextModel {
updateShapeSizeAndPosition: (id: string, position: Coord, size: Size) => void;
updateShapePosition: (id: string, position: Coord) => void;
selectionInfo: SelectionInfo;
deleteSelectedShape: () => void;
deleteSelectedShape: (id: any) => void;
}
23 changes: 16 additions & 7 deletions src/pods/toolbar/components/delete-button/delete-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@ import { DeleteIcon } from '@/common/components/icons/delete-icon.component';
import { ToolbarButton } from '@/pods/toolbar/components/toolbar-button/toolbar-button';
import classes from '@/pods/toolbar/toolbar.pod.module.css';
import { useCanvasContext } from '@/core/providers';
import { SHORTCUTS } from '../../shortcut/shortcut.const';

export const DeleteButton = () => {
const { deleteSelectedShape, selectionInfo } = useCanvasContext();
const { selectionInfo, deleteSelectedShape } = useCanvasContext();

const handleDeleteSelectedItemClick = () => {
if (selectionInfo.selectedShapeId) {
deleteSelectedShape(selectionInfo.selectedShapeId);
}
};

return (
<ToolbarButton
onClick={deleteSelectedShape}
icon={<DeleteIcon />}
label="Delete"
onClick={handleDeleteSelectedItemClick}
className={classes.button}
disabled={selectionInfo.selectedShapeId ? false : true}
>
<DeleteIcon />
<span>Delete</span>
</ToolbarButton>
disabled={!selectionInfo.selectedShapeId}
shortcutOptions={SHORTCUTS.delete}
children={<></>}
/>
);
};
48 changes: 41 additions & 7 deletions src/pods/toolbar/components/toolbar-button/toolbar-button.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,51 @@
import useShortcut from '../../shortcut/shortcut.hook';
/* import { isMacOS } from '@/common/helpers/platform.helpers'; */
import { ShortcutOptions } from '../../shortcut/shortcut.model';

interface Props {
children: React.ReactNode;
onClick?: () => void;
className?: string;
disabled?: boolean;
shortcutOptions?: ShortcutOptions;
icon?: React.ReactNode;
label: string;
}
export const ToolbarButton: React.FC<Props> = ({
children,
onClick,
className,
disabled,
}) => {
export const ToolbarButton: React.FC<Props> = props => {
const {
children,
onClick = () => {},
className,
disabled,
icon,
label,
shortcutOptions,
} = props;

/* const shortcutCommand = isMacOS() ? 'Ctrl' : 'Alt';
const showTooltip = shortcutOptions && !disabled;
const tooltipText = `(${shortcutCommand} + ${shortcutOptions?.targetKeyLabel})`; */

useShortcut({
...shortcutOptions,
targetKey: shortcutOptions?.targetKey || [],
callback: onClick,
});

return (
<button onClick={onClick} className={className} disabled={disabled}>
<button
onClick={onClick}
className={className}
disabled={disabled}
aria-describedby={shortcutOptions?.id}
>
<span aria-hidden={true}>{icon}</span>
<span>{label}</span>
{/* {showTooltip && (
<span role="tooltip" id={shortcutOptions?.id}>
{tooltipText}
</span>
)} */}
{children}
</button>
);
Expand Down
14 changes: 14 additions & 0 deletions src/pods/toolbar/shortcut/shortcut.const.ts
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',
},
};
35 changes: 35 additions & 0 deletions src/pods/toolbar/shortcut/shortcut.hook.tsx
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;
6 changes: 6 additions & 0 deletions src/pods/toolbar/shortcut/shortcut.model.ts
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;
}

0 comments on commit 8210364

Please sign in to comment.