-
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.
Site Editor: Add search to templates and template parts in the Naviga…
…tion Panel (#26665) Add search functionality to Templates and Template parts menus in the Navigation panel.
- Loading branch information
1 parent
d3ded78
commit 54b6de8
Showing
5 changed files
with
152 additions
and
29 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
73 changes: 73 additions & 0 deletions
73
packages/edit-site/src/components/navigation-sidebar/navigation-panel/search-results.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,73 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { map } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useMemo } from '@wordpress/element'; | ||
import { __experimentalNavigationGroup as NavigationGroup } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { normalizedSearch } from './utils'; | ||
import { useSelect } from '@wordpress/data'; | ||
import TemplateNavigationItem from './template-navigation-item'; | ||
|
||
export default function SearchResults( { items, search } ) { | ||
const itemType = items?.length > 0 ? items[ 0 ].type : null; | ||
|
||
const itemInfos = useSelect( | ||
( select ) => { | ||
if ( itemType === 'wp_template' ) { | ||
const { | ||
__experimentalGetTemplateInfo: getTemplateInfo, | ||
} = select( 'core/editor' ); | ||
|
||
return items.map( ( item ) => ( { | ||
slug: item.slug, | ||
...getTemplateInfo( item ), | ||
} ) ); | ||
} | ||
|
||
return items.map( ( item ) => ( { | ||
slug: item.slug, | ||
title: item.title?.rendered, | ||
description: item.excerpt?.rendered, | ||
} ) ); | ||
}, | ||
[ items, itemType ] | ||
); | ||
|
||
const itemsFiltered = useMemo( () => { | ||
if ( items === null || search.length === 0 ) { | ||
return []; | ||
} | ||
|
||
return items.filter( ( { slug } ) => { | ||
const { title, description } = itemInfos.find( | ||
( info ) => info.slug === slug | ||
); | ||
|
||
return ( | ||
normalizedSearch( slug, search ) || | ||
normalizedSearch( title, search ) || | ||
normalizedSearch( description, search ) | ||
); | ||
} ); | ||
}, [ items, itemInfos, search ] ); | ||
|
||
return ( | ||
<NavigationGroup title={ __( 'Search results' ) }> | ||
{ map( itemsFiltered, ( item ) => ( | ||
<TemplateNavigationItem | ||
item={ item } | ||
key={ `${ item.type }-${ item.id }` } | ||
/> | ||
) ) } | ||
</NavigationGroup> | ||
); | ||
} |
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
11 changes: 11 additions & 0 deletions
11
packages/edit-site/src/components/navigation-sidebar/navigation-panel/utils.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,11 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { deburr } from 'lodash'; | ||
|
||
// @see packages/block-editor/src/components/inserter/search-items.js | ||
export const normalizeInput = ( input ) => | ||
deburr( input ).replace( /^\//, '' ).toLowerCase(); | ||
|
||
export const normalizedSearch = ( title, search ) => | ||
-1 !== normalizeInput( title ).indexOf( normalizeInput( search ) ); |