Skip to content

Commit c011903

Browse files
authored
Merge pull request #9 from github/ol-selection
OrderedList fixes
2 parents 3b9c9de + d872610 commit c011903

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

index.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -480,16 +480,31 @@ function multilineStyle(textarea: HTMLTextAreaElement, arg: StyleArgs) {
480480

481481
function 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

test/test.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,19 @@ describe('markdown-toolbbar-element', function() {
398398
it('creates ordered list without selection', function() {
399399
setVisualValue('apple\n|pear\nbanana\n')
400400
clickToolbar('md-ordered-list')
401-
assert.equal('apple\n\n|1. |\n\npear\nbanana\n', visualValue())
401+
assert.equal('apple\n\n1. |\n\npear\nbanana\n', visualValue())
402+
})
403+
404+
it('undo an ordered list without selection', function() {
405+
setVisualValue('apple\n1. |pear\nbanana\n')
406+
clickToolbar('md-ordered-list')
407+
assert.equal('apple\n|pear\nbanana\n', visualValue())
408+
})
409+
410+
it('undo an ordered list without selection and puts cursor at the right position', function() {
411+
setVisualValue('apple\n1. pea|r\nbanana\n')
412+
clickToolbar('md-ordered-list')
413+
assert.equal('apple\npea|r\nbanana\n', visualValue())
402414
})
403415

404416
it('creates ordered list by selecting one line', function() {

0 commit comments

Comments
 (0)