UNPKG

4.97 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 { Children } from "react";
17import { isElementOfType } from "../../common/utils";
18import { Hotkey } from "./hotkey";
19import { comboMatches, getKeyCombo, parseKeyCombo } from "./hotkeyParser";
20import { hideHotkeysDialogAfterDelay, isHotkeysDialogShowing, showHotkeysDialog } from "./hotkeysDialog";
21var SHOW_DIALOG_KEY = "?";
22export var HotkeyScope;
23(function (HotkeyScope) {
24 HotkeyScope["LOCAL"] = "local";
25 HotkeyScope["GLOBAL"] = "global";
26})(HotkeyScope || (HotkeyScope = {}));
27var HotkeysEvents = /** @class */ (function () {
28 function HotkeysEvents(scope) {
29 var _this = this;
30 this.scope = scope;
31 this.actions = [];
32 this.handleKeyDown = function (e) {
33 var combo = getKeyCombo(e);
34 var isTextInput = _this.isTextInput(e);
35 if (!isTextInput && comboMatches(parseKeyCombo(SHOW_DIALOG_KEY), combo)) {
36 if (isHotkeysDialogShowing()) {
37 hideHotkeysDialogAfterDelay();
38 }
39 else {
40 showHotkeysDialog(_this.actions.map(function (action) { return action.props; }));
41 }
42 return;
43 }
44 else if (isHotkeysDialogShowing()) {
45 return;
46 }
47 _this.invokeNamedCallbackIfComboRecognized(combo, "onKeyDown", e);
48 };
49 this.handleKeyUp = function (e) {
50 if (isHotkeysDialogShowing()) {
51 return;
52 }
53 _this.invokeNamedCallbackIfComboRecognized(getKeyCombo(e), "onKeyUp", e);
54 };
55 }
56 HotkeysEvents.prototype.count = function () {
57 return this.actions.length;
58 };
59 HotkeysEvents.prototype.clear = function () {
60 this.actions = [];
61 };
62 HotkeysEvents.prototype.setHotkeys = function (props) {
63 var _this = this;
64 var actions = [];
65 Children.forEach(props.children, function (child) {
66 if (isElementOfType(child, Hotkey) && _this.isScope(child.props)) {
67 actions.push({
68 combo: parseKeyCombo(child.props.combo),
69 props: child.props,
70 });
71 }
72 });
73 this.actions = actions;
74 };
75 HotkeysEvents.prototype.invokeNamedCallbackIfComboRecognized = function (combo, callbackName, e) {
76 var _a, _b;
77 var isTextInput = this.isTextInput(e);
78 for (var _i = 0, _c = this.actions; _i < _c.length; _i++) {
79 var action = _c[_i];
80 var shouldIgnore = (isTextInput && !action.props.allowInInput) || action.props.disabled;
81 if (!shouldIgnore && comboMatches(action.combo, combo)) {
82 if (action.props.preventDefault) {
83 e.preventDefault();
84 }
85 if (action.props.stopPropagation) {
86 // set a flag just for unit testing. not meant to be referenced in feature work.
87 e.isPropagationStopped = true;
88 e.stopPropagation();
89 }
90 (_b = (_a = action.props)[callbackName]) === null || _b === void 0 ? void 0 : _b.call(_a, e);
91 }
92 }
93 };
94 HotkeysEvents.prototype.isScope = function (props) {
95 return (props.global ? HotkeyScope.GLOBAL : HotkeyScope.LOCAL) === this.scope;
96 };
97 HotkeysEvents.prototype.isTextInput = function (e) {
98 var elem = e.target;
99 // we check these cases for unit testing, but this should not happen
100 // during normal operation
101 if (elem == null || elem.closest == null) {
102 return false;
103 }
104 var editable = elem.closest("input, textarea, [contenteditable=true]");
105 if (editable == null) {
106 return false;
107 }
108 // don't let checkboxes, switches, and radio buttons prevent hotkey behavior
109 if (editable.tagName.toLowerCase() === "input") {
110 var inputType = editable.type;
111 if (inputType === "checkbox" || inputType === "radio") {
112 return false;
113 }
114 }
115 // don't let read-only fields prevent hotkey behavior
116 if (editable.readOnly) {
117 return false;
118 }
119 return true;
120 };
121 return HotkeysEvents;
122}());
123export { HotkeysEvents };
124//# sourceMappingURL=hotkeysEvents.js.map
\No newline at end of file