import player from './player' ; import editor , { EditorTool } from './editor' ; import saveLoad from './save-load' ; import undo from './undo' ; import { Voice } from './note' ; import { debounce } from 'lodash' ; const DEBOUNCE_TIME_MS = 100 ; class KeyboardManager { constructor ( ) { document . addEventListener ( 'keydown' , this . handleKeyDown , false ) ; } handlePlayPause = debounce ( ( ) => { player . togglePlay ( ) ; } , DEBOUNCE_TIME_MS ) ; handleKeyDown = ( e ) => { if ( e . key === ' ' ) { this . handlePlayPause ( ) ; } else if ( e . key === 's' && ! e . metaKey ) { editor . selectVoice ( Voice . SOPRANO ) ; editor . selectTool ( EditorTool . DRAW , false ) ; } else if ( e . key === 'a' && ! e . metaKey ) { editor . selectVoice ( Voice . ALTO ) ; editor . selectTool ( EditorTool . DRAW , false ) ; } else if ( e . key === 't' && ! e . metaKey ) { editor . selectVoice ( Voice . TENOR ) ; editor . selectTool ( EditorTool . DRAW , false ) ; } else if ( e . key === 'b' && ! e . metaKey ) { editor . selectVoice ( Voice . BASS ) ; editor . selectTool ( EditorTool . DRAW , false ) ; } else if ( e . key === 'z' && e . metaKey && ! e . shiftKey ) { undo . undo ( ) ; } else if ( e . key === 'z' && e . metaKey && e . shiftKey ) { undo . redo ( ) ; } else if ( e . key === 's' && e . metaKey ) { e . preventDefault ( ) ; saveLoad . saveJSON ( ) ; return false ; } else if ( e . key === 'l' && e . metaKey ) { e . preventDefault ( ) ; saveLoad . loadJSON ( ) ; return false ; } else if ( e . key === 'Backspace' ) { editor . removeMaskedNotes ( ) ; } } ; } export default new KeyboardManager ( ) ;