@@ -480,16 +480,31 @@ function multilineStyle(textarea: HTMLTextAreaElement, arg: StyleArgs) {
480
480
481
481
function orderedList ( textarea : HTMLTextAreaElement ) : SelectionRange {
482
482
const orderedListRegex = / ^ \d + \. \s + /
483
+ const noInitialSelection = textarea . selectionStart === textarea . selectionEnd
483
484
let selectionEnd
484
485
let selectionStart
485
486
let text = textarea . value . slice ( textarea . selectionStart , textarea . selectionEnd )
487
+ let textToUnstyle = text
486
488
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 ) )
489
498
490
499
if ( undoStyling ) {
491
- lines = lines . map ( line => line . replace ( orderedListRegex , '' ) )
500
+ lines = linesToUnstyle . map ( line => line . replace ( orderedListRegex , '' ) )
492
501
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
+ }
493
508
} else {
494
509
lines = ( function ( ) {
495
510
let i
@@ -506,6 +521,7 @@ function orderedList(textarea: HTMLTextAreaElement): SelectionRange {
506
521
const { newlinesToAppend, newlinesToPrepend} = newlinesToSurroundSelectedText ( textarea )
507
522
selectionStart = textarea . selectionStart + newlinesToAppend . length
508
523
selectionEnd = selectionStart + text . length
524
+ if ( noInitialSelection ) selectionStart = selectionEnd
509
525
text = newlinesToAppend + text + newlinesToPrepend
510
526
}
511
527
0 commit comments