File tree Expand file tree Collapse file tree 1 file changed +10
-8
lines changed
Expand file tree Collapse file tree 1 file changed +10
-8
lines changed Original file line number Diff line number Diff line change @@ -4,7 +4,13 @@ import { SelectionText } from './SelectionText';
44
55export default function shortcuts ( e : React . KeyboardEvent < HTMLTextAreaElement > ) {
66 const api = new SelectionText ( e . target as HTMLTextAreaElement ) ;
7- if ( e . code && e . code . toLowerCase ( ) === 'tab' ) {
7+ /**
8+ * Support of shortcuts for React v16
9+ * https://github.com/uiwjs/react-textarea-code-editor/issues/128
10+ * https://blog.saeloun.com/2021/04/23/react-keyboard-event-code.html
11+ */
12+ const code = ( e . code || e . nativeEvent . code ) . toLocaleLowerCase ( ) ;
13+ if ( code === 'tab' ) {
814 stopPropagation ( e ) ;
915 if ( api . start === api . end ) {
1016 api . insertText ( ' ' ) . position ( api . start + 2 , api . end + 2 ) ;
@@ -16,20 +22,16 @@ export default function shortcuts(e: React.KeyboardEvent<HTMLTextAreaElement>) {
1622 api . insertText ( ' ' ) . position ( api . start + 2 , api . end ) ;
1723 }
1824 api . notifyChange ( ) ;
19- } else if ( e . code && e . code . toLowerCase ( ) === 'enter' ) {
25+ } else if ( code === 'enter' ) {
2026 stopPropagation ( e ) ;
2127 const indent = `\n${ api . getIndentText ( ) } ` ;
2228 api . insertText ( indent ) . position ( api . start + indent . length , api . start + indent . length ) ;
2329 api . notifyChange ( ) ;
24- } else if (
25- e . code &&
26- / ^ ( q u o t e | b a c k q u o t e | b r a c k e t l e f t | d i g i t 9 | c o m m a ) $ / . test ( e . code . toLowerCase ( ) ) &&
27- api . getSelectedValue ( )
28- ) {
30+ } else if ( code && / ^ ( q u o t e | b a c k q u o t e | b r a c k e t l e f t | d i g i t 9 | c o m m a ) $ / . test ( code ) && api . getSelectedValue ( ) ) {
2931 stopPropagation ( e ) ;
3032 const val = api . getSelectedValue ( ) ;
3133 let txt = '' ;
32- switch ( e . code . toLowerCase ( ) ) {
34+ switch ( code ) {
3335 case 'quote' :
3436 txt = `'${ val } '` ;
3537 if ( e . shiftKey ) {
You can’t perform that action at this time.
0 commit comments