Skip to content
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

Convert navigation link to use hooks and context. #28996

Merged
merged 2 commits into from
Feb 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 70 additions & 98 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@
* External dependencies
*/
import classnames from 'classnames';
import { escape, head } from 'lodash';
import { escape } from 'lodash';

/**
* WordPress dependencies
*/
import { compose } from '@wordpress/compose';
import { createBlock } from '@wordpress/blocks';
import {
useSelect,
useDispatch,
withDispatch,
withSelect,
} from '@wordpress/data';
import { useSelect, useDispatch } from '@wordpress/data';
import {
KeyboardShortcuts,
PanelBody,
Expand Down Expand Up @@ -125,24 +119,15 @@ function getSuggestionsQuery( type ) {
}
}

function NavigationLinkEdit( {
export default function NavigationLinkEdit( {
attributes,
hasDescendants,
isSelected,
isImmediateParentOfSelectedBlock,
isParentOfSelectedBlock,
setAttributes,
showSubmenuIcon,
insertLinkBlock,
textColor,
backgroundColor,
inlineStyles,
selectedBlockHasDescendants,
userCanCreatePages = false,
userCanCreatePosts = false,
insertBlocksAfter,
mergeBlocks,
onReplace,
context,
clientId,
} ) {
const {
label,
Expand All @@ -157,18 +142,75 @@ function NavigationLinkEdit( {
url,
opensInNewTab,
};
const { textColor, backgroundColor, style, showSubmenuIcon } = context;
const { saveEntityRecord } = useDispatch( coreStore );
const { insertBlock } = useDispatch( blockEditorStore );
const [ isLinkOpen, setIsLinkOpen ] = useState( false );
const listItemRef = useRef( null );
const isDraggingWithin = useIsDraggingWithin( listItemRef );
const itemLabelPlaceholder = __( 'Add link…' );
const ref = useRef();

const isDraggingBlocks = useSelect(
( select ) => select( blockEditorStore ).isDraggingBlocks(),
[]
const {
isDraggingBlocks,
isParentOfSelectedBlock,
isImmediateParentOfSelectedBlock,
hasDescendants,
selectedBlockHasDescendants,
numberOfDescendants,
userCanCreatePages,
userCanCreatePosts,
} = useSelect(
( select ) => {
const {
getClientIdsOfDescendants,
hasSelectedInnerBlock,
getSelectedBlockClientId,
isDraggingBlocks: _isDraggingBlocks,
} = select( blockEditorStore );

const selectedBlockId = getSelectedBlockClientId();

const descendants = getClientIdsOfDescendants( [ clientId ] )
.length;

return {
isParentOfSelectedBlock: hasSelectedInnerBlock(
clientId,
true
),
isImmediateParentOfSelectedBlock: hasSelectedInnerBlock(
clientId,
false
),
hasDescendants: !! descendants,
selectedBlockHasDescendants: !! getClientIdsOfDescendants( [
selectedBlockId,
] )?.length,
numberOfDescendants: descendants,
isDraggingBlocks: _isDraggingBlocks(),
userCanCreatePages: select( coreStore ).canUser(
'create',
'pages'
),
userCanCreatePosts: select( coreStore ).canUser(
'create',
'posts'
),
};
},
[ clientId ]
);

/**
* Insert a link block when submenu is added.
*/
function insertLinkBlock() {
const insertionPoint = numberOfDescendants;
const blockToInsert = createBlock( 'core/navigation-link' );
insertBlock( blockToInsert, insertionPoint, clientId );
}

// Show the LinkControl on mount if the URL is empty
// ( When adding a new menu item)
// This can't be done in the useState call because it conflicts
Expand Down Expand Up @@ -256,15 +298,14 @@ function NavigationLinkEdit( {
'is-dragging-within': isDraggingWithin,
'has-link': !! url,
'has-child': hasDescendants,
'has-text-color': !! textColor || !! inlineStyles?.color?.text,
'has-text-color': !! textColor || !! style?.color?.text,
[ `has-${ textColor }-color` ]: !! textColor,
'has-background':
!! backgroundColor || !! inlineStyles?.color?.background,
'has-background': !! backgroundColor || !! style?.color?.background,
[ `has-${ backgroundColor }-background-color` ]: !! backgroundColor,
} ),
style: {
color: inlineStyles?.color?.text,
backgroundColor: inlineStyles?.color?.background,
color: style?.color?.text,
backgroundColor: style?.color?.background,
},
} );

Expand Down Expand Up @@ -445,7 +486,7 @@ function NavigationLinkEdit( {
</Popover>
) }
</div>
{ showSubmenuIcon && (
{ hasDescendants && showSubmenuIcon && (
<span className="wp-block-navigation-link__submenu-icon">
<ItemSubmenuIcon />
</span>
Expand All @@ -455,72 +496,3 @@ function NavigationLinkEdit( {
</Fragment>
);
}

export default compose( [
withSelect( ( select, ownProps ) => {
const {
getBlockAttributes,
getClientIdsOfDescendants,
hasSelectedInnerBlock,
getBlockParentsByBlockName,
getSelectedBlockClientId,
} = select( blockEditorStore );
const { clientId } = ownProps;
const rootBlock = head(
getBlockParentsByBlockName( clientId, 'core/navigation' )
);
const navigationBlockAttributes = getBlockAttributes( rootBlock );
const hasDescendants = !! getClientIdsOfDescendants( [ clientId ] )
.length;
const showSubmenuIcon =
!! navigationBlockAttributes.showSubmenuIcon && hasDescendants;
const inlineStyles = navigationBlockAttributes.style;
const isParentOfSelectedBlock = hasSelectedInnerBlock( clientId, true );
const isImmediateParentOfSelectedBlock = hasSelectedInnerBlock(
clientId,
false
);
const selectedBlockId = getSelectedBlockClientId();
const selectedBlockHasDescendants = !! getClientIdsOfDescendants( [
selectedBlockId,
] )?.length;

return {
isParentOfSelectedBlock,
isImmediateParentOfSelectedBlock,
hasDescendants,
selectedBlockHasDescendants,
showSubmenuIcon,
inlineStyles,
textColor: navigationBlockAttributes.textColor,
backgroundColor: navigationBlockAttributes.backgroundColor,
userCanCreatePages: select( coreStore ).canUser(
'create',
'pages'
),
userCanCreatePosts: select( coreStore ).canUser(
'create',
'posts'
),
};
} ),
withDispatch( ( dispatch, ownProps, registry ) => {
return {
insertLinkBlock() {
const { clientId } = ownProps;

const { insertBlock } = dispatch( blockEditorStore );

const { getClientIdsOfDescendants } = registry.select(
'core/block-editor'
);
const navItems = getClientIdsOfDescendants( [ clientId ] );
const insertionPoint = navItems.length ? navItems.length : 0;

const blockToInsert = createBlock( 'core/navigation-link' );

insertBlock( blockToInsert, insertionPoint, clientId );
},
};
} ),
] )( NavigationLinkEdit );