Commit 1f8ac8a9 authored by Robert Knight's avatar Robert Knight

Ensure cursor is positioned after block prefix when creating lists

Improve behavior when the user clicks the toolbar buttons to create a
list or quote and the cursor is positioned at the start of the line with
no selection. In this case, after the command is applied, the cursor
should be positioned ready for the user to start typing the quote or
list.

When the selection is empty and text is inserted at the cursor position,
it is ambiguous whether the selection is 'before' or 'after' the
replaced text. The fix here is to treat the selection as being 'before'
the replaced text.

Fixes #3091
parent c293c687
...@@ -41,14 +41,14 @@ function replaceText(state, pos, length, text) { ...@@ -41,14 +41,14 @@ function replaceText(state, pos, length, text) {
var newSelectionStart = state.selectionStart; var newSelectionStart = state.selectionStart;
var newSelectionEnd = state.selectionEnd; var newSelectionEnd = state.selectionEnd;
if (newSelectionEnd <= pos) { if (newSelectionStart >= pos + length) {
// 1. Selection is before replaced text: Leave selection unchanged // 1. Selection is after replaced text:
} else if (newSelectionStart >= pos + length) {
// 2. Selection is after replaced text:
// Increment (start, end) by difference in length between original and // Increment (start, end) by difference in length between original and
// replaced text // replaced text
newSelectionStart += text.length - length; newSelectionStart += text.length - length;
newSelectionEnd += text.length - length; newSelectionEnd += text.length - length;
} else if (newSelectionEnd <= pos) {
// 2. Selection is before replaced text: Leave selection unchanged
} else if (newSelectionStart <= pos && } else if (newSelectionStart <= pos &&
newSelectionEnd >= pos + length) { newSelectionEnd >= pos + length) {
// 3. Selection fully contains replaced text: // 3. Selection fully contains replaced text:
...@@ -148,7 +148,7 @@ function toggleSpanStyle(state, prefix, suffix, placeholder) { ...@@ -148,7 +148,7 @@ function toggleSpanStyle(state, prefix, suffix, placeholder) {
if (state.selectionStart === state.selectionEnd && placeholder) { if (state.selectionStart === state.selectionEnd && placeholder) {
newState = replaceText(state, state.selectionStart, 0, placeholder); newState = replaceText(state, state.selectionStart, 0, placeholder);
newState.selectionEnd = newState.selectionStart + placeholder.length; newState.selectionStart = newState.selectionEnd - placeholder.length;
} }
if (selectionPrefix === prefix && selectionSuffix === suffix) { if (selectionPrefix === prefix && selectionSuffix === suffix) {
......
...@@ -84,6 +84,10 @@ describe('markdown commands', function () { ...@@ -84,6 +84,10 @@ describe('markdown commands', function () {
'preserves the selection': { 'preserves the selection': {
input: 'one <sel>two\nthree </sel>four', input: 'one <sel>two\nthree </sel>four',
output: '> one <sel>two\n> three </sel>four', output: '> one <sel>two\n> three </sel>four',
},
'inserts the block prefix before an empty selection': {
input: '<sel></sel>',
output: '> <sel></sel>',
} }
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment