import { EMPTY , Observable , Subject , defer , finalize , merge , share , takeLast , takeUntil } from "rxjs" ; import { getElement , getElements , getOptionalElement } from "~/browser" ; import { renderAnnotation } from "~/templates" ; import { Component } from "../../../_" ; import { Annotation , mountAnnotation } from "../_" ; /**
 * Mount options
 */ interface MountOptions { print$ : Observable < boolean > ; } param container returns function findAnnotationMarkers ( container ) { const markers = [ ] ; for ( const comment of getElements ( ".c, .c1, .cm" , container ) ) { let match ; let text = comment . firstChild as Text ; while ( ( match = /\((\d+)\)/ . exec ( text . textContent ! ) ) ) { const marker = text . splitText ( match . index ) ; text = marker . splitText ( match [ 0 ] . length ) ; markers . push ( marker ) ; } } return markers ; } param source param target function swap ( source , target ) { target . append ( ... Array . from ( source . childNodes ) ) ; } param el param container param options returns export function mountAnnotationList ( el , container , { print$ } ) { const annotations = new Map < number , HTMLElement > ( ) ; for ( const marker of findAnnotationMarkers ( container ) ) { const [ , id ] = marker . textContent ! . match ( /\((\d+)\)/ ) ! ; if ( getOptionalElement ( `li:nth-child(${ id })` , el ) ) { annotations . set ( + id , renderAnnotation ( + id ) ) ; marker . replaceWith ( annotations . get ( + id ) ! ) ; } } if ( annotations . size === 0 ) return EMPTY ; return defer ( ( ) => { const done$ = new Subject < void > ( ) ; print$ . pipe ( takeUntil ( done$ . pipe ( takeLast ( 1 ) ) ) ) . subscribe ( active => { el . hidden = ! active ; for ( const [ id , annotation ] of annotations ) { const inner = getElement ( ".md-typeset" , annotation ) ; const child = getElement ( `li:nth-child(${ id })` , el ) ; if ( ! active ) swap ( child , inner ) ; else swap ( inner , child ) ; } } ) ; return merge ( ... [ ... annotations ] . map ( ( [ , annotation ] ) => ( mountAnnotation ( annotation , container ) ) ) ) . pipe ( finalize ( ( ) => done$ . complete ( ) ) , share ( ) ) ; } ) ; }