@turbox3d/event-manager
Version:
Large-scale productivity application event management library
99 lines • 2.85 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import { warn } from '@turbox3d/shared';
import { HotKeyListener } from './listener';
/**
* 快捷键系统
*/
var HotKeyController = /*#__PURE__*/function () {
function HotKeyController() {
var _this = this;
_classCallCheck(this, HotKeyController);
this.keyMap = {};
/**
* 触发快捷键回调
*/
this.handler = function (hotkey, keyEventType) {
if (_this.keyMap[hotkey]) {
var handler = _this.keyMap[hotkey].handler;
handler(keyEventType);
}
};
this.listener = new HotKeyListener(document, this.handler);
}
/**
* 添加快捷键
*
* 快捷键冲突会给 Warn 并添加失败
*/
return _createClass(HotKeyController, [{
key: "on",
value: function on(config) {
var _this2 = this;
var hotkeys = Array.isArray(config.key) ? config.key : [config.key];
hotkeys.forEach(function (hotkey) {
_this2.listener.register(hotkey);
if (_this2.keyMap[hotkey]) {
var oldConfig = _this2.keyMap[hotkey];
warn('conflicted hot key detected,please check again:');
console.table({
hotkey: hotkey,
name: oldConfig.name || '',
info: oldConfig.info || ''
});
} else {
_this2.keyMap[hotkey] = config;
}
});
return this;
}
/**
* 移除快捷键
*/
}, {
key: "off",
value: function off(key, handler) {
var _this3 = this;
var hotkeys = Array.isArray(key) ? key : [key];
hotkeys.forEach(function (hotkey) {
_this3.listener.deregister(hotkey);
var config = _this3.keyMap[hotkey];
if (config && config.handler === handler) {
delete _this3.keyMap[hotkey];
}
});
return this;
}
// enable() {
// // todo: 1. 启用整个 context 2. 启用某个 key 3. key 是否需要增加匹配模式
// }
// disable() {
// //
// }
/**
* 获取展示热键列表(仅返回 show 字段设置为 true 的数据)
*
* 该接口设计用来给展示快捷键的地方用
*/
}, {
key: "getHotKeyData",
value: function getHotKeyData() {
var _this4 = this;
var data = [];
// todo 这样数据可能一个被拆成了多个返回
Object.keys(this.keyMap).forEach(function (hotkey) {
var config = _this4.keyMap[hotkey];
if (config.show) {
data.push({
key: hotkey,
name: config.name,
description: config.description
});
}
});
return data;
}
}]);
}();
var HotKey = new HotKeyController();
export { HotKey, HotKeyController };