@@ -42,7 +42,7 @@ function handleIndentSelection(textarea, e) {
4242 triggerEditorContentChanged ( textarea ) ;
4343}
4444
45- function handleNewLine ( textarea , e ) {
45+ function handleNewline ( textarea , e ) {
4646 const selStart = textarea . selectionStart ;
4747 const selEnd = textarea . selectionEnd ;
4848 if ( selEnd !== selStart ) return ; // do not process when there is a selection
@@ -57,11 +57,11 @@ function handleNewLine(textarea, e) {
5757 if ( ! line ) return ; // if the line is empty, do nothing, let the browser handle it
5858
5959 // parse the indention
60- const indention = line . match ( / ^ \s * / ) [ 0 ] ;
60+ const indention = / ^ \s * / . exec ( line ) [ 0 ] ;
6161 line = line . slice ( indention . length ) ;
6262
6363 // parse the prefixes: "1. ", "- ", "* ", "[ ] ", "[x] "
64- const prefixMatch = line . match ( / ^ ( [ 0 - 9 ] + \. | [ - * ] | \[ \] | \[ x \] ) \s / ) ;
64+ const prefixMatch = / ^ ( [ 0 - 9 ] + \. | [ - * ] | \[ \] | \[ x \] ) \s / . exec ( line ) ;
6565 let prefix = '' ;
6666 if ( prefixMatch ) {
6767 prefix = prefixMatch [ 0 ] ;
@@ -89,10 +89,12 @@ function handleNewLine(textarea, e) {
8989
9090export function initTextareaMarkdown ( textarea ) {
9191 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
9394 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 ) ;
9698 }
9799 } ) ;
98100}
0 commit comments