From c37a8c9683ba8527349057566894b71bc9925a13 Mon Sep 17 00:00:00 2001 From: shreeraj Date: Sun, 22 Nov 2026 23:23:08 +0530 Subject: [PATCH] Quote and task list the whole line when nothing is selected With an empty selection the quote and task list buttons are meant to act on the whole line. wordSelectionEnd already stops at a newline instead of whitespace when `multiline` is set, but wordSelectionStart was never given the same treatment, so it still stopped at the previous space. Clicking Quote with the cursor in the middle of a line therefore split the line: The quick brown| fox -> "The quick \n\n> brown| fox" and with the cursor at the end of a line only the last word was quoted. At the very end of the text the start was not expanded at all, so the button added an empty quote below the line instead of quoting it. Pass `multiline` to wordSelectionStart so the selection starts at the beginning of the line, including when the cursor is at the end of the text. Styles that are not multiline behave as before. --- src/index.ts | 7 +++-- test/test.js | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 88b5883..2dc8ea8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -441,9 +441,10 @@ function repeat(string: string, n: number): string { return Array(n + 1).join(string) } -function wordSelectionStart(text: string, i: number): number { +function wordSelectionStart(text: string, i: number, multiline: boolean): number { let index = i - while (text[index] && text[index - 1] != null && !text[index - 1].match(/\s/)) { + const breakpoint = multiline ? /\n/ : /\s/ + while ((multiline || text[index]) && text[index - 1] != null && !text[index - 1].match(breakpoint)) { index-- } return index @@ -538,7 +539,7 @@ function expandSelectedText( multiline = false ): string { if (textarea.selectionStart === textarea.selectionEnd) { - textarea.selectionStart = wordSelectionStart(textarea.value, textarea.selectionStart) + textarea.selectionStart = wordSelectionStart(textarea.value, textarea.selectionStart, multiline) textarea.selectionEnd = wordSelectionEnd(textarea.value, textarea.selectionEnd, multiline) } else { const expandedSelectionStart = textarea.selectionStart - prefixToUse.length diff --git a/test/test.js b/test/test.js index 84ba29e..0ff5049 100644 --- a/test/test.js +++ b/test/test.js @@ -532,6 +532,83 @@ describe('markdown-toolbar-element', function () { clickToolbar('md-quote') assert.equal('|Hey,\n\nThis looks great.\n\nThanks,\nJosh|\n\nEmailed me that last week.', visualValue()) }) + + it('quotes full line of text when the cursor is in the middle of the line', function () { + setVisualValue('The quick brown| fox jumps over the lazy dog') + clickToolbar('md-quote') + assert.equal('> The quick brown| fox jumps over the lazy dog', visualValue()) + }) + + it('quotes full line of text when the cursor is at the end of the line', function () { + setVisualValue('The quick brown fox|\nThe lazy dog') + clickToolbar('md-quote') + assert.equal('> The quick brown fox|\n\nThe lazy dog', visualValue()) + }) + + it('quotes the last line of text when the cursor is at the end of the text', function () { + setVisualValue('First line\nThe quick brown fox|') + clickToolbar('md-quote') + assert.equal('First line\n\n> The quick brown fox|', visualValue()) + }) + + it('inserts an empty quote when the cursor is on an empty line', function () { + setVisualValue('First line\n\n|\nThird line') + clickToolbar('md-quote') + assert.equal('First line\n\n> |\n\nThird line', visualValue()) + }) + + it('quotes only the line with the cursor in the middle of other lines', function () { + setVisualValue('First line\nSecond li|ne\nThird line') + clickToolbar('md-quote') + assert.equal('First line\n\n> Second li|ne\n\nThird line', visualValue()) + }) + + it('quotes the full line wherever the cursor is on the line', function () { + const textarea = document.querySelector('textarea') + const lines = ['The quick brown fox', 'jumps over the lazy dog'] + for (let line = 0; line < lines.length; line++) { + const lineStart = line === 0 ? 0 : lines[0].length + 1 + for (let offset = 0; offset <= lines[line].length; offset++) { + textarea.value = lines.join('\n') + textarea.setSelectionRange(lineStart + offset, lineStart + offset) + clickToolbar('md-quote') + const expected = line === 0 ? `> ${lines[0]}\n\n${lines[1]}` : `${lines[0]}\n\n> ${lines[1]}` + assert.equal(textarea.value, expected, `line ${line}, cursor offset ${offset}`) + assert.equal( + textarea.selectionStart, + lineStart + offset + (line === 0 ? 2 : 3), + `selection of line ${line}, offset ${offset}` + ) + assert.equal(textarea.selectionEnd, textarea.selectionStart) + } + } + }) + + it('unquotes the full line of text when the cursor is in the middle of the line', function () { + setVisualValue('> The quick| brown fox jumps over the lazy dog') + clickToolbar('md-quote') + assert.equal('The quick| brown fox jumps over the lazy dog', visualValue()) + }) + }) + + describe('task list', function () { + it('turns the full line into a task when the cursor is in the middle of the line', function () { + setVisualValue('Buy mi|lk and eggs') + clickToolbar('md-task-list') + assert.equal('- [ ] Buy mi|lk and eggs', visualValue()) + }) + + it('turns the full line into a task when the cursor is at the end of the text', function () { + setVisualValue('Buy milk and eggs|') + clickToolbar('md-task-list') + assert.equal('- [ ] Buy milk and eggs|', visualValue()) + }) + + it('turns only the line with the cursor into a task in the middle of other lines', function () { + setVisualValue('First line\nBuy mi|lk\nThird line') + clickToolbar('md-task-list') + assert.equal('First line\n\n- [ ] Buy mi|lk\n\nThird line', visualValue()) + }) }) describe('mention', function () {