Skip to content

Commit

Permalink
Fix issue where block inserted in wrong place when selection is in ne…
Browse files Browse the repository at this point in the history
…sted block list, but root appender is used. (#23668)

* Remove use of getSelectionEnd to determine where blocks should be inserted

* Add e2e tests
  • Loading branch information
talldan authored and youknowriad committed Jul 13, 2020
1 parent 9cefb98 commit 8cfc303
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
5 changes: 1 addition & 4 deletions packages/block-editor/src/components/inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,11 @@ export default compose( [
getBlockRootClientId,
hasInserterItems,
__experimentalGetAllowedBlocks,
getBlockSelectionEnd,
} = select( 'core/block-editor' );
const { getBlockVariations } = select( 'core/blocks' );

rootClientId =
rootClientId ||
getBlockRootClientId( clientId || getBlockSelectionEnd() ) ||
undefined;
rootClientId || getBlockRootClientId( clientId ) || undefined;

const allowedBlocks = __experimentalGetAllowedBlocks( rootClientId );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,15 @@ Foo</pre>
lines preserved[/myshortcode]
<!-- /wp:shortcode -->"
`;
exports[`adding blocks inserts blocks at root level when using the root appender while selection is in an inner block 1`] = `
"<!-- wp:buttons -->
<div class=\\"wp-block-buttons\\"><!-- wp:button -->
<div class=\\"wp-block-button\\"><a class=\\"wp-block-button__link\\">1.1</a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons -->
<!-- wp:paragraph -->
<p>2</p>
<!-- /wp:paragraph -->"
`;
27 changes: 27 additions & 0 deletions packages/e2e-tests/specs/editor/various/adding-blocks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,31 @@ describe( 'adding blocks', () => {
'block-editor-inserter__toggle'
);
} );

// Check for regression of https://github.com/WordPress/gutenberg/issues/23263
it( 'inserts blocks at root level when using the root appender while selection is in an inner block', async () => {
await insertBlock( 'Buttons' );
await page.keyboard.type( '1.1' );

// After inserting the Buttons block the inner button block should be selected.
const selectedButtonBlocks = await page.$$(
'.wp-block-button.is-selected'
);
expect( selectedButtonBlocks.length ).toBe( 1 );

// Specifically click the root container appender.
await page.click(
'.block-editor-block-list__layout.is-root-container > .block-list-appender .block-editor-inserter__toggle'
);

// Insert a paragraph block.
await page.waitForSelector( '.block-editor-inserter__search-input' );
await page.keyboard.type( 'Paragraph' );
await page.click( '.editor-block-list-item-paragraph' );
await page.keyboard.type( '2' );

// The snapshot should show a buttons block followed by a paragraph.
// The buttons block should contain a single button.
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
} );

0 comments on commit 8cfc303

Please sign in to comment.