|
| 1 | +import PropTypes from 'prop-types'; |
| 2 | +import { useDispatch, useSelector } from 'react-redux'; |
| 3 | +import { updateFileContent } from '../actions/files'; |
| 4 | +import { |
| 5 | + collapseConsole, |
| 6 | + collapseSidebar, |
| 7 | + expandConsole, |
| 8 | + expandSidebar, |
| 9 | + showErrorModal, |
| 10 | + startSketch, |
| 11 | + stopSketch |
| 12 | +} from '../actions/ide'; |
| 13 | +import { setAllAccessibleOutput } from '../actions/preferences'; |
| 14 | +import { cloneProject, saveProject } from '../actions/project'; |
| 15 | +import useKeyDownHandlers from '../hooks/useKeyDownHandlers'; |
| 16 | +import { |
| 17 | + getAuthenticated, |
| 18 | + getIsUserOwner, |
| 19 | + getSketchOwner |
| 20 | +} from '../selectors/users'; |
| 21 | + |
| 22 | +export const useIDEKeyHandlers = ({ getContent }) => { |
| 23 | + const dispatch = useDispatch(); |
| 24 | + |
| 25 | + const sidebarIsExpanded = useSelector((state) => state.ide.sidebarIsExpanded); |
| 26 | + const consoleIsExpanded = useSelector((state) => state.ide.consoleIsExpanded); |
| 27 | + |
| 28 | + const isUserOwner = useSelector(getIsUserOwner); |
| 29 | + const isAuthenticated = useSelector(getAuthenticated); |
| 30 | + const sketchOwner = useSelector(getSketchOwner); |
| 31 | + |
| 32 | + const syncFileContent = () => { |
| 33 | + const file = getContent(); |
| 34 | + dispatch(updateFileContent(file.id, file.content)); |
| 35 | + }; |
| 36 | + |
| 37 | + useKeyDownHandlers({ |
| 38 | + 'ctrl-s': (e) => { |
| 39 | + e.preventDefault(); |
| 40 | + e.stopPropagation(); |
| 41 | + if (isUserOwner || (isAuthenticated && !sketchOwner)) { |
| 42 | + dispatch(saveProject(getContent())); |
| 43 | + } else if (isAuthenticated) { |
| 44 | + dispatch(cloneProject()); |
| 45 | + } else { |
| 46 | + dispatch(showErrorModal('forceAuthentication')); |
| 47 | + } |
| 48 | + }, |
| 49 | + 'ctrl-shift-enter': (e) => { |
| 50 | + e.preventDefault(); |
| 51 | + e.stopPropagation(); |
| 52 | + dispatch(stopSketch()); |
| 53 | + }, |
| 54 | + 'ctrl-enter': (e) => { |
| 55 | + e.preventDefault(); |
| 56 | + e.stopPropagation(); |
| 57 | + syncFileContent(); |
| 58 | + dispatch(startSketch()); |
| 59 | + }, |
| 60 | + 'ctrl-shift-1': (e) => { |
| 61 | + e.preventDefault(); |
| 62 | + dispatch(setAllAccessibleOutput(true)); |
| 63 | + }, |
| 64 | + 'ctrl-shift-2': (e) => { |
| 65 | + e.preventDefault(); |
| 66 | + dispatch(setAllAccessibleOutput(false)); |
| 67 | + }, |
| 68 | + 'ctrl-b': (e) => { |
| 69 | + e.preventDefault(); |
| 70 | + dispatch( |
| 71 | + // TODO: create actions 'toggleConsole', 'toggleSidebar', etc. |
| 72 | + sidebarIsExpanded ? collapseSidebar() : expandSidebar() |
| 73 | + ); |
| 74 | + }, |
| 75 | + 'ctrl-`': (e) => { |
| 76 | + e.preventDefault(); |
| 77 | + dispatch(consoleIsExpanded ? collapseConsole() : expandConsole()); |
| 78 | + } |
| 79 | + }); |
| 80 | +}; |
| 81 | + |
| 82 | +const IDEKeyHandlers = ({ getContent }) => { |
| 83 | + useIDEKeyHandlers({ getContent }); |
| 84 | + return null; |
| 85 | +}; |
| 86 | + |
| 87 | +// Most actions can be accessed via redux, but those involving the cmController |
| 88 | +// must be provided via props. |
| 89 | +IDEKeyHandlers.propTypes = { |
| 90 | + getContent: PropTypes.func.isRequired |
| 91 | +}; |
| 92 | + |
| 93 | +export default IDEKeyHandlers; |
0 commit comments