diff --git a/src/model/modifier/RichTextEditorUtil.js b/src/model/modifier/RichTextEditorUtil.js index 9cbfdbab11..d3b7a7b55b 100644 --- a/src/model/modifier/RichTextEditorUtil.js +++ b/src/model/modifier/RichTextEditorUtil.js @@ -380,9 +380,9 @@ const RichTextEditorUtil = { }, /** - * When a collapsed cursor is at the start of an empty styled block, - * changes block to 'unstyled'. Returns null if block or selection does not - * meet that criteria. + * When a collapsed cursor is at the start of the first styled block, or + * an empty styled block, changes block to 'unstyled'. Returns null if + * block or selection does not meet that criteria. */ tryToRemoveBlockStyle: function(editorState: EditorState): ?ContentState { var selection = editorState.getSelection(); @@ -391,7 +391,9 @@ const RichTextEditorUtil = { var key = selection.getAnchorKey(); var content = editorState.getCurrentContent(); var block = content.getBlockForKey(key); - if (block.getLength() > 0) { + + var firstBlock = content.getFirstBlock(); + if (block.getLength() > 0 && block !== firstBlock) { return null; } diff --git a/src/model/modifier/__tests__/RichTextEditorUtil-test.js b/src/model/modifier/__tests__/RichTextEditorUtil-test.js index e6d74e7b90..6f9b508720 100644 --- a/src/model/modifier/__tests__/RichTextEditorUtil-test.js +++ b/src/model/modifier/__tests__/RichTextEditorUtil-test.js @@ -63,7 +63,7 @@ describe('RichTextEditorUtil', () => { // Remove the current text from the blockquote. const resetBlockquote = DraftModifier.removeRange( - editorState.getCurrentContent(), + contentState, new SelectionState({ anchorKey: lastBlockKey, anchorOffset: 0, @@ -86,6 +86,27 @@ describe('RichTextEditorUtil', () => { expect(lastBlockNow.getText()).toBe(''); }); + it('resets the current block type at the start of the first block', () => { + const contentState = editorState.getCurrentContent(); + + const setListItem = DraftModifier.setBlockType( + contentState, + selectionState, + 'unordered-list-item', + ); + + const withListItem = EditorState.push( + editorState, + setListItem, + 'change-block-type', + ); + + const afterBackspace = onBackspace(withListItem); + const firstBlockNow = afterBackspace.getCurrentContent().getFirstBlock(); + + expect(firstBlockNow.getType()).toBe('unstyled'); + }); + it('removes a preceding atomic block', () => { const withAtomicBlock = insertAtomicBlock(editorState); const afterBackspace = onBackspace(withAtomicBlock);