Merged features tied to Royal Gold funding goal

This commit is contained in:
squidfunk
2022-07-20 17:25:55 +02:00
parent 5a9c295822
commit de438d5d9d
55 changed files with 1205 additions and 120 deletions

View File

@@ -30,6 +30,7 @@ import { getElement, getLocation } from "~/browser"
* Feature flag
*/
export type Flag =
| "announce.dismiss" /* Dismissable announcement bar */
| "content.code.annotate" /* Code annotations */
| "content.tabs.link" /* Link content tabs */
| "header.autohide" /* Hide header */

View File

@@ -56,7 +56,9 @@ import {
import {
getComponentElement,
getComponentElements,
mountAnnounce,
mountBackToTop,
mountConsent,
mountContent,
mountDialog,
mountHeader,
@@ -177,6 +179,10 @@ const main$ = document$
/* Set up control component observables */
const control$ = merge(
/* Consent */
...getComponentElements("consent")
.map(el => mountConsent(el, { target$ })),
/* Dialog */
...getComponentElements("dialog")
.map(el => mountDialog(el, { alert$ })),
@@ -201,6 +207,10 @@ const control$ = merge(
/* Set up content component observables */
const content$ = defer(() => merge(
/* Announcement bar */
...getComponentElements("announce")
.map(el => mountAnnounce(el)),
/* Content */
...getComponentElements("content")
.map(el => mountContent(el, { target$, print$ })),

View File

@@ -32,6 +32,7 @@ import { getElement, getElements } from "~/browser"
export type ComponentType =
| "announce" /* Announcement bar */
| "container" /* Container */
| "consent" /* Consent */
| "content" /* Content */
| "dialog" /* Dialog */
| "header" /* Header */
@@ -76,6 +77,7 @@ export type Component<
interface ComponentTypeMap {
"announce": HTMLElement /* Announcement bar */
"container": HTMLElement /* Container */
"consent": HTMLElement /* Consent */
"content": HTMLElement /* Content */
"dialog": HTMLElement /* Dialog */
"header": HTMLElement /* Header */

View File

@@ -0,0 +1,110 @@
/*
* Copyright (c) 2016-2022 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.
*/
import {
EMPTY,
Observable,
Subject,
defer,
finalize,
fromEvent,
map,
startWith,
tap
} from "rxjs"
import { feature } from "~/_"
import { getElement } from "~/browser"
import { Component } from "../_"
/* ----------------------------------------------------------------------------
* Types
* ------------------------------------------------------------------------- */
/**
* Announcement bar
*/
export interface Announce {
hash: number /* Content hash */
}
/* ----------------------------------------------------------------------------
* Functions
* ------------------------------------------------------------------------- */
/**
* Watch announcement bar
*
* @param el - Announcement bar element
*
* @returns Announcement bar observable
*/
export function watchAnnounce(
el: HTMLElement
): Observable<Announce> {
const button = getElement(".md-typeset > :first-child", el)
return fromEvent(button, "click", { once: true })
.pipe(
map(() => getElement(".md-typeset", el)),
map(content => ({ hash: __md_hash(content.innerHTML) }))
)
}
/**
* Mount announcement bar
*
* @param el - Announcement bar element
*
* @returns Announcement bar component observable
*/
export function mountAnnounce(
el: HTMLElement
): Observable<Component<Announce>> {
if (!feature("announce.dismiss") || !el.childElementCount)
return EMPTY
/* Mount component on subscription */
return defer(() => {
const push$ = new Subject<Announce>()
push$
.pipe(
startWith({ hash: __md_get<number>("__announce") })
)
.subscribe(({ hash }) => {
if (hash && hash === (__md_get<number>("__announce") ?? hash)) {
el.hidden = true
/* Persist preference in local storage */
__md_set<number>("__announce", hash)
}
})
/* Create and return component */
return watchAnnounce(el)
.pipe(
tap(state => push$.next(state)),
finalize(() => push$.complete()),
map(state => ({ ref: el, ...state }))
)
})
}

View File

@@ -0,0 +1,108 @@
/*
* Copyright (c) 2016-2022 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.
*/
import {
Observable,
Subject,
finalize,
map,
tap
} from "rxjs"
import { Component } from "../_"
/* ----------------------------------------------------------------------------
* Types
* ------------------------------------------------------------------------- */
/**
* Consent
*/
export interface Consent {
hidden: boolean /* Consent is hidden */
}
/* ----------------------------------------------------------------------------
* Helper types
* ------------------------------------------------------------------------- */
/**
* Watch options
*/
interface WatchOptions {
target$: Observable<HTMLElement> /* Target observable */
}
/**
* Mount options
*/
interface MountOptions {
target$: Observable<HTMLElement> /* Target observable */
}
/* ----------------------------------------------------------------------------
* Functions
* ------------------------------------------------------------------------- */
/**
* Watch consent
*
* @param el - Consent element
* @param options - Options
*
* @returns Consent observable
*/
export function watchConsent(
el: HTMLElement, { target$ }: WatchOptions
): Observable<Consent> {
return target$
.pipe(
map(target => ({ hidden: target !== el }))
)
}
/* ------------------------------------------------------------------------- */
/**
* Mount consent
*
* @param el - Consent element
* @param options - Options
*
* @returns Consent component observable
*/
export function mountConsent(
el: HTMLElement, options: MountOptions
): Observable<Component<Consent>> {
const internal$ = new Subject<Consent>()
internal$.subscribe(({ hidden }) => {
el.hidden = hidden
})
/* Create and return component */
return watchConsent(el, options)
.pipe(
tap(state => internal$.next(state)),
finalize(() => internal$.complete()),
map(state => ({ ref: el, ...state }))
)
}

View File

@@ -21,6 +21,8 @@
*/
export * from "./_"
export * from "./announce"
export * from "./consent"
export * from "./content"
export * from "./dialog"
export * from "./header"