1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | import { Children } from "react";
|
17 | import { isElementOfType } from "../../common/utils";
|
18 | import { Hotkey } from "./hotkey";
|
19 | import { comboMatches, getKeyCombo, parseKeyCombo } from "./hotkeyParser";
|
20 | import { hideHotkeysDialogAfterDelay, isHotkeysDialogShowing, showHotkeysDialog } from "./hotkeysDialog";
|
21 | var SHOW_DIALOG_KEY = "?";
|
22 | export var HotkeyScope;
|
23 | (function (HotkeyScope) {
|
24 | HotkeyScope["LOCAL"] = "local";
|
25 | HotkeyScope["GLOBAL"] = "global";
|
26 | })(HotkeyScope || (HotkeyScope = {}));
|
27 | var HotkeysEvents = (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 |
|
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 |
|
100 |
|
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 |
|
109 | if (editable.tagName.toLowerCase() === "input") {
|
110 | var inputType = editable.type;
|
111 | if (inputType === "checkbox" || inputType === "radio") {
|
112 | return false;
|
113 | }
|
114 | }
|
115 |
|
116 | if (editable.readOnly) {
|
117 | return false;
|
118 | }
|
119 | return true;
|
120 | };
|
121 | return HotkeysEvents;
|
122 | }());
|
123 | export { HotkeysEvents };
|
124 |
|
\ | No newline at end of file |