Skip to content

Commit

Permalink
Fix rich text utils backspace not clearing the block type at the start.
Browse files Browse the repository at this point in the history
Summary:
**Summary**

When backspacing at the start of the first block using rich text utils, the block type should be cleared.

Example of me frantically trying to clear the first block type **without** this PR by pressing backspace at the beginning of the document:

![clear-first-block-type](https://cloud.githubusercontent.com/assets/424704/19821170/d3296d9a-9d11-11e6-990d-e2277bbfb833.gif)

Followed by an example of me clearing the first block type **with** this PR:

![clear-first-block-type-working](https://cloud.githubusercontent.com/assets/424704/19821197/01b4826c-9d12-11e6-853b-32417ec93f10.gif)

**Test Plan**
- Pull master.
- Open the rich text example.
- Create a list.
- Put your cursor at the first offset of the first block, backspace to reset the block type, notice that it doesn't work.
- Pull this branch.
- Open the rich text example.
- Create a list.
- Put your cursor at the first offset of the first block, backspace to reset the block type, rejoice in joy as the list item disappears.
- Celebrate. 🎉
Closes facebookarchive/draft-js#748

Differential Revision: D6065808

fbshipit-source-id: d4b9028e255be1bdf405cf228cfd16c877872c95
  • Loading branch information
aforismesen authored and facebook-github-bot committed Oct 17, 2017
1 parent 9c79e8e commit f2f8f10
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/model/modifier/RichTextEditorUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}

Expand Down
23 changes: 22 additions & 1 deletion src/model/modifier/__tests__/RichTextEditorUtil-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down

0 comments on commit f2f8f10

Please sign in to comment.