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

Fix issue where block inserted in wrong place when selection is in nested block list, but root appender is used. #23668

Merged
merged 2 commits into from
Jul 8, 2020
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
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() ) ||
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The issue was that rootClientId and clientId are undefined for the root appender, so this fallback to getBlockSelectionEnd would result in blocks being inserted in the wrong place (and potentially the wrong inserter items displayed).

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();
} );
} );