-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Navigation: Use the ListView in the Navigation block inspector controls #49417
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c486ca6
Navigtion: Use the ListView in the Navigation block inspector controls
scruffian 2f0f8a6
add undefined block settings menu
scruffian 0977891
move list view back to the index
scruffian ccdc3fd
remove unlock
scruffian 2ebb4ed
revert
scruffian 7e0c4d9
revert
scruffian e946645
fix description
scruffian c23b638
update label for test
scruffian a073a19
more text changes to fix the tests for list view
scruffian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/block-library/src/navigation-link/use-inserted-block.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { store as blockEditorStore } from '@wordpress/block-editor'; | ||
|
||
export const useInsertedBlock = ( insertedBlockClientId ) => { | ||
const { insertedBlockAttributes, insertedBlockName } = useSelect( | ||
( select ) => { | ||
const { getBlockName, getBlockAttributes } = | ||
select( blockEditorStore ); | ||
|
||
return { | ||
insertedBlockAttributes: getBlockAttributes( | ||
insertedBlockClientId | ||
), | ||
insertedBlockName: getBlockName( insertedBlockClientId ), | ||
}; | ||
}, | ||
[ insertedBlockClientId ] | ||
); | ||
|
||
const { updateBlockAttributes } = useDispatch( blockEditorStore ); | ||
|
||
const setInsertedBlockAttributes = ( _updatedAttributes ) => { | ||
if ( ! insertedBlockClientId ) return; | ||
updateBlockAttributes( insertedBlockClientId, _updatedAttributes ); | ||
}; | ||
|
||
if ( ! insertedBlockClientId ) { | ||
return { | ||
insertedBlockAttributes: undefined, | ||
insertedBlockName: undefined, | ||
setInsertedBlockAttributes, | ||
}; | ||
} | ||
|
||
return { | ||
insertedBlockAttributes, | ||
insertedBlockName, | ||
setInsertedBlockAttributes, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ import { | |
Spinner, | ||
} from '@wordpress/components'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { useState, useEffect } from '@wordpress/element'; | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
|
||
/** | ||
|
@@ -23,9 +24,16 @@ import { unlock } from '../../private-apis'; | |
import DeletedNavigationWarning from './deleted-navigation-warning'; | ||
import useNavigationMenu from '../use-navigation-menu'; | ||
import LeafMoreMenu from './leaf-more-menu'; | ||
import { updateAttributes } from '../../navigation-link/update-attributes'; | ||
import { LinkUI } from '../../navigation-link/link-ui'; | ||
import { useInsertedBlock } from '../../navigation-link/use-inserted-block'; | ||
|
||
/* translators: %s: The name of a menu. */ | ||
const actionLabel = __( "Switch to '%s'" ); | ||
const BLOCKS_WITH_LINK_UI_SUPPORT = [ | ||
'core/navigation-link', | ||
'core/navigation-submenu', | ||
]; | ||
|
||
const MainContent = ( { | ||
clientId, | ||
|
@@ -34,7 +42,7 @@ const MainContent = ( { | |
isNavigationMenuMissing, | ||
onCreateNew, | ||
} ) => { | ||
const { OffCanvasEditor } = unlock( blockEditorPrivateApis ); | ||
const { PrivateListView } = unlock( blockEditorPrivateApis ); | ||
|
||
// Provide a hierarchy of clientIds for the given Navigation block (clientId). | ||
// This is required else the list view will display the entire block tree. | ||
|
@@ -45,6 +53,42 @@ const MainContent = ( { | |
}, | ||
[ clientId ] | ||
); | ||
|
||
const [ clientIdWithOpenLinkUI, setClientIdWithOpenLinkUI ] = useState(); | ||
const { lastInsertedBlockClientId } = useSelect( ( select ) => { | ||
const { getLastInsertedBlocksClientIds } = unlock( | ||
select( blockEditorStore ) | ||
); | ||
const lastInsertedBlocksClientIds = getLastInsertedBlocksClientIds(); | ||
return { | ||
lastInsertedBlockClientId: | ||
lastInsertedBlocksClientIds && lastInsertedBlocksClientIds[ 0 ], | ||
}; | ||
}, [] ); | ||
|
||
const { | ||
insertedBlockAttributes, | ||
insertedBlockName, | ||
setInsertedBlockAttributes, | ||
} = useInsertedBlock( lastInsertedBlockClientId ); | ||
|
||
const hasExistingLinkValue = insertedBlockAttributes?.url; | ||
|
||
useEffect( () => { | ||
if ( | ||
lastInsertedBlockClientId && | ||
BLOCKS_WITH_LINK_UI_SUPPORT?.includes( insertedBlockName ) && | ||
! hasExistingLinkValue // don't re-show the Link UI if the block already has a link value. | ||
) { | ||
setClientIdWithOpenLinkUI( lastInsertedBlockClientId ); | ||
} | ||
}, [ | ||
lastInsertedBlockClientId, | ||
clientId, | ||
insertedBlockName, | ||
hasExistingLinkValue, | ||
] ); | ||
|
||
const { navigationMenu } = useNavigationMenu( currentMenuId ); | ||
|
||
if ( currentMenuId && isNavigationMenuMissing ) { | ||
|
@@ -59,19 +103,46 @@ const MainContent = ( { | |
? sprintf( | ||
/* translators: %s: The name of a menu. */ | ||
__( 'Structure for navigation menu: %s' ), | ||
navigationMenu?.title || __( 'Untitled menu' ) | ||
navigationMenu?.title?.rendered || __( 'Untitled menu' ) | ||
) | ||
: __( | ||
'You have not yet created any menus. Displaying a list of your Pages' | ||
); | ||
|
||
const renderLinkUI = ( block ) => { | ||
return ( | ||
clientIdWithOpenLinkUI === block.clientId && ( | ||
<LinkUI | ||
clientId={ lastInsertedBlockClientId } | ||
link={ insertedBlockAttributes } | ||
onClose={ () => setClientIdWithOpenLinkUI( null ) } | ||
hasCreateSuggestion={ false } | ||
onChange={ ( updatedValue ) => { | ||
updateAttributes( | ||
updatedValue, | ||
setInsertedBlockAttributes, | ||
insertedBlockAttributes | ||
); | ||
setClientIdWithOpenLinkUI( null ); | ||
} } | ||
onCancel={ () => setClientIdWithOpenLinkUI( null ) } | ||
/> | ||
) | ||
); | ||
}; | ||
|
||
return ( | ||
<OffCanvasEditor | ||
blocks={ clientIdsTree } | ||
parentClientId={ clientId } | ||
isExpanded={ true } | ||
LeafMoreMenu={ LeafMoreMenu } | ||
description={ description } | ||
/> | ||
<div className="wp-block-navigation__menu-inspector-controls"> | ||
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. Can we avoid this extra div? 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. no, we need the classname on a div, not the table element for the horizontal scroll to work |
||
<PrivateListView | ||
blocks={ clientIdsTree } | ||
rootClientId={ clientId } | ||
isExpanded | ||
description={ description } | ||
showAppender | ||
blockSettingsMenu={ LeafMoreMenu } | ||
renderAdditionalBlockUI={ renderLinkUI } | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 removed this in my commit because
BlockSettingsDropdown
is the default value, and it meant that there were fewer things that needed to be imported here.This change does mean that it can be overridden by props passed to
ListView
now, so if that shouldn't be possible, then it needs to be added back asundefined
or something.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.
Yeah we can't do that because the point it to restrict this from being prop being used by third parties.
undefined
should work though, I'll push that now.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.
Done in 74a8ac2