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

Fix enter and copy past behaviour not respects allowedBlocks and child blocks restrictions #7230

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions core-blocks/heading/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import './editor.scss';

export default function HeadingEdit( {
attributes,
canInsertSibling,
setAttributes,
mergeBlocks,
insertBlocksAfter,
Expand Down Expand Up @@ -66,6 +67,13 @@ export default function HeadingEdit( {
onSplit={
insertBlocksAfter ?
( before, after, ...blocks ) => {
if ( ! canInsertSibling( 'core/paragraph' ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would there be any way to move this to block-list inside insertBlocksAfter to avoid having the block implementor care about this? I feel like this logic should be part of core, not the block.

// 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,
Expand Down
8 changes: 8 additions & 0 deletions core-blocks/list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ export const settings = {
render() {
const {
attributes,
canInsertSibling,
insertBlocksAfter,
setAttributes,
mergeBlocks,
Expand Down Expand Up @@ -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' ) );
}
Expand Down
8 changes: 8 additions & 0 deletions core-blocks/paragraph/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class ParagraphBlock extends Component {

render() {
const {
canInsertSibling,
attributes,
setAttributes,
insertBlocksAfter,
Expand Down Expand Up @@ -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 } ) );
}
Expand Down
14 changes: 12 additions & 2 deletions editor/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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' && (
Expand Down Expand Up @@ -607,6 +614,7 @@ export class BlockListBlock extends Component {

const applyWithSelect = withSelect( ( select, { uid, rootUID } ) => {
const {
canInsertBlockType,
isBlockSelected,
getPreviousBlockUid,
getNextBlockUid,
Expand Down Expand Up @@ -651,6 +659,7 @@ const applyWithSelect = withSelect( ( select, { uid, rootUID } ) => {
block,
isSelected,
hasFixedToolbar,
canInsertBlockType,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm passing here the selector, because we need to use it, with parameters that we don't know yet here. I think this has no implications because the reference should always be the same but it is something we should keep an eye on. I'm also open to suggestions to avoid passing the selector directly.

};
} );

Expand All @@ -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 );
},
Expand Down