1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | import { __assign, __spreadArray } from "tslib";
|
17 | import * as React from "react";
|
18 | import { shallowCompareKeys } from "../../common/utils";
|
19 | import { HotkeysDialog2 } from "../../components/hotkeys/hotkeysDialog2";
|
20 | var initialHotkeysState = { hasProvider: false, hotkeys: [], isDialogOpen: false };
|
21 | var noOpDispatch = function () { return null; };
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | export var HotkeysContext = React.createContext([initialHotkeysState, noOpDispatch]);
|
33 | var hotkeysReducer = function (state, action) {
|
34 | switch (action.type) {
|
35 | case "ADD_HOTKEYS":
|
36 |
|
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 |
|
62 |
|
63 |
|
64 |
|
65 | export 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 |
|
74 | return (React.createElement(HotkeysContext.Provider, { value: [state, dispatch] },
|
75 | children,
|
76 | hasExistingContext ? undefined : dialog));
|
77 | };
|
78 |
|
\ | No newline at end of file |