Skip to content

Commit feacc96

Browse files
committed
Add expand line selection logic
1 parent ec5f52a commit feacc96

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

src/index.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,22 @@ function styleSelectedText(textarea: HTMLTextAreaElement, styleArgs: StyleArgs)
433433
insertText(textarea, result)
434434
}
435435

436-
export function selectionIndexForLine(lines: string[], line: number): SelectionRange | null {
436+
function expandSelectionToLine(textarea: HTMLTextAreaElement) {
437+
const lines = textarea.value.split('\n')
438+
let counter = 0
439+
for (let index = 0; index < lines.length; index++) {
440+
const lineLength = lines[index].length + 1
441+
if (textarea.selectionStart >= counter && textarea.selectionStart <= counter + lineLength) {
442+
textarea.selectionStart = counter
443+
}
444+
if (textarea.selectionEnd > counter && textarea.selectionEnd <= counter + lineLength) {
445+
textarea.selectionEnd = counter + lineLength
446+
}
447+
counter += lineLength
448+
}
449+
}
450+
451+
function selectionIndexForLine(lines: string[], line: number): SelectionRange | null {
437452
let counter = 0
438453
for (let index = 0; index < lines.length; index++) {
439454
if (index === line) {
@@ -636,7 +651,9 @@ function listStyle(textarea: HTMLTextAreaElement, style: StyleArgs): SelectionRa
636651

637652
const prefix = '- '
638653

639-
//let selectedText = expandSelectedText(textarea, prefix, '', style.multiline)
654+
// Expand selection to full lines
655+
656+
expandSelectionToLine(textarea)
640657

641658
// Style only the selected line
642659
if (noInitialSelection) {
@@ -648,10 +665,10 @@ function listStyle(textarea: HTMLTextAreaElement, style: StyleArgs): SelectionRa
648665

649666
// Select whole line
650667
const range = selectionIndexForLine(lines, currentLine)
651-
if (range) {
652-
textarea.selectionStart = range.selectionStart ?? 0
653-
textarea.selectionEnd = range.selectionEnd ?? 0
654-
}
668+
// if (range) {
669+
// textarea.selectionStart = range.selectionStart ?? 0
670+
// textarea.selectionEnd = range.selectionEnd ?? 0
671+
// }
655672

656673
// line with caret
657674
//lines[linesBefore.length - 1] = prefix + linesBefore[linesBefore.length - 1]
@@ -661,7 +678,7 @@ function listStyle(textarea: HTMLTextAreaElement, style: StyleArgs): SelectionRa
661678
// if (style.surroundWithNewlines) {
662679
const {newlinesToAppend, newlinesToPrepend} = newlinesToSurroundSelectedText(textarea)
663680
selectionStart = selectionStart + prefix.length + 1
664-
selectionEnd = selectionStart
681+
selectionEnd = selectionEnd + prefix.length + 1
665682
text = newlinesToAppend + text + newlinesToPrepend
666683

667684
return {text, selectionStart, selectionEnd}

0 commit comments

Comments
 (0)