Skip to content

Commit d872610

Browse files
committed
Ensure undoing a line with OL works without selection
1 parent acaa749 commit d872610

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

index.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -484,13 +484,27 @@ function orderedList(textarea: HTMLTextAreaElement): SelectionRange {
484484
let selectionEnd
485485
let selectionStart
486486
let text = textarea.value.slice(textarea.selectionStart, textarea.selectionEnd)
487+
let textToUnstyle = text
487488
let lines = text.split('\n')
488-
489-
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))
490498

491499
if (undoStyling) {
492-
lines = lines.map(line => line.replace(orderedListRegex, ''))
500+
lines = linesToUnstyle.map(line => line.replace(orderedListRegex, ''))
493501
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+
}
494508
} else {
495509
lines = (function() {
496510
let i

0 commit comments

Comments
 (0)