Skip to content

Commit

Permalink
WIP: Navigation Link variations
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks committed Aug 20, 2020
1 parent 3f89946 commit daac2ce
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 65 deletions.
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/link-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ function LinkControl( {
createSuggestion,
withCreateSuggestion,
inputValue: propInputValue = '',
suggestionsQuery = {},
} ) {
if ( withCreateSuggestion === undefined && createSuggestion ) {
withCreateSuggestion = true;
Expand Down Expand Up @@ -209,6 +210,7 @@ function LinkControl( {
showInitialSuggestions={ showInitialSuggestions }
allowDirectEntry={ ! noDirectEntry }
showSuggestions={ showSuggestions }
suggestionsQuery={ suggestionsQuery }
>
<div className="block-editor-link-control__search-actions">
<Button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ const LinkControlSearchInput = forwardRef(
fetchSuggestions = null,
allowDirectEntry = true,
showInitialSuggestions = false,
suggestionsQuery = {},
},
ref
) => {
const genericSearchHandler = useSearchHandler(
suggestionsQuery,
allowDirectEntry,
withCreateSuggestion
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,18 @@ export const handleDirectEntry = ( val ) => {
] );
};

export const handleEntitySearch = async (
const handleEntitySearch = async (
val,
args,
{ isInitialSuggestions, type, subtype },
fetchSearchSuggestions,
directEntryHandler,
withCreateSuggestion
) => {
let results = await Promise.all( [
fetchSearchSuggestions( val, {
...( args.isInitialSuggestions ? { perPage: 3 } : {} ),
...( isInitialSuggestions
? { perPage: 3, type, subtype }
: { type, subtype } ),
} ),
directEntryHandler( val ),
] );
Expand All @@ -65,12 +67,12 @@ export const handleEntitySearch = async (
// just for good measure. That way once the actual results run out we always
// have a URL option to fallback on.
results =
couldBeURL && ! args.isInitialSuggestions
couldBeURL && ! isInitialSuggestions
? results[ 0 ].concat( results[ 1 ] )
: results[ 0 ];

// If displaying initial suggestions just return plain results.
if ( args.isInitialSuggestions ) {
if ( isInitialSuggestions ) {
return results;
}

Expand Down Expand Up @@ -101,6 +103,7 @@ export const handleEntitySearch = async (
};

export default function useSearchHandler(
{ type, subtype },
allowDirectEntry,
withCreateSuggestion
) {
Expand All @@ -117,12 +120,12 @@ export default function useSearchHandler(
: handleNoop;

return useCallback(
( val, args ) => {
( val, { isInitialSuggestions } ) => {
return isURLLike( val )
? directEntryHandler( val, args )
? directEntryHandler( val, { isInitialSuggestions } )
: handleEntitySearch(
val,
args,
{ isInitialSuggestions, type, subtype },
fetchSearchSuggestions,
directEntryHandler,
withCreateSuggestion
Expand Down
8 changes: 7 additions & 1 deletion packages/block-library/src/navigation-link/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@
"rel": {
"type": "string"
},
"id": {
"objectType": {
"type": "string"
},
"objectName": {
"type": "string"
},
"objectId": {
"type": "number"
},
"opensInNewTab": {
Expand Down
31 changes: 27 additions & 4 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function NavigationLinkEdit( {
mergeBlocks,
onReplace,
} ) {
const { label, opensInNewTab, url, description, rel } = attributes;
const { label, opensInNewTab, url, description, rel, type } = attributes;
const link = {
url,
opensInNewTab,
Expand Down Expand Up @@ -119,8 +119,7 @@ function NavigationLinkEdit( {
}

async function handleCreatePage( pageTitle ) {
const type = 'page';
const page = await saveEntityRecord( 'postType', type, {
const page = await saveEntityRecord( 'postType', 'page', {
title: pageTitle,
status: 'publish',
} );
Expand Down Expand Up @@ -234,10 +233,32 @@ function NavigationLinkEdit( {
showInitialSuggestions={ true }
withCreateSuggestion={ userCanCreatePages }
createSuggestion={ handleCreatePage }
suggestionsQuery={ ( () => {
switch ( type ) {
case 'post':
case 'page':
return {
type: 'post',
subtype: type,
};
case 'category':
case 'tag':
return {
type: 'term',
subtype:
type === 'category'
? 'category'
: 'post_tag',
};
default:
return {};
}
} )() }
onChange={ ( {
title: newTitle = '',
url: newURL = '',
opensInNewTab: newOpensInNewTab,
type,
id,
} = {} ) =>
setAttributes( {
Expand Down Expand Up @@ -265,7 +286,9 @@ function NavigationLinkEdit( {
return escape( normalizedURL );
} )(),
opensInNewTab: newOpensInNewTab,
id,
objectType: 'post_type',
objectName: type,
objectId: id,
} )
}
/>
Expand Down
40 changes: 40 additions & 0 deletions packages/block-library/src/navigation-link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,56 @@ export { metadata, name };

export const settings = {
title: __( 'Link' ),

icon,

description: __( 'Add a page, link, or another item to your navigation.' ),

variations: [
{
name: 'link',
isDefault: true,
title: __( 'Link' ),
description: __( 'A link to a URL.' ),
attributes: {},
},
{
name: 'post',
title: __( 'Post' ),
description: __( 'A link to a post.' ),
attributes: { type: 'post' },
},
{
name: 'page',
title: __( 'Page' ),
description: __( 'A link to a page.' ),
attributes: { type: 'page' },
},
{
name: 'category',
title: __( 'Category' ),
description: __( 'A link to a category.' ),
attributes: { type: 'category' },
},
{
name: 'tag',
title: __( 'Tag' ),
description: __( 'A link to a tag.' ),
attributes: { type: 'tag' },
},
],

__experimentalLabel: ( { label } ) => label,

merge( leftAttributes, { label: rightLabel = '' } ) {
return {
...leftAttributes,
label: leftAttributes.label + rightLabel,
};
},

edit,

save,

deprecated: [
Expand Down
24 changes: 21 additions & 3 deletions packages/block-library/src/navigation-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,33 @@ function render_block_core_navigation_link( $attributes, $content, $block ) {
'<a class="wp-block-navigation-link__content" ';

// Start appending HTML attributes to anchor tag.
if ( isset( $attributes['url'] ) ) {
$html .= ' href="' . esc_url( $attributes['url'] ) . '"';

if (
isset( $attributes['objectType'] ) &&
isset( $attributes['objectName'] ) &&
isset( $attributes['objectId'] )
) {
if ( 'post_type' === $attributes['objectType'] ) {
$url = get_permalink( $attributes['objectId'] );
} elseif ( 'taxonomy' === $attributes['objectType'] ) {
$url = get_term_link( $attributes['objectId'] );
} elseif ( 'post_type_archive' === $attributes['objectType'] ) {
$url = get_post_type_archive_link( $attributes['objectName'] );
}
}

if ( ! isset( $url ) && isset( $attributes['url'] ) ) {
$url = $attributes['url'];
}

if ( isset( $url ) ) {
$html .= ' href="' . esc_url( $url ) . '"';
}

if ( isset( $attributes['opensInNewTab'] ) && true === $attributes['opensInNewTab'] ) {
$html .= ' target="_blank" ';
}

// Start appending HTML attributes to anchor tag.
if ( isset( $attributes['rel'] ) ) {
$html .= ' rel="' . esc_attr( $attributes['rel'] ) . '"';
} elseif ( isset( $attributes['nofollow'] ) && $attributes['nofollow'] ) {
Expand Down
7 changes: 5 additions & 2 deletions packages/block-library/src/navigation/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useRef } from '@wordpress/element';
import { useRef, useState } from '@wordpress/element';
import {
InnerBlocks,
InspectorControls,
Expand Down Expand Up @@ -72,6 +72,8 @@ function Navigation( {
clientId
);

const [ isPlaceholderAllowed, setIsPlaceholderAllowed ] = useState( true );

//
// HANDLERS
//
Expand All @@ -86,12 +88,13 @@ function Navigation( {
}

// If we don't have existing items then show the Placeholder
if ( ! hasExistingNavItems ) {
if ( ! hasExistingNavItems && isPlaceholderAllowed ) {
return (
<Block.div>
<NavigationPlaceholder
ref={ ref }
onCreate={ ( blocks, selectNavigationBlock ) => {
setIsPlaceholderAllowed( false );
updateInnerBlocks( blocks );
if ( selectNavigationBlock ) {
selectBlock( clientId );
Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/navigation/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ $navigation-item-height: 46px;
}

// Polish the Appender.
.wp-block-navigation__container {
min-height: $navigation-item-height;
}

.wp-block-navigation .block-list-appender {
margin: 0;
display: flex;
Expand Down
Loading

0 comments on commit daac2ce

Please sign in to comment.