wiki/list-editor/keymap.js

52 lines
1.1 KiB
JavaScript
Raw Normal View History

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)
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)
}
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