Skip to content

Commit

Permalink
fix: various search fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Mar 5, 2018
1 parent 1ff2bd8 commit b797c96
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/common-elements/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from './dropdown';
export * from './mixins';
export * from './tabs';
export * from './samples';
export * from './perfect-scrollbar';
1 change: 1 addition & 0 deletions src/components/Endpoint/styled.elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import styled, { withProps } from '../../styled-components';
export const OperationEndpointWrap = styled.div`
cursor: pointer;
position: relative;
margin-bottom: 5px;
`;

export const ServerRelativeURL = styled.span`
Expand Down
2 changes: 1 addition & 1 deletion src/components/Redoc/Redoc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Redoc extends React.Component<RedocProps> {
};

componentDidMount() {
this.props.store.menu.updateOnHash();
this.props.store.onDidMount();
}

componentWillUnmount() {
Expand Down
25 changes: 19 additions & 6 deletions src/components/SearchBox/SearchBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,20 @@ export interface SearchBoxProps {
marker: MarkerService;
getItemById: (id: string) => IMenuItem | undefined;
onActivate: (item: IMenuItem) => void;

className?: string;
}

export interface SearchBoxState {
results: any;
term: string;
}

interface SearchResult {
item: IMenuItem;
score: number;
}

export class SearchBox extends React.PureComponent<SearchBoxProps, SearchBoxState> {
constructor(props) {
super(props);
Expand Down Expand Up @@ -145,8 +152,14 @@ export class SearchBox extends React.PureComponent<SearchBoxProps, SearchBoxStat
};

render() {
const items: IMenuItem[] = this.state.results.map(res => this.props.getItemById(res.id));
items.sort((a, b) => (a.depth > b.depth ? 1 : a.depth < b.depth ? -1 : 0));
const results: SearchResult[] = this.state.results.map(res => ({
item: this.props.getItemById(res.id),
score: res.score,
}));
results.sort(
(a, b) =>
a.item.depth > b.item.depth ? 1 : a.item.depth < b.item.depth ? -1 : b.score - a.score,
);

return (
<div>
Expand All @@ -158,14 +171,14 @@ export class SearchBox extends React.PureComponent<SearchBoxProps, SearchBoxStat
type="text"
onChange={this.search}
/>
{items.length > 0 && (
{results.length > 0 && (
<SearchResultsBox>
{items.map(item => (
{results.map(res => (
<MenuItem
item={item}
item={res.item}
onActivate={this.props.onActivate}
withoutChildren={true}
key={item.id}
key={res.item.id}
/>
))}
</SearchResultsBox>
Expand Down
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './RedocStandalone';
export * from './Redoc/Redoc';
// export * from './Redoc/elements';
export * from './Schema/';
export * from './SearchBox/SearchBox';
export * from './Operation/Operation';
export * from './RedocStandalone';

Expand Down
10 changes: 8 additions & 2 deletions src/services/AppStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,21 @@ export class AppStore {
this.spec = new SpecStore(spec, specUrl, this.options);
this.menu = new MenuStore(this.spec, this.scroll);

this.search = new SearchStore(this.spec);
this.search = new SearchStore();
this.search.indexItems(this.menu.items);
this.search.done();

this.disposer = observe(this.menu, 'activeItemIdx', change => {
this.updateMarkOnMenu(change.newValue as number);
});
}

onDidMount() {
this.menu.updateOnHash();
this.updateMarkOnMenu(this.menu.activeItemIdx);
}

updateMarkOnMenu(idx: number) {
console.log('update marker');
const start = Math.max(0, idx);
const end = Math.min(this.menu.flatItems.length, start + 5);

Expand Down
1 change: 0 additions & 1 deletion src/services/MarkerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export class MarkerService {
}

mark(term?: string) {
console.log('mark', term);
if (!term && !this.prevTerm) return;
this.map.forEach(val => {
val.unmark();
Expand Down
1 change: 1 addition & 0 deletions src/services/MenuStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface IMenuItem {
id: string;
absoluteIdx?: number;
name: string;
description?: string;
depth: number;
active: boolean;
items: IMenuItem[];
Expand Down
13 changes: 5 additions & 8 deletions src/services/SearchStore.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import { SpecStore } from '../index';
import { GroupModel, OperationModel } from './models';
import { OperationModel } from './models';
import worker from './SearchWorker.worker';
import { IMenuItem } from './MenuStore';

export class SearchStore {
searchWorker = new worker();

constructor(private spec: SpecStore) {
this.indexGroups(this.spec.operationGroups);
this.done();
}
constructor() {}

indexGroups(groups: Array<GroupModel | OperationModel>) {
indexItems(groups: Array<IMenuItem | OperationModel>) {
groups.forEach(group => {
if (group.type !== 'group') {
this.add(group.name, group.description || '', group.id);
}
this.indexGroups(group.items);
this.indexItems(group.items);
});
}

Expand Down
8 changes: 6 additions & 2 deletions src/services/SearchWorker.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export interface SearchDocument {
id: string;
}

export interface SearchResult extends SearchDocument {
score: number;
}

const store: { [id: string]: SearchDocument } = {};

let resolveIndex: (v: lunr.Index) => void;
Expand All @@ -39,7 +43,7 @@ export async function done() {
resolveIndex(builder.build());
}

export async function search(q: string): Promise<SearchDocument[]> {
export async function search(q: string): Promise<SearchResult[]> {
if (q.trim().length === 0) {
return [];
}
Expand All @@ -54,5 +58,5 @@ export async function search(q: string): Promise<SearchDocument[]> {
t.term(exp, {});
});
})
.map(res => store[res.ref]);
.map(res => ({ ...store[res.ref], score: res.score }));
}

0 comments on commit b797c96

Please sign in to comment.