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