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: Drag & drop allows to move blocks to not allowed destinations #7212

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
27 changes: 18 additions & 9 deletions editor/components/block-drop-zone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ class BlockDropZone extends Component {
}

export default compose(
withSelect( ( select ) => {
const { canInsertBlockType, getBlockName, getEditorSettings } = select( 'core/editor' );
const { templateLock } = getEditorSettings();

return {
isLocked: !! templateLock,
canInsertBlockType,
getBlockName,
};
} ),
withDispatch( ( dispatch, ownProps ) => {
Copy link
Member

Choose a reason for hiding this comment

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

Regardless to what you decide in response to DropZone component, I wanted to leave a note about a way how you can avoid passing down selectors as props. A while back we have added 3rd param registry to withDispatch method which allows you to get access to the same selectors without exposing them as props. There is an example added in the docs: https://github.com/WordPress/gutenberg/tree/master/packages/data#withdispatch-mapdispatchtoprops-function--function (2nd example).

In this case, it would be something like the following:

withDispatch( ( dispatch, ownProps, { select } ) => {
    // ...
    moveBlockToPosition( uid, fromRootUID, index ) {
        const { rootUID, layout } = ownProps;
        const { canInsertBlockType, getBlockName } = select( 'core/editor' );
        // and the rest as before
    }
}

const {
insertBlocks,
Expand Down Expand Up @@ -132,16 +142,15 @@ export default compose(
updateBlockAttributes( ...args );
},
moveBlockToPosition( uid, fromRootUID, index ) {
const { rootUID, layout } = ownProps;
moveBlockToPosition( uid, fromRootUID, rootUID, layout, index );
const { canInsertBlockType, getBlockName, rootUID, layout } = ownProps;
const blockName = getBlockName( uid );
// currently the only constraint to move inside the same parent is locking
// locking was already handled
// it is not possible to use drag & drop if locking is active
if ( rootUID === fromRootUID || canInsertBlockType( blockName, rootUID ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

While this is good for now, I wonder if we should bake a validation mechanism inside DropZone. That way we can change the cursor to not-allowed when we're hovering an area where we can't drop.

moveBlockToPosition( uid, fromRootUID, rootUID, layout, index );
}
},
};
} ),
withSelect( ( select ) => {
const { templateLock } = select( 'core/editor' ).getEditorSettings();

return {
isLocked: !! templateLock,
};
} )
)( BlockDropZone );
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exports[`DefaultBlockAppender should append a default block when input focused 1
className="editor-default-block-appender"
data-root-uid=""
>
<WithDispatch(WithSelect(BlockDropZone)) />
<WithSelect(WithDispatch(BlockDropZone)) />
<input
aria-label="Add block"
className="editor-default-block-appender__content"
Expand Down Expand Up @@ -53,7 +53,7 @@ exports[`DefaultBlockAppender should match snapshot 1`] = `
className="editor-default-block-appender"
data-root-uid=""
>
<WithDispatch(WithSelect(BlockDropZone)) />
<WithSelect(WithDispatch(BlockDropZone)) />
<input
aria-label="Add block"
className="editor-default-block-appender__content"
Expand Down Expand Up @@ -83,7 +83,7 @@ exports[`DefaultBlockAppender should optionally show without prompt 1`] = `
className="editor-default-block-appender"
data-root-uid=""
>
<WithDispatch(WithSelect(BlockDropZone)) />
<WithSelect(WithDispatch(BlockDropZone)) />
<input
aria-label="Add block"
className="editor-default-block-appender__content"
Expand Down