@@ -2,28 +2,30 @@ import React from 'react';
22import { stopPropagation } from './utils' ;
33import { SelectionText } from './SelectionText' ;
44
5- export default function shortcuts ( e : React . KeyboardEvent < HTMLTextAreaElement > ) {
5+ export default function shortcuts ( e : React . KeyboardEvent < HTMLTextAreaElement > , indentWidth : number = 2 ) {
66 const api = new SelectionText ( e . target as HTMLTextAreaElement ) ;
77 /**
88 * Support of shortcuts for React v16
99 * https://github.com/uiwjs/react-textarea-code-editor/issues/128
1010 * https://blog.saeloun.com/2021/04/23/react-keyboard-event-code.html
1111 */
1212 const code = ( e . code || e . nativeEvent . code ) . toLocaleLowerCase ( ) ;
13+ const indent = ' ' . repeat ( indentWidth ) ;
14+
1315 if ( code === 'tab' ) {
1416 stopPropagation ( e ) ;
1517 if ( api . start === api . end ) {
1618 if ( e . shiftKey ) {
17- api . lineStarRemove ( ' ' ) ;
19+ api . lineStarRemove ( indent ) ;
1820 } else {
19- api . insertText ( ' ' ) . position ( api . start + 2 , api . end + 2 ) ;
21+ api . insertText ( indent ) . position ( api . start + indentWidth , api . end + indentWidth ) ;
2022 }
2123 } else if ( api . getSelectedValue ( ) . indexOf ( '\n' ) > - 1 && e . shiftKey ) {
22- api . lineStarRemove ( ' ' ) ;
24+ api . lineStarRemove ( indent ) ;
2325 } else if ( api . getSelectedValue ( ) . indexOf ( '\n' ) > - 1 ) {
24- api . lineStarInstert ( ' ' ) ;
26+ api . lineStarInstert ( indent ) ;
2527 } else {
26- api . insertText ( ' ' ) . position ( api . start + 2 , api . end ) ;
28+ api . insertText ( indent ) . position ( api . start + indentWidth , api . end ) ;
2729 }
2830 api . notifyChange ( ) ;
2931 } else if ( code === 'enter' ) {
0 commit comments