@@ -480,16 +480,31 @@ function multilineStyle(textarea: HTMLTextAreaElement, arg: StyleArgs) {
480480
481481function orderedList ( textarea : HTMLTextAreaElement ) : SelectionRange {
482482 const orderedListRegex = / ^ \d + \. \s + /
483+ const noInitialSelection = textarea . selectionStart === textarea . selectionEnd
483484 let selectionEnd
484485 let selectionStart
485486 let text = textarea . value . slice ( textarea . selectionStart , textarea . selectionEnd )
487+ let textToUnstyle = text
486488 let lines = text . split ( '\n' )
487-
488- const undoStyling = lines . every ( line => orderedListRegex . test ( line ) )
489+ let startOfLine , endOfLine
490+ if ( noInitialSelection ) {
491+ const linesBefore = textarea . value . slice ( 0 , textarea . selectionStart ) . split ( / \n / )
492+ startOfLine = textarea . selectionStart - linesBefore [ linesBefore . length - 1 ] . length
493+ endOfLine = wordSelectionEnd ( textarea . value , textarea . selectionStart , true )
494+ textToUnstyle = textarea . value . slice ( startOfLine , endOfLine )
495+ }
496+ const linesToUnstyle = textToUnstyle . split ( '\n' )
497+ const undoStyling = linesToUnstyle . every ( line => orderedListRegex . test ( line ) )
489498
490499 if ( undoStyling ) {
491- lines = lines . map ( line => line . replace ( orderedListRegex , '' ) )
500+ lines = linesToUnstyle . map ( line => line . replace ( orderedListRegex , '' ) )
492501 text = lines . join ( '\n' )
502+ if ( noInitialSelection && startOfLine && endOfLine ) {
503+ const lengthDiff = linesToUnstyle [ 0 ] . length - lines [ 0 ] . length
504+ selectionStart = selectionEnd = textarea . selectionStart - lengthDiff
505+ textarea . selectionStart = startOfLine
506+ textarea . selectionEnd = endOfLine
507+ }
493508 } else {
494509 lines = ( function ( ) {
495510 let i
@@ -506,6 +521,7 @@ function orderedList(textarea: HTMLTextAreaElement): SelectionRange {
506521 const { newlinesToAppend, newlinesToPrepend} = newlinesToSurroundSelectedText ( textarea )
507522 selectionStart = textarea . selectionStart + newlinesToAppend . length
508523 selectionEnd = selectionStart + text . length
524+ if ( noInitialSelection ) selectionStart = selectionEnd
509525 text = newlinesToAppend + text + newlinesToPrepend
510526 }
511527
0 commit comments