@@ -72,6 +72,7 @@ import UnsavedChangesIndicator from '../UnsavedChangesIndicator';
72
72
import { EditorContainer , EditorHolder } from './MobileEditor' ;
73
73
import { FolderIcon } from '../../../../common/icons' ;
74
74
import IconButton from '../../../../common/IconButton' ;
75
+ import { EditorKeyMapsContext } from './contexts' ;
75
76
76
77
emmet ( CodeMirror ) ;
77
78
@@ -104,6 +105,7 @@ class Editor extends React.Component {
104
105
this . showFind = this . showFind . bind ( this ) ;
105
106
this . showReplace = this . showReplace . bind ( this ) ;
106
107
this . getContent = this . getContent . bind ( this ) ;
108
+ this . updateKeyMaps = this . updateKeyMaps . bind ( this ) ;
107
109
}
108
110
109
111
componentDidMount ( ) {
@@ -153,36 +155,9 @@ class Editor extends React.Component {
153
155
154
156
delete this . _cm . options . lint . options . errors ;
155
157
156
- const replaceCommand =
157
- metaKey === 'Ctrl' ? `${ metaKey } -H` : `${ metaKey } -Option-F` ;
158
- this . _cm . setOption ( 'extraKeys' , {
159
- Tab : ( cm ) => {
160
- if ( ! cm . execCommand ( 'emmetExpandAbbreviation' ) ) return ;
161
- // might need to specify and indent more?
162
- const selection = cm . doc . getSelection ( ) ;
163
- if ( selection . length > 0 ) {
164
- cm . execCommand ( 'indentMore' ) ;
165
- } else {
166
- cm . replaceSelection ( ' ' . repeat ( INDENTATION_AMOUNT ) ) ;
167
- }
168
- } ,
169
- Enter : 'emmetInsertLineBreak' ,
170
- Esc : 'emmetResetAbbreviation' ,
171
- [ `Shift-Tab` ] : false ,
172
- [ `${ metaKey } -Enter` ] : ( ) => null ,
173
- [ `Shift-${ metaKey } -Enter` ] : ( ) => null ,
174
- [ `${ metaKey } -F` ] : 'findPersistent' ,
175
- [ `Shift-${ metaKey } -F` ] : this . tidyCode ,
176
- [ `${ metaKey } -G` ] : 'findPersistentNext' ,
177
- [ `Shift-${ metaKey } -G` ] : 'findPersistentPrev' ,
178
- [ replaceCommand ] : 'replace' ,
179
- // Cassie Tarakajian: If you don't set a default color, then when you
180
- // choose a color, it deletes characters inline. This is a
181
- // hack to prevent that.
182
- [ `${ metaKey } -K` ] : ( cm , event ) =>
183
- cm . state . colorpicker . popup_color_picker ( { length : 0 } ) ,
184
- [ `${ metaKey } -.` ] : 'toggleComment' // Note: most adblockers use the shortcut ctrl+.
185
- } ) ;
158
+ const { keyMaps } = this . context ;
159
+
160
+ this . updateKeyMaps ( keyMaps ) ;
186
161
187
162
this . initializeDocuments ( this . props . files ) ;
188
163
this . _cm . swapDoc ( this . _docs [ this . props . file . id ] ) ;
@@ -236,6 +211,16 @@ class Editor extends React.Component {
236
211
}
237
212
238
213
componentDidUpdate ( prevProps ) {
214
+ const currentKeyMaps = this . context ?. keyMaps ;
215
+ const prevKeyMaps = this . prevKeyMapsRef ?. current ;
216
+
217
+ if (
218
+ prevKeyMaps &&
219
+ JSON . stringify ( currentKeyMaps ) !== JSON . stringify ( prevKeyMaps )
220
+ ) {
221
+ this . updateKeyMaps ( currentKeyMaps ) ;
222
+ }
223
+ this . prevKeyMapsRef = { current : currentKeyMaps } ;
239
224
if ( this . props . file . id !== prevProps . file . id ) {
240
225
const fileMode = this . getFileMode ( this . props . file . name ) ;
241
226
if ( fileMode === 'javascript' ) {
@@ -515,6 +500,42 @@ class Editor extends React.Component {
515
500
} ) ;
516
501
}
517
502
503
+ updateKeyMaps ( keyMaps ) {
504
+ const replaceCommand =
505
+ metaKey === 'Ctrl' ? `${ metaKey } -H` : `${ metaKey } -Option-F` ;
506
+
507
+ this . _cm . setOption ( 'extraKeys' , {
508
+ Tab : ( cm ) => {
509
+ if ( ! cm . execCommand ( 'emmetExpandAbbreviation' ) ) return ;
510
+ // might need to specify and indent more?
511
+ const selection = cm . doc . getSelection ( ) ;
512
+ if ( selection . length > 0 ) {
513
+ cm . execCommand ( 'indentMore' ) ;
514
+ } else {
515
+ cm . replaceSelection ( ' ' . repeat ( INDENTATION_AMOUNT ) ) ;
516
+ }
517
+ } ,
518
+ Enter : 'emmetInsertLineBreak' ,
519
+ Esc : 'emmetResetAbbreviation' ,
520
+ [ `Shift-Tab` ] : false ,
521
+ [ `${ metaKey } -Enter` ] : ( ) => null ,
522
+ [ `Shift-${ metaKey } -Enter` ] : ( ) => null ,
523
+ [ `${ metaKey } -F` ] : 'findPersistent' ,
524
+ [ `${ keyMaps . tidy } ` ] : this . tidyCode ,
525
+ [ `${ metaKey } -G` ] : 'findPersistentNext' ,
526
+ [ `Shift-${ metaKey } -G` ] : 'findPersistentPrev' ,
527
+ [ replaceCommand ] : 'replace' ,
528
+ // Cassie Tarakajian: If you don't set a default color, then when you
529
+ // choose a color, it deletes characters inline. This is a
530
+ // hack to prevent that.
531
+ [ `${ metaKey } -K` ] : ( cm , event ) =>
532
+ cm . state . colorpicker . popup_color_picker ( { length : 0 } ) ,
533
+ [ `${ metaKey } -.` ] : 'toggleComment' // Note: most adblockers use the shortcut ctrl+.
534
+ } ) ;
535
+ }
536
+
537
+ static contextType = EditorKeyMapsContext ;
538
+
518
539
render ( ) {
519
540
const editorSectionClass = classNames ( {
520
541
editor : true ,
0 commit comments