1
0
mirror of https://github.com/squidfunk/mkdocs-material.git synced 2024-11-12 01:50:52 +01:00

Refactored table of contents component

This commit is contained in:
squidfunk 2020-02-14 12:27:13 +01:00
parent f150a6c9b5
commit d934bb37c0
5 changed files with 74 additions and 82 deletions

View File

@ -22,4 +22,3 @@
export * from "./hero" export * from "./hero"
export * from "./tabs" export * from "./tabs"
export * from "./toc"

View File

@ -1,23 +0,0 @@
/*
* Copyright (c) 2016-2020 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.
*/
export * from "./_"

View File

@ -25,3 +25,4 @@ export * from "./header"
export * from "./main" export * from "./main"
export * from "./navigation" export * from "./navigation"
export * from "./search" export * from "./search"
export * from "./toc"

View File

@ -40,17 +40,17 @@ import {
* ------------------------------------------------------------------------- */ * ------------------------------------------------------------------------- */
/** /**
* Navigation for breakpoint below screen * Navigation below screen breakpoint
*/ */
export interface NavigationBelowScreen { export interface NavigationBelowScreen {
layer: NavigationLayer /* Navigation layer */ layer: NavigationLayer /* Active layer */
} }
/** /**
* Navigation for breakpoint above screen * Navigation above screen breakpoint
*/ */
export interface NavigationAboveScreen { export interface NavigationAboveScreen {
sidebar: Sidebar /* Navigation sidebar */ sidebar: Sidebar /* Sidebar */
} }
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
@ -71,7 +71,7 @@ export type Navigation =
*/ */
interface MountOptions { interface MountOptions {
main$: Observable<Main> /* Main area observable */ main$: Observable<Main> /* Main area observable */
viewport$: Observable<Viewport> /* Viewport offset observable */ viewport$: Observable<Viewport> /* Viewport observable */
screen$: Observable<boolean> /* Screen media observable */ screen$: Observable<boolean> /* Screen media observable */
} }
@ -87,22 +87,22 @@ interface MountOptions {
* @return Operator function * @return Operator function
*/ */
export function mountNavigation( export function mountNavigation(
options: MountOptions { main$, viewport$, screen$ }: MountOptions
): OperatorFunction<HTMLElement, Navigation> { ): OperatorFunction<HTMLElement, Navigation> {
return pipe( return pipe(
switchMap(el => options.screen$ switchMap(el => screen$
.pipe( .pipe(
switchMap(screen => { switchMap(screen => {
/* Mount sidebar for screen and above */ /* Mount navigation above screen breakpoint */
if (screen) { if (screen) {
return watchSidebar(el, options) return watchSidebar(el, { main$, viewport$ })
.pipe( .pipe(
paintSidebar(el), paintSidebar(el),
map(sidebar => ({ sidebar })) map(sidebar => ({ sidebar }))
) )
/* Mount navigation layer otherwise */ /* Mount navigation below screen breakpoint */
} else { } else {
const els = getElements("nav", el) const els = getElements("nav", el)
return watchNavigationLayer(els) return watchNavigationLayer(els)

View File

@ -20,8 +20,14 @@
* IN THE SOFTWARE. * IN THE SOFTWARE.
*/ */
import { Observable, OperatorFunction, combineLatest, pipe } from "rxjs" import {
import { map, shareReplay } from "rxjs/operators" Observable,
OperatorFunction,
combineLatest,
of,
pipe
} from "rxjs"
import { map, shareReplay, switchMap } from "rxjs/operators"
import { import {
AnchorList, AnchorList,
@ -35,86 +41,95 @@ import {
watchAnchorList, watchAnchorList,
watchSidebar watchSidebar
} from "observables" } from "observables"
import { switchMapIf } from "utilities"
/* ---------------------------------------------------------------------------- /* ----------------------------------------------------------------------------
* Types * Types
* ------------------------------------------------------------------------- */ * ------------------------------------------------------------------------- */
/** /**
* Table of contents state * Table of contents below tablet breakpoint
*/ */
export interface TableOfContentsState { export interface TableOfContentsBelowTablet {} // tslint:disable-line
sidebar: Sidebar /* Sidebar state */
/**
* Table of contents above tablet breakpoint
*/
export interface TableOfContentsAboveTablet {
sidebar: Sidebar /* Sidebar */
anchors: AnchorList /* Anchor list */ anchors: AnchorList /* Anchor list */
} }
/* ------------------------------------------------------------------------- */
/**
* Table of contents
*/
export type TableOfContents =
| TableOfContentsBelowTablet
| TableOfContentsAboveTablet
/* ---------------------------------------------------------------------------- /* ----------------------------------------------------------------------------
* Helper types * Helper types
* ------------------------------------------------------------------------- */ * ------------------------------------------------------------------------- */
/** /**
* Options * Mount options
*/ */
interface Options { interface MountOptions {
header$: Observable<Header> /* Header observable */ header$: Observable<Header> /* Header observable */
main$: Observable<Main> /* Main area observable */ main$: Observable<Main> /* Main area observable */
viewport$: Observable<Viewport> /* Viewport observable */ viewport$: Observable<Viewport> /* Viewport observable */
tablet$: Observable<boolean> /* Media tablet observable */ tablet$: Observable<boolean> /* Tablet media observable */
} }
/* ---------------------------------------------------------------------------- /* ----------------------------------------------------------------------------
* Functions * Functions
* ------------------------------------------------------------------------- */ * ------------------------------------------------------------------------- */
/**
* Watch table of contents
*
* @param el - Table of contents element
* @param agent - Agent
* @param options - Options
*
* @return Table of contents state observable
*/
export function watchTableOfContents(
el: HTMLElement, { header$, main$, viewport$ }: Options
): Observable<TableOfContentsState> {
/* Watch and paint sidebar */
const sidebar$ = watchSidebar(el, { main$, viewport$ })
.pipe(
paintSidebar(el)
)
/* Watch and paint anchor list (scroll spy) */
const els = getElements<HTMLAnchorElement>(".md-nav__link", el)
const anchors$ = watchAnchorList(els, { header$, viewport$ })
.pipe(
paintAnchorList(els)
)
/* Combine into a single hot observable */
return combineLatest([sidebar$, anchors$])
.pipe(
map(([sidebar, anchors]) => ({ sidebar, anchors }))
)
}
/* ------------------------------------------------------------------------- */
/** /**
* Mount table of contents from source observable * Mount table of contents from source observable
* *
* @param agent - Agent
* @param options - Options * @param options - Options
* *
* @return Operator function * @return Operator function
*/ */
export function mountTableOfContents( export function mountTableOfContents(
options: Options { header$, main$, viewport$, tablet$}: MountOptions
): OperatorFunction<HTMLElement, TableOfContentsState> { ): OperatorFunction<HTMLElement, TableOfContents> {
return pipe( return pipe(
switchMapIf(options.tablet$, el => watchTableOfContents(el, options)), switchMap(el => tablet$
.pipe(
switchMap(tablet => {
/* Mount table of contents above tablet breakpoint */
if (tablet) {
const els = getElements<HTMLAnchorElement>(".md-nav__link", el)
/* Watch and paint sidebar */
const sidebar$ = watchSidebar(el, { main$, viewport$ })
.pipe(
paintSidebar(el)
)
/* Watch and paint anchor list (scroll spy) */
const anchors$ = watchAnchorList(els, { header$, viewport$ })
.pipe(
paintAnchorList(els)
)
/* Combine into a single hot observable */
return combineLatest([sidebar$, anchors$])
.pipe(
map(([sidebar, anchors]) => ({ sidebar, anchors }))
)
/* Mount table of contents below tablet breakpoint */
} else {
return of({})
}
})
)
),
shareReplay(1) shareReplay(1)
) )
} }