UNPKG

@logicflow/core

Version:

LogicFlow, help you quickly create flowcharts

80 lines (79 loc) 2.87 kB
import Mousetrap from 'mousetrap'; import { forEach, isArray } from 'lodash-es'; var Keyboard = /** @class */ (function () { function Keyboard(options) { var _a; var lf = options.lf; if (!options.keyboard) { options.keyboard = { enabled: false }; } this.options = options; this.target = lf.container; this.mousetrap = new Mousetrap(this.target); if ((_a = options.keyboard) === null || _a === void 0 ? void 0 : _a.enabled) { this.enable(true); } } Keyboard.prototype.formatKey = function (key) { return key .toLowerCase() .replace(/\s/g, '') .replace('delete', 'del') .replace('cmd', 'command'); }; Keyboard.prototype.getKeys = function (keys) { var _this = this; return (isArray(keys) ? keys : [keys]).map(function (key) { return _this.formatKey(key); }); }; Object.defineProperty(Keyboard.prototype, "disabled", { get: function () { var _a; return ((_a = this.options.keyboard) === null || _a === void 0 ? void 0 : _a.enabled) !== true; }, enumerable: false, configurable: true }); Keyboard.prototype.on = function (keys, callback, action) { this.mousetrap.bind(this.getKeys(keys), callback, action); }; Keyboard.prototype.off = function (keys, action) { this.mousetrap.unbind(this.getKeys(keys), action); }; Keyboard.prototype.enable = function (force) { if (this.disabled || force) { this.options.keyboard.enabled = true; if (this.target instanceof HTMLElement) { this.target.setAttribute('tabindex', '-1'); // 去掉节点被选中时 container 出现的边框 this.target.style.outline = 'none'; } } }; Keyboard.prototype.disable = function () { if (!this.disabled) { this.options.keyboard.enabled = false; if (this.target instanceof HTMLElement) { this.target.removeAttribute('tabindex'); } } }; Keyboard.prototype.initShortcuts = function () { var _this = this; var shortcuts = this.options.keyboard.shortcuts; if (shortcuts) { if (isArray(shortcuts)) { forEach(shortcuts, function (_a) { var keys = _a.keys, callback = _a.callback, action = _a.action; _this.on(keys, callback, action); }); } else { var keys = shortcuts.keys, callback = shortcuts.callback, action = shortcuts.action; this.on(keys, callback, action); } } }; return Keyboard; }()); export { Keyboard }; export default Keyboard;