function canonicalKey(event) { let name = []; if (event.altKey) name.push('M') if (event.ctrlKey) name.push('C') if (event.metaKey) name.push('H') if (event.shiftKey) name.push('S') name.push(event.key) return name.join('-') } function handleKeyDown(editor, event) { if (event.repeating) return true; const key = canonicalKey(event) if (this.keys.hasOwnProperty(key)) { const action = this.keys[key] if (editor.hasOwnProperty(action)) { console.log('Key down ' + key + ', calling action ' + action) return editor[action](event) } else { console.warn('Unknown action on editor: ' + action) } } else { console.log('Key down ' + key) } return true } function mapKey(key, action) { if (this.keys.hasOwnProperty(key)) { console.warn(`Re-defining ${key} to call ${action}`) } else { console.log(`Defining ${key} to call ${action}`) } this.keys[key] = action } function Keymap() { return { keys: {}, mapKey, handleKey: handleKeyDown } } export default Keymap