UNPKG

5.13 kBJavaScriptView Raw
1/*
2 * Copyright 2016 Palantir Technologies, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import { __extends } from "tslib";
17import * as React from "react";
18import { HOTKEYS_WARN_DECORATOR_NEEDS_REACT_ELEMENT, HOTKEYS_WARN_DECORATOR_NO_METHOD } from "../../common/errors";
19import { getDisplayName, isFunction } from "../../common/utils";
20import { HotkeyScope, HotkeysEvents } from "./hotkeysEvents";
21/* eslint-disable deprecation/deprecation */
22/** @deprecated use `useHotkeys` hook or `<HotkeysTarget2>` component */
23export function HotkeysTarget(WrappedComponent) {
24 var _a;
25 if (!isFunction(WrappedComponent.prototype.renderHotkeys)) {
26 console.warn(HOTKEYS_WARN_DECORATOR_NO_METHOD);
27 }
28 return _a = /** @class */ (function (_super) {
29 __extends(HotkeysTargetClass, _super);
30 function HotkeysTargetClass() {
31 var _this = _super !== null && _super.apply(this, arguments) || this;
32 /** @internal */
33 _this.globalHotkeysEvents = new HotkeysEvents(HotkeyScope.GLOBAL);
34 /** @internal */
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 // attach global key event listeners
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 // always return `element` in case caller is distinguishing between `null` and `undefined`
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//# sourceMappingURL=hotkeysTarget.js.map
\No newline at end of file