Refactored patches by making them self-contained

This commit is contained in:
squidfunk 2020-02-19 10:14:59 +01:00
parent d1afa51726
commit 57817eac99
4 changed files with 30 additions and 46 deletions

View File

@ -206,15 +206,11 @@ export function initialize(config: unknown) {
.subscribe() .subscribe()
patchTables({ document$ }) patchTables({ document$ })
.subscribe()
patchDetails({ document$, hash$ }) patchDetails({ document$, hash$ })
.subscribe()
/* Force 1px scroll offset to trigger overflow scrolling */ /* Force 1px scroll offset to trigger overflow scrolling */
if (navigator.userAgent.match(/(iPad|iPhone|iPod)/g)) if (navigator.userAgent.match(/(iPad|iPhone|iPod)/g))
patchScrollfix({ document$ }) patchScrollfix({ document$ })
.subscribe()

View File

@ -21,12 +21,18 @@
*/ */
import { identity } from "ramda" import { identity } from "ramda"
import { Observable, animationFrameScheduler, fromEvent, merge, of } from "rxjs" import {
Observable,
animationFrameScheduler,
fromEvent,
merge,
of
} from "rxjs"
import { import {
filter, filter,
map, map,
mapTo,
observeOn, observeOn,
shareReplay,
switchMap, switchMap,
switchMapTo, switchMapTo,
tap tap
@ -57,16 +63,13 @@ interface MountOptions {
* printing, so the whole content of the page is included. * printing, so the whole content of the page is included.
* *
* @param options - Options * @param options - Options
*
* @return Details elements observable
*/ */
export function patchDetails( export function patchDetails(
{ document$, hash$ }: MountOptions { document$, hash$ }: MountOptions
): Observable<HTMLDetailsElement[]> { ): void {
const els$ = document$ const els$ = document$
.pipe( .pipe(
map(() => getElements<HTMLDetailsElement>("details")), map(() => getElements<HTMLDetailsElement>("details"))
shareReplay(1)
) )
/* Open all details before printing */ /* Open all details before printing */
@ -99,14 +102,11 @@ export function patchDetails(
/* Defer anchor jump to next animation frame */ /* Defer anchor jump to next animation frame */
observeOn(animationFrameScheduler), observeOn(animationFrameScheduler),
tap(() => { mapTo(hash)
)
)
)
.subscribe(hash => {
location.hash = hash location.hash = hash
}) })
)
)
)
.subscribe()
/* Return details elements */
return els$
} }

View File

@ -21,13 +21,7 @@
*/ */
import { Observable, fromEvent, merge } from "rxjs" import { Observable, fromEvent, merge } from "rxjs"
import { import { map, mapTo, switchMap } from "rxjs/operators"
map,
mapTo,
shareReplay,
switchMap,
tap
} from "rxjs/operators"
import { getElements } from "observables" import { getElements } from "observables"
@ -56,19 +50,18 @@ interface MountOptions {
* @see https://bit.ly/2SCtAOO - Original source * @see https://bit.ly/2SCtAOO - Original source
* *
* @param options - Options * @param options - Options
*
* @return Elements observable
*/ */
export function patchScrollfix( export function patchScrollfix(
{ document$ }: MountOptions { document$ }: MountOptions
): Observable<HTMLElement> { ): void {
return document$ document$
.pipe( .pipe(
map(() => getElements("[data-md-scrollfix]")), map(() => getElements("[data-md-scrollfix]")),
switchMap(els => merge( switchMap(els => merge(...els.map(el => (
...els.map(el => fromEvent(el, "touchstart").pipe(mapTo(el)))) fromEvent(el, "touchstart").pipe(mapTo(el))
), ))))
tap(el => { )
.subscribe(el => {
const top = el.scrollTop const top = el.scrollTop
/* We're at the top of the container */ /* We're at the top of the container */
@ -79,7 +72,5 @@ export function patchScrollfix(
} else if (top + el.offsetHeight === el.scrollHeight) { } else if (top + el.offsetHeight === el.scrollHeight) {
el.scrollTop = top - 1 el.scrollTop = top - 1
} }
}), })
shareReplay(1)
)
} }

View File

@ -21,7 +21,7 @@
*/ */
import { Observable } from "rxjs" import { Observable } from "rxjs"
import { map, shareReplay, tap } from "rxjs/operators" import { map } from "rxjs/operators"
import { getElements } from "observables" import { getElements } from "observables"
import { renderTable } from "templates" import { renderTable } from "templates"
@ -48,22 +48,19 @@ interface MountOptions {
* scrolling on smaller screen sizes. * scrolling on smaller screen sizes.
* *
* @param options - Options * @param options - Options
*
* @return Table elements observable
*/ */
export function patchTables( export function patchTables(
{ document$ }: MountOptions { document$ }: MountOptions
): Observable<HTMLTableElement[]> { ): void {
const placeholder = document.createElement("table") const placeholder = document.createElement("table")
return document$ document$
.pipe( .pipe(
map(() => getElements<HTMLTableElement>("table:not([class])")), map(() => getElements<HTMLTableElement>("table:not([class])"))
tap(els => { )
.subscribe(els => {
for (const el of els) { for (const el of els) {
el.replaceWith(placeholder) el.replaceWith(placeholder)
placeholder.replaceWith(renderTable(el)) placeholder.replaceWith(renderTable(el))
} }
}), })
shareReplay(1)
)
} }