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