@@ -2,28 +2,30 @@ import React from 'react';
2
2
import { stopPropagation } from './utils' ;
3
3
import { SelectionText } from './SelectionText' ;
4
4
5
- export default function shortcuts ( e : React . KeyboardEvent < HTMLTextAreaElement > ) {
5
+ export default function shortcuts ( e : React . KeyboardEvent < HTMLTextAreaElement > , indentWidth : number = 2 ) {
6
6
const api = new SelectionText ( e . target as HTMLTextAreaElement ) ;
7
7
/**
8
8
* Support of shortcuts for React v16
9
9
* https://github.com/uiwjs/react-textarea-code-editor/issues/128
10
10
* https://blog.saeloun.com/2021/04/23/react-keyboard-event-code.html
11
11
*/
12
12
const code = ( e . code || e . nativeEvent . code ) . toLocaleLowerCase ( ) ;
13
+ const indent = ' ' . repeat ( indentWidth ) ;
14
+
13
15
if ( code === 'tab' ) {
14
16
stopPropagation ( e ) ;
15
17
if ( api . start === api . end ) {
16
18
if ( e . shiftKey ) {
17
- api . lineStarRemove ( ' ' ) ;
19
+ api . lineStarRemove ( indent ) ;
18
20
} else {
19
- api . insertText ( ' ' ) . position ( api . start + 2 , api . end + 2 ) ;
21
+ api . insertText ( indent ) . position ( api . start + indentWidth , api . end + indentWidth ) ;
20
22
}
21
23
} else if ( api . getSelectedValue ( ) . indexOf ( '\n' ) > - 1 && e . shiftKey ) {
22
- api . lineStarRemove ( ' ' ) ;
24
+ api . lineStarRemove ( indent ) ;
23
25
} else if ( api . getSelectedValue ( ) . indexOf ( '\n' ) > - 1 ) {
24
- api . lineStarInstert ( ' ' ) ;
26
+ api . lineStarInstert ( indent ) ;
25
27
} else {
26
- api . insertText ( ' ' ) . position ( api . start + 2 , api . end ) ;
28
+ api . insertText ( indent ) . position ( api . start + indentWidth , api . end ) ;
27
29
}
28
30
api . notifyChange ( ) ;
29
31
} else if ( code === 'enter' ) {
0 commit comments