import { Observable , Subject , animationFrameScheduler , auditTime , combineLatest , defer , distinctUntilChanged , finalize , map , tap , withLatestFrom } from "rxjs" ; import { Viewport , getElement , getElementOffset } from "~/browser" ; import { Component } from "../_" ; import { Header } from "../header" ; import { Main } from "../main" ; /**
 * Sidebar
 */ export interface Sidebar { height : number ; locked : boolean ; } /**
 * Watch options
 */ interface WatchOptions { viewport$ : Observable < Viewport > ; main$ : Observable < Main > ; } /**
 * Mount options
 */ interface MountOptions { viewport$ : Observable < Viewport > ; header$ : Observable < Header > ; main$ : Observable < Main > ; } param el param options returns export function watchSidebar ( el , { viewport$ , main$ } ) { const parent = el . parentElement ! ; const adjust = parent . offsetTop - parent . parentElement ! . offsetTop ; return combineLatest ( [ main$ , viewport$ ] ) . pipe ( map ( ( [ { offset , height } , { offset : { y } } ] ) => { height = height + Math . min ( adjust , Math . max ( 0 , y - offset ) ) - adjust ; return { height , locked : y >= offset + adjust } ; } ) , distinctUntilChanged ( ( a , b ) => ( a . height === b . height && a . locked === b . locked ) ) ) ; } param el param options returns export function mountSidebar ( el , { header$ , ... options } ) { const inner = getElement ( ".md-sidebar__scrollwrap" , el ) ; const { y } = getElementOffset ( inner ) ; return defer ( ( ) => { const push$ = new Subject < Sidebar > ( ) ; push$ . pipe ( auditTime ( 0 , animationFrameScheduler ) , withLatestFrom ( header$ ) ) . subscribe ( { next ( [ { height } , { height : offset } ] ) { inner . style . height = `${ height - 2 * y }px` ; el . style . top = `${ offset }px` ; } , complete ( ) { inner . style . height = "" ; el . style . top = "" ; } } ) ; return watchSidebar ( el , options ) . pipe ( tap ( state => push$ . next ( state ) ) , finalize ( ( ) => push$ . complete ( ) ) , map ( state => ( { ref : el , ... state } ) ) ) ; } ) ; }