substance
Version:
Substance is a JavaScript library for web-based content editing. It provides building blocks for realizing custom text editors and web-based publishing system. It is developed to power our online editing platform [Substance](http://substance.io).
38 lines (32 loc) • 1.09 kB
JavaScript
import { parseKeyEvent, parseKeyCombo } from '../util'
import AbstractKeyboardManager from './AbstractKeyboardManager'
export default class KeyboardManager extends AbstractKeyboardManager {
constructor (bindings, commandCallback, contextProvider) {
super()
this.contextProvider = contextProvider
this.bindings = {}
bindings.forEach(({ key, spec }) => {
if (!spec.command) throw new Error("'spec.command' is required")
const hook = () => {
return commandCallback(spec.command)
}
const type = spec.type || 'keydown'
if (type !== 'textinput') {
key = parseKeyEvent(parseKeyCombo(key))
}
// initializing on-the-fly
if (!this.bindings[type]) { this.bindings[type] = {} }
if (!this.bindings[type][key]) { this.bindings[type][key] = [] }
this.bindings[type][key].push(hook)
})
}
_getBindings (type, key) {
const bindingsByType = this.bindings[type]
if (bindingsByType) {
return bindingsByType[key]
}
}
_getContext () {
return this.contextProvider.context
}
}