From 4c665751fd2b0c76bdd92ca683df1d40ed11c7be Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Thu, 30 Jun 2022 19:46:05 +0100 Subject: [PATCH 1/2] Avoid auto picking menu if uncontrolled blocks are present --- packages/block-library/src/navigation/edit/index.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/navigation/edit/index.js b/packages/block-library/src/navigation/edit/index.js index eda2a2b7967df..028de486e405e 100644 --- a/packages/block-library/src/navigation/edit/index.js +++ b/packages/block-library/src/navigation/edit/index.js @@ -207,11 +207,16 @@ function Navigation( { hasResolvedCanUserCreateNavigationMenu, } = useNavigationMenu( ref ); - // Attempt to retrieve and prioritize any existing navigation menu unless - // a specific ref is allocated or the user is explicitly creating a new menu. The aim is - // for the block to "just work" from a user perspective using existing data. + // Attempt to retrieve and prioritize any existing navigation menu unless: + // - the are uncontrolled inner blocks already present in the block. + // - the user is creating a new menu. + // - there is more than 1 Navigation Post (wp_navigation). + // This attempts to pick the first menu if there is a single Navigation Post. If more + // than 1 exists then no attempt to automatically pick a menu is made. + // The aim is for the block to "just work" from a user perspective using existing data. useEffect( () => { if ( + hasUncontrolledInnerBlocks || isCreatingNavigationMenu || ref || ! navigationMenus?.length || From 4c5200576a9da8f5780be390fe691a684360bc93 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 6 Jul 2022 11:53:27 +0100 Subject: [PATCH 2/2] Test assert uncontrolled blocks are always respected --- .../specs/editor/blocks/navigation.test.js | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/packages/e2e-tests/specs/editor/blocks/navigation.test.js b/packages/e2e-tests/specs/editor/blocks/navigation.test.js index f2fc9ec2c9e99..42d55ff81d227 100644 --- a/packages/e2e-tests/specs/editor/blocks/navigation.test.js +++ b/packages/e2e-tests/specs/editor/blocks/navigation.test.js @@ -1393,6 +1393,51 @@ Expected mock function not to be called but it was called with: ["POST", "http:/ expect( linkText ).toBe( 'WordPress' ); } ); + it( 'does not automatically use the first Navigation Menu if uncontrolled inner blocks are present', async () => { + const pageTitle = 'A Test Page'; + + await createNavigationMenu( { + title: 'Example Navigation', + content: + '', + } ); + + await rest( { + method: 'POST', + path: `/wp/v2/pages/`, + data: { + status: 'publish', + title: pageTitle, + content: 'Hello world', + }, + } ); + + await createNewPost(); + + await clickOnMoreMenuItem( 'Code editor' ); + + const codeEditorInput = await page.waitForSelector( + '.editor-post-text-editor' + ); + await codeEditorInput.click(); + + const markup = + ''; + await page.keyboard.type( markup ); + await clickButton( 'Exit code editor' ); + + await waitForBlock( 'Navigation' ); + + const hasUncontrolledInnerBlocks = await page.evaluate( () => { + const blocks = wp.data + .select( 'core/block-editor' ) + .getBlocks(); + return !! blocks[ 0 ]?.innerBlocks?.length; + } ); + + expect( hasUncontrolledInnerBlocks ).toBe( true ); + } ); + it( 'does not automatically use first Navigation Menu if more than one exists', async () => { await createNavigationMenu( { title: 'Example Navigation',