Skip to content

Commit

Permalink
unlock page list inside navigation block prompt to convert on select …
Browse files Browse the repository at this point in the history
…or drag
  • Loading branch information
draganescu committed Mar 3, 2023
1 parent f596d2e commit e9bcf96
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 35 deletions.
16 changes: 15 additions & 1 deletion docs/reference-guides/data/data-core-block-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,20 @@ _Returns_

- `string`: Client Id of moving block.

### hasDraggedInnerBlock

Returns true if one of the block's inner blocks is dragged.

_Parameters_

- _state_ `Object`: Editor state.
- _clientId_ `string`: Block client ID.
- _deep_ `boolean`: Perform a deep check.

_Returns_

- `boolean`: Whether the block has an inner block dragged

### hasInserterItems

Determines whether there are items to show in the inserter.
Expand Down Expand Up @@ -909,7 +923,7 @@ _Parameters_

_Returns_

- `boolean`: Whether the block as an inner block selected
- `boolean`: Whether the block has an inner block selected

### isAncestorBeingDragged

Expand Down
19 changes: 18 additions & 1 deletion packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1199,7 +1199,7 @@ export function isBlockSelected( state, clientId ) {
* @param {string} clientId Block client ID.
* @param {boolean} deep Perform a deep check.
*
* @return {boolean} Whether the block as an inner block selected
* @return {boolean} Whether the block has an inner block selected
*/
export function hasSelectedInnerBlock( state, clientId, deep = false ) {
return getBlockOrder( state, clientId ).some(
Expand All @@ -1210,6 +1210,23 @@ export function hasSelectedInnerBlock( state, clientId, deep = false ) {
);
}

/**
* Returns true if one of the block's inner blocks is dragged.
*
* @param {Object} state Editor state.
* @param {string} clientId Block client ID.
* @param {boolean} deep Perform a deep check.
*
* @return {boolean} Whether the block has an inner block dragged
*/
export function hasDraggedInnerBlock( state, clientId, deep = false ) {
return getBlockOrder( state, clientId ).some(
( innerClientId ) =>
isBlockBeingDragged( state, innerClientId ) ||
( deep && hasDraggedInnerBlock( state, innerClientId, deep ) )
);
}

/**
* Returns true if the block corresponding to the specified client ID is
* currently selected but isn't the last of the selected blocks. Here "last"
Expand Down
14 changes: 1 addition & 13 deletions packages/block-library/src/page-list-item/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useEffect } from '@wordpress/element';
import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
Expand Down Expand Up @@ -35,12 +34,7 @@ function useFrontPageId() {
}, [] );
}

export default function PageListItemEdit( {
context,
attributes,
isSelected,
setAttributes,
} ) {
export default function PageListItemEdit( { context, attributes } ) {
const { id, label, link, hasChildren } = attributes;
const isNavigationChild = 'showSubmenuIcon' in context;
const frontPageId = useFrontPageId();
Expand All @@ -55,12 +49,6 @@ export default function PageListItemEdit( {

const innerBlocksProps = useInnerBlocksProps( blockProps );

useEffect( () => {
if ( isSelected ) {
setAttributes( { isSelected: true } );
}
}, [ isSelected ] );

return (
<li
key={ id }
Expand Down
71 changes: 51 additions & 20 deletions packages/block-library/src/page-list/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import {
Button,
} from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
import { useMemo, useState, useEffect } from '@wordpress/element';
import { useMemo, useState, useEffect, useCallback } from '@wordpress/element';
import { useEntityRecords } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import { useSelect, useDispatch } from '@wordpress/data';

/**
* Internal dependencies
Expand Down Expand Up @@ -121,7 +121,7 @@ export default function PageListEdit( {
} ) {
const { parentPageID } = attributes;
const [ isOpen, setOpen ] = useState( false );
const openModal = () => setOpen( true );
const openModal = useCallback( () => setOpen( true ), [] );
const closeModal = () => setOpen( false );

const { records: pages, hasResolved: hasResolvedPages } = useEntityRecords(
Expand Down Expand Up @@ -243,38 +243,69 @@ export default function PageListEdit( {
parentPageID,
] );

const innerBlocksProps = useInnerBlocksProps( blockProps, {
allowedBlocks: [ 'core/page-list-item' ],
renderAppender: false,
__unstableDisableDropZone: true,
templateLock: 'all',
onInput: NOOP,
onChange: () => {
if ( hasResolvedPages ) {
openModal();
}
},
value: blockList,
} );

const { isNested } = useSelect(
const {
isNested,
hasSelectedChild,
parentBlock,
hasDraggedChild,
isChildOfNavigation,
} = useSelect(
( select ) => {
const { getBlockParentsByBlockName } = select( blockEditorStore );
const {
getBlockParentsByBlockName,
hasSelectedInnerBlock,
getBlockRootClientId,
hasDraggedInnerBlock,
} = select( blockEditorStore );
const blockParents = getBlockParentsByBlockName(
clientId,
'core/navigation-submenu',
true
);
const navigationBlockParents = getBlockParentsByBlockName(
clientId,
'core/navigation',
true
);
return {
isNested: blockParents.length > 0,
isChildOfNavigation: navigationBlockParents.length > 0,
hasSelectedChild: hasSelectedInnerBlock( clientId, true ),
hasDraggedChild: hasDraggedInnerBlock( clientId, true ),
parentBlock: getBlockRootClientId( clientId ),
};
},
[ clientId ]
);

const innerBlocksProps = useInnerBlocksProps( blockProps, {
allowedBlocks: [ 'core/page-list-item' ],
renderAppender: false,
__unstableDisableDropZone: true,
templateLock: isChildOfNavigation ? false : 'all',
onInput: NOOP,
onChange: NOOP,
value: blockList,
} );

const { selectBlock } = useDispatch( blockEditorStore );

useEffect( () => {
if ( hasSelectedChild || hasDraggedChild ) {
openModal();
selectBlock( parentBlock );
}
}, [
hasSelectedChild,
hasDraggedChild,
parentBlock,
selectBlock,
openModal,
] );

useEffect( () => {
setAttributes( { isNested } );
}, [ isNested ] );
}, [ isNested, setAttributes ] );

return (
<>
Expand Down

0 comments on commit e9bcf96

Please sign in to comment.