Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Mobile] - Prevent deleting content when backspacing in the first Paragraph block #62069

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/block-editor/src/components/block-list/block.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,8 +669,16 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps, registry ) => {
}

moveFirstItemUp( rootClientId );
} else {
removeBlock( clientId );
} else if (
getBlockName( clientId ) !== getDefaultBlockName()
) {
const replacement = switchToBlockType(
getBlock( clientId ),
getDefaultBlockName()
);
if ( replacement && replacement.length ) {
replaceBlocks( clientId, replacement );
}
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,8 @@ export class RichText extends Component {
this.comesFromAztec = true;
this.firedAfterTextChanged = event.nativeEvent.firedAfterTextChanged;
const value = this.createRecord();
const { start, end, text } = value;
const { start, end, text, activeFormats } = value;
const hasActiveFormats = activeFormats && !! activeFormats.length;
let newValue;

// Always handle full content deletion ourselves.
Expand All @@ -415,8 +416,8 @@ export class RichText extends Component {

// Only process delete if the key press occurs at an uncollapsed edge.
if (
! onDelete ||
! isCollapsed( value ) ||
hasActiveFormats ||
( isReverse && start !== 0 ) ||
( ! isReverse && end !== text.length )
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ exports[`Heading block inserts block 1`] = `
<!-- /wp:heading -->"
`;

exports[`Heading block should keep the heading when there is an empty paragraph block before and backspace is pressed at the start 1`] = `
"<!-- wp:heading -->
<h2 class="wp-block-heading">A quick brown fox jumps over the lazy dog.</h2>
<!-- /wp:heading -->"
`;

exports[`Heading block should merge with an empty Paragraph block and keep being the Heading block 1`] = `
"<!-- wp:heading -->
<h2 class="wp-block-heading">A quick brown fox jumps over the lazy dog.</h2>
Expand All @@ -29,3 +35,9 @@ exports[`Heading block should set a text color 1`] = `
<h2 class="wp-block-heading has-pale-pink-color has-text-color">A quick brown fox jumps over the lazy dog.</h2>
<!-- /wp:heading -->"
`;

exports[`Heading block should transform to a paragraph block when pressing backspace at the beginning of the first heading block 1`] = `
"<!-- wp:paragraph -->
<p>A quick brown fox jumps over the lazy dog.</p>
<!-- /wp:paragraph -->"
`;
65 changes: 65 additions & 0 deletions packages/block-library/src/heading/test/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,69 @@ describe( 'Heading block', () => {
// Assert
expect( getEditorHtml() ).toMatchSnapshot();
} );

it( 'should transform to a paragraph block when pressing backspace at the beginning of the first heading block', async () => {
// Arrange
await initializeEditor();

// Act
await addBlock( screen, 'Heading' );
const headingBlock = getBlock( screen, 'Heading' );
fireEvent.press( headingBlock );

const headingTextInput =
within( headingBlock ).getByPlaceholderText( 'Heading' );
typeInRichText(
headingTextInput,
'A quick brown fox jumps over the lazy dog.',
{ finalSelectionStart: 0, finalSelectionEnd: 0 }
);

fireEvent( headingTextInput, 'onKeyDown', {
nativeEvent: {},
preventDefault() {},
keyCode: BACKSPACE,
} );

// Assert
expect( getEditorHtml() ).toMatchSnapshot();
} );

it( 'should keep the heading when there is an empty paragraph block before and backspace is pressed at the start', async () => {
// Arrange
await initializeEditor();
await addBlock( screen, 'Paragraph' );

// Act
const paragraphBlock = getBlock( screen, 'Paragraph' );
fireEvent.press( paragraphBlock );
const paragraphTextInput =
within( paragraphBlock ).getByPlaceholderText( 'Start writing…' );
fireEvent( paragraphTextInput, 'onKeyDown', {
nativeEvent: {},
preventDefault() {},
keyCode: ENTER,
} );

await addBlock( screen, 'Heading' );
const headingBlock = getBlock( screen, 'Heading', { rowIndex: 2 } );
fireEvent.press( headingBlock );

const headingTextInput =
within( headingBlock ).getByPlaceholderText( 'Heading' );
typeInRichText(
headingTextInput,
'A quick brown fox jumps over the lazy dog.',
{ finalSelectionStart: 0, finalSelectionEnd: 0 }
);

fireEvent( headingTextInput, 'onKeyDown', {
nativeEvent: {},
preventDefault() {},
keyCode: BACKSPACE,
} );

// Assert
expect( getEditorHtml() ).toMatchSnapshot();
} );
} );
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Paragraph block should prevent deleting the first Paragraph block when pressing backspace at the start 1`] = `
"<!-- wp:paragraph -->
<p>A quick brown fox jumps over the lazy dog.</p>
<!-- /wp:paragraph -->"
`;

exports[`Paragraph block should render without crashing and match snapshot 1`] = `
<View
style={
Expand Down
26 changes: 26 additions & 0 deletions packages/block-library/src/paragraph/test/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,32 @@ describe( 'Paragraph block', () => {
expect( screen.toJSON() ).toMatchSnapshot();
} );

it( 'should prevent deleting the first Paragraph block when pressing backspace at the start', async () => {
// Arrange
const screen = await initializeEditor();
await addBlock( screen, 'Paragraph' );

// Act
const paragraphBlock = getBlock( screen, 'Paragraph' );
fireEvent.press( paragraphBlock );
const paragraphTextInput =
within( paragraphBlock ).getByPlaceholderText( 'Start writing…' );
typeInRichText(
paragraphTextInput,
'A quick brown fox jumps over the lazy dog.',
{ finalSelectionStart: 0, finalSelectionEnd: 0 }
);

fireEvent( paragraphTextInput, 'onKeyDown', {
nativeEvent: {},
preventDefault() {},
keyCode: BACKSPACE,
} );

// Assert
expect( getEditorHtml() ).toMatchSnapshot();
} );

it( 'should bold text', async () => {
// Arrange
const screen = await initializeEditor();
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ For each user feature we should also add a importance categorization label to i
-->

## Unreleased
- [*] Prevent deleting content when backspacing in the first Paragraph block [#62069]

## 1.119.0
- [internal] Remove circular dependencies within the components package [#61102]
Expand Down
Loading