1
0
mirror of https://github.com/squidfunk/mkdocs-material.git synced 2025-01-12 06:02:13 +01:00

549 lines
15 KiB
TypeScript
Raw Normal View History

2019-09-29 00:30:56 +02:00
/*
2020-02-11 11:05:21 +01:00
* Copyright (c) 2016-2020 Martin Donath <martin.donath@squidfunk.com>
2019-09-29 00:30:56 +02:00
*
* 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
// tslint:disable
2020-02-10 18:32:28 +01:00
import "../stylesheets/app.scss"
import "../stylesheets/app-palette.scss"
2020-02-17 17:20:08 +01:00
import { values } from "ramda"
2019-12-17 09:53:16 +01:00
import {
merge,
2020-02-17 11:35:36 +01:00
combineLatest,
2020-02-20 17:42:46 +01:00
animationFrameScheduler,
fromEvent,
of,
NEVER
2019-11-27 19:12:49 +01:00
} from "rxjs"
2019-12-17 09:53:16 +01:00
import {
delay,
switchMap,
2020-02-14 17:36:31 +01:00
tap,
filter,
2020-02-17 11:35:36 +01:00
withLatestFrom,
observeOn,
2020-02-20 14:44:41 +01:00
take,
mapTo,
2020-02-20 17:42:46 +01:00
shareReplay,
sample,
share,
map,
pluck,
debounceTime,
distinctUntilKeyChanged,
2020-02-22 15:24:15 +01:00
distinctUntilChanged,
bufferCount
2019-11-27 19:12:49 +01:00
} from "rxjs/operators"
2019-09-29 00:30:56 +02:00
import {
2019-12-18 17:14:20 +01:00
watchToggle,
setToggle,
getElements,
getLocation,
2020-01-26 16:03:49 +01:00
watchMedia,
2020-02-12 19:13:03 +01:00
watchDocument,
2020-02-16 00:23:50 +01:00
watchLocation,
2020-02-12 19:13:03 +01:00
watchLocationHash,
watchViewport,
2020-02-21 10:18:49 +01:00
setupToggles,
2020-02-20 14:44:41 +01:00
useToggle,
2020-02-20 17:42:46 +01:00
getElement,
setViewportOffset,
ViewportOffset
2020-02-12 19:13:03 +01:00
} from "./observables"
2020-02-13 18:29:44 +01:00
import { setupSearchWorker } from "./workers"
2020-02-19 08:57:36 +01:00
import { setScrollLock, resetScrollLock } from "actions"
2020-02-13 18:29:44 +01:00
import {
2020-02-13 23:42:12 +01:00
mountHeader,
mountHero,
2020-02-13 18:29:44 +01:00
mountMain,
2020-02-13 18:50:39 +01:00
mountNavigation,
mountSearch,
mountTableOfContents,
mountTabs,
2020-02-13 18:50:39 +01:00
useComponent,
2020-02-21 10:18:49 +01:00
setupComponents,
2020-02-20 14:44:41 +01:00
mountHeaderTitle,
mountSearchQuery,
mountSearchReset,
mountSearchResult
} from "components"
2020-02-19 08:57:36 +01:00
import { setupClipboard } from "./integrations/clipboard"
import { setupKeyboard } from "./integrations/keyboard"
2020-02-19 11:42:51 +01:00
import {
patchTables,
patchDetails,
patchScrollfix,
patchSource
} from "patches"
import { isConfig } from "utilities"
2020-02-20 14:44:41 +01:00
import { setupDialog } from "integrations/dialog"
2019-12-18 17:14:20 +01:00
2020-02-14 17:45:32 +01:00
/* ------------------------------------------------------------------------- */
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")
2020-02-12 19:13:03 +01:00
/* Test for iOS */
if (navigator.userAgent.match(/(iPad|iPhone|iPod)/g))
document.documentElement.classList.add("ios")
2019-12-22 17:30:55 +01:00
/* ----------------------------------------------------------------------------
* Functions
* ------------------------------------------------------------------------- */
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)}`)
/* Setup user interface observables */
2020-02-16 00:23:50 +01:00
const location$ = watchLocation()
const hash$ = watchLocationHash()
2020-02-12 19:13:03 +01:00
const viewport$ = watchViewport()
const tablet$ = watchMedia("(min-width: 960px)")
const screen$ = watchMedia("(min-width: 1220px)")
2020-02-12 19:13:03 +01:00
/* Setup document observable */
const document$ = config.feature.instant
? watchDocument({ location$ })
: watchDocument()
2020-02-14 17:45:32 +01:00
2020-02-21 10:18:49 +01:00
/* Setup toggle bindings */
setupToggles([
2020-02-14 17:45:32 +01:00
"drawer", /* Toggle for drawer */
"search" /* Toggle for search */
], { document$ })
2020-02-17 17:20:08 +01:00
/* Setup component bindings */
2020-02-21 10:18:49 +01:00
setupComponents([
2020-02-14 17:45:32 +01:00
"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 */
], { document$ })
2019-11-27 19:12:49 +01:00
2020-02-21 10:18:49 +01:00
/* ----------------------------------------------------------------------- */
const worker = setupSearchWorker(config.worker.search, {
base: config.base
})
/* ----------------------------------------------------------------------- */
/* Create header observable */
2020-02-13 18:29:44 +01:00
const header$ = useComponent("header")
.pipe(
2020-02-20 17:42:46 +01:00
mountHeader({ viewport$ }),
shareReplay(1)
)
2020-02-13 18:29:44 +01:00
const main$ = useComponent("main")
.pipe(
2020-02-20 17:42:46 +01:00
mountMain({ header$, viewport$ }),
shareReplay(1)
)
2020-02-12 19:13:03 +01:00
/* ----------------------------------------------------------------------- */
2020-02-20 14:44:41 +01:00
/* Mount search query */
const query$ = useComponent<HTMLInputElement>("search-query")
.pipe(
mountSearchQuery(worker),
shareReplay(1) // TODO: this must be put onto EVERY component!
)
/* Mount search reset */
const reset$ = useComponent("search-reset")
.pipe(
2020-02-20 17:42:46 +01:00
mountSearchReset(),
shareReplay(1)
2020-02-20 14:44:41 +01:00
)
/* Mount search result */
const result$ = useComponent("search-result")
.pipe(
2020-02-20 17:42:46 +01:00
mountSearchResult(worker, { query$ }),
shareReplay(1)
2020-02-20 14:44:41 +01:00
)
/* ----------------------------------------------------------------------- */
2020-02-13 18:29:44 +01:00
const search$ = useComponent("search")
2020-02-12 19:13:03 +01:00
.pipe(
2020-02-20 17:42:46 +01:00
mountSearch({ query$, reset$, result$ }),
shareReplay(1)
2019-11-27 19:12:49 +01:00
)
2020-02-20 14:44:41 +01:00
/* ----------------------------------------------------------------------- */
2020-02-13 18:29:44 +01:00
const navigation$ = useComponent("navigation")
2019-11-27 19:12:49 +01:00
.pipe(
2020-02-20 17:42:46 +01:00
mountNavigation({ header$, main$, viewport$, screen$ }),
shareReplay(1)
2019-11-27 19:12:49 +01:00
)
2020-02-13 18:29:44 +01:00
const toc$ = useComponent("toc")
.pipe(
2020-02-20 17:42:46 +01:00
mountTableOfContents({ header$, main$, viewport$, tablet$ }),
shareReplay(1)
2019-11-27 19:12:49 +01:00
)
2020-02-13 18:29:44 +01:00
const tabs$ = useComponent("tabs")
2019-11-27 19:12:49 +01:00
.pipe(
2020-02-20 17:42:46 +01:00
mountTabs({ header$, viewport$, screen$ }),
shareReplay(1)
2019-12-22 17:30:55 +01:00
)
2020-02-13 18:29:44 +01:00
const hero$ = useComponent("hero")
2019-12-22 17:30:55 +01:00
.pipe(
2020-02-20 17:42:46 +01:00
mountHero({ header$, viewport$ }),
shareReplay(1)
2019-11-27 19:12:49 +01:00
)
2020-02-17 16:25:49 +01:00
const title$ = useComponent("header-title")
.pipe(
2020-02-20 17:42:46 +01:00
mountHeaderTitle({ header$, viewport$ }),
shareReplay(1)
)
2020-02-13 23:42:12 +01:00
/* ----------------------------------------------------------------------- */
const keyboard$ = setupKeyboard()
2020-02-19 17:29:18 +01:00
patchTables({ document$ })
patchDetails({ document$, hash$ })
patchSource({ document$ })
/* Force 1px scroll offset to trigger overflow scrolling */
if (navigator.userAgent.match(/(iPad|iPhone|iPod)/g))
patchScrollfix({ document$ })
2020-02-20 14:44:41 +01:00
/* Setup clipboard and dialog */
const dialog$ = setupDialog()
const clipboard$ = setupClipboard({ document$, dialog$ })
2019-12-22 17:30:55 +01:00
/* ----------------------------------------------------------------------- */
2020-02-02 16:51:42 +01:00
// Close drawer and search on hash change
2020-02-20 14:44:41 +01:00
// put into navigation...
2020-02-16 00:23:50 +01:00
hash$.subscribe(x => {
2020-02-13 18:29:44 +01:00
useToggle("drawer").subscribe(el => {
setToggle(el, false)
})
2020-02-02 16:51:42 +01:00
})
2020-02-20 14:44:41 +01:00
// put into search...
2020-02-16 00:23:50 +01:00
hash$
.pipe(
2020-02-20 14:44:41 +01:00
switchMap(hash => useToggle("search")
.pipe(
filter(x => x.checked), // only active
tap(toggle => setToggle(toggle, false)),
delay(125), // ensure that it runs after the body scroll reset...
mapTo(hash)
)
)
2020-02-16 00:23:50 +01:00
)
2020-02-20 14:44:41 +01:00
.subscribe(hash => {
getElement(`[id="${hash}"]`)!.scrollIntoView()
2020-02-20 14:44:41 +01:00
})
2020-02-16 00:23:50 +01:00
2020-02-20 14:44:41 +01:00
// Scroll lock // document -> document$ => { body } !?
// put into search...
2020-02-17 11:35:36 +01:00
const toggle$ = useToggle("search")
combineLatest([
toggle$.pipe(switchMap(watchToggle)),
tablet$,
])
.pipe(
withLatestFrom(viewport$),
switchMap(([[toggle, tablet], { offset: { y }}]) => {
const active = toggle && !tablet
2020-02-20 14:44:41 +01:00
return document$
2020-02-17 11:35:36 +01:00
.pipe(
2020-02-20 14:44:41 +01:00
delay(active ? 400 : 100), // TOOD: directly combine this with the hash!
2020-02-17 11:35:36 +01:00
observeOn(animationFrameScheduler),
2020-02-20 14:44:41 +01:00
tap(({ body }) => active
? setScrollLock(body, y)
: resetScrollLock(body)
2020-02-17 11:35:36 +01:00
)
)
})
)
.subscribe()
/* ----------------------------------------------------------------------- */
/**
* Location change
*/
2020-02-22 15:24:15 +01:00
interface State {
url: URL // TODO: use URL!?
data?: ViewportOffset
}
function isInternalLink(el: HTMLAnchorElement | URL) {
return el.hostname === location.hostname
}
function isAnchorLink(el: HTMLAnchorElement | URL) {
return el.hash.length > 0
}
2020-02-22 15:24:15 +01:00
function compareState(
{ url: a }: State, { url: b }: State
) {
return a.href === b.href
}
2020-02-20 17:42:46 +01:00
// instant loading
if (config.feature.instant) {
/* Disable automatic scroll restoration, as it doesn't work nicely */
if ("scrollRestoration" in history)
history.scrollRestoration = "manual"
/* Resolve relative links for stability */
for (const selector of [
`link[rel="shortcut icon"]`,
`link[rel="stylesheet"]`
])
for (const el of getElements<HTMLLinkElement>(selector))
el.href = el.href
/* Intercept internal link clicks */
const internal$ = fromEvent<MouseEvent>(document.body, "click")
.pipe(
filter(ev => !(ev.metaKey || ev.ctrlKey)),
switchMap(ev => {
if (ev.target instanceof HTMLElement) {
const el = ev.target.closest("a")
if (el && isInternalLink(el)) {
if (!isAnchorLink(el))
ev.preventDefault()
return of(el.href)
2020-02-20 17:42:46 +01:00
}
}
return NEVER
}),
distinctUntilChanged(),
2020-02-22 15:24:15 +01:00
map<string, State>(href => ({ url: new URL(href) })),
share()
)
/* Intercept internal links to dispatch */
const dispatch$ = internal$
.pipe(
filter(({ url }) => !isAnchorLink(url)),
share()
)
/* Intercept popstate events (history back and forward) */
const popstate$ = fromEvent<PopStateEvent>(window, "popstate")
.pipe(
2020-02-22 15:24:15 +01:00
filter(ev => ev.state !== null),
map<PopStateEvent, State>(ev => ({
url: new URL(location.href),
data: ev.state
})),
share()
)
/* Emit location change */
merge(dispatch$, popstate$)
.pipe(
pluck("url")
)
.subscribe(location$)
/* Add dispatched link to history */
internal$
.pipe(
2020-02-22 15:24:15 +01:00
// TODO: must start with the current location and ignore the first emission
distinctUntilChanged(compareState),
filter(({ url }) => !isAnchorLink(url))
)
.subscribe(({ url }) => {
// console.log(`History.Push ${url}`)
history.pushState({}, "", url.toString())
})
2020-02-22 15:24:15 +01:00
// special case
merge(internal$, popstate$)
.pipe(
bufferCount(2, 1),
// filter(([prev, next]) => {
// return prev.url.href.match(next.url.href) !== null
// && isAnchorLink(prev.url)
// })
)
.subscribe(([prev, next]) => {
console.log(`<- ${prev.url}`)
console.log(`-> ${next.url}`)
if (
prev.url.href.match(next.url.href) !== null &&
isAnchorLink(prev.url)
) {
dialog$.next(`Potential Candidate: ${JSON.stringify(next.data)}`, ) // awesome debugging.
setViewportOffset(next.data || { y: 0 })
}
// console.log("Potential Candidate")
})
// .subscribe((x) => console.log(x[0].url.toString(), x[1].url.toString()))
// filter(([prev, next]) => {
// return prev.url.href.match(next.url.href) !== null
// && isAnchorLink(prev.url)
// }),
// map(([, next]) => next)
// // distinctUntilChanged(compareLocationChange),
// // filter(({ url }) => !isAnchorLink(url))
// )
// .subscribe(({ url }) => {
// console.log(`Restore ${url}`)
// })
/* Persist viewport offset in history before hash change */
viewport$
.pipe(
debounceTime(250),
distinctUntilKeyChanged("offset"),
)
.subscribe(({ offset }) => {
2020-02-22 15:24:15 +01:00
// console.log("Update", offset)
history.replaceState(offset, "")
})
/* */
merge(dispatch$, popstate$)
.pipe(
sample(document$),
withLatestFrom(document$),
)
2020-02-22 15:24:15 +01:00
.subscribe(([{ url, data }, { title, head }]) => {
console.log("Done", url.href, data)
// setDocumentTitle
document.title = title
// replace meta tags
for (const selector of [
`link[rel="canonical"]`,
`meta[name="author"]`,
`meta[name="description"]`
]) {
const next = getElement(selector, head)
const prev = getElement(selector, document.head)
if (
typeof next !== "undefined" &&
typeof prev !== "undefined"
) {
prev.replaceWith(next)
}
}
// // TODO: this doesnt work as expected
// if (!data) {
// const { hash } = new URL(href)
// if (hash) {
// const el = getElement(hash)
// if (typeof el !== "undefined") {
// el.scrollIntoView()
// return
// }
// }
// }
// console.log(ev)
// if (!data)
setViewportOffset(data || { y: 0 }) // push state!
})
// internal$.subscribe(({ url }) => {
// console.log(`Internal ${url}`)
// })
// dispatch$.subscribe(({ url }) => {
2020-02-22 15:24:15 +01:00
// console.log(`Dispatch ${url}`)
// })
popstate$.subscribe(({ url }) => {
2020-02-22 15:24:15 +01:00
console.log(`Popstate ${url.href}`, url)
2020-02-20 17:42:46 +01:00
})
}
2020-02-20 17:42:46 +01:00
/* ----------------------------------------------------------------------- */
// if we use a single tab outside of search, unhide all permalinks.
// TODO: experimental. necessary!?
keyboard$
.pipe(
filter(key => key.mode === "global" && ["Tab"].includes(key.type)),
take(1)
)
.subscribe(() => {
for (const link of getElements(".headerlink"))
link.style.visibility = "visible"
})
2020-02-02 17:18:18 +01:00
/* ----------------------------------------------------------------------- */
2019-12-22 17:30:55 +01:00
const state = {
2020-02-13 18:29:44 +01:00
search$,
2020-02-20 14:44:41 +01:00
clipboard$,
location$,
hash$,
keyboard$,
dialog$,
2019-12-22 17:30:55 +01:00
main$,
navigation$,
toc$,
tabs$,
2020-02-17 16:25:49 +01:00
hero$,
2020-02-20 14:44:41 +01:00
title$ // TODO: header title
}
2020-02-13 18:29:44 +01:00
const { ...rest } = state
merge(...values(rest))
2019-12-22 17:30:55 +01:00
.subscribe() // potential memleak <-- use takeUntil
2019-11-27 19:12:49 +01:00
2019-12-22 17:30:55 +01:00
return {
2020-02-12 19:13:03 +01:00
// agent,
2019-12-22 17:30:55 +01:00
state
}
2019-09-29 00:30:56 +02:00
}