@@ -438,11 +438,11 @@ function expandSelectionToLine(textarea: HTMLTextAreaElement) {
438438 let counter = 0
439439 for ( let index = 0 ; index < lines . length ; index ++ ) {
440440 const lineLength = lines [ index ] . length + 1
441- if ( textarea . selectionStart >= counter && textarea . selectionStart <= counter + lineLength ) {
441+ if ( textarea . selectionStart >= counter && textarea . selectionStart < counter + lineLength ) {
442442 textarea . selectionStart = counter
443443 }
444- if ( textarea . selectionEnd > counter && textarea . selectionEnd <= counter + lineLength ) {
445- textarea . selectionEnd = counter + lineLength
444+ if ( textarea . selectionEnd >= counter && textarea . selectionEnd < counter + lineLength ) {
445+ textarea . selectionEnd = counter + lineLength - 1
446446 }
447447 counter += lineLength
448448 }
@@ -642,49 +642,31 @@ function listStyle(textarea: HTMLTextAreaElement, style: StyleArgs): SelectionRa
642642 const noInitialSelection = textarea . selectionStart === textarea . selectionEnd
643643 let selectionStart = textarea . selectionStart
644644 let selectionEnd = textarea . selectionEnd
645- let lines : string [ ] = [ ]
646- let text = textarea . value . slice ( textarea . selectionStart , textarea . selectionEnd )
647- let startOfLine , endOfLine
648645
649646 undoOrderedListStyle ( textarea )
650647 undoUnorderedListStyle ( textarea )
651648
652649 const prefix = '- '
653650
654- // Expand selection to full lines
655-
651+ // Select whole line
656652 expandSelectionToLine ( textarea )
657653
658- // Style only the selected line
659- if ( noInitialSelection ) {
660- const linesBefore = textarea . value . slice ( 0 , textarea . selectionStart ) . split ( / \n / )
661- lines = textarea . value . split ( '\n' )
662-
663- const currentLine = linesBefore . length - 1
664- const currentLineText = lines [ currentLine ]
665-
666- // Select whole line
667- const range = selectionIndexForLine ( lines , currentLine )
668- // if (range) {
669- // textarea.selectionStart = range.selectionStart ?? 0
670- // textarea.selectionEnd = range.selectionEnd ?? 0
671- // }
654+ const selectedText = textarea . value . slice ( textarea . selectionStart , textarea . selectionEnd )
655+ const lines = selectedText . split ( '\n' ) . map ( ( value , index ) => {
656+ return `${ prefix } ${ value } `
657+ } )
672658
673- // line with caret
674- //lines[linesBefore.length - 1] = prefix + linesBefore[linesBefore.length - 1]
659+ const { newlinesToAppend, newlinesToPrepend} = newlinesToSurroundSelectedText ( textarea )
675660
676- text = prefix + currentLineText
677-
678- // if (style.surroundWithNewlines) {
679- const { newlinesToAppend, newlinesToPrepend} = newlinesToSurroundSelectedText ( textarea )
680- selectionStart = selectionStart + prefix . length + 1
681- selectionEnd = selectionEnd + prefix . length + 1
682- text = newlinesToAppend + text + newlinesToPrepend
683-
684- return { text, selectionStart, selectionEnd}
661+ if ( noInitialSelection ) {
662+ selectionStart = Math . max ( selectionStart + prefix . length + newlinesToAppend . length , 0 )
663+ selectionEnd = selectionStart
664+ } else {
665+ selectionStart = Math . max ( selectionStart + prefix . length + newlinesToAppend . length , 0 )
666+ selectionEnd = selectionEnd + newlinesToAppend . length + prefix . length * lines . length
685667 }
686668
687- text = lines . join ( '\n' )
669+ const text = newlinesToAppend + lines . join ( '\n' ) + newlinesToPrepend
688670
689671 return { text, selectionStart, selectionEnd}
690672}
0 commit comments