diff --git a/core-blocks/heading/edit.js b/core-blocks/heading/edit.js index 60869aca35e3f1..a682629abaf614 100644 --- a/core-blocks/heading/edit.js +++ b/core-blocks/heading/edit.js @@ -15,6 +15,7 @@ import './editor.scss'; export default function HeadingEdit( { attributes, + canInsertSibling, setAttributes, mergeBlocks, insertBlocksAfter, @@ -66,6 +67,13 @@ export default function HeadingEdit( { onSplit={ insertBlocksAfter ? ( before, after, ...blocks ) => { + if ( ! canInsertSibling( 'core/paragraph' ) ) { + // If it is not possible to insert a paragraph + // don't split the block just try to insert pasted blocks after it. + // Unsupported blocks are ignored. + insertBlocksAfter( blocks ); + return; + } setAttributes( { content: before } ); insertBlocksAfter( [ ...blocks, diff --git a/core-blocks/list/index.js b/core-blocks/list/index.js index 8c3924f87a15e8..02ae5a582a36fa 100644 --- a/core-blocks/list/index.js +++ b/core-blocks/list/index.js @@ -252,6 +252,7 @@ export const settings = { render() { const { attributes, + canInsertSibling, insertBlocksAfter, setAttributes, mergeBlocks, @@ -302,6 +303,13 @@ export const settings = { onSplit={ insertBlocksAfter ? ( before, after, ...blocks ) => { + if ( ! canInsertSibling( 'core/list' ) ) { + // If it is not possible to insert a list + // don't split the block just try to insert pasted blocks after it. + // Unsupported blocks are ignored. + insertBlocksAfter( blocks ); + return; + } if ( ! blocks.length ) { blocks.push( createBlock( 'core/paragraph' ) ); } diff --git a/core-blocks/paragraph/index.js b/core-blocks/paragraph/index.js index d7df95c47dc7ef..2fc2d217cc42aa 100644 --- a/core-blocks/paragraph/index.js +++ b/core-blocks/paragraph/index.js @@ -139,6 +139,7 @@ class ParagraphBlock extends Component { render() { const { + canInsertSibling, attributes, setAttributes, insertBlocksAfter, @@ -233,6 +234,13 @@ class ParagraphBlock extends Component { } } onSplit={ insertBlocksAfter ? ( before, after, ...blocks ) => { + if ( ! canInsertSibling( name ) ) { + // If it is not possible to insert block of the same type + // don't split the block just try to insert pasted blocks after it. + // Unsupported blocks are ignored. + insertBlocksAfter( blocks ); + return; + } if ( after ) { blocks.push( createBlock( name, { content: after } ) ); } diff --git a/editor/components/block-list/block.js b/editor/components/block-list/block.js index 70eb8f82088c9e..0c8251c25d0b41 100644 --- a/editor/components/block-list/block.js +++ b/editor/components/block-list/block.js @@ -2,7 +2,7 @@ * External dependencies */ import classnames from 'classnames'; -import { get, reduce, size, castArray, first, last, noop } from 'lodash'; +import { get, reduce, size, castArray, filter, first, last, noop } from 'lodash'; import tinymce from 'tinymce'; /** @@ -60,6 +60,7 @@ export class BlockListBlock extends Component { this.setBlockListRef = this.setBlockListRef.bind( this ); this.bindBlockNode = this.bindBlockNode.bind( this ); + this.canInsertSibling = this.canInsertSibling.bind( this ); this.setAttributes = this.setAttributes.bind( this ); this.maybeHover = this.maybeHover.bind( this ); this.hideHoverEffects = this.hideHoverEffects.bind( this ); @@ -123,6 +124,11 @@ export class BlockListBlock extends Component { } } + canInsertSibling( blockName ) { + const { canInsertBlockType, rootUID } = this.props; + return canInsertBlockType( blockName, rootUID ); + } + setBlockListRef( node ) { // Disable reason: The root return element uses a component to manage // event nesting, but the parent block list layout needs the raw DOM @@ -562,6 +568,7 @@ export class BlockListBlock extends Component { id={ uid } isSelectionEnabled={ this.props.isSelectionEnabled } toggleSelection={ this.props.toggleSelection } + canInsertSibling={ this.canInsertSibling } /> ) } { isValid && mode === 'html' && ( @@ -607,6 +614,7 @@ export class BlockListBlock extends Component { const applyWithSelect = withSelect( ( select, { uid, rootUID } ) => { const { + canInsertBlockType, isBlockSelected, getPreviousBlockUid, getNextBlockUid, @@ -651,6 +659,7 @@ const applyWithSelect = withSelect( ( select, { uid, rootUID } ) => { block, isSelected, hasFixedToolbar, + canInsertBlockType, }; } ); @@ -674,7 +683,8 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps ) => { selectBlock( uid, initialPosition ); }, onInsertBlocks( blocks, index ) { - const { rootUID, layout } = ownProps; + const { canInsertBlockType, rootUID, layout } = ownProps; + blocks = filter( blocks, ( { name } ) => canInsertBlockType( name, rootUID ) ); blocks = blocks.map( ( block ) => cloneBlock( block, { layout } ) ); insertBlocks( blocks, index, rootUID ); },