Skip to content

Commit 222c6fd

Browse files
Gerardo Pachecogeriuxtwstokes
authored andcommitted
[Mobile] - Prevent deleting content when backspacing in the first Paragraph block (#62069)
* Mobile - Prevent deleting content when backspacing in the first Paragraph block at start * Update Changelog Co-authored-by: geriux <geriux@git.wordpress.org> Co-authored-by: twstokes <twstokes@git.wordpress.org>
1 parent 562e8ab commit 222c6fd

File tree

7 files changed

+123
-4
lines changed

7 files changed

+123
-4
lines changed

packages/block-editor/src/components/block-list/block.native.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,8 +669,16 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps, registry ) => {
669669
}
670670

671671
moveFirstItemUp( rootClientId );
672-
} else {
673-
removeBlock( clientId );
672+
} else if (
673+
getBlockName( clientId ) !== getDefaultBlockName()
674+
) {
675+
const replacement = switchToBlockType(
676+
getBlock( clientId ),
677+
getDefaultBlockName()
678+
);
679+
if ( replacement && replacement.length ) {
680+
replaceBlocks( clientId, replacement );
681+
}
674682
}
675683
}
676684
},

packages/block-editor/src/components/rich-text/native/index.native.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,8 @@ export class RichText extends Component {
402402
this.comesFromAztec = true;
403403
this.firedAfterTextChanged = event.nativeEvent.firedAfterTextChanged;
404404
const value = this.createRecord();
405-
const { start, end, text } = value;
405+
const { start, end, text, activeFormats } = value;
406+
const hasActiveFormats = activeFormats && !! activeFormats.length;
406407
let newValue;
407408

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

416417
// Only process delete if the key press occurs at an uncollapsed edge.
417418
if (
418-
! onDelete ||
419419
! isCollapsed( value ) ||
420+
hasActiveFormats ||
420421
( isReverse && start !== 0 ) ||
421422
( ! isReverse && end !== text.length )
422423
) {

packages/block-library/src/heading/test/__snapshots__/index.native.js.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ exports[`Heading block inserts block 1`] = `
1212
<!-- /wp:heading -->"
1313
`;
1414

15+
exports[`Heading block should keep the heading when there is an empty paragraph block before and backspace is pressed at the start 1`] = `
16+
"<!-- wp:heading -->
17+
<h2 class="wp-block-heading">A quick brown fox jumps over the lazy dog.</h2>
18+
<!-- /wp:heading -->"
19+
`;
20+
1521
exports[`Heading block should merge with an empty Paragraph block and keep being the Heading block 1`] = `
1622
"<!-- wp:heading -->
1723
<h2 class="wp-block-heading">A quick brown fox jumps over the lazy dog.</h2>
@@ -29,3 +35,9 @@ exports[`Heading block should set a text color 1`] = `
2935
<h2 class="wp-block-heading has-pale-pink-color has-text-color">A quick brown fox jumps over the lazy dog.</h2>
3036
<!-- /wp:heading -->"
3137
`;
38+
39+
exports[`Heading block should transform to a paragraph block when pressing backspace at the beginning of the first heading block 1`] = `
40+
"<!-- wp:paragraph -->
41+
<p>A quick brown fox jumps over the lazy dog.</p>
42+
<!-- /wp:paragraph -->"
43+
`;

packages/block-library/src/heading/test/index.native.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,69 @@ describe( 'Heading block', () => {
189189
// Assert
190190
expect( getEditorHtml() ).toMatchSnapshot();
191191
} );
192+
193+
it( 'should transform to a paragraph block when pressing backspace at the beginning of the first heading block', async () => {
194+
// Arrange
195+
await initializeEditor();
196+
197+
// Act
198+
await addBlock( screen, 'Heading' );
199+
const headingBlock = getBlock( screen, 'Heading' );
200+
fireEvent.press( headingBlock );
201+
202+
const headingTextInput =
203+
within( headingBlock ).getByPlaceholderText( 'Heading' );
204+
typeInRichText(
205+
headingTextInput,
206+
'A quick brown fox jumps over the lazy dog.',
207+
{ finalSelectionStart: 0, finalSelectionEnd: 0 }
208+
);
209+
210+
fireEvent( headingTextInput, 'onKeyDown', {
211+
nativeEvent: {},
212+
preventDefault() {},
213+
keyCode: BACKSPACE,
214+
} );
215+
216+
// Assert
217+
expect( getEditorHtml() ).toMatchSnapshot();
218+
} );
219+
220+
it( 'should keep the heading when there is an empty paragraph block before and backspace is pressed at the start', async () => {
221+
// Arrange
222+
await initializeEditor();
223+
await addBlock( screen, 'Paragraph' );
224+
225+
// Act
226+
const paragraphBlock = getBlock( screen, 'Paragraph' );
227+
fireEvent.press( paragraphBlock );
228+
const paragraphTextInput =
229+
within( paragraphBlock ).getByPlaceholderText( 'Start writing…' );
230+
fireEvent( paragraphTextInput, 'onKeyDown', {
231+
nativeEvent: {},
232+
preventDefault() {},
233+
keyCode: ENTER,
234+
} );
235+
236+
await addBlock( screen, 'Heading' );
237+
const headingBlock = getBlock( screen, 'Heading', { rowIndex: 2 } );
238+
fireEvent.press( headingBlock );
239+
240+
const headingTextInput =
241+
within( headingBlock ).getByPlaceholderText( 'Heading' );
242+
typeInRichText(
243+
headingTextInput,
244+
'A quick brown fox jumps over the lazy dog.',
245+
{ finalSelectionStart: 0, finalSelectionEnd: 0 }
246+
);
247+
248+
fireEvent( headingTextInput, 'onKeyDown', {
249+
nativeEvent: {},
250+
preventDefault() {},
251+
keyCode: BACKSPACE,
252+
} );
253+
254+
// Assert
255+
expect( getEditorHtml() ).toMatchSnapshot();
256+
} );
192257
} );

packages/block-library/src/paragraph/test/__snapshots__/edit.native.js.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`Paragraph block should prevent deleting the first Paragraph block when pressing backspace at the start 1`] = `
4+
"<!-- wp:paragraph -->
5+
<p>A quick brown fox jumps over the lazy dog.</p>
6+
<!-- /wp:paragraph -->"
7+
`;
8+
39
exports[`Paragraph block should render without crashing and match snapshot 1`] = `
410
<View
511
style={

packages/block-library/src/paragraph/test/edit.native.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,32 @@ describe( 'Paragraph block', () => {
6464
expect( screen.toJSON() ).toMatchSnapshot();
6565
} );
6666

67+
it( 'should prevent deleting the first Paragraph block when pressing backspace at the start', async () => {
68+
// Arrange
69+
const screen = await initializeEditor();
70+
await addBlock( screen, 'Paragraph' );
71+
72+
// Act
73+
const paragraphBlock = getBlock( screen, 'Paragraph' );
74+
fireEvent.press( paragraphBlock );
75+
const paragraphTextInput =
76+
within( paragraphBlock ).getByPlaceholderText( 'Start writing…' );
77+
typeInRichText(
78+
paragraphTextInput,
79+
'A quick brown fox jumps over the lazy dog.',
80+
{ finalSelectionStart: 0, finalSelectionEnd: 0 }
81+
);
82+
83+
fireEvent( paragraphTextInput, 'onKeyDown', {
84+
nativeEvent: {},
85+
preventDefault() {},
86+
keyCode: BACKSPACE,
87+
} );
88+
89+
// Assert
90+
expect( getEditorHtml() ).toMatchSnapshot();
91+
} );
92+
6793
it( 'should bold text', async () => {
6894
// Arrange
6995
const screen = await initializeEditor();

packages/react-native-editor/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ For each user feature we should also add a importance categorization label to i
1010
-->
1111

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

1415
## 1.119.0
1516
- [internal] Remove circular dependencies within the components package [#61102]

0 commit comments

Comments
 (0)