@@ -74,9 +74,7 @@ class Index extends React.Component {
74
74
75
75
componentDidMount ( ) {
76
76
document . onblur = ( ) => {
77
- this . setState ( {
78
- focusedPane : null ,
79
- } ) ;
77
+ this . setFocus ( null ) ;
80
78
}
81
79
}
82
80
@@ -141,18 +139,14 @@ class Index extends React.Component {
141
139
}
142
140
143
141
handleInsertButtonClick = ( ) => {
144
- this . setState ( {
145
- focusedPane : this . state . insertPanelsAreOpen ? null : 'InsertPanels' ,
146
- } ) ;
142
+ this . setFocusIf ( 'insertPanelsAreOpen' , null , 'InsertPanels' )
147
143
this . setPersistentState ( {
148
144
insertPanelsAreOpen : ! this . state . insertPanelsAreOpen ,
149
145
} ) ;
150
146
}
151
147
152
148
handleNodeFormatButtonClick = ( ) => {
153
- this . setState ( {
154
- focusedPane : this . state . nodeFormatDrawerIsOpen ? null : 'NodeFormatDrawer' ,
155
- } ) ;
149
+ this . setFocusIf ( 'nodeFormatDrawerIsOpen' , null , 'NodeFormatDrawer' )
156
150
this . setPersistentState ( {
157
151
nodeFormatDrawerIsOpen : ! this . state . nodeFormatDrawerIsOpen ,
158
152
edgeFormatDrawerIsOpen : false ,
@@ -163,15 +157,11 @@ class Index extends React.Component {
163
157
this . setPersistentState ( {
164
158
nodeFormatDrawerIsOpen : false ,
165
159
} ) ;
166
- this . setState ( {
167
- focusedPane : null ,
168
- } ) ;
160
+ this . setFocus ( null ) ;
169
161
}
170
162
171
163
handleEdgeFormatButtonClick = ( ) => {
172
- this . setState ( {
173
- focusedPane : this . state . edgeFormatDrawerIsOpen ? null : 'EdgeFormatDrawer' ,
174
- } ) ;
164
+ this . setFocusIf ( 'edgeFormatDrawerIsOpen' , null , 'EdgeFormatDrawer' )
175
165
this . setPersistentState ( {
176
166
edgeFormatDrawerIsOpen : ! this . state . edgeFormatDrawerIsOpen ,
177
167
nodeFormatDrawerIsOpen : false ,
@@ -182,9 +172,7 @@ class Index extends React.Component {
182
172
this . setPersistentState ( {
183
173
edgeFormatDrawerIsOpen : false ,
184
174
} ) ;
185
- this . setState ( {
186
- focusedPane : null ,
187
- } ) ;
175
+ this . setFocus ( null ) ;
188
176
}
189
177
190
178
handleSettingsClick = ( ) => {
@@ -408,44 +396,48 @@ class Index extends React.Component {
408
396
}
409
397
410
398
handleTextEditorFocus = ( ) => {
411
- this . setState ( {
412
- focusedPane : 'TextEditor' ,
413
- } ) ;
399
+ this . setFocus ( 'TextEditor' ) ;
414
400
}
415
401
416
402
handleTextEditorBlur = ( ) => {
417
- if ( this . state . focusedPane === 'TextEditor' ) {
418
- this . setState ( {
419
- focusedPane : null ,
420
- } ) ;
421
- }
403
+ this . setFocusIfFocusIs ( 'TextEditor' , null ) ;
422
404
}
423
405
424
406
handleGraphFocus = ( ) => {
425
- this . setState ( {
426
- focusedPane : 'Graph' ,
427
- } ) ;
407
+ this . setFocus ( 'Graph' ) ;
428
408
}
429
409
430
410
handleInsertPanelsClick = ( ) => {
431
- this . setState ( {
432
- focusedPane : 'InsertPanels' ,
433
- } ) ;
411
+ this . setFocus ( 'InsertPanels' ) ;
434
412
}
435
413
436
414
handleNodeFormatDrawerClick = ( ) => {
437
- this . setState ( ( state ) => {
438
- return {
439
- focusedPane : state . nodeFormatDrawerIsOpen ? 'NodeFormatDrawer' : null ,
440
- }
441
- } ) ;
415
+ this . setFocusIf ( 'nodeFormatDrawerIsOpen' , 'NodeFormatDrawer' , null )
442
416
}
443
417
444
418
handleEdgeFormatDrawerClick = ( ) => {
419
+ this . setFocus ( 'EdgeFormatDrawer' ) ;
420
+ this . setFocusIf ( 'edgeFormatDrawerIsOpen' , 'EdgeFormatDrawer' , null )
421
+ }
422
+
423
+ setFocus = ( focusedPane ) => {
424
+ this . setState ( ( state ) => ( state . focusedPane !== focusedPane && {
425
+ focusedPane : focusedPane ,
426
+ } ) || null ) ;
427
+ }
428
+
429
+ setFocusIfFocusIs = ( currentlyFocusedPane , newFocusedPane ) => {
430
+ this . setState ( ( state ) => ( state . focusedPane === currentlyFocusedPane && {
431
+ focusedPane : newFocusedPane ,
432
+ } ) || null ) ;
433
+ }
434
+
435
+ setFocusIf = ( stateProperty , focusedPaneIf , focusedPaneElse ) => {
445
436
this . setState ( ( state ) => {
446
- return {
447
- focusedPane : state . edgeFormatDrawerIsOpen ? 'EdgeFormatDrawer' : null ,
448
- }
437
+ const focusedPane = state [ stateProperty ] ? focusedPaneIf : focusedPaneElse ;
438
+ return ( state . focusedPane !== focusedPane && {
439
+ focusedPane : focusedPane ,
440
+ } ) || null ;
449
441
} ) ;
450
442
}
451
443
0 commit comments