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 1 commit
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 @@ -374,9 +374,9 @@ const RichTextEditorUtil = {
},

/**
* When a collapsed cursor is at the start of an empty styled block, allow
* certain key commands (newline, backspace) to simply change the
* style of the block instead of the default behavior.
* When a collapsed cursor is at the start of the first block or an empty
* styled block, allow certain key commands (newline, backspace) to simply
* change the style of the block instead of the default behavior.
*/
tryToRemoveBlockStyle: function(editorState: EditorState): ?ContentState {
var selection = editorState.getSelection();
Expand All @@ -385,7 +385,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 @@ -59,7 +59,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 @@ -82,6 +82,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