Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Fix rich text utils backspace not clearing the block type at the start. #748

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice - thanks for fixing that.

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', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for writing a test!

testing_i_love_testing

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