Cleaning the codebase
This commit is contained in:
54
src/lib/utils/keyboard.ts
Normal file
54
src/lib/utils/keyboard.ts
Normal file
@ -0,0 +1,54 @@
|
||||
export interface KeyboardActions {
|
||||
onMutate?: () => void;
|
||||
onRandom?: () => void;
|
||||
onProcess?: () => void;
|
||||
onDownload?: () => void;
|
||||
onDurationDecrease?: (large: boolean) => void;
|
||||
onDurationIncrease?: (large: boolean) => void;
|
||||
onVolumeDecrease?: (large: boolean) => void;
|
||||
onVolumeIncrease?: (large: boolean) => void;
|
||||
onEscape?: () => void;
|
||||
}
|
||||
|
||||
export function createKeyboardHandler(actions: KeyboardActions) {
|
||||
return (event: KeyboardEvent) => {
|
||||
if (event.target instanceof HTMLInputElement) return;
|
||||
|
||||
const key = event.key.toLowerCase();
|
||||
const isLargeAdjustment = event.shiftKey;
|
||||
|
||||
switch (key) {
|
||||
case 'm':
|
||||
actions.onMutate?.();
|
||||
break;
|
||||
case 'r':
|
||||
actions.onRandom?.();
|
||||
break;
|
||||
case 'p':
|
||||
actions.onProcess?.();
|
||||
break;
|
||||
case 's':
|
||||
actions.onDownload?.();
|
||||
break;
|
||||
case 'arrowleft':
|
||||
event.preventDefault();
|
||||
actions.onDurationDecrease?.(isLargeAdjustment);
|
||||
break;
|
||||
case 'arrowright':
|
||||
event.preventDefault();
|
||||
actions.onDurationIncrease?.(isLargeAdjustment);
|
||||
break;
|
||||
case 'arrowdown':
|
||||
event.preventDefault();
|
||||
actions.onVolumeDecrease?.(isLargeAdjustment);
|
||||
break;
|
||||
case 'arrowup':
|
||||
event.preventDefault();
|
||||
actions.onVolumeIncrease?.(isLargeAdjustment);
|
||||
break;
|
||||
case 'escape':
|
||||
actions.onEscape?.();
|
||||
break;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user