@@ -11,6 +11,8 @@ import { bindActionCreators } from 'redux';
11
11
import * as IDEActions from '../actions/ide' ;
12
12
import * as ProjectActions from '../actions/project' ;
13
13
import * as ConsoleActions from '../actions/console' ;
14
+ import * as PreferencesActions from '../actions/preferences' ;
15
+
14
16
15
17
// Local Imports
16
18
import Editor from '../components/Editor' ;
@@ -30,7 +32,9 @@ import useAsModal from '../../../components/useAsModal';
30
32
import { PreferencesIcon } from '../../../common/icons' ;
31
33
import Dropdown from '../../../components/Dropdown' ;
32
34
33
- import { useEffectWithComparison } from '../../../utils/custom-hooks' ;
35
+ import { useEffectWithComparison , useEventListener } from '../../../utils/custom-hooks' ;
36
+
37
+ import * as device from '../../../utils/device' ;
34
38
35
39
const withChangeDot = ( title , unsavedChanges = false ) => (
36
40
< span >
@@ -69,6 +73,69 @@ const getNatOptions = (username = undefined) =>
69
73
const isUserOwner = ( { project, user } ) =>
70
74
project && project . owner && project . owner . id === user . id ;
71
75
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
+
72
139
const autosave = ( autosaveInterval , setAutosaveInterval ) => ( props , prevProps ) => {
73
140
const {
74
141
autosaveProject, preferences, ide, selectedFile : file , project
@@ -95,14 +162,14 @@ const autosave = (autosaveInterval, setAutosaveInterval) => (props, prevProps) =
95
162
}
96
163
} ;
97
164
98
-
99
165
const MobileIDEView = ( props ) => {
100
166
const {
101
167
ide, preferences, project, selectedFile, user, params, unsavedChanges, collapseConsole,
102
168
stopSketch, startSketch, getProject, clearPersistedState, autosaveProject
103
169
} = props ;
104
170
105
- const [ tmController , setTmController ] = useState ( null ) ; // eslint-disable-line
171
+
172
+ const [ cmController , setCmController ] = useState ( null ) ; // eslint-disable-line
106
173
107
174
const { username } = user ;
108
175
const { consoleIsExpanded } = ide ;
@@ -139,6 +206,10 @@ const MobileIDEView = (props) => {
139
206
autosaveProject, preferences, ide, selectedFile
140
207
} ) ;
141
208
209
+ // useEventListener('keydown', () => alert('haha'));
210
+ useEventListener ( 'keydown' , handleGlobalKeydown ( props , cmController ) ) ;
211
+
212
+
142
213
return (
143
214
< Screen fullscreen >
144
215
< Header
@@ -159,7 +230,7 @@ const MobileIDEView = (props) => {
159
230
</ Header >
160
231
161
232
< IDEWrapper >
162
- < Editor provideController = { setTmController } />
233
+ < Editor provideController = { setCmController } />
163
234
</ IDEWrapper >
164
235
165
236
< Footer >
@@ -174,6 +245,22 @@ const MobileIDEView = (props) => {
174
245
) ;
175
246
} ;
176
247
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
+
177
264
MobileIDEView . propTypes = {
178
265
ide : PropTypes . shape ( {
179
266
consoleIsExpanded : PropTypes . bool . isRequired ,
@@ -215,8 +302,10 @@ MobileIDEView.propTypes = {
215
302
stopSketch : PropTypes . func . isRequired ,
216
303
getProject : PropTypes . func . isRequired ,
217
304
clearPersistedState : PropTypes . func . isRequired ,
218
- collapseConsole : PropTypes . func . isRequired ,
219
305
autosaveProject : PropTypes . func . isRequired ,
306
+
307
+
308
+ ...handleGlobalKeydownProps
220
309
} ;
221
310
222
311
function mapStateToProps ( state ) {
@@ -238,7 +327,8 @@ function mapStateToProps(state) {
238
327
const mapDispatchToProps = dispatch => bindActionCreators ( {
239
328
...ProjectActions ,
240
329
...IDEActions ,
241
- ...ConsoleActions
330
+ ...ConsoleActions ,
331
+ ...PreferencesActions
242
332
} , dispatch ) ;
243
333
244
334
export default withRouter ( connect ( mapStateToProps , mapDispatchToProps ) ( MobileIDEView ) ) ;
0 commit comments