2019-11-29 12:51:58 +08:00
|
|
|
import Eventful from 'zrender/src/mixin/Eventful';
|
2019-12-06 10:05:14 +08:00
|
|
|
import {keyboardEvents} from './config/keyboardEvents';
|
2019-11-29 12:51:58 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
this.enable = function (opts) {
|
|
|
|
opts = opts || {};
|
|
|
|
this._keyOnDownUp = opts.keyOnDownUp || true;
|
|
|
|
|
2020-03-31 14:41:17 +08:00
|
|
|
window.addEventListener('keyup', keydownHandle, false);
|
2019-11-29 12:51:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
this.disable = function () {
|
2020-03-31 14:41:17 +08:00
|
|
|
window.removeEventListener('keyup', keydownHandle, false);
|
2019-11-29 12:51:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
this.dispose = function() {
|
|
|
|
this.disable();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
initData() {
|
2019-12-06 10:05:14 +08:00
|
|
|
|
2019-11-29 12:51:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
keydown(e) {
|
|
|
|
if (this._keyOnDownUp && !e.repeat) {
|
2019-12-06 10:05:14 +08:00
|
|
|
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;
|
2019-11-29 12:51:58 +08:00
|
|
|
}
|
2019-12-06 10:05:14 +08:00
|
|
|
this.trigger(this.events.Keyboard, str);
|
2019-11-29 12:51:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default KeyboardController;
|