5.49 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";
17/**
18 * @fileoverview This component is DEPRECATED, and the code is frozen.
19 * All changes & bugfixes should be made to HotkeysTarget2 instead.
20 */
21/* eslint-disable deprecation/deprecation */
22import * as React from "react";
23import { isFunction } from "../common/utils";
24import { HotkeyScope, HotkeysEvents } from "./hotkeysEvents";
25import { getDisplayName } from "./legacyCommon";
26var HOTKEYS_WARN_DECORATOR_NO_METHOD = "[Blueprint] @HotkeysTargetLegacy-decorated class should implement renderHotkeys.";
27var HOTKEYS_WARN_DECORATOR_NEEDS_REACT_ELEMENT = "[Blueprint] \"@HotkeysTargetLegacy-decorated components must return a single JSX.Element or an empty render.";
28/** @deprecated use `useHotkeys` hook or `<HotkeysTarget2>` component */
29export function HotkeysTargetLegacy(WrappedComponent) {
30 var _a;
31 if (!isFunction(WrappedComponent.prototype.renderHotkeys)) {
32 console.warn(HOTKEYS_WARN_DECORATOR_NO_METHOD);
33 }
34 return _a = /** @class */ (function (_super) {
35 __extends(HotkeysTargetClass, _super);
36 function HotkeysTargetClass() {
37 var _this = _super !== null && _super.apply(this, arguments) || this;
38 /** @internal */
39 _this.globalHotkeysEvents = new HotkeysEvents(HotkeyScope.GLOBAL);
40 /** @internal */
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 // attach global key event listeners
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 // always return `element` in case caller is distinguishing between `null` and `undefined`
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//# sourceMappingURL=hotkeysTargetLegacy.js.map
\No newline at end of file