1
0
mirror of https://github.com/squidfunk/mkdocs-material.git synced 2025-01-11 21:52:14 +01:00

584 lines
16 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-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 {
EMPTY,
2019-12-17 09:53:16 +01:00
Observable,
merge,
of,
fromEvent,
2020-02-12 19:13:03 +01:00
OperatorFunction,
pipe
2019-11-27 19:12:49 +01:00
} from "rxjs"
2019-12-17 09:53:16 +01:00
import {
delay,
filter,
map,
pluck,
switchMap,
switchMapTo,
tap,
2020-02-02 16:19:01 +01:00
distinctUntilKeyChanged,
2020-02-12 19:13:03 +01:00
shareReplay
2019-11-27 19:12:49 +01:00
} from "rxjs/operators"
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,
mountTableOfContents,
mountTabs,
switchComponent,
watchComponentMap,
2019-12-18 17:14:20 +01:00
} from "./components"
2019-09-29 00:30:56 +02:00
import {
2020-02-12 19:13:03 +01:00
watchHeader,
watchSearchQuery,
watchSearchReset,
2019-12-18 17:14:20 +01:00
getElement,
watchToggle,
setToggle,
getElements,
2020-01-26 16:03:49 +01:00
watchMedia,
2020-02-12 19:13:03 +01:00
watchDocument,
watchLocationHash,
watchMain,
watchViewport,
watchKeyboard
} from "./observables"
import {
2020-02-12 19:13:03 +01:00
isSearchResultMessage,
setupSearchWorker
} from "./workers"
2019-12-24 17:59:26 +01:00
import { renderSource } from "templates"
2020-02-12 19:13:03 +01:00
import { not, takeIf } from "utilities"
2020-02-02 17:18:18 +01:00
import { renderClipboard } from "templates/clipboard"
2020-02-12 19:13:03 +01:00
import { fetchGitHubStats } from "modules/source/github"
import { mountNavigation } from "components2/navigation"
import { mountSearchResult } from "components2/search"
import { renderTable } from "templates/table"
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")
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
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-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
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) {
return value > 999
? `${(value / 1000).toFixed(1)}k`
: `${(value)}`
2019-12-24 17:59:26 +01:00
}
// github repository...
const [, user, repo] = el.href.match(/^.+github\.com\/([^\/]+)\/?([^\/]+)?.*$/i)
2019-12-24 17:59:26 +01:00
2020-02-12 19:13:03 +01:00
// storage memoization!?
// get, if not available, exec and persist
// getOrRetrieve... storage$.
2019-12-24 17:59:26 +01:00
// Show repo stats
if (user && repo) {
2020-02-12 19:13:03 +01:00
return fetchGitHubStats(user, repo)
2019-12-24 17:59:26 +01:00
.pipe(
2020-02-12 19:13:03 +01:00
map(({ stargazers_count, forks_count }) => ([
`${format(stargazers_count || 0)} Stars`,
`${format(forks_count || 0)} Forks`
])),
tap(data => sessionStorage.setItem("repository", JSON.stringify(data)))
2019-12-24 17:59:26 +01:00
)
// Show user or organization stats
} else if (user) {
2020-02-12 19:13:03 +01:00
return fetchGitHubStats(user)
2019-12-24 17:59:26 +01:00
.pipe(
2020-02-12 19:13:03 +01:00
map(({ public_repos }) => ([
`${format(public_repos || 0)} Repositories`
])),
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-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-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")
sources.forEach(repo => {
repo.dataset.mdState = "done"
repo.appendChild(
renderSource(facts)
)
})
2019-12-24 17:59:26 +01:00
}
})
2020-02-12 19:13:03 +01:00
// pass config here!?
const document$ = watchDocument()
const hash$ = watchLocationHash()
const viewport$ = watchViewport()
const screen$ = watchMedia("(min-width: 960px)")
const tablet$ = watchMedia("(min-width: 1220px)")
/* ----------------------------------------------------------------------- */
/* Create component map observable */
2020-02-12 19:13:03 +01:00
const components$ = watchComponentMap(names, { document$ })
const component = <T extends HTMLElement>(name: Component): Observable<T> => {
2019-11-27 19:12:49 +01:00
return components$
.pipe(
switchComponent<T>(name)
2019-11-27 19:12:49 +01:00
)
}
/* Create header observable */
const header$ = component("header") // TODO:!
.pipe(
switchMap(watchHeader)
)
2019-09-29 00:30:56 +02: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
)
)
)
.subscribe()
2020-02-12 19:13:03 +01:00
// DONE
const main$ = component("main")
.pipe(
2020-02-12 19:13:03 +01:00
switchMap(el => watchMain(el, { header$, viewport$ })),
shareReplay(1) // TODO: mount!?
)
2020-02-12 19:13:03 +01:00
// ---------------------------------------------------------------------------
/* ----------------------------------------------------------------------- */
const drawer = getElement<HTMLInputElement>("[data-md-toggle=drawer]")!
/* ----------------------------------------------------------------------- */
// build a single search observable???
2019-12-22 17:30:55 +01:00
const query$ = component<HTMLInputElement>("search-query")
.pipe(
2020-02-12 19:13:03 +01:00
switchMap(el => watchSearchQuery(el))
)
2020-02-12 19:13:03 +01:00
const sw = setupSearchWorker(config.worker.search, {
base: config.base,
query$
})
const result$ = sw.rx$ // move worker initialization into mountSearch ?
.pipe(
filter(isSearchResultMessage),
pluck("data")
)
2020-02-12 19:13:03 +01:00
const search = getElement<HTMLInputElement>("[data-md-toggle=search]")!
const searchActive$ = watchToggle(search)
.pipe(
delay(400)
)
2019-12-18 17:14:20 +01:00
2019-12-22 17:30:55 +01:00
query$
.pipe(
2020-02-12 19:13:03 +01:00
distinctUntilKeyChanged("focus"),
2019-12-22 17:30:55 +01:00
tap(query => {
if (query.focus)
2020-02-12 19:13:03 +01:00
setToggle(search, query.focus) // paintSearchQuery?
// console.log(query)
2019-12-22 17:30:55 +01:00
})
)
.subscribe()
2020-02-12 19:13:03 +01:00
// implement toggle function that returns the toggles as observable...
const reset$ = component("search-reset")
2019-11-27 19:12:49 +01:00
.pipe(
2020-02-12 19:13:03 +01:00
switchMap(watchSearchReset)
2019-11-27 19:12:49 +01:00
)
2020-02-12 19:13:03 +01:00
/* ----------------------------------------------------------------------- */
// DONE (partly)
2019-12-22 17:30:55 +01:00
const navigation$ = component("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
)
2019-12-22 17:30:55 +01:00
const toc$ = component("toc")
.pipe(
2020-02-12 19:13:03 +01:00
mountTableOfContents({ header$, main$, viewport$, tablet$ })
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-12 19:13:03 +01:00
mountSearchResult({ viewport$, result$, query$: query$.pipe(
2020-02-02 16:19:01 +01:00
distinctUntilKeyChanged("value"),
) })
2019-12-22 17:30:55 +01:00
) // temporary fix
2020-02-12 19:13:03 +01:00
// mount hideable...
2019-12-22 17:30:55 +01:00
const tabs$ = component("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
)
const hero$ = component("hero")
.pipe(
2020-02-12 19:13:03 +01:00
mountHero({ header$, viewport$, screen$ })
2019-11-27 19:12:49 +01:00
)
2020-02-12 19:13:03 +01:00
// function watchKeyboard
const key$ = watchKeyboard()
2020-02-02 16:19:01 +01:00
// shortcodes
key$
.pipe(
2020-02-02 16:19:01 +01:00
takeIf(not(searchActive$))
)
2020-02-12 19:13:03 +01:00
.subscribe(key => {
2020-02-02 16:19:01 +01:00
if (
document.activeElement && (
["TEXTAREA", "SELECT", "INPUT"].includes(
document.activeElement.tagName
) ||
document.activeElement instanceof HTMLElement &&
document.activeElement.isContentEditable
)
) {
// do nothing...
} else {
2020-02-12 19:13:03 +01:00
if (key.type === "KeyS" || key.type === "KeyF") {
2020-02-02 16:19:01 +01:00
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 */
2020-02-12 19:13:03 +01:00
tap(key => {
console.log("jo", key)
if (key.type === "Enter") {
2020-02-02 16:19:01 +01:00
if (document.activeElement === getElement("[data-md-component=search-query]")) {
2020-02-12 19:13:03 +01:00
key.claim()
2020-02-02 16:19:01 +01:00
// intercept hash change after search closed
} else {
setToggle(search, false)
}
}
2020-02-12 19:13:03 +01:00
if (key.type === "ArrowUp" || key.type === "ArrowDown") {
2020-02-02 16:19:01 +01:00
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))
2020-02-12 19:13:03 +01:00
const x = Math.max(0, (i + active.length + (key.type === "ArrowUp" ? -1 : +1)) % active.length)
2020-02-02 16:19:01 +01:00
active[x].focus()
/* Prevent scrolling of page */
2020-02-12 19:13:03 +01:00
key.claim()
2020-02-02 16:19:01 +01:00
2020-02-12 19:13:03 +01:00
} else if (key.type === "Escape" || key.type === "Tab") {
2020-02-02 16:19:01 +01:00
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
// 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")),
tap(el => el.focus()) // TODO: only if element isnt focused! setFocus? setToggle?
2019-11-27 19:12:49 +01:00
)
.subscribe()
2020-02-12 19:13:03 +01:00
// focusable -> setFocus(true, false)
2019-12-22 17:30:55 +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")
Array.prototype.forEach.call(details, detail => {
detail.setAttribute("open", "")
})
})
2020-02-02 16:51:42 +01:00
// Close drawer and search on hash change
2020-02-12 19:13:03 +01:00
hash$.subscribe(() => {
2020-02-02 16:51:42 +01:00
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-02-12 19:13:03 +01:00
/* Clipboard.js integration */
2020-02-02 17:18:18 +01:00
if (Clipboard.isSupported()) {
2020-02-12 19:13:03 +01:00
const blocks = getElements("pre > code")
for (const [index, block] of blocks.entries()) {
const parent = block.parentElement!
parent.id = `__code_${index}`
parent.insertBefore(renderClipboard(parent.id), block)
}
2020-02-02 17:18:18 +01:00
/* Initialize Clipboard listener */
2020-02-12 19:13:03 +01:00
const copy = new Clipboard(".md-clipboard") // create observable...
2020-02-02 17:18:18 +01:00
/* Success handler */
2020-02-12 19:13:03 +01:00
// copy.on("success", action => {
// alert("Copied to clipboard") // TODO: integrate snackbar
// // TODO: add a snackbar/notification
2020-02-02 17:18:18 +01:00
2020-02-12 19:13:03 +01:00
// })
2020-02-02 17:18:18 +01:00
}
2020-02-12 19:13:03 +01:00
/* Wrap all data tables for better overflow scrolling */
const tables = getElements<HTMLTableElement>("table:not([class])")
const placeholder = document.createElement("table")
tables.forEach(table => {
table.replaceWith(placeholder)
placeholder.replaceWith(renderTable(table))
})
// search lock
let lastOffset = 0
tablet$.pipe(
switchMap(active => {
return !active ? watchToggle(search) : EMPTY
}),
switchMap(toggle => {
if (toggle) {
console.log("ACTIVE")
return of(document.body)
.pipe(
tap(() => lastOffset = window.pageYOffset),
delay(400),
tap(() => {
window.scrollTo(0, 0),
console.log("scrolled... to top, locked body")
document.body.dataset.mdState = "lock"
})
)
} else {
console.log("INACTIVE")
return of(document.body)
.pipe(
tap(() => document.body.dataset.mdState = ""),
delay(100),
tap(() => {
window.scrollTo(0, lastOffset)
})
)
}
return EMPTY
})
)
.subscribe(x => console.log("SEARCHLOCK", x))
2020-02-02 17:18:18 +01:00
/* ----------------------------------------------------------------------- */
2020-02-12 19:13:03 +01:00
// get headerHEIGHT! only if header is sticky!
// // lockHeader at...
// const direction$ = agent.viewport.offset$.pipe(
// bufferCount(2, 1), // determine scroll direction
// map(([{ y: y0 }, { y: y1 }]) => y1 > y0),
// distinctUntilChanged(),
// )
// document.body.style.minHeight = "100vh"
// // if true => then + HEADER. otherwise not
// let last = 0
// combineLatest([direction$, header$]).pipe(
// tap(([direction, { height }]) => { // TODO: only if sticky!
// const offset = 48
// console.log(window.pageYOffset, height, last)
// if (Math.abs(window.pageYOffset - last) < height + offset) { // TODO: add sensitivity offset!
// return
// }
// if (direction) {
// document.body.style.height = `${window.pageYOffset + offset + height}px`
// } else {
// document.body.style.height = `${window.pageYOffset - offset}px` // offset
// }
// last = window.pageYOffset
// })
// )
// .subscribe()
// // toiggle
2020-02-02 18:28:58 +01:00
/* ----------------------------------------------------------------------- */
2019-12-22 17:30:55 +01:00
const state = {
search: {
query$,
result$: resultComponent$,
reset$,
},
main$,
navigation$,
toc$,
tabs$,
hero$
}
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 {
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
}
2020-02-12 19:13:03 +01:00
// function mountSearchQuery(
// ): OperatorFunction<HTMLInputElement, SearchQuery> {
// return pipe(
// switchMap(el => watchSearchQuery(el))
// )
// }