File tree Expand file tree Collapse file tree 7 files changed +48
-12
lines changed
packages/autocomplete-plugin-recent-searches/src Expand file tree Collapse file tree 7 files changed +48
-12
lines changed Original file line number Diff line number Diff line change 1414 },
1515 {
1616 "path" : " packages/autocomplete-plugin-recent-searches/dist/umd/index.production.js" ,
17- "maxSize" : " 2.5 kB"
17+ "maxSize" : " 3 kB"
1818 },
1919 {
2020 "path" : " packages/autocomplete-plugin-query-suggestions/dist/umd/index.production.js" ,
Original file line number Diff line number Diff line change 55 CreateRecentSearchesPluginParams ,
66 RecentSearchesPluginData ,
77} from './createRecentSearchesPlugin' ;
8- import { RecentSearchesItem } from './types' ;
8+ import { Highlighted , RecentSearchesItem } from './types' ;
99import {
1010 LOCAL_STORAGE_KEY ,
1111 createLocalStorage ,
@@ -33,7 +33,7 @@ export type CreateRecentSearchesLocalStorageOptions<
3333 /**
3434 * Function to search in the recent items.
3535 */
36- search ?( params : SearchParams < TItem > ) : TItem [ ] ;
36+ search ?( params : SearchParams < TItem > ) : Array < Highlighted < TItem > > ;
3737} ;
3838
3939type LocalStorageRecentSearchesPluginOptions <
Original file line number Diff line number Diff line change 11import { MaybePromise } from '@algolia/autocomplete-shared' ;
22
3- import { RecentSearchesItem } from './types/RecentSearchesItem ' ;
3+ import { Highlighted , RecentSearchesItem } from './types' ;
44
55export type RecentSearchesStore < TItem extends RecentSearchesItem > = {
66 add ( item : TItem ) : void ;
@@ -11,7 +11,7 @@ export type RecentSearchesStore<TItem extends RecentSearchesItem> = {
1111export type RecentSearchesStorage < TItem extends RecentSearchesItem > = {
1212 onAdd ( item : TItem ) : void ;
1313 onRemove ( id : string ) : void ;
14- getAll ( query ?: string ) : MaybePromise < TItem [ ] > ;
14+ getAll ( query ?: string ) : MaybePromise < Array < Highlighted < TItem > > > ;
1515} ;
1616
1717export function createStore < TItem extends RecentSearchesItem > (
Original file line number Diff line number Diff line change 1- import { SourceTemplates } from '@algolia/autocomplete-js' ;
1+ import { SourceTemplates , reverseHighlightHit } from '@algolia/autocomplete-js' ;
22
33import { recentIcon , resetIcon } from './icons' ;
44import { RecentSearchesItem } from './types' ;
@@ -19,7 +19,10 @@ export function getTemplates<TItem extends RecentSearchesItem>({
1919 icon . innerHTML = recentIcon ;
2020 const title = document . createElement ( 'div' ) ;
2121 title . className = 'aa-ItemTitle' ;
22- title . innerText = item . query ;
22+ title . innerHTML = reverseHighlightHit ( {
23+ hit : item as any ,
24+ attribute : 'query' ,
25+ } ) ;
2326 content . appendChild ( icon ) ;
2427 content . appendChild ( title ) ;
2528
Original file line number Diff line number Diff line change 1+ export type Highlighted < TItem > = TItem & {
2+ _highlightResult : {
3+ query : {
4+ value : string ;
5+ } ;
6+ } ;
7+ } ;
Original file line number Diff line number Diff line change 1+ export * from './Highlighted' ;
12export * from './RecentSearchesItem' ;
Original file line number Diff line number Diff line change 1- import { RecentSearchesItem } from '../../types' ;
1+ import { Highlighted , RecentSearchesItem } from '../../types' ;
2+
3+ type HighlightParams < TItem > = {
4+ item : TItem ;
5+ query : string ;
6+ } ;
7+
8+ function highlight < TItem extends RecentSearchesItem > ( {
9+ item,
10+ query,
11+ } : HighlightParams < TItem > ) : Highlighted < TItem > {
12+ return {
13+ ...item ,
14+ _highlightResult : {
15+ query : {
16+ value : query
17+ ? item . query . replace (
18+ new RegExp ( query , 'g' ) ,
19+ `__aa-highlight__${ query } __/aa-highlight__`
20+ )
21+ : item . query ,
22+ } ,
23+ } ,
24+ } ;
25+ }
226
327export type SearchParams < TItem > = {
428 query : string ;
@@ -10,12 +34,13 @@ export function search<TItem extends RecentSearchesItem>({
1034 query,
1135 items,
1236 limit,
13- } : SearchParams < TItem > ) {
37+ } : SearchParams < TItem > ) : Array < Highlighted < TItem > > {
1438 if ( ! query ) {
15- return items . slice ( 0 , limit ) ;
39+ return items . slice ( 0 , limit ) . map ( ( item ) => highlight ( { item , query } ) ) ;
1640 }
1741
1842 return items
19- . filter ( ( item ) => item . query . toLowerCase ( ) . startsWith ( query . toLowerCase ( ) ) )
20- . slice ( 0 , limit ) ;
43+ . filter ( ( item ) => item . query . toLowerCase ( ) . includes ( query . toLowerCase ( ) ) )
44+ . slice ( 0 , limit )
45+ . map ( ( item ) => highlight ( { item, query } ) ) ;
2146}
You can’t perform that action at this time.
0 commit comments