diff --git a/editor/components/default-block-appender/test/__snapshots__/index.js.snap b/editor/components/default-block-appender/test/__snapshots__/index.js.snap index d4f1508d7c6b9..92c4bfe85d345 100644 --- a/editor/components/default-block-appender/test/__snapshots__/index.js.snap +++ b/editor/components/default-block-appender/test/__snapshots__/index.js.snap @@ -36,7 +36,7 @@ exports[`DefaultBlockAppender should append a default block when input focused 1 value="Write your story" /> - 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! - + `; @@ -66,7 +66,7 @@ exports[`DefaultBlockAppender should match snapshot 1`] = ` value="Write your story" /> - 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! - + `; @@ -96,7 +96,7 @@ exports[`DefaultBlockAppender should optionally show without prompt 1`] = ` value="" /> - 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! - + `; diff --git a/editor/components/inserter/index.js b/editor/components/inserter/index.js index e20c95c203300..ce6ea0a229701 100644 --- a/editor/components/inserter/index.js +++ b/editor/components/inserter/index.js @@ -3,17 +3,14 @@ */ import { __ } from '@wordpress/i18n'; import { Dropdown, IconButton } from '@wordpress/components'; -import { createBlock, isUnmodifiedDefaultBlock } from '@wordpress/blocks'; import { Component, compose } from '@wordpress/element'; -import { withSelect, withDispatch } from '@wordpress/data'; +import { withSelect } from '@wordpress/data'; /** * Internal dependencies */ import InserterMenu from './menu'; -export { default as InserterResultsPortal } from './results-portal'; - class Inserter extends Component { constructor() { super( ...arguments ); @@ -32,15 +29,13 @@ class Inserter extends Component { render() { const { - items, + hasBlockTypes, position, title, children, - onInsertBlock, - rootUID, } = this.props; - if ( items.length === 0 ) { + if ( ! hasBlockTypes ) { return null; } @@ -65,13 +60,7 @@ class Inserter extends Component { ) } renderContent={ ( { onClose } ) => { - const onSelect = ( item ) => { - onInsertBlock( item ); - - onClose(); - }; - - return ; + return ; } } /> ); @@ -80,32 +69,15 @@ class Inserter extends Component { export default compose( [ withSelect( ( select ) => { + const { + getBlockTypes, + } = select( 'core/blocks' ); const { getEditedPostAttribute, - getBlockInsertionPoint, - getSelectedBlock, - getInserterItems, } = select( 'core/editor' ); - const insertionPoint = getBlockInsertionPoint(); - const { rootUID } = insertionPoint; return { + hasBlockTypes: getBlockTypes().length > 0, title: getEditedPostAttribute( 'title' ), - insertionPoint, - selectedBlock: getSelectedBlock(), - items: getInserterItems( rootUID ), - rootUID, }; } ), - withDispatch( ( dispatch, ownProps ) => ( { - onInsertBlock: ( item ) => { - const { insertionPoint, selectedBlock } = ownProps; - const { index, rootUID, layout } = insertionPoint; - const { name, initialAttributes } = item; - const insertedBlock = createBlock( name, { ...initialAttributes, layout } ); - if ( selectedBlock && isUnmodifiedDefaultBlock( selectedBlock ) ) { - return dispatch( 'core/editor' ).replaceBlocks( selectedBlock.uid, insertedBlock ); - } - return dispatch( 'core/editor' ).insertBlock( insertedBlock, index, rootUID ); - }, - } ) ), ] )( Inserter ); diff --git a/editor/components/inserter/menu.js b/editor/components/inserter/menu.js index 88fc5bb1c6606..d69839dc0dbc6 100644 --- a/editor/components/inserter/menu.js +++ b/editor/components/inserter/menu.js @@ -27,7 +27,12 @@ import { PanelBody, withSafeTimeout, } from '@wordpress/components'; -import { getCategories, isSharedBlock } from '@wordpress/blocks'; +import { + createBlock, + getCategories, + isSharedBlock, + isUnmodifiedDefaultBlock, +} from '@wordpress/blocks'; import { withDispatch, withSelect } from '@wordpress/data'; /** @@ -276,22 +281,48 @@ export class InserterMenu extends Component { } export default compose( - withSelect( ( select, { rootUID } ) => { + withSelect( ( select ) => { const { getChildBlockNames, } = select( 'core/blocks' ); const { + getBlockInsertionPoint, getBlockName, + getInserterItems, + getSelectedBlock, } = select( 'core/editor' ); + const insertionPoint = getBlockInsertionPoint(); + const { rootUID } = insertionPoint; const rootBlockName = getBlockName( rootUID ); return { + insertionPoint, + items: getInserterItems( rootUID ), rootChildBlocks: getChildBlockNames( rootBlockName ), + rootUID, + selectedBlock: getSelectedBlock(), }; } ), - withDispatch( ( dispatch ) => ( { + withDispatch( ( dispatch, ownProps ) => ( { fetchSharedBlocks: dispatch( 'core/editor' ).fetchSharedBlocks, showInsertionPoint: dispatch( 'core/editor' ).showInsertionPoint, hideInsertionPoint: dispatch( 'core/editor' ).hideInsertionPoint, + onSelect: ( item ) => { + const { insertionPoint, onClose, selectedBlock } = ownProps; + const { index, rootUID, layout } = insertionPoint; + const { name, initialAttributes } = item; + + const insertedBlock = createBlock( name, { ...initialAttributes, layout } ); + + if ( selectedBlock && isUnmodifiedDefaultBlock( selectedBlock ) ) { + dispatch( 'core/editor' ).replaceBlocks( selectedBlock.uid, insertedBlock ); + } else { + dispatch( 'core/editor' ).insertBlock( insertedBlock, index, rootUID ); + } + + if ( onClose ) { + onClose(); + } + }, } ) ), withSpokenMessages, withInstanceId, diff --git a/editor/components/rich-text/tokens/ui/index.js b/editor/components/rich-text/tokens/ui/index.js index 0d0be6eb0fe4b..04effd2f45c4a 100644 --- a/editor/components/rich-text/tokens/ui/index.js +++ b/editor/components/rich-text/tokens/ui/index.js @@ -10,7 +10,7 @@ import { withSelect } from '@wordpress/data'; * Internal dependencies */ import './style.scss'; -import { InserterResultsPortal } from '../../../inserter'; +import InserterResultsPortal from '../../../inserter/results-portal'; class TokenUI extends Component { constructor() {