From 555f6562ff6f67925df6982ed1be9febd5c8d6f7 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Wed, 12 Sep 2018 13:22:51 +0100 Subject: [PATCH] Code Editor: unselect blocks and disable inserter (#9815) * Code Editor: unselect blocks and disable inserter * chore: Make comment a sentence * Adding a test for the code editor to ensure blocks are unselected and inserter disabled --- .../components/header/header-toolbar/index.js | 5 +-- edit-post/store/effects.js | 7 ++++- .../editor/src/components/inserter/index.js | 2 ++ test/e2e/specs/editor-modes.test.js | 31 ++++++++++++++++++- 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/edit-post/components/header/header-toolbar/index.js b/edit-post/components/header/header-toolbar/index.js index 8ce53890a9916..6283eab3a79e1 100644 --- a/edit-post/components/header/header-toolbar/index.js +++ b/edit-post/components/header/header-toolbar/index.js @@ -25,7 +25,7 @@ import { import './style.scss'; import FullscreenModeClose from '../fullscreen-mode-close'; -function HeaderToolbar( { hasFixedToolbar, isLargeViewport } ) { +function HeaderToolbar( { hasFixedToolbar, isLargeViewport, mode } ) { return (
- + { __( 'Welcome to the wonderful world of blocks! Click the “+” (“Add block”) button to add a new block. There are blocks available for all kind of content: you can insert text, headings, images, lists, and lots more!' ) } @@ -53,6 +53,7 @@ function HeaderToolbar( { hasFixedToolbar, isLargeViewport } ) { export default compose( [ withSelect( ( select ) => ( { hasFixedToolbar: select( 'core/edit-post' ).isFeatureActive( 'fixedToolbar' ), + mode: select( 'core/edit-post' ).getEditorMode(), } ) ), withViewportMatch( { isLargeViewport: 'medium' } ), ] )( HeaderToolbar ); diff --git a/edit-post/store/effects.js b/edit-post/store/effects.js index 651bc6682e669..83f10a742de9c 100644 --- a/edit-post/store/effects.js +++ b/edit-post/store/effects.js @@ -6,7 +6,7 @@ import { reduce, some } from 'lodash'; /** * WordPress dependencies */ -import { select, subscribe } from '@wordpress/data'; +import { select, subscribe, dispatch } from '@wordpress/data'; import { speak } from '@wordpress/a11y'; import { __ } from '@wordpress/i18n'; import apiFetch from '@wordpress/api-fetch'; @@ -120,6 +120,11 @@ const effects = { .then( () => store.dispatch( metaBoxUpdatesSuccess() ) ); }, SWITCH_MODE( action ) { + // Unselect blocks when we switch to the code editor. + if ( action.mode !== 'visual' ) { + dispatch( 'core/editor' ).clearSelectedBlock(); + } + const message = action.mode === 'visual' ? __( 'Visual editor selected' ) : __( 'Code editor selected' ); speak( message, 'assertive' ); }, diff --git a/packages/editor/src/components/inserter/index.js b/packages/editor/src/components/inserter/index.js index cb061f4e60da5..be1aada97c5e4 100644 --- a/packages/editor/src/components/inserter/index.js +++ b/packages/editor/src/components/inserter/index.js @@ -39,6 +39,7 @@ class Inserter extends Component { children, onInsertBlock, rootClientId, + disabled, } = this.props; if ( items.length === 0 ) { @@ -61,6 +62,7 @@ class Inserter extends Component { className="editor-inserter__toggle" aria-haspopup="true" aria-expanded={ isOpen } + disabled={ disabled } > { children } diff --git a/test/e2e/specs/editor-modes.test.js b/test/e2e/specs/editor-modes.test.js index 6c451607aedb2..b5c8cf2639a0d 100644 --- a/test/e2e/specs/editor-modes.test.js +++ b/test/e2e/specs/editor-modes.test.js @@ -1,7 +1,7 @@ /** * Internal dependencies */ -import { clickBlockAppender, newPost } from '../support/utils'; +import { clickBlockAppender, newPost, switchToEditor } from '../support/utils'; describe( 'Editing modes (visual/HTML)', () => { beforeEach( async () => { @@ -80,4 +80,33 @@ describe( 'Editing modes (visual/HTML)', () => { htmlBlockContent = await page.$eval( '.editor-block-list__layout .editor-block-list__block .editor-block-list__block-html-textarea', ( node ) => node.textContent ); expect( htmlBlockContent ).toEqual( '

Hello world!

' ); } ); + + it( 'the code editor should unselect blocks and disable the inserter', async () => { + // The paragraph block should be selected + const title = await page.$eval( + '.editor-block-inspector__card-title', + ( element ) => element.innerText + ); + expect( title ).toBe( 'Paragraph' ); + + // The Block inspector should be active + let blockInspectorTab = await page.$( '.edit-post-sidebar__panel-tab.is-active[aria-label="Block settings"]' ); + expect( blockInspectorTab ).not.toBeNull(); + + // Switch to Code Editor + await switchToEditor( 'Code' ); + + // The Block inspector should not be active anymore + blockInspectorTab = await page.$( '.edit-post-sidebar__panel-tab.is-active[aria-label="Block settings"]' ); + expect( blockInspectorTab ).toBeNull(); + + // No block is selected + await page.click( '.edit-post-sidebar__panel-tab[aria-label="Block settings"]' ); + const noBlocksElement = await page.$( '.editor-block-inspector__no-blocks' ); + expect( noBlocksElement ).not.toBeNull(); + + // The inserter is disabled + const disabledInserter = await page.$( '.editor-inserter > button:disabled' ); + expect( disabledInserter ).not.toBeNull(); + } ); } );