1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | var _a;
|
17 | import { __assign, __spreadArrays } from "tslib";
|
18 | import * as React from "react";
|
19 | import { HotkeysDialog2 } from "../../components/hotkeys/hotkeysDialog2";
|
20 | var initialHotkeysState = { hotkeys: [], isDialogOpen: false };
|
21 | var noOpDispatch = function () { return null; };
|
22 |
|
23 | export var HotkeysContext = (_a = React.createContext) === null || _a === void 0 ? void 0 : _a.call(React, [
|
24 | initialHotkeysState,
|
25 | noOpDispatch,
|
26 | ]);
|
27 | var hotkeysReducer = function (state, action) {
|
28 | switch (action.type) {
|
29 | case "ADD_HOTKEYS":
|
30 | return __assign(__assign({}, state), { hotkeys: __spreadArrays(state.hotkeys, action.payload) });
|
31 | case "REMOVE_HOTKEYS":
|
32 | return __assign(__assign({}, state), { hotkeys: state.hotkeys.filter(function (key) { return action.payload.indexOf(key) === -1; }) });
|
33 | case "OPEN_DIALOG":
|
34 | return __assign(__assign({}, state), { isDialogOpen: true });
|
35 | case "CLOSE_DIALOG":
|
36 | return __assign(__assign({}, state), { isDialogOpen: false });
|
37 | default:
|
38 | return state;
|
39 | }
|
40 | };
|
41 |
|
42 |
|
43 |
|
44 | export var HotkeysProvider = function (_a) {
|
45 | var _b;
|
46 | var children = _a.children, dialogProps = _a.dialogProps, renderDialog = _a.renderDialog;
|
47 | var _c = React.useReducer(hotkeysReducer, initialHotkeysState), state = _c[0], dispatch = _c[1];
|
48 | var handleDialogClose = React.useCallback(function () { return dispatch({ type: "CLOSE_DIALOG" }); }, []);
|
49 | 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 })));
|
50 | return (React.createElement(HotkeysContext.Provider, { value: [state, dispatch] },
|
51 | children,
|
52 | dialog));
|
53 | };
|
54 |
|
\ | No newline at end of file |