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)
console.log('Key down ' + key)
if (this.keys.hasOwnProperty(key)) {
const action = this.keys[key]
if (editor.hasOwnProperty(action)) {
console.log('Calling action ' + action)
return editor[action](event)
} else {
console.warn('Unknown action on editor: ' + action)
}
}
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