2020-11-01 17:11:16 +00:00
|
|
|
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)) {
|
2020-11-08 20:15:54 +00:00
|
|
|
console.log('Key down ' + key + ', calling action ' + action)
|
2020-11-01 17:11:16 +00:00
|
|
|
return editor[action](event)
|
|
|
|
} else {
|
|
|
|
console.warn('Unknown action on editor: ' + action)
|
|
|
|
}
|
2020-11-08 20:15:54 +00:00
|
|
|
} else {
|
|
|
|
console.log('Key down ' + key)
|
2020-11-01 17:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|