From f596d2e2183b61c18ba6601201f0f39674fe2010 Mon Sep 17 00:00:00 2001 From: Andrei Draganescu Date: Fri, 3 Mar 2023 13:47:06 +0200 Subject: [PATCH 1/2] open convert to links modal on select of a page item --- .../block-library/src/page-list-item/edit.js | 15 +++++- packages/block-library/src/page-list/edit.js | 53 +++++++++---------- 2 files changed, 38 insertions(+), 30 deletions(-) diff --git a/packages/block-library/src/page-list-item/edit.js b/packages/block-library/src/page-list-item/edit.js index 2c9a6a69d5e4a7..58c239276bf239 100644 --- a/packages/block-library/src/page-list-item/edit.js +++ b/packages/block-library/src/page-list-item/edit.js @@ -2,10 +2,10 @@ * External dependencies */ 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'; @@ -35,7 +35,12 @@ function useFrontPageId() { }, [] ); } -export default function PageListItemEdit( { context, attributes } ) { +export default function PageListItemEdit( { + context, + attributes, + isSelected, + setAttributes, +} ) { const { id, label, link, hasChildren } = attributes; const isNavigationChild = 'showSubmenuIcon' in context; const frontPageId = useFrontPageId(); @@ -50,6 +55,12 @@ export default function PageListItemEdit( { context, attributes } ) { const innerBlocksProps = useInnerBlocksProps( blockProps ); + useEffect( () => { + if ( isSelected ) { + setAttributes( { isSelected: true } ); + } + }, [ isSelected ] ); + return (
  • setOpen( true ); - const closeModal = () => setOpen( false ); - - return ( - <> - - - { __( 'Edit' ) } - - - { isOpen && ( - - ) } - - ); -} - export default function PageListEdit( { context, clientId, @@ -143,6 +120,9 @@ export default function PageListEdit( { setAttributes, } ) { const { parentPageID } = attributes; + const [ isOpen, setOpen ] = useState( false ); + const openModal = () => setOpen( true ); + const closeModal = () => setOpen( false ); const { records: pages, hasResolved: hasResolvedPages } = useEntityRecords( 'postType', @@ -269,7 +249,11 @@ export default function PageListEdit( { __unstableDisableDropZone: true, templateLock: 'all', onInput: NOOP, - onChange: NOOP, + onChange: () => { + if ( hasResolvedPages ) { + openModal(); + } + }, value: blockList, } ); @@ -325,10 +309,23 @@ export default function PageListEdit( { ) } { allowConvertToLinks && ( - + <> + + + { __( 'Edit' ) } + + + { isOpen && ( + + ) } + ) } Date: Fri, 3 Mar 2023 18:57:16 +0200 Subject: [PATCH 2/2] unlock page list inside navigation block prompt to convert on select or drag --- .../data/data-core-block-editor.md | 16 ++++- packages/block-editor/src/store/selectors.js | 19 ++++- .../block-library/src/page-list-item/edit.js | 14 +--- packages/block-library/src/page-list/edit.js | 71 +++++++++++++------ 4 files changed, 85 insertions(+), 35 deletions(-) diff --git a/docs/reference-guides/data/data-core-block-editor.md b/docs/reference-guides/data/data-core-block-editor.md index b0f60d9621d580..28e90fd7672e43 100644 --- a/docs/reference-guides/data/data-core-block-editor.md +++ b/docs/reference-guides/data/data-core-block-editor.md @@ -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. @@ -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 diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 5ba407d14b608c..17a2934227d7be 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -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( @@ -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" diff --git a/packages/block-library/src/page-list-item/edit.js b/packages/block-library/src/page-list-item/edit.js index 58c239276bf239..7e20cdc5ae2d21 100644 --- a/packages/block-library/src/page-list-item/edit.js +++ b/packages/block-library/src/page-list-item/edit.js @@ -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'; @@ -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(); @@ -55,12 +49,6 @@ export default function PageListItemEdit( { const innerBlocksProps = useInnerBlocksProps( blockProps ); - useEffect( () => { - if ( isSelected ) { - setAttributes( { isSelected: true } ); - } - }, [ isSelected ] ); - return (
  • setOpen( true ); + const openModal = useCallback( () => setOpen( true ), [] ); const closeModal = () => setOpen( false ); const { records: pages, hasResolved: hasResolvedPages } = useEntityRecords( @@ -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 ( <>