UNPKG

4.25 kBJavaScriptView Raw
1/*
2 * Copyright 2021 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 */
16var _a;
17import { __assign, __spreadArrays } from "tslib";
18import * as React from "react";
19import { shallowCompareKeys } from "../../common/utils";
20import { HotkeysDialog2 } from "../../components/hotkeys/hotkeysDialog2";
21var initialHotkeysState = { hotkeys: [], isDialogOpen: false };
22var noOpDispatch = function () { return null; };
23// N.B. we can remove this optional call guard once Blueprint depends on React 16
24/**
25 * A React context used to register and deregister hotkeys as components are mounted and unmounted in an application.
26 * Users should take care to make sure that only _one_ of these is instantiated and used within an application, especially
27 * if using global hotkeys.
28 *
29 * You will likely not be using this HotkeysContext directly, except in cases where you need to get a direct handle on an
30 * exisitng context instance for advanced use cases involving nested HotkeysProviders.
31 *
32 * For more information, see the [HotkeysProvider documentation](https://blueprintjs.com/docs/#core/context/hotkeys-provider).
33 */
34export var HotkeysContext = (_a = React.createContext) === null || _a === void 0 ? void 0 : _a.call(React, [initialHotkeysState, noOpDispatch]);
35var hotkeysReducer = function (state, action) {
36 switch (action.type) {
37 case "ADD_HOTKEYS":
38 // only pick up unique hotkeys which haven't been registered already
39 var newUniqueHotkeys = [];
40 for (var _i = 0, _a = action.payload; _i < _a.length; _i++) {
41 var a = _a[_i];
42 var isUnique = true;
43 for (var _b = 0, _c = state.hotkeys; _b < _c.length; _b++) {
44 var b = _c[_b];
45 isUnique && (isUnique = !shallowCompareKeys(a, b, { exclude: ["onKeyDown", "onKeyUp"] }));
46 }
47 if (isUnique) {
48 newUniqueHotkeys.push(a);
49 }
50 }
51 return __assign(__assign({}, state), { hotkeys: __spreadArrays(state.hotkeys, newUniqueHotkeys) });
52 case "REMOVE_HOTKEYS":
53 return __assign(__assign({}, state), { hotkeys: state.hotkeys.filter(function (key) { return action.payload.indexOf(key) === -1; }) });
54 case "OPEN_DIALOG":
55 return __assign(__assign({}, state), { isDialogOpen: true });
56 case "CLOSE_DIALOG":
57 return __assign(__assign({}, state), { isDialogOpen: false });
58 default:
59 return state;
60 }
61};
62/**
63 * Hotkeys context provider, necessary for the `useHotkeys` hook.
64 */
65export var HotkeysProvider = function (_a) {
66 var _b;
67 var children = _a.children, dialogProps = _a.dialogProps, renderDialog = _a.renderDialog, value = _a.value;
68 var hasExistingContext = value != null;
69 var _c = value !== null && value !== void 0 ? value : React.useReducer(hotkeysReducer, initialHotkeysState), state = _c[0], dispatch = _c[1];
70 var handleDialogClose = React.useCallback(function () { return dispatch({ type: "CLOSE_DIALOG" }); }, []);
71 var dialog = (_b = renderDialog === null || renderDialog === void 0 ? void 0 : renderDialog(state, { handleDialogClose: handleDialogClose })) !== null && _b !== void 0 ? _b : (React.createElement(HotkeysDialog2, __assign({}, dialogProps, { isOpen: state.isDialogOpen, hotkeys: state.hotkeys, onClose: handleDialogClose })));
72 // if we are working with an existing context, we don't need to generate our own dialog
73 return (React.createElement(HotkeysContext.Provider, { value: [state, dispatch] },
74 children,
75 hasExistingContext ? undefined : dialog));
76};
77//# sourceMappingURL=hotkeysProvider.js.map
\No newline at end of file