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

feat(recent-searches): add search highlighting #370

Merged
merged 4 commits into from
Nov 12, 2020
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
2 changes: 1 addition & 1 deletion bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
{
"path": "packages/autocomplete-plugin-recent-searches/dist/umd/index.production.js",
"maxSize": "2.5 kB"
"maxSize": "3 kB"
},
{
"path": "packages/autocomplete-plugin-query-suggestions/dist/umd/index.production.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
CreateRecentSearchesPluginParams,
RecentSearchesPluginData,
} from './createRecentSearchesPlugin';
import { RecentSearchesItem } from './types';
import { Highlighted, RecentSearchesItem } from './types';
import {
LOCAL_STORAGE_KEY,
createLocalStorage,
Expand Down Expand Up @@ -33,7 +33,7 @@ export type CreateRecentSearchesLocalStorageOptions<
/**
* Function to search in the recent items.
*/
search?(params: SearchParams<TItem>): TItem[];
search?(params: SearchParams<TItem>): Array<Highlighted<TItem>>;
};

type LocalStorageRecentSearchesPluginOptions<
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MaybePromise } from '@algolia/autocomplete-shared';

import { RecentSearchesItem } from './types/RecentSearchesItem';
import { Highlighted, RecentSearchesItem } from './types';

export type RecentSearchesStore<TItem extends RecentSearchesItem> = {
add(item: TItem): void;
Expand All @@ -11,7 +11,7 @@ export type RecentSearchesStore<TItem extends RecentSearchesItem> = {
export type RecentSearchesStorage<TItem extends RecentSearchesItem> = {
onAdd(item: TItem): void;
onRemove(id: string): void;
getAll(query?: string): MaybePromise<TItem[]>;
getAll(query?: string): MaybePromise<Array<Highlighted<TItem>>>;
};

export function createStore<TItem extends RecentSearchesItem>(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SourceTemplates } from '@algolia/autocomplete-js';
import { SourceTemplates, reverseHighlightHit } from '@algolia/autocomplete-js';

import { recentIcon, resetIcon } from './icons';
import { RecentSearchesItem } from './types';
Expand All @@ -19,7 +19,10 @@ export function getTemplates<TItem extends RecentSearchesItem>({
icon.innerHTML = recentIcon;
const title = document.createElement('div');
title.className = 'aa-ItemTitle';
title.innerText = item.query;
title.innerHTML = reverseHighlightHit({
hit: item as any,
attribute: 'query',
});
content.appendChild(icon);
content.appendChild(title);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type Highlighted<TItem> = TItem & {
_highlightResult: {
query: {
value: string;
};
};
};
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './Highlighted';
export * from './RecentSearchesItem';
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
import { RecentSearchesItem } from '../../types';
import { Highlighted, RecentSearchesItem } from '../../types';

type HighlightParams<TItem> = {
item: TItem;
query: string;
};

function highlight<TItem extends RecentSearchesItem>({
item,
query,
}: HighlightParams<TItem>): Highlighted<TItem> {
return {
...item,
_highlightResult: {
query: {
value: query
? item.query.replace(
new RegExp(query, 'g'),
`__aa-highlight__${query}__/aa-highlight__`
)
: item.query,
},
},
};
}

export type SearchParams<TItem> = {
query: string;
Expand All @@ -10,12 +34,13 @@ export function search<TItem extends RecentSearchesItem>({
query,
items,
limit,
}: SearchParams<TItem>) {
}: SearchParams<TItem>): Array<Highlighted<TItem>> {
if (!query) {
return items.slice(0, limit);
return items.slice(0, limit).map((item) => highlight({ item, query }));
}

return items
.filter((item) => item.query.toLowerCase().startsWith(query.toLowerCase()))
.slice(0, limit);
.filter((item) => item.query.toLowerCase().includes(query.toLowerCase()))
Copy link
Contributor

@Haroenv Haroenv Nov 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you use indexOf here (instead of includes), you already have the start index of the highlight & the end index will be that + the length of the query. You'd need to store this info temporarily on the object and remove that in highlight though

.slice(0, limit)
.map((item) => highlight({ item, query }));
}