svg-layout-designer-react/src/Components/Editor/Actions/Shortcuts.ts
2023-02-23 13:54:38 +01:00

34 lines
989 B
TypeScript

import { type Dispatch, type SetStateAction } from 'react';
import { type IHistoryState } from '../../../Interfaces/IHistoryState';
import { ENABLE_SHORTCUTS } from '../../../utils/default';
export function OnKey(
event: KeyboardEvent,
history: IHistoryState[],
historyCurrentStep: number,
setHistoryCurrentStep: Dispatch<SetStateAction<number>>,
deleteAction: () => void,
resetState: () => void
): void {
if (!ENABLE_SHORTCUTS) {
return;
}
event.preventDefault();
if (event.isComposing || event.keyCode === 229) {
return;
}
if (event.key === 'z' &&
event.ctrlKey &&
historyCurrentStep > 0) {
setHistoryCurrentStep(historyCurrentStep - 1);
} else if (event.key === 'y' &&
event.ctrlKey &&
historyCurrentStep < history.length - 1) {
setHistoryCurrentStep(historyCurrentStep + 1);
} else if (event.key === 'Delete') {
deleteAction();
} else if (event.key === 'Escape') {
resetState();
}
}