UNPKG

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