Skip to content

Commit 8ab63c1

Browse files
authored
Merge pull request #8 from github/list
Fix expandSelectedText breaking sentences for block changes
2 parents e250d96 + 094c2c7 commit 8ab63c1

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

index.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,10 @@ function wordSelectionStart(text: string, i: number): number {
256256
return index
257257
}
258258

259-
function wordSelectionEnd(text: string, i: number): number {
259+
function wordSelectionEnd(text: string, i: number, multiline: boolean): number {
260260
let index = i
261-
while (text[index] && !text[index].match(/\s/)) {
261+
const breakpoint = multiline ? /\n/ : /\s/
262+
while (text[index] && !text[index].match(breakpoint)) {
262263
index++
263264
}
264265
return index
@@ -322,10 +323,15 @@ function styleSelectedText(textarea: HTMLTextAreaElement, styleArgs: StyleArgs)
322323
insertText(textarea, result)
323324
}
324325

325-
function expandSelectedText(textarea: HTMLTextAreaElement, prefixToUse: string, suffixToUse: string): string {
326+
function expandSelectedText(
327+
textarea: HTMLTextAreaElement,
328+
prefixToUse: string,
329+
suffixToUse: string,
330+
multiline: boolean = false
331+
): string {
326332
if (textarea.selectionStart === textarea.selectionEnd) {
327333
textarea.selectionStart = wordSelectionStart(textarea.value, textarea.selectionStart)
328-
textarea.selectionEnd = wordSelectionEnd(textarea.value, textarea.selectionEnd)
334+
textarea.selectionEnd = wordSelectionEnd(textarea.value, textarea.selectionEnd, multiline)
329335
} else {
330336
const expandedSelectionStart = textarea.selectionStart - prefixToUse.length
331337
const expandedSelectionEnd = textarea.selectionEnd + suffixToUse.length
@@ -399,7 +405,7 @@ function blockStyle(textarea: HTMLTextAreaElement, arg: StyleArgs): SelectionRan
399405
prefixToUse = ` ${prefixToUse}`
400406
}
401407
}
402-
selectedText = expandSelectedText(textarea, prefixToUse, suffixToUse)
408+
selectedText = expandSelectedText(textarea, prefixToUse, suffixToUse, arg.multiline)
403409
let selectionStart = textarea.selectionStart
404410
let selectionEnd = textarea.selectionEnd
405411
const hasReplaceNext = replaceNext.length > 0 && suffixToUse.indexOf(replaceNext) > -1 && selectedText.length > 0

test/test.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,12 @@ describe('markdown-toolbbar-element', function() {
300300
assert.equal('> |Butts|\n\nThe quick brown fox jumps over the lazy dog', visualValue())
301301
})
302302

303+
it('quotes full line of text when you click the quote icon', function() {
304+
setVisualValue('|The quick brown fox jumps over the lazy dog')
305+
clickToolbar('md-quote')
306+
assert.equal('> |The quick brown fox jumps over the lazy dog', visualValue())
307+
})
308+
303309
it('prefixes newlines when quoting an existing line on an existing', function() {
304310
setVisualValue('The quick brown fox jumps over the lazy dog|Butts|')
305311
clickToolbar('md-quote')
@@ -351,12 +357,18 @@ describe('markdown-toolbbar-element', function() {
351357
})
352358

353359
describe('lists', function() {
354-
it('turns line into list when you click the unordered list icon', function() {
360+
it('turns line into list when you click the unordered list icon with selection', function() {
355361
setVisualValue('One\n|Two|\nThree\n')
356362
clickToolbar('md-unordered-list')
357363
assert.equal('One\n\n- |Two|\n\nThree\n', visualValue())
358364
})
359365

366+
it('turns line into list when you click the unordered list icon without selection', function() {
367+
setVisualValue('One\n|Two and two\nThree\n')
368+
clickToolbar('md-unordered-list')
369+
assert.equal('One\n\n- |Two and two\n\nThree\n', visualValue())
370+
})
371+
360372
it('turns multiple lines into list when you click the unordered list icon', function() {
361373
setVisualValue('One\n|Two\nThree|\n')
362374
clickToolbar('md-unordered-list')
@@ -383,6 +395,12 @@ describe('markdown-toolbbar-element', function() {
383395
assert.equal('|Two|', visualValue())
384396
})
385397

398+
it('creates ordered list without selection', function() {
399+
setVisualValue('apple\n|pear\nbanana\n')
400+
clickToolbar('md-ordered-list')
401+
assert.equal('apple\n\n|1. |\n\npear\nbanana\n', visualValue())
402+
})
403+
386404
it('creates ordered list by selecting one line', function() {
387405
setVisualValue('apple\n|pear|\nbanana\n')
388406
clickToolbar('md-ordered-list')

0 commit comments

Comments
 (0)