Skip to content

Commit

Permalink
[Block Editor]: Fix caret position on block merging (#34169)
Browse files Browse the repository at this point in the history
* [Block Editor]: Fix caret position on block merging

* add e2e tests

* update e2e tests
  • Loading branch information
ntsekouras authored Sep 1, 2021
1 parent c427574 commit 003d83c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,8 @@ export function* mergeBlocks( firstBlockClientId, secondBlockClientId ) {
},
},
...blocksWithTheSameType.slice( 1 ),
]
],
0 // If we don't pass the `indexToSelect` it will default to the last block.
);
}

Expand Down
6 changes: 3 additions & 3 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1281,9 +1281,9 @@ function selectionHelper( state = {}, action ) {
return state;
}

const indexToSelect =
action.indexToSelect || action.blocks.length - 1;
const blockToSelect = action.blocks[ indexToSelect ];
const blockToSelect =
action.blocks[ action.indexToSelect ] ||
action.blocks[ action.blocks.length - 1 ];

if ( ! blockToSelect ) {
return {};
Expand Down
24 changes: 24 additions & 0 deletions packages/block-editor/src/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2397,6 +2397,30 @@ describe( 'state', () => {
expect( state.selectionEnd ).toEqual( expected.selectionEnd );
} );

it( 'should replace the selected block when is explicitly passed (`indexToSelect`)', () => {
const original = deepFreeze( {
selectionStart: { clientId: 'chicken' },
selectionEnd: { clientId: 'chicken' },
} );
const action = {
type: 'REPLACE_BLOCKS',
clientIds: [ 'chicken' ],
blocks: [
{ clientId: 'rigas' },
{ clientId: 'chicken' },
{ clientId: 'wings' },
],
indexToSelect: 0,
};
const state = selection( original, action );
expect( state ).toEqual(
expect.objectContaining( {
selectionStart: { clientId: 'rigas' },
selectionEnd: { clientId: 'rigas' },
} )
);
} );

it( 'should reset if replacing with empty set', () => {
const original = deepFreeze( {
selectionStart: { clientId: 'chicken' },
Expand Down
46 changes: 46 additions & 0 deletions packages/e2e-tests/specs/editor/various/splitting-merging.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,50 @@ describe( 'splitting and merging blocks', () => {

expect( await getEditedPostContent() ).toMatchSnapshot();
} );

describe( 'test restore selection when merge produces more than one block', () => {
it( 'on forward delete', async () => {
await insertBlock( 'Paragraph' );
await page.keyboard.type( 'hi' );
await insertBlock( 'List' );
await page.keyboard.type( 'item 1' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( 'item 2' );
await pressKeyTimes( 'ArrowUp', 2 );
await page.keyboard.press( 'Delete' );
// Carret should be in the first block and at the proper position.
await page.keyboard.type( '-' );
expect( await getEditedPostContent() ).toMatchInlineSnapshot( `
"<!-- wp:paragraph -->
<p>hi-item 1</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>item 2</p>
<!-- /wp:paragraph -->"
` );
} );
it( 'on backspace', async () => {
await insertBlock( 'Paragraph' );
await page.keyboard.type( 'hi' );
await insertBlock( 'List' );
await page.keyboard.type( 'item 1' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( 'item 2' );
await page.keyboard.press( 'ArrowUp' );
await pressKeyTimes( 'ArrowLeft', 6 );
await page.keyboard.press( 'Backspace' );
// Carret should be in the first block and at the proper position.
await page.keyboard.type( '-' );
expect( await getEditedPostContent() ).toMatchInlineSnapshot( `
"<!-- wp:paragraph -->
<p>hi-item 1</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>item 2</p>
<!-- /wp:paragraph -->"
` );
} );
} );
} );

0 comments on commit 003d83c

Please sign in to comment.