-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
486 additions
and
54 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type {Router} from '@diplodoc/components'; | ||
|
||
import {createContext, useContext} from 'react'; | ||
|
||
export interface RouterConfig extends Router { | ||
depth: number; | ||
} | ||
|
||
export const RouterContext = createContext<RouterConfig>({ | ||
pathname: '/', | ||
depth: 0, | ||
}); | ||
|
||
RouterContext.displayName = 'RouterContext'; | ||
|
||
export const RouterProvider = RouterContext.Provider; | ||
|
||
export function useRouter() { | ||
return useContext(RouterContext); | ||
} |
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,5 @@ | ||
import React from 'react'; | ||
|
||
export const Search = () => { | ||
return <div>Initial</div>; | ||
}; |
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,17 @@ | ||
.Suggest { | ||
margin-right: 20px; | ||
min-width: 200px; | ||
transition: min-width 0.3s; | ||
|
||
.dc-root_focused-search & { | ||
min-width: 500px; | ||
} | ||
|
||
&__Item { | ||
&__Marker { | ||
background: var(--g-color-base-neutral-medium); | ||
padding: 0 3px 1px; | ||
border-radius: 4px; | ||
} | ||
} | ||
} |
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,41 @@ | ||
import type {ISearchProvider, SearchSuggestApi} from '@diplodoc/components'; | ||
|
||
import React, {useCallback, useRef} from 'react'; | ||
import {SearchSuggest} from '@diplodoc/components'; | ||
|
||
import {updateRootClassName} from '../../utils'; | ||
|
||
import {useProvider} from './useProvider'; | ||
import './Suggest.scss'; | ||
|
||
export function Suggest() { | ||
const provider: ISearchProvider | null = useProvider(); | ||
const suggest = useRef<SearchSuggestApi>(null); | ||
|
||
const onFocus = useCallback(() => { | ||
updateRootClassName({focusSearch: true}); | ||
}, []); | ||
|
||
const onBlur = useCallback(() => { | ||
updateRootClassName({focusSearch: false}); | ||
setTimeout(() => { | ||
if (suggest.current) { | ||
suggest.current.close(); | ||
} | ||
}, 100); | ||
}, []); | ||
|
||
if (!provider) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<SearchSuggest | ||
ref={suggest} | ||
provider={provider} | ||
onFocus={onFocus} | ||
onBlur={onBlur} | ||
classNameContainer={'Suggest'} | ||
/> | ||
); | ||
} |
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,13 @@ | ||
import type {SearchConfig, WorkerApi, WorkerConfig} from './types'; | ||
|
||
import {createContext} from 'react'; | ||
|
||
export type {SearchConfig, WorkerConfig, WorkerApi}; | ||
|
||
export const SearchContext = createContext<SearchConfig | null | undefined>(null); | ||
|
||
SearchContext.displayName = 'SearchContext'; | ||
|
||
export const SearchProvider = SearchContext.Provider; | ||
|
||
export {Search} from './Search'; |
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,86 @@ | ||
import type {ISearchProvider, ISearchResult} from '@diplodoc/components'; | ||
import type {SearchConfig, WorkerConfig} from '../types'; | ||
|
||
export class SearchProvider implements ISearchProvider { | ||
private worker!: Promise<Worker>; | ||
|
||
private config: SearchConfig; | ||
|
||
constructor(config: SearchConfig) { | ||
this.config = config; | ||
} | ||
|
||
init = () => { | ||
this.worker = initWorker({ | ||
...this.config, | ||
base: this.base, | ||
mark: 'Suggest__Item__Marker', | ||
}); | ||
}; | ||
|
||
async suggest(query: string) { | ||
return this.request({ | ||
type: 'suggest', | ||
query, | ||
}) as Promise<ISearchResult[]>; | ||
} | ||
|
||
async search(query: string) { | ||
return this.request({ | ||
type: 'search', | ||
query, | ||
}) as Promise<ISearchResult[]>; | ||
} | ||
|
||
// Temporary disable link to search page | ||
// TODO: Implement search page | ||
link = () => null; | ||
|
||
// link = (query: string) => { | ||
// const params = query ? `?query=${encodeURIComponent(query)}` : ''; | ||
// | ||
// return `${this.base}/${this.config.link}${params}`; | ||
// }; | ||
|
||
private get base() { | ||
return window.location.pathname | ||
.split('/') | ||
.slice(0, -(this.config.depth + 1)) | ||
.join('/'); | ||
} | ||
|
||
private async request(message: object) { | ||
return request(await this.worker, message); | ||
} | ||
} | ||
|
||
async function initWorker(config: WorkerConfig) { | ||
const worker = new Worker(new URL('../worker/index.ts', import.meta.url)); | ||
|
||
await request(worker, {...config, type: 'init'}); | ||
|
||
return worker; | ||
} | ||
|
||
function request(worker: Worker, message: object) { | ||
const channel = new MessageChannel(); | ||
|
||
return new Promise((resolve, reject) => { | ||
channel.port1.onmessage = (message) => { | ||
if (message.data.error) { | ||
// eslint-disable-next-line no-console | ||
console.error(message.data.error); | ||
|
||
reject(message.data.error); | ||
} else { | ||
resolve(message.data.result); | ||
} | ||
}; | ||
|
||
channel.port1.onmessageerror = (message) => { | ||
reject(message.data.error); | ||
}; | ||
|
||
worker.postMessage(message, [channel.port2]); | ||
}); | ||
} |
Oops, something went wrong.