@@ -72,6 +72,7 @@ import UnsavedChangesIndicator from '../UnsavedChangesIndicator';
7272import { EditorContainer , EditorHolder } from './MobileEditor' ;
7373import { FolderIcon } from '../../../../common/icons' ;
7474import IconButton from '../../../../common/IconButton' ;
75+ import { EditorKeyMapsContext } from './contexts' ;
7576
7677emmet ( CodeMirror ) ;
7778
@@ -104,6 +105,7 @@ class Editor extends React.Component {
104105 this . showFind = this . showFind . bind ( this ) ;
105106 this . showReplace = this . showReplace . bind ( this ) ;
106107 this . getContent = this . getContent . bind ( this ) ;
108+ this . updateKeyMaps = this . updateKeyMaps . bind ( this ) ;
107109 }
108110
109111 componentDidMount ( ) {
@@ -153,36 +155,9 @@ class Editor extends React.Component {
153155
154156 delete this . _cm . options . lint . options . errors ;
155157
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 ) ;
186161
187162 this . initializeDocuments ( this . props . files ) ;
188163 this . _cm . swapDoc ( this . _docs [ this . props . file . id ] ) ;
@@ -236,6 +211,16 @@ class Editor extends React.Component {
236211 }
237212
238213 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 } ;
239224 if ( this . props . file . id !== prevProps . file . id ) {
240225 const fileMode = this . getFileMode ( this . props . file . name ) ;
241226 if ( fileMode === 'javascript' ) {
@@ -515,6 +500,42 @@ class Editor extends React.Component {
515500 } ) ;
516501 }
517502
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+
518539 render ( ) {
519540 const editorSectionClass = classNames ( {
520541 editor : true ,
0 commit comments