@@ -11,6 +11,8 @@ import { bindActionCreators } from 'redux';
1111import * as IDEActions from '../actions/ide' ;
1212import * as ProjectActions from '../actions/project' ;
1313import * as ConsoleActions from '../actions/console' ;
14+ import * as PreferencesActions from '../actions/preferences' ;
15+
1416
1517// Local Imports
1618import Editor from '../components/Editor' ;
@@ -30,7 +32,9 @@ import useAsModal from '../../../components/useAsModal';
3032import { PreferencesIcon } from '../../../common/icons' ;
3133import Dropdown from '../../../components/Dropdown' ;
3234
33- import { useEffectWithComparison } from '../../../utils/custom-hooks' ;
35+ import { useEffectWithComparison , useEventListener } from '../../../utils/custom-hooks' ;
36+
37+ import * as device from '../../../utils/device' ;
3438
3539const withChangeDot = ( title , unsavedChanges = false ) => (
3640 < span >
@@ -69,6 +73,69 @@ const getNatOptions = (username = undefined) =>
6973const isUserOwner = ( { project, user } ) =>
7074 project && project . owner && project . owner . id === user . id ;
7175
76+ // TODO: This could go into <Editor />
77+ const handleGlobalKeydown = ( props , cmController ) => ( e ) => {
78+ alert ( 'haha' ) ;
79+ const {
80+ user, project, ide,
81+ setAllAccessibleOutput,
82+ saveProject, cloneProject, showErrorModal, startSketch, stopSketch,
83+ expandSidebar, collapseSidebar, expandConsole, collapseConsole,
84+ closeNewFolderModal, closeUploadFileModal, closeNewFileModal
85+ } = props ;
86+
87+
88+ const isMac = device . isMac ( ) ;
89+
90+ // const ctrlDown = (e.metaKey && this.isMac) || (e.ctrlKey && !this.isMac);
91+ const ctrlDown = ( isMac ? e . metaKey : e . ctrlKey ) ;
92+
93+ if ( ctrlDown ) {
94+ if ( e . shiftKey ) {
95+ if ( e . keyCode === 13 ) {
96+ e . preventDefault ( ) ;
97+ e . stopPropagation ( ) ;
98+ stopSketch ( ) ;
99+ } else if ( e . keyCode === 13 ) {
100+ e . preventDefault ( ) ;
101+ e . stopPropagation ( ) ;
102+ startSketch ( ) ;
103+ // 50 === 2
104+ } else if ( e . keyCode === 50
105+ ) {
106+ e . preventDefault ( ) ;
107+ setAllAccessibleOutput ( false ) ;
108+ // 49 === 1
109+ } else if ( e . keyCode === 49 ) {
110+ e . preventDefault ( ) ;
111+ setAllAccessibleOutput ( true ) ;
112+ }
113+ } else if ( e . keyCode === 83 ) {
114+ // 83 === s
115+ e . preventDefault ( ) ;
116+ e . stopPropagation ( ) ;
117+ if ( isUserOwner ( props ) || ( user . authenticated && ! project . owner ) ) saveProject ( cmController . getContent ( ) ) ;
118+ else if ( user . authenticated ) cloneProject ( ) ;
119+ else showErrorModal ( 'forceAuthentication' ) ;
120+
121+ // 13 === enter
122+ } else if ( e . keyCode === 66 ) {
123+ e . preventDefault ( ) ;
124+ if ( ! ide . sidebarIsExpanded ) expandSidebar ( ) ;
125+ else collapseSidebar ( ) ;
126+ }
127+ } else if ( e . keyCode === 192 && e . ctrlKey ) {
128+ e . preventDefault ( ) ;
129+ if ( ide . consoleIsExpanded ) collapseConsole ( ) ;
130+ else expandConsole ( ) ;
131+ } else if ( e . keyCode === 27 ) {
132+ if ( ide . newFolderModalVisible ) closeNewFolderModal ( ) ;
133+ else if ( ide . uploadFileModalVisible ) closeUploadFileModal ( ) ;
134+ else if ( ide . modalIsVisible ) closeNewFileModal ( ) ;
135+ }
136+ } ;
137+
138+
72139const autosave = ( autosaveInterval , setAutosaveInterval ) => ( props , prevProps ) => {
73140 const {
74141 autosaveProject, preferences, ide, selectedFile : file , project
@@ -95,14 +162,14 @@ const autosave = (autosaveInterval, setAutosaveInterval) => (props, prevProps) =
95162 }
96163} ;
97164
98-
99165const MobileIDEView = ( props ) => {
100166 const {
101167 ide, preferences, project, selectedFile, user, params, unsavedChanges, collapseConsole,
102168 stopSketch, startSketch, getProject, clearPersistedState, autosaveProject
103169 } = props ;
104170
105- const [ tmController , setTmController ] = useState ( null ) ; // eslint-disable-line
171+
172+ const [ cmController , setCmController ] = useState ( null ) ; // eslint-disable-line
106173
107174 const { username } = user ;
108175 const { consoleIsExpanded } = ide ;
@@ -139,6 +206,10 @@ const MobileIDEView = (props) => {
139206 autosaveProject, preferences, ide, selectedFile
140207 } ) ;
141208
209+ // useEventListener('keydown', () => alert('haha'));
210+ useEventListener ( 'keydown' , handleGlobalKeydown ( props , cmController ) ) ;
211+
212+
142213 return (
143214 < Screen fullscreen >
144215 < Header
@@ -159,7 +230,7 @@ const MobileIDEView = (props) => {
159230 </ Header >
160231
161232 < IDEWrapper >
162- < Editor provideController = { setTmController } />
233+ < Editor provideController = { setCmController } />
163234 </ IDEWrapper >
164235
165236 < Footer >
@@ -174,6 +245,22 @@ const MobileIDEView = (props) => {
174245 ) ;
175246} ;
176247
248+ const handleGlobalKeydownProps = {
249+ expandConsole : PropTypes . func . isRequired ,
250+ collapseConsole : PropTypes . func . isRequired ,
251+ expandSidebar : PropTypes . func . isRequired ,
252+ collapseSidebar : PropTypes . func . isRequired ,
253+
254+ setAllAccessibleOutput : PropTypes . func . isRequired ,
255+ saveProject : PropTypes . func . isRequired ,
256+ cloneProject : PropTypes . func . isRequired ,
257+ showErrorModal : PropTypes . func . isRequired ,
258+
259+ closeNewFolderModal : PropTypes . func . isRequired ,
260+ closeUploadFileModal : PropTypes . func . isRequired ,
261+ closeNewFileModal : PropTypes . func . isRequired ,
262+ } ;
263+
177264MobileIDEView . propTypes = {
178265 ide : PropTypes . shape ( {
179266 consoleIsExpanded : PropTypes . bool . isRequired ,
@@ -215,8 +302,10 @@ MobileIDEView.propTypes = {
215302 stopSketch : PropTypes . func . isRequired ,
216303 getProject : PropTypes . func . isRequired ,
217304 clearPersistedState : PropTypes . func . isRequired ,
218- collapseConsole : PropTypes . func . isRequired ,
219305 autosaveProject : PropTypes . func . isRequired ,
306+
307+
308+ ...handleGlobalKeydownProps
220309} ;
221310
222311function mapStateToProps ( state ) {
@@ -238,7 +327,8 @@ function mapStateToProps(state) {
238327const mapDispatchToProps = dispatch => bindActionCreators ( {
239328 ...ProjectActions ,
240329 ...IDEActions ,
241- ...ConsoleActions
330+ ...ConsoleActions ,
331+ ...PreferencesActions
242332} , dispatch ) ;
243333
244334export default withRouter ( connect ( mapStateToProps , mapDispatchToProps ) ( MobileIDEView ) ) ;
0 commit comments