2019-09-29 00:30:56 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016-2019 Martin Donath <martin.donath@squidfunk.com>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-12-22 17:30:55 +01:00
|
|
|
// TODO: remove this after we finished refactoring
|
2019-12-20 18:03:31 +01:00
|
|
|
// tslint:disable
|
|
|
|
|
2019-12-22 17:30:55 +01:00
|
|
|
import { identity, values } from "ramda"
|
2019-12-17 09:53:16 +01:00
|
|
|
import {
|
2019-12-20 18:03:31 +01:00
|
|
|
EMPTY,
|
2019-12-17 09:53:16 +01:00
|
|
|
Observable,
|
2019-12-18 17:14:20 +01:00
|
|
|
Subject,
|
2019-12-20 18:03:31 +01:00
|
|
|
forkJoin,
|
2019-12-17 09:53:16 +01:00
|
|
|
merge,
|
2019-12-22 17:30:55 +01:00
|
|
|
of
|
2019-11-27 19:12:49 +01:00
|
|
|
} from "rxjs"
|
2019-12-22 17:30:55 +01:00
|
|
|
import { ajax } from "rxjs/ajax"
|
2019-12-17 09:53:16 +01:00
|
|
|
import {
|
|
|
|
delay,
|
|
|
|
filter,
|
|
|
|
map,
|
|
|
|
pluck,
|
|
|
|
switchMap,
|
|
|
|
switchMapTo,
|
2019-12-20 18:03:31 +01:00
|
|
|
take,
|
2019-12-17 09:53:16 +01:00
|
|
|
tap,
|
2019-11-27 19:12:49 +01:00
|
|
|
} from "rxjs/operators"
|
2019-11-22 18:42:25 +01:00
|
|
|
|
2019-11-18 21:24:56 +01:00
|
|
|
import {
|
2019-11-27 19:12:49 +01:00
|
|
|
Component,
|
2019-12-17 09:53:16 +01:00
|
|
|
paintHeaderShadow,
|
2019-12-22 17:30:55 +01:00
|
|
|
mountHero,
|
|
|
|
mountMain,
|
|
|
|
mountNavigation,
|
|
|
|
mountSearchResult,
|
|
|
|
mountTableOfContents,
|
|
|
|
mountTabs,
|
2019-11-26 17:56:45 +01:00
|
|
|
switchComponent,
|
2019-11-22 18:42:25 +01:00
|
|
|
watchComponentMap,
|
|
|
|
watchHeader,
|
2019-12-22 17:30:55 +01:00
|
|
|
watchSearchQuery,
|
|
|
|
watchSearchReset
|
2019-12-18 17:14:20 +01:00
|
|
|
} from "./components"
|
2019-12-22 17:30:55 +01:00
|
|
|
import { SearchIndexOptions } from "./modules"
|
2019-09-29 00:30:56 +02:00
|
|
|
import {
|
2019-12-18 17:14:20 +01:00
|
|
|
getElement,
|
2019-12-20 18:03:31 +01:00
|
|
|
setupAgent,
|
2019-12-18 17:14:20 +01:00
|
|
|
watchToggle,
|
2019-12-22 17:30:55 +01:00
|
|
|
watchWorker,
|
|
|
|
setToggle
|
2019-11-27 19:12:49 +01:00
|
|
|
} from "./utilities"
|
2019-12-20 18:03:31 +01:00
|
|
|
import {
|
2019-12-22 17:30:55 +01:00
|
|
|
PackerMessage,
|
|
|
|
PackerMessageType,
|
2019-12-20 18:03:31 +01:00
|
|
|
SearchMessage,
|
|
|
|
SearchMessageType,
|
|
|
|
SearchSetupMessage,
|
|
|
|
isSearchDumpMessage,
|
|
|
|
isSearchResultMessage
|
|
|
|
} from "./workers"
|
2019-12-18 17:14:20 +01:00
|
|
|
|
2019-12-22 17:30:55 +01:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Types
|
|
|
|
* ------------------------------------------------------------------------- */
|
|
|
|
|
2019-12-18 17:14:20 +01:00
|
|
|
/**
|
|
|
|
* Configuration
|
|
|
|
*/
|
|
|
|
export interface Config {
|
|
|
|
base: string /* Base URL */
|
|
|
|
worker: {
|
2019-12-22 17:30:55 +01:00
|
|
|
search: string /* Search worker URL */
|
|
|
|
packer: string /* Packer worker URL */
|
2019-12-18 17:14:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:30:55 +01:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* TODO: where do we put this stuff?
|
|
|
|
* ------------------------------------------------------------------------- */
|
2019-12-18 17:14:20 +01:00
|
|
|
|
2019-12-22 17:30:55 +01:00
|
|
|
document.documentElement.classList.remove("no-js")
|
|
|
|
document.documentElement.classList.add("js")
|
|
|
|
|
|
|
|
const names: Component[] = [
|
|
|
|
"container", /* Container */
|
|
|
|
"header", /* Header */
|
|
|
|
"header-title", /* Header title */
|
|
|
|
"hero", /* Hero */
|
|
|
|
"main", /* Main area */
|
|
|
|
"navigation", /* Navigation */
|
|
|
|
"search", /* Search */
|
|
|
|
"search-query", /* Search input */
|
|
|
|
"search-reset", /* Search reset */
|
|
|
|
"search-result", /* Search results */
|
|
|
|
"tabs", /* Tabs */
|
|
|
|
"toc" /* Table of contents */
|
|
|
|
]
|
2019-12-18 17:14:20 +01:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
2019-12-22 17:30:55 +01:00
|
|
|
* Helper functions
|
2019-12-18 17:14:20 +01:00
|
|
|
* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that the given value is a valid configuration
|
|
|
|
*
|
2019-12-22 17:30:55 +01:00
|
|
|
* We could use `jsonschema` or any other schema validation framework, but that
|
|
|
|
* would just add more bloat to the bundle, so we'll keep it plain and simple.
|
|
|
|
*
|
2019-12-18 17:14:20 +01:00
|
|
|
* @param config - Configuration
|
|
|
|
*
|
|
|
|
* @return Test result
|
|
|
|
*/
|
2019-12-22 17:30:55 +01:00
|
|
|
function isConfig(config: any): config is Config {
|
2019-12-18 17:14:20 +01:00
|
|
|
return typeof config === "object"
|
|
|
|
&& typeof config.base === "string"
|
2019-12-22 17:30:55 +01:00
|
|
|
&& typeof config.worker === "object"
|
|
|
|
&& typeof config.worker.search === "string"
|
|
|
|
&& typeof config.worker.packer === "string"
|
2019-12-18 17:14:20 +01:00
|
|
|
}
|
2019-09-29 00:30:56 +02:00
|
|
|
|
2019-12-20 18:03:31 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Rogue control characters must be filtered before handing the query to the
|
|
|
|
* search index, as lunr will throw otherwise.
|
|
|
|
*/
|
2019-12-22 17:30:55 +01:00
|
|
|
function prepare(value: string): string {
|
2019-12-20 18:03:31 +01:00
|
|
|
const newvalue = value
|
|
|
|
.replace(/(?:^|\s+)[*+-:^~]+(?=\s+|$)/g, "")
|
|
|
|
.trim()
|
|
|
|
|
|
|
|
return newvalue ? newvalue.replace(/\s+|$/g, "* ") : ""
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:30:55 +01:00
|
|
|
function setupWorkers(config: Config) {
|
2019-12-18 17:14:20 +01:00
|
|
|
const worker = new Worker(config.worker.search)
|
|
|
|
const packer = new Worker(config.worker.packer)
|
|
|
|
|
|
|
|
const packerMessage$ = new Subject<PackerMessage>()
|
2019-12-20 18:03:31 +01:00
|
|
|
const packer$ = watchWorker(packer, { send$: packerMessage$ })
|
2019-12-18 17:14:20 +01:00
|
|
|
|
|
|
|
// send a message, then switchMapTo worker!
|
|
|
|
|
|
|
|
packer$.subscribe(message => {
|
2019-12-22 17:30:55 +01:00
|
|
|
// console.log("PACKER.MSG", message.data.length)
|
2019-12-18 17:14:20 +01:00
|
|
|
// is always packed!
|
2019-12-20 18:03:31 +01:00
|
|
|
if (message.type === PackerMessageType.BINARY && message.data[0] !== "{")
|
|
|
|
localStorage.setItem("index", message.data)
|
2019-12-18 17:14:20 +01:00
|
|
|
})
|
|
|
|
|
2019-12-20 18:03:31 +01:00
|
|
|
// storing = experimental feature
|
2019-12-18 17:14:20 +01:00
|
|
|
|
2019-12-20 18:03:31 +01:00
|
|
|
const searchMessage$ = new Subject<SearchMessage>()
|
2019-12-18 17:14:20 +01:00
|
|
|
|
2019-12-20 18:03:31 +01:00
|
|
|
const search$ = watchWorker(worker, { send$: searchMessage$ })
|
2019-12-18 17:14:20 +01:00
|
|
|
|
2019-12-20 18:03:31 +01:00
|
|
|
/* Link search to packer */
|
|
|
|
search$
|
|
|
|
.pipe(
|
|
|
|
filter(isSearchDumpMessage),
|
|
|
|
map(message => ({
|
|
|
|
type: PackerMessageType.STRING,
|
|
|
|
data: message.data
|
|
|
|
})),
|
|
|
|
tap(message => packerMessage$.next(message)) // send message and wait!
|
|
|
|
// switchMapTo(packer$)
|
|
|
|
)
|
|
|
|
.subscribe()
|
2019-12-18 17:14:20 +01:00
|
|
|
|
2019-12-20 18:03:31 +01:00
|
|
|
const data$ = ajax({
|
2019-12-18 17:14:20 +01:00
|
|
|
url: `${config.base}/search/search_index.json`,
|
|
|
|
responseType: "json",
|
|
|
|
withCredentials: true
|
|
|
|
})
|
2019-12-22 17:30:55 +01:00
|
|
|
.pipe<SearchIndexOptions>(
|
2019-12-20 18:03:31 +01:00
|
|
|
pluck("response")
|
|
|
|
// take(1)
|
|
|
|
)
|
|
|
|
|
|
|
|
const fromLocal = localStorage.getItem("index")
|
|
|
|
|
|
|
|
;
|
|
|
|
(fromLocal ? of({
|
|
|
|
type: PackerMessageType.BINARY,
|
|
|
|
data: localStorage.getItem("index")!
|
|
|
|
}) : EMPTY)
|
|
|
|
.subscribe(x => {
|
|
|
|
// console.log("send message to packer")
|
|
|
|
packerMessage$.next(x)
|
|
|
|
})
|
|
|
|
|
|
|
|
const index$ = fromLocal ? packer$.pipe(pluck("data"), take(1)) : of(undefined) // of(localStorage.getItem("index"))
|
|
|
|
|
|
|
|
// index$.subscribe(xx => console.log("INDEX", xx))
|
|
|
|
|
|
|
|
forkJoin([data$, index$])
|
|
|
|
.pipe<SearchSetupMessage>(
|
|
|
|
map(([data, index]) => ({
|
2019-12-18 17:14:20 +01:00
|
|
|
type: SearchMessageType.SETUP,
|
2019-12-20 18:03:31 +01:00
|
|
|
data: { ...data, index }
|
2019-12-18 17:14:20 +01:00
|
|
|
}))
|
|
|
|
)
|
|
|
|
.subscribe(message => {
|
|
|
|
searchMessage$.next(message) // TODO: this shall not complete
|
|
|
|
})
|
|
|
|
|
2019-12-22 17:30:55 +01:00
|
|
|
return [search$, searchMessage$] as const
|
|
|
|
}
|
2019-11-22 18:42:25 +01:00
|
|
|
|
2019-12-22 17:30:55 +01:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Functions
|
|
|
|
* ------------------------------------------------------------------------- */
|
2019-12-20 18:03:31 +01:00
|
|
|
|
2019-12-22 17:30:55 +01:00
|
|
|
/**
|
|
|
|
* Initialize Material for MkDocs
|
|
|
|
*
|
|
|
|
* @param config - Configuration
|
|
|
|
*/
|
|
|
|
export function initialize(config: unknown) {
|
|
|
|
if (!isConfig(config))
|
|
|
|
throw new SyntaxError(`Invalid configuration: ${JSON.stringify(config)}`)
|
2019-12-20 18:03:31 +01:00
|
|
|
|
2019-12-22 17:30:55 +01:00
|
|
|
// pass config here!?
|
|
|
|
const agent = setupAgent() // TODO: add a config parameter here to configure media queries
|
2019-12-20 18:03:31 +01:00
|
|
|
|
2019-12-22 17:30:55 +01:00
|
|
|
const [
|
|
|
|
searchWorkerRecv$,
|
|
|
|
searchMessage$
|
|
|
|
] = setupWorkers(config)
|
2019-11-22 18:42:25 +01:00
|
|
|
|
2019-11-26 17:56:45 +01:00
|
|
|
/* ----------------------------------------------------------------------- */
|
2019-11-22 18:42:25 +01:00
|
|
|
|
2019-11-26 17:56:45 +01:00
|
|
|
/* Create component map observable */
|
2019-12-22 17:30:55 +01:00
|
|
|
const components$ = watchComponentMap(names, { document$: agent.document.load$ })
|
2019-12-20 18:03:31 +01:00
|
|
|
const component = <T extends HTMLElement>(name: Component): Observable<T> => {
|
2019-11-27 19:12:49 +01:00
|
|
|
return components$
|
|
|
|
.pipe(
|
2019-12-20 18:03:31 +01:00
|
|
|
switchComponent<T>(name)
|
2019-11-27 19:12:49 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-11-26 17:56:45 +01:00
|
|
|
/* Create header observable */
|
2019-12-20 18:03:31 +01:00
|
|
|
const header$ = component("header") // TODO:!
|
2019-11-26 17:56:45 +01:00
|
|
|
.pipe(
|
|
|
|
switchMap(watchHeader)
|
2019-11-18 21:24:56 +01:00
|
|
|
)
|
2019-09-29 00:30:56 +02:00
|
|
|
|
2019-12-20 18:03:31 +01:00
|
|
|
/* Create header shadow toggle */
|
|
|
|
component("header")
|
|
|
|
.pipe(
|
|
|
|
switchMap(el => main$
|
|
|
|
.pipe(
|
2019-12-22 17:30:55 +01:00
|
|
|
paintHeaderShadow(el) // technically, this could be done in paintMain
|
2019-12-20 18:03:31 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.subscribe()
|
|
|
|
|
2019-12-22 17:30:55 +01:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2019-12-20 18:03:31 +01:00
|
|
|
// watchSearchResult // emit, if at bottom...
|
|
|
|
// receive results as a second observable!? filter stuff, paint
|
|
|
|
|
2019-12-22 17:30:55 +01:00
|
|
|
const result$ = searchWorkerRecv$ // move worker initialization into mountSearch ?
|
2019-12-20 18:03:31 +01:00
|
|
|
.pipe(
|
2019-12-22 17:30:55 +01:00
|
|
|
tap(m => console.log("message from worker", m)),
|
2019-12-20 18:03:31 +01:00
|
|
|
filter(isSearchResultMessage),
|
|
|
|
pluck("data")
|
|
|
|
)
|
|
|
|
|
2019-12-22 17:30:55 +01:00
|
|
|
// handleSearchResult <-- operator
|
2019-12-20 18:03:31 +01:00
|
|
|
|
2019-12-22 17:30:55 +01:00
|
|
|
const query$ = component<HTMLInputElement>("search-query")
|
2019-12-20 18:03:31 +01:00
|
|
|
.pipe(
|
2019-12-22 17:30:55 +01:00
|
|
|
switchMap(el => watchSearchQuery(el, { prepare }))
|
2019-12-20 18:03:31 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
query$
|
2019-12-22 17:30:55 +01:00
|
|
|
.pipe<SearchMessage>(
|
|
|
|
map(query => ({ // put this into some function...
|
2019-12-20 18:03:31 +01:00
|
|
|
type: SearchMessageType.QUERY,
|
2019-12-22 17:30:55 +01:00
|
|
|
data: query.value
|
2019-12-20 18:03:31 +01:00
|
|
|
})), // TODO. ugly...
|
2019-12-22 17:30:55 +01:00
|
|
|
// distinctUntilKeyChanged("data")
|
2019-12-20 18:03:31 +01:00
|
|
|
)
|
2019-12-22 17:30:55 +01:00
|
|
|
.subscribe(searchMessage$)
|
2019-12-20 18:03:31 +01:00
|
|
|
|
2019-12-22 17:30:55 +01:00
|
|
|
// create the message subject internally... and link it to the worker...?
|
|
|
|
// watchSearchWorker(worker, agent, { query$ }) // message internally...
|
2019-12-18 17:14:20 +01:00
|
|
|
|
2019-12-22 17:30:55 +01:00
|
|
|
query$
|
2019-12-20 18:03:31 +01:00
|
|
|
.pipe(
|
2019-12-22 17:30:55 +01:00
|
|
|
tap(query => {
|
|
|
|
if (query.focus)
|
|
|
|
setToggle(search, true)
|
|
|
|
})
|
2019-12-20 18:03:31 +01:00
|
|
|
)
|
|
|
|
.subscribe()
|
|
|
|
|
2019-11-26 17:56:45 +01:00
|
|
|
/* ----------------------------------------------------------------------- */
|
2019-11-22 18:42:25 +01:00
|
|
|
|
2019-12-22 17:30:55 +01:00
|
|
|
const main$ = component("main")
|
2019-11-27 19:12:49 +01:00
|
|
|
.pipe(
|
2019-12-22 17:30:55 +01:00
|
|
|
mountMain(agent, { header$ })
|
2019-11-27 19:12:49 +01:00
|
|
|
)
|
|
|
|
|
2019-12-22 17:30:55 +01:00
|
|
|
const navigation$ = component("navigation")
|
2019-11-27 19:12:49 +01:00
|
|
|
.pipe(
|
2019-12-22 17:30:55 +01:00
|
|
|
mountNavigation(agent, { main$ })
|
2019-11-27 19:12:49 +01:00
|
|
|
)
|
|
|
|
|
2019-12-22 17:30:55 +01:00
|
|
|
const toc$ = component("toc")
|
2019-11-22 18:42:25 +01:00
|
|
|
.pipe(
|
2019-12-22 17:30:55 +01:00
|
|
|
mountTableOfContents(agent, { header$, main$ })
|
2019-11-27 19:12:49 +01:00
|
|
|
)
|
|
|
|
|
2019-12-22 17:30:55 +01:00
|
|
|
// TODO: naming?
|
|
|
|
const resultComponent$ = component("search-result")
|
|
|
|
.pipe(
|
|
|
|
mountSearchResult(agent, { result$, query$: query$.pipe(pluck("value")) })
|
|
|
|
) // temporary fix
|
|
|
|
|
|
|
|
const tabs$ = component("tabs")
|
2019-11-27 19:12:49 +01:00
|
|
|
.pipe(
|
2019-12-22 17:30:55 +01:00
|
|
|
mountTabs(agent, { header$ })
|
|
|
|
)
|
|
|
|
|
|
|
|
const hero$ = component("hero")
|
|
|
|
.pipe(
|
|
|
|
mountHero(agent, { header$ })
|
2019-11-27 19:12:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
const drawer = getElement<HTMLInputElement>("[data-md-toggle=drawer]")!
|
|
|
|
const search = getElement<HTMLInputElement>("[data-md-toggle=search]")!
|
|
|
|
|
|
|
|
const a$ = watchToggle(search)
|
|
|
|
.pipe(
|
|
|
|
filter(identity),
|
|
|
|
delay(400)
|
|
|
|
)
|
|
|
|
|
2019-12-22 17:30:55 +01:00
|
|
|
const reset$ = component("search-reset")
|
2019-11-27 19:12:49 +01:00
|
|
|
.pipe(
|
|
|
|
switchMap(watchSearchReset)
|
|
|
|
)
|
|
|
|
|
2019-12-22 17:30:55 +01:00
|
|
|
merge(a$, reset$)
|
2019-11-27 19:12:49 +01:00
|
|
|
.pipe(
|
2019-12-22 17:30:55 +01:00
|
|
|
switchMapTo(component<HTMLInputElement>("search-query")),
|
|
|
|
tap(el => el.focus())
|
2019-11-27 19:12:49 +01:00
|
|
|
)
|
|
|
|
.subscribe()
|
|
|
|
|
2019-12-22 17:30:55 +01:00
|
|
|
/* ----------------------------------------------------------------------- */
|
2019-12-20 18:03:31 +01:00
|
|
|
|
2019-12-22 17:30:55 +01:00
|
|
|
const state = {
|
|
|
|
search: {
|
|
|
|
query$,
|
|
|
|
result$: resultComponent$,
|
|
|
|
reset$,
|
|
|
|
},
|
|
|
|
main$,
|
|
|
|
navigation$,
|
|
|
|
toc$,
|
|
|
|
tabs$,
|
|
|
|
hero$
|
2019-12-20 18:03:31 +01:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:30:55 +01:00
|
|
|
const { search: temp, ...rest } = state
|
|
|
|
merge(...values(rest), ...values(temp))
|
|
|
|
.subscribe() // potential memleak <-- use takeUntil
|
2019-11-27 19:12:49 +01:00
|
|
|
|
2019-12-22 17:30:55 +01:00
|
|
|
return {
|
|
|
|
agent,
|
|
|
|
state
|
2019-11-26 17:56:45 +01:00
|
|
|
}
|
2019-09-29 00:30:56 +02:00
|
|
|
}
|