1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | import { __extends } from "tslib";
|
17 | import * as React from "react";
|
18 | import { HOTKEYS_WARN_DECORATOR_NEEDS_REACT_ELEMENT, HOTKEYS_WARN_DECORATOR_NO_METHOD } from "../../common/errors";
|
19 | import { getDisplayName, isFunction } from "../../common/utils";
|
20 | import { HotkeyScope, HotkeysEvents } from "./hotkeysEvents";
|
21 |
|
22 |
|
23 | export function HotkeysTarget(WrappedComponent) {
|
24 | var _a;
|
25 | if (!isFunction(WrappedComponent.prototype.renderHotkeys)) {
|
26 | console.warn(HOTKEYS_WARN_DECORATOR_NO_METHOD);
|
27 | }
|
28 | return _a = (function (_super) {
|
29 | __extends(HotkeysTargetClass, _super);
|
30 | function HotkeysTargetClass() {
|
31 | var _this = _super !== null && _super.apply(this, arguments) || this;
|
32 |
|
33 | _this.globalHotkeysEvents = new HotkeysEvents(HotkeyScope.GLOBAL);
|
34 |
|
35 | _this.localHotkeysEvents = new HotkeysEvents(HotkeyScope.LOCAL);
|
36 | return _this;
|
37 | }
|
38 | HotkeysTargetClass.prototype.componentDidMount = function () {
|
39 | if (_super.prototype.componentDidMount != null) {
|
40 | _super.prototype.componentDidMount.call(this);
|
41 | }
|
42 |
|
43 | document.addEventListener("keydown", this.globalHotkeysEvents.handleKeyDown);
|
44 | document.addEventListener("keyup", this.globalHotkeysEvents.handleKeyUp);
|
45 | };
|
46 | HotkeysTargetClass.prototype.componentWillUnmount = function () {
|
47 | var _a;
|
48 | (_a = _super.prototype.componentWillUnmount) === null || _a === void 0 ? void 0 : _a.call(this);
|
49 | document.removeEventListener("keydown", this.globalHotkeysEvents.handleKeyDown);
|
50 | document.removeEventListener("keyup", this.globalHotkeysEvents.handleKeyUp);
|
51 | this.globalHotkeysEvents.clear();
|
52 | this.localHotkeysEvents.clear();
|
53 | };
|
54 | HotkeysTargetClass.prototype.render = function () {
|
55 | var _this = this;
|
56 | var element = _super.prototype.render.call(this);
|
57 | if (element == null) {
|
58 |
|
59 | return element;
|
60 | }
|
61 | if (!React.isValidElement(element)) {
|
62 | console.warn(HOTKEYS_WARN_DECORATOR_NEEDS_REACT_ELEMENT);
|
63 | return element;
|
64 | }
|
65 | if (isFunction(this.renderHotkeys)) {
|
66 | var hotkeys = this.renderHotkeys();
|
67 | if (this.localHotkeysEvents) {
|
68 | this.localHotkeysEvents.setHotkeys(hotkeys.props);
|
69 | }
|
70 | if (this.globalHotkeysEvents) {
|
71 | this.globalHotkeysEvents.setHotkeys(hotkeys.props);
|
72 | }
|
73 | if (this.localHotkeysEvents.count() > 0) {
|
74 | var tabIndex = hotkeys.props.tabIndex === undefined ? 0 : hotkeys.props.tabIndex;
|
75 | var _a = element.props, existingKeyDown_1 = _a.onKeyDown, existingKeyUp_1 = _a.onKeyUp;
|
76 | var handleKeyDownWrapper = function (e) {
|
77 | _this.localHotkeysEvents.handleKeyDown(e.nativeEvent);
|
78 | existingKeyDown_1 === null || existingKeyDown_1 === void 0 ? void 0 : existingKeyDown_1(e);
|
79 | };
|
80 | var handleKeyUpWrapper = function (e) {
|
81 | _this.localHotkeysEvents.handleKeyUp(e.nativeEvent);
|
82 | existingKeyUp_1 === null || existingKeyUp_1 === void 0 ? void 0 : existingKeyUp_1(e);
|
83 | };
|
84 | return React.cloneElement(element, {
|
85 | onKeyDown: handleKeyDownWrapper,
|
86 | onKeyUp: handleKeyUpWrapper,
|
87 | tabIndex: tabIndex,
|
88 | });
|
89 | }
|
90 | }
|
91 | return element;
|
92 | };
|
93 | return HotkeysTargetClass;
|
94 | }(WrappedComponent)),
|
95 | _a.displayName = "HotkeysTarget(" + getDisplayName(WrappedComponent) + ")",
|
96 | _a;
|
97 | }
|
98 |
|
\ | No newline at end of file |