1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | import { __spreadArray } from "tslib";
|
17 | import * as React from "react";
|
18 | import { HOTKEYS_PROVIDER_NOT_FOUND } from "../../common/errors";
|
19 | import { elementIsTextInput } from "../../common/utils/domUtils";
|
20 | import { comboMatches, getKeyCombo, parseKeyCombo } from "../../components/hotkeys/hotkeyParser";
|
21 | import { HotkeysContext } from "../../context";
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | export function useHotkeys(keys, options) {
|
30 | if (options === void 0) { options = {}; }
|
31 | var _a = options.document, document = _a === void 0 ? getDefaultDocument() : _a, _b = options.showDialogKeyCombo, showDialogKeyCombo = _b === void 0 ? "?" : _b;
|
32 | var localKeys = React.useMemo(function () {
|
33 | return keys
|
34 | .filter(function (k) { return !k.global; })
|
35 | .map(function (k) { return ({
|
36 | combo: parseKeyCombo(k.combo),
|
37 | config: k,
|
38 | }); });
|
39 | }, [keys]);
|
40 | var globalKeys = React.useMemo(function () {
|
41 | return keys
|
42 | .filter(function (k) { return k.global; })
|
43 | .map(function (k) { return ({
|
44 | combo: parseKeyCombo(k.combo),
|
45 | config: k,
|
46 | }); });
|
47 | }, [keys]);
|
48 |
|
49 | var _c = React.useContext(HotkeysContext), state = _c[0], dispatch = _c[1];
|
50 | React.useEffect(function () {
|
51 | if (!state.hasProvider) {
|
52 | console.warn(HOTKEYS_PROVIDER_NOT_FOUND);
|
53 | }
|
54 | }, [state.hasProvider]);
|
55 |
|
56 | React.useEffect(function () {
|
57 | var payload = __spreadArray(__spreadArray([], globalKeys.map(function (k) { return k.config; }), true), localKeys.map(function (k) { return k.config; }), true);
|
58 | dispatch({ type: "ADD_HOTKEYS", payload: payload });
|
59 | return function () { return dispatch({ type: "REMOVE_HOTKEYS", payload: payload }); };
|
60 | }, [dispatch, globalKeys, localKeys]);
|
61 | var invokeNamedCallbackIfComboRecognized = React.useCallback(function (global, combo, callbackName, e) {
|
62 | var _a, _b;
|
63 | var isTextInput = elementIsTextInput(e.target);
|
64 | for (var _i = 0, _c = global ? globalKeys : localKeys; _i < _c.length; _i++) {
|
65 | var key = _c[_i];
|
66 | var _d = key.config, _e = _d.allowInInput, allowInInput = _e === void 0 ? false : _e, _f = _d.disabled, disabled = _f === void 0 ? false : _f, _g = _d.preventDefault, preventDefault = _g === void 0 ? false : _g, _h = _d.stopPropagation, stopPropagation = _h === void 0 ? false : _h;
|
67 | var shouldIgnore = (isTextInput && !allowInInput) || disabled;
|
68 | if (!shouldIgnore && comboMatches(key.combo, combo)) {
|
69 | if (preventDefault) {
|
70 | e.preventDefault();
|
71 | }
|
72 | if (stopPropagation) {
|
73 |
|
74 | e.isPropagationStopped = true;
|
75 | e.stopPropagation();
|
76 | }
|
77 | (_b = (_a = key.config)[callbackName]) === null || _b === void 0 ? void 0 : _b.call(_a, e);
|
78 | }
|
79 | }
|
80 | }, [globalKeys, localKeys]);
|
81 | var handleGlobalKeyDown = React.useCallback(function (e) {
|
82 |
|
83 | var combo = getKeyCombo(e);
|
84 | var isTextInput = elementIsTextInput(e.target);
|
85 | if (!isTextInput && comboMatches(parseKeyCombo(showDialogKeyCombo), combo)) {
|
86 | dispatch({ type: "OPEN_DIALOG" });
|
87 | }
|
88 | else {
|
89 | invokeNamedCallbackIfComboRecognized(true, getKeyCombo(e), "onKeyDown", e);
|
90 | }
|
91 | }, [dispatch, invokeNamedCallbackIfComboRecognized, showDialogKeyCombo]);
|
92 | var handleGlobalKeyUp = React.useCallback(function (e) { return invokeNamedCallbackIfComboRecognized(true, getKeyCombo(e), "onKeyUp", e); }, [invokeNamedCallbackIfComboRecognized]);
|
93 | var handleLocalKeyDown = React.useCallback(function (e) {
|
94 | return invokeNamedCallbackIfComboRecognized(false, getKeyCombo(e.nativeEvent), "onKeyDown", e.nativeEvent);
|
95 | }, [invokeNamedCallbackIfComboRecognized]);
|
96 | var handleLocalKeyUp = React.useCallback(function (e) {
|
97 | return invokeNamedCallbackIfComboRecognized(false, getKeyCombo(e.nativeEvent), "onKeyUp", e.nativeEvent);
|
98 | }, [invokeNamedCallbackIfComboRecognized]);
|
99 | React.useEffect(function () {
|
100 |
|
101 | document.addEventListener("keydown", handleGlobalKeyDown);
|
102 | document.addEventListener("keyup", handleGlobalKeyUp);
|
103 | return function () {
|
104 | document.removeEventListener("keydown", handleGlobalKeyDown);
|
105 | document.removeEventListener("keyup", handleGlobalKeyUp);
|
106 | };
|
107 | }, [handleGlobalKeyDown, handleGlobalKeyUp]);
|
108 | return { handleKeyDown: handleLocalKeyDown, handleKeyUp: handleLocalKeyUp };
|
109 | }
|
110 | function getDefaultDocument() {
|
111 | if (typeof window === "undefined") {
|
112 | return undefined;
|
113 | }
|
114 | return window.document;
|
115 | }
|
116 |
|
\ | No newline at end of file |