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

Disable Zoom Out if no section root to allow for Theme opt in #67232

Merged
merged 11 commits into from
Dec 2, 2024
9 changes: 8 additions & 1 deletion packages/block-editor/src/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ function InserterMenu(
( select ) => unlock( select( blockEditorStore ) ).isZoomOut(),
[]
);
const hasSectionRootClientId = useSelect( ( select ) => {
const { getSectionRootClientId } = unlock( select( blockEditorStore ) );
const theRoot = getSectionRootClientId();
getdave marked this conversation as resolved.
Show resolved Hide resolved
return theRoot?.length > 0;
}, [] );
const [ filterValue, setFilterValue, delayedFilterValue ] =
useDebouncedInput( __experimentalFilterValue );
const [ hoveredItem, setHoveredItem ] = useState( null );
Expand All @@ -81,7 +86,9 @@ function InserterMenu(
const [ selectedTab, setSelectedTab ] = useState( getInitialTab() );

const shouldUseZoomOut =
selectedTab === 'patterns' || selectedTab === 'media';
hasSectionRootClientId &&
( selectedTab === 'patterns' || selectedTab === 'media' );

useZoomOut( shouldUseZoomOut && isLargeViewport );

const [ destinationRootClientId, onInsertBlocks, onToggleInsertionPoint ] =
Expand Down
16 changes: 13 additions & 3 deletions packages/editor/src/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
PATTERN_POST_TYPE,
NAVIGATION_POST_TYPE,
} from '../../store/constants';
import { unlock } from '../../lock-unlock';

const toolbarVariations = {
distractionFreeDisabled: { y: '-50px' },
Expand Down Expand Up @@ -102,6 +103,13 @@ function Header( {
( hasFixedToolbar &&
( ! hasBlockSelection || isBlockToolsCollapsed ) ) );
const hasBackButton = useHasBackButton();

const hasSectionRootClientId = useSelect( ( select ) => {
const { getSectionRootClientId } = unlock( select( blockEditorStore ) );
const theRoot = getSectionRootClientId();
return theRoot?.length > 0;
}, [] );
getdave marked this conversation as resolved.
Show resolved Hide resolved

/*
* The edit-post-header classname is only kept for backward compatability
* as some plugins might be relying on its presence.
Expand Down Expand Up @@ -169,9 +177,11 @@ function Header( {
forceIsAutosaveable={ forceIsDirty }
/>

{ canBeZoomedOut && isWideViewport && (
<ZoomOutToggle disabled={ forceDisableBlockTools } />
) }
{ canBeZoomedOut &&
isWideViewport &&
hasSectionRootClientId && (
<ZoomOutToggle disabled={ forceDisableBlockTools } />
) }

{ ( isWideViewport || ! showIconLabels ) && (
<PinnedItems.Slot scope="core" />
Expand Down
3 changes: 1 addition & 2 deletions test/e2e/specs/editor/various/parsing-patterns.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ test.describe( 'Parsing patterns', () => {
} );
} );

// Exit zoom out mode and select the inner buttons block to ensure
// Select the inner buttons block to ensure
// the correct insertion point is selected.
await page.getByRole( 'button', { name: 'Zoom Out' } ).click();
await editor.selectBlocks(
editor.canvas.locator( 'role=document[name="Block: Button"i]' )
);
Expand Down
11 changes: 9 additions & 2 deletions test/e2e/specs/editor/various/pattern-overrides.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,16 @@

await editor.setContent( '' );

// Insert the core/group block with a tagName of 'main'
await editor.insertBlock( {
name: 'core/block',
attributes: { ref: id },
name: 'core/group',
attributes: { tagName: 'main' },
innerBlocks: [
{
name: 'core/block',
attributes: { ref: id },
getdave marked this conversation as resolved.
Show resolved Hide resolved
},
],
} );

const patternBlock = editor.canvas.getByRole( 'document', {
Expand All @@ -298,7 +305,7 @@
'inert',
'true'
);
await expect( blockWithOverrides ).not.toHaveAttribute(

Check failure on line 308 in test/e2e/specs/editor/various/pattern-overrides.spec.js

View workflow job for this annotation

GitHub Actions / Playwright - 4

[chromium] › editor/various/pattern-overrides.spec.js:240:3 › Pattern Overrides › block editing modes › blocks with bindings in a synced pattern are editable

1) [chromium] › editor/various/pattern-overrides.spec.js:240:3 › Pattern Overrides › block editing modes › blocks with bindings in a synced pattern are editable, and all other blocks are disabled › Zoomed in / Design mode Error: Timed out 5000ms waiting for expect(locator).not.toHaveAttribute(expected) Locator: locator('[name="editor-canvas"]').contentFrame().getByRole('document', { name: 'Block: Paragraph' }).filter({ hasText: 'Pattern Overrides' }) Expected string: not "true" Received string: "true" Call log: - expect.not.toHaveAttribute with timeout 5000ms - waiting for locator('[name="editor-canvas"]').contentFrame().getByRole('document', { name: 'Block: Paragraph' }).filter({ hasText: 'Pattern Overrides' }) - locator resolved to <p inert="true" role="document" data-empty="false" aria-multiline="true" data-title="Paragraph" contenteditable="true" data-type="core/paragraph" aria-label="Block: Paragraph" data-wp-block-attribute-key="content" id="block-e4857cb2-5ca8-44ea-971b-c3e5502196a2" data-block="e4857cb2-5ca8-44ea-971b-c3e5502196a2" class="block-editor-rich-text__editable block-editor-block-list__block wp-block wp-block-paragraph rich-text">Pattern Overrides</p> - unexpected value "true" - locator resolved to <p inert="true" role="document" data-empty="false" aria-multiline="true" data-title="Paragraph" contenteditable="true" data-type="core/paragraph" aria-label="Block: Paragraph" data-wp-block-attribute-key="content" id="block-e4857cb2-5ca8-44ea-971b-c3e5502196a2" data-block="e4857cb2-5ca8-44ea-971b-c3e5502196a2" class="block-editor-rich-text__editable block-editor-block-list__block wp-block wp-block-paragraph rich-text">Pattern Overrides</p> - unexpected value "true" - locator resolved to <p inert="true" role="document" data-empty="false" aria-multiline="true" data-title="Paragraph" contenteditable="true" data-type="core/paragraph" aria-label="Block: Paragraph" data-wp-block-attribute-key="content" id="block-e4857cb2-5ca8-44ea-971b-c3e5502196a2" data-block="e4857cb2-5ca8-44ea-971b-c3e5502196a2" class="block-editor-rich-text__editable block-editor-block-list__block wp-block wp-block-paragraph rich-text">Pattern Overrides</p> - unexpected value "true" - locator resolved to <p inert="true" role="document" data-empty="false" aria-multiline="true" data-title="Paragraph" contenteditable="true" data-type="core/paragraph" aria-label="Block: Paragraph" data-wp-block-attribute-key="content" id="block-e4857cb2-5ca8-44ea-971b-c3e5502196a2" data-block="e4857cb2-5ca8-44ea-971b-c3e5502196a2" class="block-editor-rich-text__editable block-editor-block-list__block wp-block wp-block-paragraph rich-text">Pattern Overrides</p> - unexpected value "true" - locator resolved to <p inert="true" role="document" data-empty="false" aria-multiline="true" data-title="Paragraph" contenteditable="true" data-type="core/paragraph" aria-label="Block: Paragraph" data-wp-block-attribute-key="content" id="block-e4857cb2-5ca8-44ea-971b-c3e5502196a2" data-block="e4857cb2-5ca8-44ea-971b-c3e5502196a2" class="block-editor-rich-text__editable block-editor-block-list__block wp-block wp-block-paragraph rich-text">Pattern Overrides</p> - unexpected value "true" - locator resolved to <p inert="true" role="document" data-empty="false" aria-multiline="true" data-title="Paragraph" contenteditable="true" data-type="core/paragraph" aria-label="Block: Paragraph" data-wp-block-attribute-key="content" id="block-e4857cb2-5ca8-44ea-971b-c3e5502196a2" data-block="e4857cb2-5ca8-44ea-971b-c3e5502196a2" class="block-editor-rich-text__editable block-editor-block-list__block wp-block wp-block-paragraph rich-text">Pattern Overrides</p> - unexpected value "true" - locator resolved to <p inert="true" role="document" data-empty="false" aria-multiline="true" data-title="Paragraph" contenteditable="true" data-type="core/paragraph" aria-label="Block: Paragraph" data-wp-block-attribute-key="content" id="block-e4857cb2-5ca8-44ea-971b-c3e5502196a2

Check failure on line 308 in test/e2e/specs/editor/various/pattern-overrides.spec.js

View workflow job for this annotation

GitHub Actions / Playwright - 4

[chromium] › editor/various/pattern-overrides.spec.js:240:3 › Pattern Overrides › block editing modes › blocks with bindings in a synced pattern are editable

1) [chromium] › editor/various/pattern-overrides.spec.js:240:3 › Pattern Overrides › block editing modes › blocks with bindings in a synced pattern are editable, and all other blocks are disabled › Zoomed in / Design mode Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: Timed out 5000ms waiting for expect(locator).not.toHaveAttribute(expected) Locator: locator('[name="editor-canvas"]').contentFrame().getByRole('document', { name: 'Block: Paragraph' }).filter({ hasText: 'Pattern Overrides' }) Expected string: not "true" Received string: "true" Call log: - expect.not.toHaveAttribute with timeout 5000ms - waiting for locator('[name="editor-canvas"]').contentFrame().getByRole('document', { name: 'Block: Paragraph' }).filter({ hasText: 'Pattern Overrides' }) - locator resolved to <p inert="true" role="document" data-empty="false" aria-multiline="true" data-title="Paragraph" contenteditable="true" data-type="core/paragraph" aria-label="Block: Paragraph" data-wp-block-attribute-key="content" id="block-77da774b-c62b-4b9a-93c5-a654a007857c" data-block="77da774b-c62b-4b9a-93c5-a654a007857c" class="block-editor-rich-text__editable block-editor-block-list__block wp-block wp-block-paragraph rich-text">Pattern Overrides</p> - unexpected value "true" - locator resolved to <p inert="true" role="document" data-empty="false" aria-multiline="true" data-title="Paragraph" contenteditable="true" data-type="core/paragraph" aria-label="Block: Paragraph" data-wp-block-attribute-key="content" id="block-77da774b-c62b-4b9a-93c5-a654a007857c" data-block="77da774b-c62b-4b9a-93c5-a654a007857c" class="block-editor-rich-text__editable block-editor-block-list__block wp-block wp-block-paragraph rich-text">Pattern Overrides</p> - unexpected value "true" - locator resolved to <p inert="true" role="document" data-empty="false" aria-multiline="true" data-title="Paragraph" contenteditable="true" data-type="core/paragraph" aria-label="Block: Paragraph" data-wp-block-attribute-key="content" id="block-77da774b-c62b-4b9a-93c5-a654a007857c" data-block="77da774b-c62b-4b9a-93c5-a654a007857c" class="block-editor-rich-text__editable block-editor-block-list__block wp-block wp-block-paragraph rich-text">Pattern Overrides</p> - unexpected value "true" - locator resolved to <p inert="true" role="document" data-empty="false" aria-multiline="true" data-title="Paragraph" contenteditable="true" data-type="core/paragraph" aria-label="Block: Paragraph" data-wp-block-attribute-key="content" id="block-77da774b-c62b-4b9a-93c5-a654a007857c" data-block="77da774b-c62b-4b9a-93c5-a654a007857c" class="block-editor-rich-text__editable block-editor-block-list__block wp-block wp-block-paragraph rich-text">Pattern Overrides</p> - unexpected value "true" - locator resolved to <p inert="true" role="document" data-empty="false" aria-multiline="true" data-title="Paragraph" contenteditable="true" data-type="core/paragraph" aria-label="Block: Paragraph" data-wp-block-attribute-key="content" id="block-77da774b-c62b-4b9a-93c5-a654a007857c" data-block="77da774b-c62b-4b9a-93c5-a654a007857c" class="block-editor-rich-text__editable block-editor-block-list__block wp-block wp-block-paragraph rich-text">Pattern Overrides</p> - unexpected value "true" - locator resolved to <p inert="true" role="document" data-empty="false" aria-multiline="true" data-title="Paragraph" contenteditable="true" data-type="core/paragraph" aria-label="Block: Paragraph" data-wp-block-attribute-key="content" id="block-77da774b-c62b-4b9a-93c5-a654a007857c" data-block="77da774b-c62b-4b9a-93c5-a654a007857c" class="block-editor-rich-text__editable block-editor-block-list__block wp-block wp-block-paragraph rich-text">Pattern Overrides</p> - unexpected value "true" - locator resolved to <p inert="true" role="document" data-empty="false" aria-multiline="true" data-title="Paragraph" contenteditable="true" data-type="core/paragraph" aria-label="

Check failure on line 308 in test/e2e/specs/editor/various/pattern-overrides.spec.js

View workflow job for this annotation

GitHub Actions / Playwright - 4

[chromium] › editor/various/pattern-overrides.spec.js:240:3 › Pattern Overrides › block editing modes › blocks with bindings in a synced pattern are editable

1) [chromium] › editor/various/pattern-overrides.spec.js:240:3 › Pattern Overrides › block editing modes › blocks with bindings in a synced pattern are editable, and all other blocks are disabled › Zoomed in / Design mode Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: Timed out 5000ms waiting for expect(locator).not.toHaveAttribute(expected) Locator: locator('[name="editor-canvas"]').contentFrame().getByRole('document', { name: 'Block: Paragraph' }).filter({ hasText: 'Pattern Overrides' }) Expected string: not "true" Received string: "true" Call log: - expect.not.toHaveAttribute with timeout 5000ms - waiting for locator('[name="editor-canvas"]').contentFrame().getByRole('document', { name: 'Block: Paragraph' }).filter({ hasText: 'Pattern Overrides' }) - locator resolved to <p inert="true" role="document" data-empty="false" aria-multiline="true" data-title="Paragraph" contenteditable="true" data-type="core/paragraph" aria-label="Block: Paragraph" data-wp-block-attribute-key="content" id="block-ff70399a-f645-4f53-a732-e568843fc000" data-block="ff70399a-f645-4f53-a732-e568843fc000" class="block-editor-rich-text__editable block-editor-block-list__block wp-block wp-block-paragraph rich-text">Pattern Overrides</p> - unexpected value "true" - locator resolved to <p inert="true" role="document" data-empty="false" aria-multiline="true" data-title="Paragraph" contenteditable="true" data-type="core/paragraph" aria-label="Block: Paragraph" data-wp-block-attribute-key="content" id="block-ff70399a-f645-4f53-a732-e568843fc000" data-block="ff70399a-f645-4f53-a732-e568843fc000" class="block-editor-rich-text__editable block-editor-block-list__block wp-block wp-block-paragraph rich-text">Pattern Overrides</p> - unexpected value "true" - locator resolved to <p inert="true" role="document" data-empty="false" aria-multiline="true" data-title="Paragraph" contenteditable="true" data-type="core/paragraph" aria-label="Block: Paragraph" data-wp-block-attribute-key="content" id="block-ff70399a-f645-4f53-a732-e568843fc000" data-block="ff70399a-f645-4f53-a732-e568843fc000" class="block-editor-rich-text__editable block-editor-block-list__block wp-block wp-block-paragraph rich-text">Pattern Overrides</p> - unexpected value "true" - locator resolved to <p inert="true" role="document" data-empty="false" aria-multiline="true" data-title="Paragraph" contenteditable="true" data-type="core/paragraph" aria-label="Block: Paragraph" data-wp-block-attribute-key="content" id="block-ff70399a-f645-4f53-a732-e568843fc000" data-block="ff70399a-f645-4f53-a732-e568843fc000" class="block-editor-rich-text__editable block-editor-block-list__block wp-block wp-block-paragraph rich-text">Pattern Overrides</p> - unexpected value "true" - locator resolved to <p inert="true" role="document" data-empty="false" aria-multiline="true" data-title="Paragraph" contenteditable="true" data-type="core/paragraph" aria-label="Block: Paragraph" data-wp-block-attribute-key="content" id="block-ff70399a-f645-4f53-a732-e568843fc000" data-block="ff70399a-f645-4f53-a732-e568843fc000" class="block-editor-rich-text__editable block-editor-block-list__block wp-block wp-block-paragraph rich-text">Pattern Overrides</p> - unexpected value "true" - locator resolved to <p inert="true" role="document" data-empty="false" aria-multiline="true" data-title="Paragraph" contenteditable="true" data-type="core/paragraph" aria-label="Block: Paragraph" data-wp-block-attribute-key="content" id="block-ff70399a-f645-4f53-a732-e568843fc000" data-block="ff70399a-f645-4f53-a732-e568843fc000" class="block-editor-rich-text__editable block-editor-block-list__block wp-block wp-block-paragraph rich-text">Pattern Overrides</p> - unexpected value "true" - locator resolved to <p inert="true" role="document" data-empty="false" aria-multiline="true" data-title="Paragraph" contenteditable="true" data-type="core/paragraph" aria-label="
'inert',
'true'
);
Expand Down
45 changes: 44 additions & 1 deletion test/e2e/specs/site-editor/zoom-out.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );

const EDITOR_ZOOM_OUT_CONTENT = `
<!-- wp:group {"style":{"spacing":{"padding":{"top":"0","bottom":"0","left":"0","right":"0"}},"dimensions":{"minHeight":"100vh"}},"backgroundColor":"base-2","layout":{"type":"flex","orientation":"vertical","verticalAlignment":"space-between"}} -->
<!-- wp:group {"tagName":"main","layout":{"type":"constrained"}} -->
<main class="wp-block-group"><!-- wp:group {"style":{"spacing":{"padding":{"top":"0","bottom":"0","left":"0","right":"0"}},"dimensions":{"minHeight":"100vh"}},"backgroundColor":"base-2","layout":{"type":"flex","orientation":"vertical","verticalAlignment":"space-between"}} -->
<div class="wp-block-group has-base-2-background-color has-background" style="min-height:100vh;padding-top:0;padding-right:0;padding-bottom:0;padding-left:0"><!-- wp:paragraph -->
<p>First Section Start</p>
<!-- /wp:paragraph -->
Expand Down Expand Up @@ -58,6 +59,21 @@ const EDITOR_ZOOM_OUT_CONTENT = `
<!-- wp:paragraph -->
<p>Fourth Section End</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group --></main>
<!-- /wp:group -->`;

const EDITOR_ZOOM_OUT_CONTENT_NO_SECTION_ROOT = `<!-- wp:group {"style":{"spacing":{"padding":{"top":"0","bottom":"0","left":"0","right":"0"}},"dimensions":{"minHeight":"100vh"}},"backgroundColor":"base-2","layout":{"type":"flex","orientation":"vertical","verticalAlignment":"space-between"}} -->
<div class="wp-block-group has-base-2-background-color has-background" style="min-height:100vh;padding-top:0;padding-right:0;padding-bottom:0;padding-left:0"><!-- wp:paragraph -->
<p>First Section Start</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"style":{"layout":{"selfStretch":"fit","flexSize":null}}} -->
<p>First Section Center</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>First Section End</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->`;

test.describe( 'Zoom Out', () => {
Expand All @@ -67,6 +83,8 @@ test.describe( 'Zoom Out', () => {

test.afterAll( async ( { requestUtils } ) => {
await requestUtils.activateTheme( 'twentytwentyone' );
await requestUtils.deleteAllTemplates( 'wp_template' );
await requestUtils.deleteAllTemplates( 'wp_template_part' );
} );

test.beforeEach( async ( { admin } ) => {
Expand Down Expand Up @@ -215,4 +233,29 @@ test.describe( 'Zoom Out', () => {
await expect( thirdSectionEnd ).toBeInViewport();
await expect( fourthSectionStart ).not.toBeInViewport();
} );

test( 'Zoom Out cannot be activated when the section root is missing', async ( {
page,
editor,
} ) => {
await editor.setContent( EDITOR_ZOOM_OUT_CONTENT_NO_SECTION_ROOT );

// Check that the Zoom Out toggle button is not visible.
await expect(
page.getByRole( 'button', { name: 'Zoom Out' } )
).toBeHidden();

// Check that activating the Patterns tab in the Inserter does not activate
// Zoom Out.
await page
.getByRole( 'button', {
name: 'Block Inserter',
exact: true,
} )
.click();

await page.getByRole( 'tab', { name: 'Patterns' } ).click();

await expect( page.locator( '.is-zoomed-out' ) ).toBeHidden();
} );
} );
Loading