Skip to content
Merged
Show file tree
Hide file tree
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
43 changes: 31 additions & 12 deletions packages/block-editor/src/components/link-control/search-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,36 @@ import { safeDecodeURI, filterURLForDisplay, getPath } from '@wordpress/url';
import { pipe } from '@wordpress/compose';
import deprecated from '@wordpress/deprecated';

const ICONS_MAP = {
post: postList,
page,
post_tag: tag,
category,
attachment: file,
const TYPES = {
post: {
icon: postList,
label: __( 'Post' ),
},
page: {
icon: page,
label: __( 'Page' ),
},
post_tag: {
icon: tag,
label: __( 'Tag' ),
},
category: {
icon: category,
label: __( 'Category' ),
},
attachment: {
icon: file,
label: __( 'Attachment' ),
},
};

function SearchItemIcon( { isURL, suggestion } ) {
let icon = null;

if ( isURL ) {
icon = globe;
} else if ( suggestion.type in ICONS_MAP ) {
icon = ICONS_MAP[ suggestion.type ];
} else if ( suggestion.type in TYPES ) {
icon = TYPES[ suggestion.type ].icon;
if ( suggestion.type === 'page' ) {
if ( suggestion.isFrontPage ) {
icon = home;
Expand Down Expand Up @@ -149,15 +164,19 @@ export const LinkControlSearchItem = ( {

function getVisualTypeName( suggestion ) {
if ( suggestion.isFrontPage ) {
return 'front page';
return __( 'Front page' );
}

if ( suggestion.isBlogHome ) {
return 'blog home';
return __( 'Blog home' );
}

// Provide translated labels for built-in post types. Ideally, the API would return the localised CPT or taxonomy label.
if ( suggestion.type in TYPES ) {
return TYPES[ suggestion.type ].label;
}

// Rename 'post_tag' to 'tag'. Ideally, the API would return the localised CPT or taxonomy label.
return suggestion.type === 'post_tag' ? 'tag' : suggestion.type;
return suggestion.type;
}

export default LinkControlSearchItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ $block-editor-link-control-number-of-actions: 1;

.components-menu-item__shortcut {
color: $gray-700;
text-transform: capitalize;
white-space: nowrap; // tags shouldn't go over two lines.
}

Expand Down
16 changes: 14 additions & 2 deletions packages/block-editor/src/components/link-control/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ import {

const mockFetchSearchSuggestions = jest.fn();

function getExpectedVisualTypeName( type ) {
const builtInLabels = {
post: 'Post',
page: 'Page',
post_tag: 'Tag',
category: 'Category',
attachment: 'Attachment',
};

return builtInLabels[ type ] || type;
}

/**
* The call to the real method `fetchRichUrlData` is wrapped in a promise in order to make it cancellable.
* Therefore if we pass any value as the mock of `fetchRichUrlData` then ALL of the tests will require
Expand Down Expand Up @@ -510,7 +522,7 @@ describe( 'Searching for a link', () => {
firstSuggestion.title
);
expect( searchResultElements[ 0 ] ).toHaveTextContent(
firstSuggestion.type
getExpectedVisualTypeName( firstSuggestion.type )
);

// The fallback URL suggestion should not be shown when input is not URL-like.
Expand Down Expand Up @@ -2078,7 +2090,7 @@ describe( 'Post types', () => {

searchResultElements.forEach( ( resultItem, index ) => {
expect( resultItem ).toHaveTextContent(
fauxEntitySuggestions[ index ].type
getExpectedVisualTypeName( fauxEntitySuggestions[ index ].type )
);
} );
} );
Expand Down
Loading