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
|
|
|
|
|
2020-02-02 17:18:18 +01:00
|
|
|
import * as Clipboard from "clipboard"
|
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,
|
2020-01-08 18:17:19 +01:00
|
|
|
of,
|
|
|
|
fromEvent,
|
|
|
|
interval,
|
|
|
|
NEVER
|
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,
|
2020-02-02 16:19:01 +01:00
|
|
|
withLatestFrom,
|
|
|
|
distinctUntilChanged,
|
|
|
|
distinctUntilKeyChanged,
|
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,
|
2019-12-24 18:06:09 +01:00
|
|
|
setToggle,
|
2020-01-08 18:17:19 +01:00
|
|
|
getElements,
|
2020-01-26 16:03:49 +01:00
|
|
|
watchMedia,
|
2020-02-02 16:19:01 +01:00
|
|
|
translate,
|
|
|
|
watchElementFocus
|
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-24 17:59:26 +01:00
|
|
|
import { renderSource } from "templates"
|
2020-02-02 16:19:01 +01:00
|
|
|
import { switchMapIf, not, takeIf } from "extensions"
|
2020-02-02 17:18:18 +01:00
|
|
|
import { renderClipboard } from "templates/clipboard"
|
2020-02-02 18:28:58 +01:00
|
|
|
import { watchActiveLayer, paintActiveLayer } from "components/navigation/layer"
|
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-24 16:52:26 +01:00
|
|
|
// Remove trailing URL, or search might not work on the 404 page.
|
|
|
|
config.base = config.base.replace(/\/$/, "")
|
|
|
|
|
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-24 17:59:26 +01:00
|
|
|
/**
|
|
|
|
* Yes, this is a super hacky implementation. Needs clean up.
|
|
|
|
*/
|
|
|
|
function repository() {
|
2020-02-02 16:19:01 +01:00
|
|
|
const el = getElement<HTMLAnchorElement>(".md-source[href]") // TODO: dont use classes
|
2019-12-24 17:59:26 +01:00
|
|
|
console.log(el)
|
|
|
|
if (!el)
|
|
|
|
return EMPTY
|
|
|
|
|
2020-01-08 18:17:19 +01:00
|
|
|
const data = sessionStorage.getItem("repository")
|
2019-12-24 17:59:26 +01:00
|
|
|
if (data) {
|
|
|
|
const x = JSON.parse(data)
|
|
|
|
return of(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: do correct rounding, see GitHub
|
|
|
|
function format(value: number) {
|
2020-01-08 18:17:19 +01:00
|
|
|
return value > 999
|
|
|
|
? `${(value / 1000).toFixed(1)}k`
|
|
|
|
: `${(value)}`
|
2019-12-24 17:59:26 +01:00
|
|
|
}
|
|
|
|
|
2020-01-08 18:17:19 +01:00
|
|
|
// github repository...
|
|
|
|
const [, user, repo] = el.href.match(/^.+github\.com\/([^\/]+)\/?([^\/]+)?.*$/i)
|
2019-12-24 17:59:26 +01:00
|
|
|
|
|
|
|
// Show repo stats
|
|
|
|
if (user && repo) {
|
|
|
|
return ajax({
|
|
|
|
url: `https://api.github.com/repos/${user}/${repo}`,
|
|
|
|
responseType: "json"
|
|
|
|
})
|
|
|
|
.pipe(
|
|
|
|
map(({ status, response }) => {
|
|
|
|
if (status === 200) {
|
|
|
|
const { stargazers_count, forks_count } = response
|
|
|
|
return [
|
|
|
|
`${format(stargazers_count)} Stars`,
|
|
|
|
`${format(forks_count)} Forks`
|
|
|
|
]
|
|
|
|
}
|
|
|
|
return []
|
|
|
|
}),
|
2020-01-08 18:17:19 +01:00
|
|
|
tap(data => sessionStorage.setItem("repository", JSON.stringify(data)))
|
2019-12-24 17:59:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Show user or organization stats
|
|
|
|
} else if (user) {
|
|
|
|
return ajax({
|
|
|
|
url: `https://api.github.com/users/${user}`,
|
|
|
|
responseType: "json"
|
|
|
|
})
|
|
|
|
.pipe(
|
|
|
|
map(({ status, response }) => {
|
|
|
|
if (status === 200) {
|
|
|
|
const { public_repos } = response
|
|
|
|
return [
|
|
|
|
`${format(public_repos)} Repositories`
|
|
|
|
]
|
|
|
|
}
|
|
|
|
return []
|
|
|
|
}),
|
2020-01-08 18:17:19 +01:00
|
|
|
tap(data => sessionStorage.setItem("repository", JSON.stringify(data)))
|
2019-12-24 17:59:26 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
return of([])
|
|
|
|
}
|
|
|
|
|
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-12-24 17:59:26 +01:00
|
|
|
// TODO: WIP repo rendering
|
|
|
|
repository().subscribe(facts => {
|
|
|
|
if (facts.length) {
|
2020-02-02 16:19:01 +01:00
|
|
|
const sources = getElements(".md-source__repository")
|
2019-12-24 18:06:09 +01:00
|
|
|
sources.forEach(repo => {
|
|
|
|
repo.dataset.mdState = "done"
|
|
|
|
repo.appendChild(
|
|
|
|
renderSource(facts)
|
|
|
|
)
|
|
|
|
})
|
2019-12-24 17:59:26 +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-24 16:52:26 +01:00
|
|
|
// tap(m => console.log("message from worker", m)),
|
2019-12-20 18:03:31 +01:00
|
|
|
filter(isSearchResultMessage),
|
2019-12-24 16:52:26 +01:00
|
|
|
pluck("data"),
|
|
|
|
// Prefix URLs with base URL
|
|
|
|
tap(result => result.forEach(item => {
|
|
|
|
item.article.location = `${config.base}/${item.article.location}`
|
|
|
|
item.sections.forEach(section => {
|
|
|
|
section.location = `${config.base}/${section.location}`
|
|
|
|
})
|
|
|
|
}))
|
2019-12-20 18:03:31 +01:00
|
|
|
)
|
|
|
|
|
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...
|
2020-02-02 16:19:01 +01:00
|
|
|
distinctUntilKeyChanged("data")
|
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(
|
2020-02-02 16:19:01 +01:00
|
|
|
mountSearchResult(agent, { result$, query$: query$.pipe(
|
|
|
|
distinctUntilKeyChanged("value"),
|
|
|
|
pluck("value")
|
|
|
|
) })
|
2019-12-22 17:30:55 +01:00
|
|
|
) // 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]")!
|
|
|
|
|
2020-02-02 16:19:01 +01:00
|
|
|
const searchActive$ = watchToggle(search)
|
2019-11-27 19:12:49 +01:00
|
|
|
.pipe(
|
|
|
|
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)
|
|
|
|
)
|
|
|
|
|
2020-02-02 16:19:01 +01:00
|
|
|
const key$ = fromEvent<KeyboardEvent>(window, "keydown").pipe(
|
|
|
|
filter(ev => !(ev.metaKey || ev.ctrlKey))
|
|
|
|
)
|
|
|
|
|
|
|
|
// filter arrow keys if search is active!
|
|
|
|
searchActive$.subscribe(console.log)
|
|
|
|
|
|
|
|
// shortcodes
|
|
|
|
key$
|
2020-01-08 18:17:19 +01:00
|
|
|
.pipe(
|
2020-02-02 16:19:01 +01:00
|
|
|
takeIf(not(searchActive$))
|
2020-01-08 18:17:19 +01:00
|
|
|
)
|
2020-02-02 16:19:01 +01:00
|
|
|
.subscribe(ev => {
|
|
|
|
if (
|
|
|
|
document.activeElement && (
|
|
|
|
["TEXTAREA", "SELECT", "INPUT"].includes(
|
|
|
|
document.activeElement.tagName
|
|
|
|
) ||
|
|
|
|
document.activeElement instanceof HTMLElement &&
|
|
|
|
document.activeElement.isContentEditable
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
// do nothing...
|
|
|
|
} else {
|
|
|
|
if (ev.keyCode === 70 || ev.keyCode === 83) {
|
|
|
|
setToggle(search, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// check which element is focused...
|
|
|
|
// note that all links have tabindex=-1
|
|
|
|
key$
|
|
|
|
.pipe(
|
|
|
|
takeIf(searchActive$),
|
|
|
|
|
|
|
|
/* Abort if meta key (macOS) or ctrl key (Windows) is pressed */
|
|
|
|
tap(ev => {
|
|
|
|
if (ev.key === "Enter") {
|
|
|
|
if (document.activeElement === getElement("[data-md-component=search-query]")) {
|
|
|
|
ev.preventDefault()
|
|
|
|
// intercept hash change after search closed
|
|
|
|
} else {
|
|
|
|
setToggle(search, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ev.key === "ArrowUp" || ev.key === "ArrowDown") {
|
|
|
|
const active = getElements("[data-md-component=search-query], [data-md-component=search-result] [href]")
|
|
|
|
const i = Math.max(0, active.findIndex(el => el === document.activeElement))
|
|
|
|
const x = Math.max(0, (i + active.length + (ev.keyCode === 38 ? -1 : +1)) % active.length)
|
|
|
|
active[x].focus()
|
|
|
|
|
|
|
|
/* Prevent scrolling of page */
|
|
|
|
ev.preventDefault()
|
|
|
|
ev.stopPropagation()
|
|
|
|
|
|
|
|
} else if (ev.key === "Escape" || ev.key === "Tab") {
|
|
|
|
setToggle(search, false)
|
|
|
|
getElement("[data-md-component=search-query]")!.blur()
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (search.checked && document.activeElement !== getElement("[data-md-component=search-query]")) {
|
|
|
|
getElement("[data-md-component=search-query]")!.focus()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.subscribe()
|
|
|
|
|
|
|
|
// TODO: close search on hashchange
|
|
|
|
// anchor jump -> always close drawer + search
|
2020-01-08 18:17:19 +01:00
|
|
|
|
|
|
|
// focus search on reset, on toggle and on keypress if open
|
2020-02-02 16:19:01 +01:00
|
|
|
merge(searchActive$.pipe(filter(identity)), reset$)
|
2019-11-27 19:12:49 +01:00
|
|
|
.pipe(
|
2019-12-22 17:30:55 +01:00
|
|
|
switchMapTo(component<HTMLInputElement>("search-query")),
|
2020-01-08 18:17:19 +01:00
|
|
|
tap(el => el.focus()) // TODO: only if element isnt focused! setFocus? setToggle?
|
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
|
|
|
|
2020-01-08 18:17:19 +01:00
|
|
|
/* Open details before printing */
|
|
|
|
merge(
|
|
|
|
watchMedia("print").pipe(filter(identity)), // Webkit
|
|
|
|
fromEvent(window, "beforeprint") // IE, FF
|
|
|
|
)
|
|
|
|
.subscribe(() => {
|
2020-02-02 17:18:18 +01:00
|
|
|
const details = getElements("details")
|
2020-01-08 18:17:19 +01:00
|
|
|
Array.prototype.forEach.call(details, detail => {
|
|
|
|
detail.setAttribute("open", "")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-02-02 16:51:42 +01:00
|
|
|
// Close drawer and search on hash change
|
|
|
|
agent.location.hash$.subscribe(() => {
|
|
|
|
setToggle(drawer, false)
|
2020-02-02 17:18:18 +01:00
|
|
|
setToggle(search, false) // we probably need to delay the anchor jump for search
|
2020-02-02 16:51:42 +01:00
|
|
|
})
|
|
|
|
|
2020-01-08 18:17:19 +01:00
|
|
|
/* ----------------------------------------------------------------------- */
|
|
|
|
|
2020-02-02 17:18:18 +01:00
|
|
|
/* Clipboard integration */
|
|
|
|
if (Clipboard.isSupported()) {
|
|
|
|
const blocks = getElements(".codehilite > pre, .highlight> pre, pre > code")
|
|
|
|
Array.prototype.forEach.call(blocks, (block, index) => {
|
|
|
|
const id = `__code_${index}`
|
|
|
|
|
|
|
|
/* Create button with message container */
|
|
|
|
const button = renderClipboard(id)
|
|
|
|
|
|
|
|
/* Link to block and insert button */
|
|
|
|
const parent = block.parentNode
|
|
|
|
parent.id = id
|
|
|
|
parent.insertBefore(button, block)
|
|
|
|
})
|
|
|
|
|
|
|
|
/* Initialize Clipboard listener */
|
|
|
|
const copy = new Clipboard(".md-clipboard")
|
|
|
|
|
|
|
|
/* Success handler */
|
|
|
|
copy.on("success", action => {
|
|
|
|
alert("Copied to clipboard") // TODO: integrate snackbar
|
|
|
|
// TODO: add a snackbar/notification
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------- */
|
|
|
|
|
2020-02-02 18:28:58 +01:00
|
|
|
const navigationlayer$ = component("navigation")
|
|
|
|
.pipe(
|
|
|
|
switchMapIf(not(agent.media.tablet$), el => watchActiveLayer(el)
|
|
|
|
.pipe(
|
|
|
|
paintActiveLayer()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.subscribe(console.log)
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------- */
|
|
|
|
|
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
|
|
|
}
|