UNPKG

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