-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Open convert to links modal on select of a page item #48723
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…or drag
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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( | ||
|
|
@@ -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, { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @draganescu and I looked into passing the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another option could have been creating a context just for this scenario that the data needed in the child could have access to, but that also seems a little convoluted.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think it's worth looking into modifying the system for inner blocks? |
||
| 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 ( | ||
| <> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can also do this inline in the
useSelectcallback and avoid introducing a new selector for just one use case.Here's a similar example:
gutenberg/packages/edit-site/src/components/save-button/index.js
Lines 23 to 25 in d19dbd6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want this new selector, it's as useful as
hasSelectedInnerBlockand this is a perfect opportunity to introduce it via using it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just sharing my personal preference of avoiding adding selector to packages scope, when I can :)