mirror of
https://github.com/squidfunk/mkdocs-material.git
synced 2024-06-14 11:52:32 +03:00
Added search status to observable
This commit is contained in:
@@ -21,9 +21,20 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { Observable, OperatorFunction, combineLatest, pipe } from "rxjs"
|
import { Observable, OperatorFunction, combineLatest, pipe } from "rxjs"
|
||||||
import { map, switchMap } from "rxjs/operators"
|
import {
|
||||||
|
filter,
|
||||||
|
map,
|
||||||
|
mapTo,
|
||||||
|
startWith,
|
||||||
|
switchMap
|
||||||
|
} from "rxjs/operators"
|
||||||
|
|
||||||
import { SearchResult } from "integrations/search"
|
import { WorkerHandler } from "browser"
|
||||||
|
import {
|
||||||
|
SearchMessage,
|
||||||
|
SearchResult,
|
||||||
|
isSearchReadyMessage
|
||||||
|
} from "integrations/search"
|
||||||
|
|
||||||
import { SearchQuery } from "../query"
|
import { SearchQuery } from "../query"
|
||||||
|
|
||||||
@@ -31,10 +42,20 @@ import { SearchQuery } from "../query"
|
|||||||
* Types
|
* Types
|
||||||
* ------------------------------------------------------------------------- */
|
* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Search status
|
||||||
|
*/
|
||||||
|
export type SearchStatus =
|
||||||
|
| "waiting" /* Search waiting for initialization */
|
||||||
|
| "ready" /* Search ready */
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Search
|
* Search
|
||||||
*/
|
*/
|
||||||
export interface Search {
|
export interface Search {
|
||||||
|
status: SearchStatus /* Search status */
|
||||||
query: SearchQuery /* Search query */
|
query: SearchQuery /* Search query */
|
||||||
result: SearchResult[] /* Search result list */
|
result: SearchResult[] /* Search result list */
|
||||||
}
|
}
|
||||||
@@ -59,18 +80,35 @@ interface MountOptions {
|
|||||||
/**
|
/**
|
||||||
* Mount search from source observable
|
* Mount search from source observable
|
||||||
*
|
*
|
||||||
|
* @param handler - Worker handler
|
||||||
* @param options - Options
|
* @param options - Options
|
||||||
*
|
*
|
||||||
* @return Operator function
|
* @return Operator function
|
||||||
*/
|
*/
|
||||||
export function mountSearch(
|
export function mountSearch(
|
||||||
|
{ rx$ }: WorkerHandler<SearchMessage>,
|
||||||
{ query$, reset$, result$ }: MountOptions
|
{ query$, reset$, result$ }: MountOptions
|
||||||
): OperatorFunction<HTMLElement, Search> {
|
): OperatorFunction<HTMLElement, Search> {
|
||||||
return pipe(
|
return pipe(
|
||||||
switchMap(() => combineLatest([query$, result$, reset$])
|
switchMap(() => {
|
||||||
.pipe(
|
|
||||||
map(([query, result]) => ({ query, result }))
|
/* Compute search status */
|
||||||
)
|
const status$ = rx$
|
||||||
)
|
.pipe(
|
||||||
|
filter(isSearchReadyMessage),
|
||||||
|
mapTo<SearchStatus>("ready"),
|
||||||
|
startWith("waiting")
|
||||||
|
) as Observable<SearchStatus>
|
||||||
|
|
||||||
|
/* Combine into single observable */
|
||||||
|
return combineLatest([status$, query$, result$, reset$])
|
||||||
|
.pipe(
|
||||||
|
map(([status, query, result]) => ({
|
||||||
|
status,
|
||||||
|
query,
|
||||||
|
result
|
||||||
|
}))
|
||||||
|
)
|
||||||
|
})
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ export function watchSearchQuery(
|
|||||||
/* Intercept focus events */
|
/* Intercept focus events */
|
||||||
const focus$ = watchElementFocus(el)
|
const focus$ = watchElementFocus(el)
|
||||||
|
|
||||||
/* Combine into a single observable */
|
/* Combine into single observable */
|
||||||
return combineLatest([value$, focus$])
|
return combineLatest([value$, focus$])
|
||||||
.pipe(
|
.pipe(
|
||||||
map(([value, focus]) => ({ value, focus }))
|
map(([value, focus]) => ({ value, focus }))
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ import {
|
|||||||
filter,
|
filter,
|
||||||
map,
|
map,
|
||||||
pluck,
|
pluck,
|
||||||
|
startWith,
|
||||||
switchMap
|
switchMap
|
||||||
} from "rxjs/operators"
|
} from "rxjs/operators"
|
||||||
|
|
||||||
@@ -85,7 +86,8 @@ export function mountSearchResult(
|
|||||||
.pipe(
|
.pipe(
|
||||||
filter(isSearchResultMessage),
|
filter(isSearchResultMessage),
|
||||||
pluck("data"),
|
pluck("data"),
|
||||||
applySearchResult(el, { query$, fetch$ })
|
applySearchResult(el, { query$, fetch$ }),
|
||||||
|
startWith([])
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ export function mountTableOfContents(
|
|||||||
applyAnchorList(els)
|
applyAnchorList(els)
|
||||||
)
|
)
|
||||||
|
|
||||||
/* Combine into a single hot observable */
|
/* Combine into single hot observable */
|
||||||
return combineLatest([sidebar$, anchors$])
|
return combineLatest([sidebar$, anchors$])
|
||||||
.pipe(
|
.pipe(
|
||||||
map(([sidebar, anchors]) => ({ sidebar, anchors }))
|
map(([sidebar, anchors]) => ({ sidebar, anchors }))
|
||||||
|
|||||||
@@ -280,7 +280,7 @@ export function initialize(config: unknown) {
|
|||||||
|
|
||||||
const search$ = useComponent("search")
|
const search$ = useComponent("search")
|
||||||
.pipe(
|
.pipe(
|
||||||
mountSearch({ query$, reset$, result$ }),
|
mountSearch(worker, { query$, reset$, result$ }),
|
||||||
shareReplay(1)
|
shareReplay(1)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user