-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Navigation Editor: Allow menu renaming (#29012)
* feature to change navigation menu name * name editor component added * add missing styles| * remove doubled hooks * remove ; * navigation post variable updated * use-menu-locations hook moved to hooks directory * console error fixed * name edition moved to the sidebar. In toolbar manu name changed to button, when clicked name editor is focused * move NameEditor to InspectorControls in InspectorAdditions in order to preserve the same order of items in navbar * use TextControl component in name-editor * changes according to CR * add missing file * changes according to CR: removed aria-label from name-display/index.js and added aria-label in /name-editor/index.js * Dispatch save requests sequentially instead of in parallel * explicitly set () => setIsMenuNameEditFocused( true ) * add translation to the file * resolved conflicts with trunk * resolved conflicts with trunk 2 * sprintf added * Update unit test to take into account the call to saveEditedEntityRecord Co-authored-by: grzegorz_marzencki <[email protected]> Co-authored-by: Daniel Richards <[email protected]>
- Loading branch information
1 parent
e8ffe04
commit e8c6106
Showing
28 changed files
with
606 additions
and
274 deletions.
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
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
37 changes: 37 additions & 0 deletions
37
packages/edit-navigation/src/components/name-display/index.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,37 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { ToolbarGroup, ToolbarButton } from '@wordpress/components'; | ||
import { BlockControls } from '@wordpress/block-editor'; | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
useSelectedMenuData, | ||
IsMenuNameControlFocusedContext, | ||
} from '../../hooks'; | ||
import { useContext } from '@wordpress/element'; | ||
|
||
import { sprintf, __ } from '@wordpress/i18n'; | ||
export default function NameDisplay() { | ||
const { menuName } = useSelectedMenuData(); | ||
const [ , setIsMenuNameEditFocused ] = useContext( | ||
IsMenuNameControlFocusedContext | ||
); | ||
return ( | ||
<BlockControls> | ||
<ToolbarGroup> | ||
<ToolbarButton | ||
aria-label={ sprintf( | ||
// translators: %s: the name of a menu. | ||
__( `Edit menu name: %s` ), | ||
menuName | ||
) } | ||
onClick={ () => setIsMenuNameEditFocused( true ) } | ||
> | ||
{ menuName } | ||
</ToolbarButton> | ||
</ToolbarGroup> | ||
</BlockControls> | ||
); | ||
} |
44 changes: 44 additions & 0 deletions
44
packages/edit-navigation/src/components/name-editor/index.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,44 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { useEffect, useRef, useContext } from '@wordpress/element'; | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { TextControl } from '@wordpress/components'; | ||
import { | ||
IsMenuNameControlFocusedContext, | ||
useMenuEntity, | ||
useSelectedMenuData, | ||
} from '../../hooks'; | ||
|
||
export function NameEditor() { | ||
const [ isMenuNameEditFocused, setIsMenuNameEditFocused ] = useContext( | ||
IsMenuNameControlFocusedContext | ||
); | ||
|
||
const { menuId } = useSelectedMenuData(); | ||
const { editMenuName, editedMenuName } = useMenuEntity( menuId ); | ||
const inputRef = useRef(); | ||
useEffect( () => { | ||
if ( isMenuNameEditFocused ) inputRef.current.focus(); | ||
}, [ isMenuNameEditFocused ] ); | ||
return ( | ||
<> | ||
<TextControl | ||
ref={ inputRef } | ||
help={ __( | ||
'A short, descriptive name used to refer to this menu elsewhere.' | ||
) } | ||
label={ __( 'Name' ) } | ||
onBlur={ () => setIsMenuNameEditFocused( false ) } | ||
className="edit-navigation-name-editor__text-control" | ||
value={ editedMenuName } | ||
onChange={ ( value ) => { | ||
editMenuName( value ); | ||
} } | ||
/> | ||
</> | ||
); | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/edit-navigation/src/components/name-editor/style.scss
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,5 @@ | ||
.edit-navigation-name-editor__text-control { | ||
.components-base-control { | ||
border: none; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
packages/edit-navigation/src/filters/add-menu-name-editor.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,31 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { addFilter } from '@wordpress/hooks'; | ||
import { createHigherOrderComponent } from '@wordpress/compose'; | ||
import NameDisplay from '../components/name-display'; | ||
|
||
const addMenuNameEditor = createHigherOrderComponent( | ||
( BlockEdit ) => ( props ) => { | ||
if ( props.name !== 'core/navigation' ) { | ||
return <BlockEdit { ...props } />; | ||
} | ||
return ( | ||
<> | ||
<BlockEdit { ...props } /> | ||
<NameDisplay /> | ||
</> | ||
); | ||
}, | ||
'withMenuName' | ||
); | ||
|
||
export default () => | ||
addFilter( | ||
'editor.BlockEdit', | ||
'core/edit-navigation/with-menu-name', | ||
addMenuNameEditor | ||
); |
22 changes: 22 additions & 0 deletions
22
packages/edit-navigation/src/filters/disable-inserting-non-navigation-blocks.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,22 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { addFilter } from '@wordpress/hooks'; | ||
/** | ||
* External dependencies | ||
*/ | ||
import { set } from 'lodash'; | ||
|
||
function disableInsertingNonNavigationBlocks( settings, name ) { | ||
if ( ! [ 'core/navigation', 'core/navigation-link' ].includes( name ) ) { | ||
set( settings, [ 'supports', 'inserter' ], false ); | ||
} | ||
return settings; | ||
} | ||
|
||
export default () => | ||
addFilter( | ||
'blocks.registerBlockType', | ||
'core/edit-navigation/disable-inserting-non-navigation-blocks', | ||
disableInsertingNonNavigationBlocks | ||
); |
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,18 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import addMenuNameEditor from './add-menu-name-editor'; | ||
import disableInsertingNonNavigationBlocks from './disable-inserting-non-navigation-blocks'; | ||
import removeEditUnsupportedFeatures from './remove-edit-unsupported-features'; | ||
import removeSettingsUnsupportedFeatures from './remove-settings-unsupported-features'; | ||
|
||
export const addFilters = ( | ||
shouldAddDisableInsertingNonNavigationBlocksFilter | ||
) => { | ||
addMenuNameEditor(); | ||
if ( shouldAddDisableInsertingNonNavigationBlocksFilter ) { | ||
disableInsertingNonNavigationBlocks(); | ||
} | ||
removeEditUnsupportedFeatures(); | ||
removeSettingsUnsupportedFeatures(); | ||
}; |
30 changes: 30 additions & 0 deletions
30
packages/edit-navigation/src/filters/remove-edit-unsupported-features.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,30 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { addFilter } from '@wordpress/hooks'; | ||
import { createHigherOrderComponent } from '@wordpress/compose'; | ||
|
||
const removeNavigationBlockEditUnsupportedFeatures = createHigherOrderComponent( | ||
( BlockEdit ) => ( props ) => { | ||
if ( props.name !== 'core/navigation' ) { | ||
return <BlockEdit { ...props } />; | ||
} | ||
|
||
return ( | ||
<BlockEdit | ||
{ ...props } | ||
hasSubmenuIndicatorSetting={ false } | ||
hasItemJustificationControls={ false } | ||
hasListViewModal={ false } | ||
/> | ||
); | ||
}, | ||
'removeNavigationBlockEditUnsupportedFeatures' | ||
); | ||
|
||
export default () => | ||
addFilter( | ||
'editor.BlockEdit', | ||
'core/edit-navigation/remove-navigation-block-edit-unsupported-features', | ||
removeNavigationBlockEditUnsupportedFeatures | ||
); |
28 changes: 28 additions & 0 deletions
28
packages/edit-navigation/src/filters/remove-settings-unsupported-features.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,28 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { addFilter } from '@wordpress/hooks'; | ||
|
||
function removeNavigationBlockSettingsUnsupportedFeatures( settings, name ) { | ||
if ( name !== 'core/navigation' ) { | ||
return settings; | ||
} | ||
|
||
return { | ||
...settings, | ||
supports: { | ||
customClassName: false, | ||
html: false, | ||
inserter: true, | ||
}, | ||
// Remove any block variations. | ||
variations: undefined, | ||
}; | ||
} | ||
|
||
export default () => | ||
addFilter( | ||
'blocks.registerBlockType', | ||
'core/edit-navigation/remove-navigation-block-settings-unsupported-features', | ||
removeNavigationBlockSettingsUnsupportedFeatures | ||
); |
Oops, something went wrong.