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-11-26 17:56:45 +01:00
|
|
|
import { shareReplay, switchMap } from "rxjs/operators"
|
2019-11-22 18:42:25 +01:00
|
|
|
|
2019-11-26 17:56:45 +01:00
|
|
|
import { isConfig } from "./config"
|
2019-11-18 21:24:56 +01:00
|
|
|
import {
|
2019-11-26 17:56:45 +01:00
|
|
|
setupSidebar,
|
|
|
|
switchComponent,
|
|
|
|
switchMapIfActive,
|
2019-11-22 18:42:25 +01:00
|
|
|
watchComponentMap,
|
|
|
|
watchHeader,
|
2019-11-26 17:56:45 +01:00
|
|
|
watchMain
|
|
|
|
} from "./theme"
|
2019-09-29 00:30:56 +02:00
|
|
|
import {
|
2019-11-26 17:56:45 +01:00
|
|
|
watchDocument,
|
|
|
|
watchDocumentSwitch,
|
2019-11-22 18:42:25 +01:00
|
|
|
watchLocation,
|
2019-11-26 17:56:45 +01:00
|
|
|
watchLocationFragment,
|
2019-11-18 21:24:56 +01:00
|
|
|
watchMedia,
|
|
|
|
watchViewportOffset,
|
2019-11-26 17:56:45 +01:00
|
|
|
watchViewportSize
|
2019-10-27 18:25:12 +01:00
|
|
|
} from "./ui"
|
2019-09-29 00:30:56 +02:00
|
|
|
|
2019-11-26 17:56:45 +01:00
|
|
|
// TBD
|
|
|
|
|
|
|
|
const names = [
|
|
|
|
"header", /* Header */
|
|
|
|
"title", /* Header title */
|
|
|
|
"search", /* Search */
|
|
|
|
"query", /* Search input */
|
|
|
|
"reset", /* Search reset */
|
|
|
|
"result", /* Search results */
|
|
|
|
"container", /* Container */
|
|
|
|
"main", /* Main area */
|
|
|
|
"hero", /* Hero */
|
|
|
|
"tabs", /* Tabs */
|
|
|
|
"navigation", /* Navigation */
|
|
|
|
"toc" /* Table of contents */
|
|
|
|
] as const // TODO: put this somewhere else... (merge with config!) JSON schema!?
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Functions
|
|
|
|
* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize Material for MkDocs
|
|
|
|
*
|
|
|
|
* @param config - Configuration
|
|
|
|
*/
|
|
|
|
export function initialize(config: unknown) {
|
|
|
|
if (!isConfig(config))
|
|
|
|
throw new SyntaxError(`Invalid configuration: ${JSON.stringify(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 viewport observables */
|
|
|
|
const offset$ = watchViewportOffset()
|
|
|
|
const size$ = watchViewportSize()
|
2019-09-29 00:30:56 +02:00
|
|
|
|
2019-11-26 17:56:45 +01:00
|
|
|
/* Create media observables */
|
|
|
|
const screen$ = watchMedia("(min-width: 1220px)")
|
|
|
|
const tablet$ = watchMedia("(min-width: 960px)")
|
2019-11-18 21:24:56 +01:00
|
|
|
|
2019-11-26 17:56:45 +01:00
|
|
|
/* Create location observables */
|
|
|
|
const url$ = watchLocation()
|
|
|
|
const fragment$ = watchLocationFragment()
|
2019-11-22 18:42:25 +01:00
|
|
|
|
2019-11-26 17:56:45 +01:00
|
|
|
/* Create document observables */
|
|
|
|
const load$ = watchDocument()
|
|
|
|
const switch$ = watchDocumentSwitch({ url$ })
|
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 */
|
|
|
|
const components$ = watchComponentMap(names, { load$, switch$ })
|
2019-11-22 18:42:25 +01:00
|
|
|
|
2019-11-26 17:56:45 +01:00
|
|
|
/* Create header observable */
|
|
|
|
const header$ = components$
|
|
|
|
.pipe(
|
|
|
|
switchComponent("header"),
|
|
|
|
switchMap(watchHeader)
|
2019-11-18 21:24:56 +01:00
|
|
|
)
|
2019-09-29 00:30:56 +02:00
|
|
|
|
2019-11-26 17:56:45 +01:00
|
|
|
/* Create main area observable */
|
|
|
|
const main$ = components$
|
|
|
|
.pipe(
|
|
|
|
switchComponent("main"),
|
|
|
|
switchMap(el => watchMain(el, { size$, offset$, header$ })),
|
|
|
|
shareReplay(1)
|
2019-11-18 21:24:56 +01:00
|
|
|
)
|
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 sidebar with navigation */
|
|
|
|
screen$
|
|
|
|
.pipe(
|
|
|
|
switchMapIfActive(() => components$ // TODO: write an observable creation function...
|
|
|
|
.pipe(
|
|
|
|
switchComponent("navigation"),
|
|
|
|
switchMap(el => setupSidebar(el, { offset$, main$ }))
|
2019-11-18 21:24:56 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2019-11-22 18:42:25 +01:00
|
|
|
.subscribe()
|
|
|
|
|
2019-11-26 17:56:45 +01:00
|
|
|
/* Create sidebar with table of contents (missing on 404 page) */
|
|
|
|
tablet$
|
2019-11-22 18:42:25 +01:00
|
|
|
.pipe(
|
2019-11-26 17:56:45 +01:00
|
|
|
switchMapIfActive(() => components$
|
|
|
|
.pipe(
|
|
|
|
switchComponent("toc"),
|
|
|
|
switchMap(el => setupSidebar(el, { offset$, main$ }))
|
|
|
|
)
|
|
|
|
)
|
2019-11-22 18:42:25 +01:00
|
|
|
)
|
2019-11-26 17:56:45 +01:00
|
|
|
.subscribe(console.log)
|
|
|
|
|
|
|
|
/* Return all observables */
|
|
|
|
return {
|
|
|
|
ui: {
|
|
|
|
document: { load$, switch$ },
|
|
|
|
location: { url$, fragment$ },
|
|
|
|
media: { screen$, tablet$ },
|
|
|
|
viewport: { offset$, size$ }
|
|
|
|
}
|
|
|
|
}
|
2019-09-29 00:30:56 +02:00
|
|
|
}
|