-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathcontexts.jsx
45 lines (39 loc) · 1.12 KB
/
contexts.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import React, { useContext } from 'react';
import PropTypes from 'prop-types';
import { createContext, useState } from 'react';
import { metaKey } from '../../../../utils/metaKey';
export const EditorKeyMapsContext = createContext();
export function EditorKeyMapProvider({ children }) {
const [keyMaps, setKeyMaps] = useState({
tidy: `Shift-${metaKey}-F`,
findPersistent: `${metaKey}-F`,
findPersistentNext: `${metaKey}-G`,
findPersistentPrev: `Shift-${metaKey}-G`,
colorPicker: `${metaKey}-K`
});
const updateKeyMap = (key, value) => {
if (key in keyMaps) {
setKeyMaps((prevKeyMaps) => ({
...prevKeyMaps,
[key]: value
}));
}
};
return (
<EditorKeyMapsContext.Provider value={{ keyMaps, updateKeyMap }}>
{children}
</EditorKeyMapsContext.Provider>
);
}
EditorKeyMapProvider.propTypes = {
children: PropTypes.node.isRequired
};
export const useEditorKeyMap = () => {
const context = useContext(EditorKeyMapsContext);
if (!context) {
throw new Error(
'useEditorKeyMap must be used within a EditorKeyMapProvider'
);
}
return context;
};