Skip to content
Merged
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
60 changes: 37 additions & 23 deletions packages/editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import {
getSelectionEnd,
remove,
isCollapsed,
LINE_SEPARATOR,
charAt,
} from '@wordpress/rich-text';
import { decodeEntities } from '@wordpress/html-entities';

Expand Down Expand Up @@ -492,8 +494,6 @@ export class RichText extends Component {
* @link https://en.wikipedia.org/wiki/Caret_navigation
*
* @param {KeyboardEvent} event Keydown event.
*
* @return {?boolean} True if the event was handled.
*/
onDeleteKeyDown( event ) {
const { onMerge, onRemove } = this.props;
Expand Down Expand Up @@ -533,7 +533,7 @@ export class RichText extends Component {
onRemove( ! isReverse );
}

return true;
event.preventDefault();
}

/**
Expand All @@ -545,32 +545,46 @@ export class RichText extends Component {
const { keyCode } = event;

if ( keyCode === DELETE || keyCode === BACKSPACE ) {
event.preventDefault();
const value = this.createRecord();
const start = getSelectionStart( value );
const end = getSelectionEnd( value );

if ( this.onDeleteKeyDown( event ) ) {
// Always handle uncollapsed selections ourselves.
Copy link
Member Author

Choose a reason for hiding this comment

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

Could use more comment sorry.

if ( ! isCollapsed( value ) ) {
this.onChange( remove( value ) );
event.preventDefault();
return;
}

const value = this.createRecord();
const start = getSelectionStart( value );
const end = getSelectionEnd( value );
if ( this.multilineTag ) {
let newValue;

if ( keyCode === BACKSPACE ) {
if ( charAt( value, start - 1 ) === LINE_SEPARATOR ) {
newValue = remove(
value,
// Only remove the line if the selection is
// collapsed.
isCollapsed( value ) ? start - 1 : start,
end
);
}
} else if ( charAt( value, end ) === LINE_SEPARATOR ) {
newValue = remove(
value,
start,
// Only remove the line if the selection is collapsed.
isCollapsed( value ) ? end + 1 : end,
);
}

if ( keyCode === BACKSPACE ) {
this.onChange( remove(
value,
// Only remove the line if the selection is
// collapsed.
isCollapsed( value ) ? start - 1 : start,
end
) );
} else {
this.onChange( remove(
value,
start,
// Only remove the line if the selection is collapsed.
isCollapsed( value ) ? end + 1 : end,
) );
if ( newValue ) {
this.onChange( newValue );
event.preventDefault();
}
}

this.onDeleteKeyDown( event );
} else if ( keyCode === ENTER ) {
event.preventDefault();

Expand Down
4 changes: 3 additions & 1 deletion packages/editor/src/components/rich-text/tinymce.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,11 @@ export default class TinyMCE extends Component {

onKeyDown( event ) {
const { keyCode } = event;
const { startContainer, startOffset, endContainer, endOffset } = getSelection().getRangeAt( 0 );
const isCollapsed = startContainer === endContainer && startOffset === endOffset;
Copy link
Member Author

Choose a reason for hiding this comment

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

getSelection().isCollapsed is not accurate, should prob be commented.


// Disables TinyMCE behaviour.
if ( keyCode === ENTER || keyCode === BACKSPACE || keyCode === DELETE ) {
if ( keyCode === ENTER || ( ! isCollapsed && ( keyCode === DELETE || keyCode === BACKSPACE ) ) ) {
event.preventDefault();
// For some reason this is needed to also prevent the insertion of
// line breaks.
Expand Down
18 changes: 18 additions & 0 deletions test/e2e/specs/writing-flow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,22 @@ describe( 'adding blocks', () => {
) );
expect( isInBlock ).toBe( true );
} );

it( 'should not delete trailing spaces when deleting a word with backspace', async () => {
await clickBlockAppender();
await page.keyboard.type( '1 2 3 4' );
await page.keyboard.press( 'Backspace' );
await page.keyboard.type( '4' );
const blockText = await page.evaluate( () => document.activeElement.textContent );
expect( blockText ).toBe( '1 2 3 4' );
} );

it( 'should not delete trailing spaces when deleting a word with alt + backspace', async () => {
await clickBlockAppender();
await page.keyboard.type( 'alpha beta gamma delta' );
await pressWithModifier( META_KEY, 'Backspace' );
await page.keyboard.type( 'delta' );
const blockText = await page.evaluate( () => document.activeElement.textContent );
expect( blockText ).toBe( 'alpha beta gamma delta' );
} );
Copy link
Contributor

Choose a reason for hiding this comment

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

I love these tests, thanks @notnownikki

} );