forked from processing/p5.js-web-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIDEKeyHandlers.jsx
109 lines (100 loc) · 2.96 KB
/
IDEKeyHandlers.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import PropTypes from 'prop-types';
import { useDispatch, useSelector } from 'react-redux';
import { updateFileContent } from '../actions/files';
import {
collapseConsole,
collapseSidebar,
expandConsole,
expandSidebar,
showErrorModal,
startSketch,
stopSketch,
newFile,
newFolder
} from '../actions/ide';
import { setAllAccessibleOutput } from '../actions/preferences';
import { cloneProject, saveProject } from '../actions/project';
import useKeyDownHandlers from '../../../common/useKeyDownHandlers';
import {
getAuthenticated,
getIsUserOwner,
getSketchOwner
} from '../selectors/users';
export const useIDEKeyHandlers = ({ getContent }) => {
const dispatch = useDispatch();
const sidebarIsExpanded = useSelector((state) => state.ide.sidebarIsExpanded);
const consoleIsExpanded = useSelector((state) => state.ide.consoleIsExpanded);
const rootFile = useSelector(
(state) => state.files.filter((file) => file.name === 'root')[0]
);
const isUserOwner = useSelector(getIsUserOwner);
const isAuthenticated = useSelector(getAuthenticated);
const sketchOwner = useSelector(getSketchOwner);
const syncFileContent = () => {
const file = getContent();
dispatch(updateFileContent(file.id, file.content));
};
useKeyDownHandlers({
'ctrl-s': (e) => {
e.preventDefault();
e.stopPropagation();
if (isUserOwner || (isAuthenticated && !sketchOwner)) {
dispatch(saveProject(getContent()));
} else if (isAuthenticated) {
dispatch(cloneProject());
} else {
dispatch(showErrorModal('forceAuthentication'));
}
},
'ctrl-shift-enter': (e) => {
e.preventDefault();
e.stopPropagation();
dispatch(stopSketch());
},
'ctrl-enter': (e) => {
e.preventDefault();
e.stopPropagation();
syncFileContent();
dispatch(startSketch());
},
'ctrl-shift-1': (e) => {
e.preventDefault();
dispatch(setAllAccessibleOutput(true));
},
'ctrl-shift-2': (e) => {
e.preventDefault();
dispatch(setAllAccessibleOutput(false));
},
'ctrl-b': (e) => {
e.preventDefault();
dispatch(
// TODO: create actions 'toggleConsole', 'toggleSidebar', etc.
sidebarIsExpanded ? collapseSidebar() : expandSidebar()
);
},
'ctrl-alt-n': (e) => {
e.preventDefault();
e.stopPropagation();
dispatch(newFile(rootFile.id));
},
'ctrl-alt-f': (e) => {
e.preventDefault();
e.stopPropagation();
dispatch(newFolder(rootFile.id));
},
'ctrl-`': (e) => {
e.preventDefault();
dispatch(consoleIsExpanded ? collapseConsole() : expandConsole());
}
});
};
const IDEKeyHandlers = ({ getContent }) => {
useIDEKeyHandlers({ getContent });
return null;
};
// Most actions can be accessed via redux, but those involving the cmController
// must be provided via props.
IDEKeyHandlers.propTypes = {
getContent: PropTypes.func.isRequired
};
export default IDEKeyHandlers;