Commit 3c9d560e authored by Nick Stenning's avatar Nick Stenning

Merge pull request #3114 from hypothesis/editor-block-formatting

Fix list/quote toolbar command when selection is empty and cursor is at start of line
parents bbc29fab 1f8ac8a9
...@@ -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) {
......
...@@ -72,23 +72,32 @@ describe('markdown commands', function () { ...@@ -72,23 +72,32 @@ describe('markdown commands', function () {
}); });
describe('block formatting', function () { describe('block formatting', function () {
function toggle(state) { var CASES = {
return commands.toggleBlockStyle(state, '> '); 'adds formatting to blocks': {
} input: 'one\n<sel>two\nthree</sel>\nfour',
output: 'one\n> <sel>two\n> three</sel>\nfour',
it('adds formatting to blocks', function () { },
var output = toggle(parseState('one\n<sel>two\nthree</sel>\nfour')); 'removes formatting from blocks': {
assert.equal(formatState(output), 'one\n> <sel>two\n> three</sel>\nfour'); input: 'one \n<sel>> two\n> three</sel>\nfour',
}); output: 'one \n<sel>two\nthree</sel>\nfour',
},
it('removes formatting from blocks', function () { 'preserves the selection': {
var output = toggle(parseState('one \n<sel>> two\n> three</sel>\nfour')); input: 'one <sel>two\nthree </sel>four',
assert.equal(formatState(output), 'one \n<sel>two\nthree</sel>\nfour'); output: '> one <sel>two\n> three </sel>four',
}); },
'inserts the block prefix before an empty selection': {
input: '<sel></sel>',
output: '> <sel></sel>',
}
};
it('preserves the selection', function () { Object.keys(CASES).forEach(function (case_) {
var output = toggle(parseState('one <sel>two\nthree </sel>four')); it(case_, function () {
assert.equal(formatState(output), '> one <sel>two\n> three </sel>four'); var output = commands.toggleBlockStyle(
parseState(CASES[case_].input), '> '
);
assert.equal(formatState(output), CASES[case_].output);
});
}); });
}); });
......
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