import Eventful from 'zrender/src/mixin/Eventful'; import {keyboardEvents} from './config/keyboardEvents'; class KeyboardController extends Eventful { constructor(jmap) { super(); this.$jmap = jmap; this.$zr = jmap.getZr(); this.events = jmap.getEvents(); this.initData(); this.initHandler(this.$zr); } initHandler(zr) { if (zr) { var keydownHandle = this.keydown.bind(this); // var dom = this.$zr.dom; this.enable = function (opts) { opts = opts || {}; this._keyOnDownUp = opts.keyOnDownUp || true; window.addEventListener('keyup', keydownHandle, false); // dom.focus(); }; this.disable = function () { window.removeEventListener('keyup', keydownHandle, false); }; this.dispose = function() { this.disable(); }; } } initData() { } keydown(e) { if (this._keyOnDownUp && !e.repeat) { if (e.key) { const currentEvent = keyboardEvents[e.key.toUpperCase()]; let str = ''; if (currentEvent && currentEvent.altKey === e.altKey && currentEvent.ctrlKey === e.ctrlKey && currentEvent.shiftKey === e.shiftKey) { str = currentEvent.event; } this.trigger(this.events.Keyboard, str); } } } } export default KeyboardController;