@@ -42,7 +42,7 @@ function handleIndentSelection(textarea, e) {
42
42
triggerEditorContentChanged ( textarea ) ;
43
43
}
44
44
45
- function handleNewLine ( textarea , e ) {
45
+ function handleNewline ( textarea , e ) {
46
46
const selStart = textarea . selectionStart ;
47
47
const selEnd = textarea . selectionEnd ;
48
48
if ( selEnd !== selStart ) return ; // do not process when there is a selection
@@ -57,11 +57,11 @@ function handleNewLine(textarea, e) {
57
57
if ( ! line ) return ; // if the line is empty, do nothing, let the browser handle it
58
58
59
59
// parse the indention
60
- const indention = line . match ( / ^ \s * / ) [ 0 ] ;
60
+ const indention = / ^ \s * / . exec ( line ) [ 0 ] ;
61
61
line = line . slice ( indention . length ) ;
62
62
63
63
// parse the prefixes: "1. ", "- ", "* ", "[ ] ", "[x] "
64
- const prefixMatch = line . match ( / ^ ( [ 0 - 9 ] + \. | [ - * ] | \[ \] | \[ x \] ) \s / ) ;
64
+ const prefixMatch = / ^ ( [ 0 - 9 ] + \. | [ - * ] | \[ \] | \[ x \] ) \s / . exec ( line ) ;
65
65
let prefix = '' ;
66
66
if ( prefixMatch ) {
67
67
prefix = prefixMatch [ 0 ] ;
@@ -89,10 +89,12 @@ function handleNewLine(textarea, e) {
89
89
90
90
export function initTextareaMarkdown ( textarea ) {
91
91
textarea . addEventListener ( 'keydown' , ( e ) => {
92
- if ( e . key === 'Tab' && ! e . ctrlKey && ! e . altKey ) {
92
+ if ( e . key === 'Tab' && ! e . ctrlKey && ! e . metaKey && ! e . altKey ) {
93
+ // use Tab/Shift-Tab to indent/unindent the selected lines
93
94
handleIndentSelection ( textarea , e ) ;
94
- } else if ( e . key === 'Enter' && ! e . shiftKey && ! e . ctrlKey && ! e . altKey ) {
95
- handleNewLine ( textarea , e ) ;
95
+ } else if ( e . key === 'Enter' && ! e . shiftKey && ! e . ctrlKey && ! e . metaKey && ! e . altKey ) {
96
+ // use Enter to insert a new line with the same indention and prefix
97
+ handleNewline ( textarea , e ) ;
96
98
}
97
99
} ) ;
98
100
}
0 commit comments