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

392 lines
10 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 {
EMPTY,
2019-12-17 09:53:16 +01:00
merge,
of,
2020-02-17 11:35:36 +01:00
combineLatest,
animationFrameScheduler
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,
take
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
getElement,
watchToggle,
getElements,
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-13 18:29:44 +01:00
watchKeyboard,
watchToggleMap,
2020-02-14 17:36:31 +01:00
useToggle,
2020-02-17 17:20:08 +01:00
getActiveElement,
mayReceiveKeyboardEvents
2020-02-12 19:13:03 +01:00
} from "./observables"
2020-02-13 18:29:44 +01:00
import { setupSearchWorker } from "./workers"
2019-12-24 17:59:26 +01:00
import { renderSource } from "templates"
2020-02-17 11:35:36 +01:00
import { setToggle, 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-17 16:25:49 +01:00
watchComponentMap,
mountHeaderTitle
} from "components"
import { mountClipboard } from "./integrations/clipboard"
2020-02-17 17:20:08 +01:00
import { patchTables, patchDetails, patchScrollfix } from "patches"
2020-02-14 17:36:31 +01:00
import { takeIf, not, isConfig } from "utilities"
import { fetchSourceFacts } from "integrations/source"
import { renderDialog } from "templates/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")
2020-02-14 17:45:32 +01:00
/* ------------------------------------------------------------------------- */
2019-12-18 17:14:20 +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
2020-02-16 00:23:50 +01:00
// console.log(el)
2019-12-24 17:59:26 +01:00
if (!el)
return EMPTY
const data = sessionStorage.getItem("repository")
2019-12-24 17:59:26 +01:00
if (data) {
const x = JSON.parse(data)
return of(x)
}
return fetchSourceFacts(el.href)
.pipe(
tap(data => sessionStorage.setItem("repository", JSON.stringify(data)))
)
2019-12-24 17:59:26 +01:00
}
// memoize
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)}`)
2020-02-12 19:13:03 +01:00
// pass config here!?
const document$ = watchDocument()
2020-02-16 00:23:50 +01:00
const location$ = watchLocation()
2020-02-12 19:13:03 +01:00
const hash$ = watchLocationHash()
const keyboard$ = watchKeyboard()
2020-02-12 19:13:03 +01:00
const viewport$ = watchViewport()
2020-02-13 18:50:39 +01:00
const tablet$ = watchMedia("(min-width: 960px)")
const screen$ = watchMedia("(min-width: 1220px)")
2020-02-12 19:13:03 +01:00
/* ----------------------------------------------------------------------- */
2020-02-14 17:45:32 +01:00
const worker = setupSearchWorker(config.worker.search, {
base: config.base
})
/* ----------------------------------------------------------------------- */
watchToggleMap([
"drawer", /* Toggle for drawer */
"search" /* Toggle for search */
], { document$ })
2020-02-17 17:20:08 +01:00
2020-02-14 17:45:32 +01:00
watchComponentMap([
"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
/* Create header observable */
2020-02-13 18:29:44 +01:00
const header$ = useComponent("header")
.pipe(
2020-02-18 10:17:57 +01:00
mountHeader({ viewport$ })
)
2020-02-13 18:29:44 +01:00
const main$ = useComponent("main")
.pipe(
2020-02-13 18:50:39 +01:00
mountMain({ header$, viewport$ })
)
2020-02-12 19:13:03 +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-17 16:25:49 +01:00
mountSearch(worker, { keyboard$ }),
2019-11-27 19:12:49 +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-12 19:13:03 +01:00
mountNavigation({ main$, viewport$, screen$ })
2019-11-27 19:12:49 +01:00
)
2020-02-13 18:29:44 +01:00
const toc$ = useComponent("toc")
.pipe(
2020-02-12 19:13:03 +01:00
mountTableOfContents({ header$, main$, viewport$, tablet$ })
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-12 19:13:03 +01:00
mountTabs({ header$, viewport$, screen$ })
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(
mountHero({ header$, viewport$ })
2019-11-27 19:12:49 +01:00
)
2020-02-17 16:25:49 +01:00
// TODO: make this part of mountHeader!?
const title$ = useComponent("header-title")
.pipe(
2020-02-17 16:25:49 +01:00
mountHeaderTitle({ header$, viewport$ })
)
2020-02-13 23:42:12 +01:00
/* ----------------------------------------------------------------------- */
// must be in another scope!
const dialog = renderDialog("Copied to Clipboard")
// snackbar for copy to clipboard
2020-02-17 17:20:08 +01:00
mountClipboard({ document$ })
.pipe(
switchMap(ev => {
ev.clearSelection()
return useComponent("container")
.pipe(
tap(el => el.appendChild(dialog)), // only set text on dialog... render once...
observeOn(animationFrameScheduler),
tap(() => dialog.dataset.mdState = "open"),
delay(2000),
tap(() => dialog.dataset.mdState = ""),
delay(400),
tap(() => dialog.remove())
)
})
)
2020-02-17 17:20:08 +01:00
.subscribe()
patchTables({ document$ })
.subscribe()
patchDetails({ document$ })
.subscribe()
/* Force 1px scroll offset to trigger overflow scrolling */
if (navigator.userAgent.match(/(iPad|iPhone|iPod)/g))
patchScrollfix({ document$ })
.subscribe()
// TODO: general keyboard handler...
// put into main!?
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-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-16 00:23:50 +01:00
// TODO:
hash$
.pipe(
switchMap(hash => {
return 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...
tap(() => {
location.hash = " "
location.hash = hash
}) // encapsulate this...
)
})
)
.subscribe()
// TODO: patch details!
/* Open details after anchor jump */
merge(hash$, of(location.hash)) // getLocationHash
.subscribe(hash => {
const el = getElement(hash)
console.log("jump to", hash)
if (typeof el !== "undefined") {
const parent = el.closest("details")
if (parent && !parent.open) { // only if it is not open!
parent.open = true
/* Hack: force reload for repositioning */ // TODO. what happens here!?
location.hash = "" // reset
requestAnimationFrame(() => {
location.hash = hash // tslint:disable-line
})
// TODO: setLocationHash() + forceLocationHashChange
}
}
})
2020-02-17 17:20:08 +01:00
// Scroll lock
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
return of(document.body)
.pipe(
delay(active ? 400 : 100),
observeOn(animationFrameScheduler),
tap(el => active
? setScrollLock(el, y)
: resetScrollLock(el)
)
)
})
)
.subscribe()
/* ----------------------------------------------------------------------- */
2020-02-17 17:20:08 +01:00
// General keyboard handlers
keyboard$
.pipe(
takeIf(not(toggle$.pipe(switchMap(watchToggle)))),
filter(key => ["s", "f"].includes(key.type)),
withLatestFrom(toggle$)
)
.subscribe(([key, toggle]) => {
const el = getActiveElement()
if (!(el && mayReceiveKeyboardEvents(el))) {
setToggle(toggle, true)
key.claim()
}
})
2020-02-02 17:18:18 +01:00
// if we use a single tab outside of search, unhide all permalinks.
keyboard$
.pipe(
takeIf(not(toggle$.pipe(switchMap(watchToggle)))),
filter(key => ["Tab"].includes(key.type)),
take(1)
)
.subscribe(() => {
for (const link of getElements(".headerlink"))
link.style.visibility = "visible"
})
// build a notification component! feed txt into it...
2020-02-17 17:20:08 +01:00
/* ----------------------------------------------------------------------- */
2020-02-02 17:18:18 +01:00
2020-02-13 23:42:12 +01:00
// TODO: WIP repo rendering
repository().subscribe(facts => {
if (facts.length) {
const sources = getElements(".md-source__repository")
sources.forEach(repo => {
repo.dataset.mdState = "done"
repo.appendChild(
renderSource(facts)
)
})
}
})
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$,
2019-12-22 17:30:55 +01:00
main$,
navigation$,
toc$,
tabs$,
2020-02-17 16:25:49 +01:00
hero$,
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
}